comment.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. /**
  3. * Comment Management Screen
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /** Load WordPress Bootstrap */
  9. require_once __DIR__ . '/admin.php';
  10. $parent_file = 'edit-comments.php';
  11. $submenu_file = 'edit-comments.php';
  12. /**
  13. * @global string $action
  14. */
  15. global $action;
  16. wp_reset_vars( array( 'action' ) );
  17. if ( isset( $_POST['deletecomment'] ) ) {
  18. $action = 'deletecomment';
  19. }
  20. if ( 'cdc' === $action ) {
  21. $action = 'delete';
  22. } elseif ( 'mac' === $action ) {
  23. $action = 'approve';
  24. }
  25. if ( isset( $_GET['dt'] ) ) {
  26. if ( 'spam' === $_GET['dt'] ) {
  27. $action = 'spam';
  28. } elseif ( 'trash' === $_GET['dt'] ) {
  29. $action = 'trash';
  30. }
  31. }
  32. if ( isset( $_REQUEST['c'] ) ) {
  33. $comment_id = absint( $_REQUEST['c'] );
  34. $comment = get_comment( $comment_id );
  35. // Prevent actions on a comment associated with a trashed post.
  36. if ( $comment && 'trash' === get_post_status( $comment->comment_post_ID ) ) {
  37. wp_die(
  38. __( 'You cannot edit this comment because the associated post is in the Trash. Please restore the post first, then try again.' )
  39. );
  40. }
  41. } else {
  42. $comment = null;
  43. }
  44. switch ( $action ) {
  45. case 'editcomment':
  46. // Used in the HTML title tag.
  47. $title = __( 'Edit Comment' );
  48. get_current_screen()->add_help_tab(
  49. array(
  50. 'id' => 'overview',
  51. 'title' => __( 'Overview' ),
  52. 'content' =>
  53. '<p>' . __( 'You can edit the information left in a comment if needed. This is often useful when you notice that a commenter has made a typographical error.' ) . '</p>' .
  54. '<p>' . __( 'You can also moderate the comment from this screen using the Status box, where you can also change the timestamp of the comment.' ) . '</p>',
  55. )
  56. );
  57. get_current_screen()->set_help_sidebar(
  58. '<p><strong>' . __( 'For more information:' ) . '</strong></p>' .
  59. '<p>' . __( '<a href="https://wordpress.org/support/article/comments-screen/">Documentation on Comments</a>' ) . '</p>' .
  60. '<p>' . __( '<a href="https://wordpress.org/support/">Support</a>' ) . '</p>'
  61. );
  62. wp_enqueue_script( 'comment' );
  63. require_once ABSPATH . 'wp-admin/admin-header.php';
  64. if ( ! $comment ) {
  65. comment_footer_die( __( 'Invalid comment ID.' ) . sprintf( ' <a href="%s">' . __( 'Go back' ) . '</a>.', 'javascript:history.go(-1)' ) );
  66. }
  67. if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
  68. comment_footer_die( __( 'Sorry, you are not allowed to edit this comment.' ) );
  69. }
  70. if ( 'trash' === $comment->comment_approved ) {
  71. comment_footer_die( __( 'This comment is in the Trash. Please move it out of the Trash if you want to edit it.' ) );
  72. }
  73. $comment = get_comment_to_edit( $comment_id );
  74. require ABSPATH . 'wp-admin/edit-form-comment.php';
  75. break;
  76. case 'delete':
  77. case 'approve':
  78. case 'trash':
  79. case 'spam':
  80. // Used in the HTML title tag.
  81. $title = __( 'Moderate Comment' );
  82. if ( ! $comment ) {
  83. wp_redirect( admin_url( 'edit-comments.php?error=1' ) );
  84. die();
  85. }
  86. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
  87. wp_redirect( admin_url( 'edit-comments.php?error=2' ) );
  88. die();
  89. }
  90. // No need to re-approve/re-trash/re-spam a comment.
  91. if ( str_replace( '1', 'approve', $comment->comment_approved ) === $action ) {
  92. wp_redirect( admin_url( 'edit-comments.php?same=' . $comment_id ) );
  93. die();
  94. }
  95. require_once ABSPATH . 'wp-admin/admin-header.php';
  96. $formaction = $action . 'comment';
  97. $nonce_action = ( 'approve' === $action ) ? 'approve-comment_' : 'delete-comment_';
  98. $nonce_action .= $comment_id;
  99. ?>
  100. <div class="wrap">
  101. <h1><?php echo esc_html( $title ); ?></h1>
  102. <?php
  103. switch ( $action ) {
  104. case 'spam':
  105. $caution_msg = __( 'You are about to mark the following comment as spam:' );
  106. $button = _x( 'Mark as spam', 'comment' );
  107. break;
  108. case 'trash':
  109. $caution_msg = __( 'You are about to move the following comment to the Trash:' );
  110. $button = __( 'Move to Trash' );
  111. break;
  112. case 'delete':
  113. $caution_msg = __( 'You are about to delete the following comment:' );
  114. $button = __( 'Permanently delete comment' );
  115. break;
  116. default:
  117. $caution_msg = __( 'You are about to approve the following comment:' );
  118. $button = __( 'Approve comment' );
  119. break;
  120. }
  121. if ( '0' !== $comment->comment_approved ) { // If not unapproved.
  122. $message = '';
  123. switch ( $comment->comment_approved ) {
  124. case '1':
  125. $message = __( 'This comment is currently approved.' );
  126. break;
  127. case 'spam':
  128. $message = __( 'This comment is currently marked as spam.' );
  129. break;
  130. case 'trash':
  131. $message = __( 'This comment is currently in the Trash.' );
  132. break;
  133. }
  134. if ( $message ) {
  135. echo '<div id="message" class="notice notice-info"><p>' . $message . '</p></div>';
  136. }
  137. }
  138. ?>
  139. <div id="message" class="notice notice-warning"><p><strong><?php _e( 'Caution:' ); ?></strong> <?php echo $caution_msg; ?></p></div>
  140. <table class="form-table comment-ays">
  141. <tr>
  142. <th scope="row"><?php _e( 'Author' ); ?></th>
  143. <td><?php comment_author( $comment ); ?></td>
  144. </tr>
  145. <?php if ( get_comment_author_email( $comment ) ) { ?>
  146. <tr>
  147. <th scope="row"><?php _e( 'Email' ); ?></th>
  148. <td><?php comment_author_email( $comment ); ?></td>
  149. </tr>
  150. <?php } ?>
  151. <?php if ( get_comment_author_url( $comment ) ) { ?>
  152. <tr>
  153. <th scope="row"><?php _e( 'URL' ); ?></th>
  154. <td><a href="<?php comment_author_url( $comment ); ?>"><?php comment_author_url( $comment ); ?></a></td>
  155. </tr>
  156. <?php } ?>
  157. <tr>
  158. <th scope="row"><?php /* translators: Column name or table row header. */ _e( 'In response to' ); ?></th>
  159. <td>
  160. <?php
  161. $post_id = $comment->comment_post_ID;
  162. if ( current_user_can( 'edit_post', $post_id ) ) {
  163. $post_link = "<a href='" . esc_url( get_edit_post_link( $post_id ) ) . "'>";
  164. $post_link .= esc_html( get_the_title( $post_id ) ) . '</a>';
  165. } else {
  166. $post_link = esc_html( get_the_title( $post_id ) );
  167. }
  168. echo $post_link;
  169. if ( $comment->comment_parent ) {
  170. $parent = get_comment( $comment->comment_parent );
  171. $parent_link = esc_url( get_comment_link( $parent ) );
  172. $name = get_comment_author( $parent );
  173. printf(
  174. /* translators: %s: Comment link. */
  175. ' | ' . __( 'In reply to %s.' ),
  176. '<a href="' . $parent_link . '">' . $name . '</a>'
  177. );
  178. }
  179. ?>
  180. </td>
  181. </tr>
  182. <tr>
  183. <th scope="row"><?php _e( 'Submitted on' ); ?></th>
  184. <td>
  185. <?php
  186. $submitted = sprintf(
  187. /* translators: 1: Comment date, 2: Comment time. */
  188. __( '%1$s at %2$s' ),
  189. /* translators: Comment date format. See https://www.php.net/manual/datetime.format.php */
  190. get_comment_date( __( 'Y/m/d' ), $comment ),
  191. /* translators: Comment time format. See https://www.php.net/manual/datetime.format.php */
  192. get_comment_date( __( 'g:i a' ), $comment )
  193. );
  194. if ( 'approved' === wp_get_comment_status( $comment ) && ! empty( $comment->comment_post_ID ) ) {
  195. echo '<a href="' . esc_url( get_comment_link( $comment ) ) . '">' . $submitted . '</a>';
  196. } else {
  197. echo $submitted;
  198. }
  199. ?>
  200. </td>
  201. </tr>
  202. <tr>
  203. <th scope="row"><?php /* translators: Field name in comment form. */ _ex( 'Comment', 'noun' ); ?></th>
  204. <td class="comment-content">
  205. <?php comment_text( $comment ); ?>
  206. <p class="edit-comment">
  207. <a href="<?php echo esc_url( admin_url( "comment.php?action=editcomment&c={$comment->comment_ID}" ) ); ?>"><?php esc_html_e( 'Edit' ); ?></a>
  208. </p>
  209. </td>
  210. </tr>
  211. </table>
  212. <form action="comment.php" method="get" class="comment-ays-submit">
  213. <p>
  214. <?php submit_button( $button, 'primary', 'submit', false ); ?>
  215. <a href="<?php echo esc_url( admin_url( 'edit-comments.php' ) ); ?>" class="button-cancel"><?php esc_html_e( 'Cancel' ); ?></a>
  216. </p>
  217. <?php wp_nonce_field( $nonce_action ); ?>
  218. <input type="hidden" name="action" value="<?php echo esc_attr( $formaction ); ?>" />
  219. <input type="hidden" name="c" value="<?php echo esc_attr( $comment->comment_ID ); ?>" />
  220. <input type="hidden" name="noredir" value="1" />
  221. </form>
  222. </div>
  223. <?php
  224. break;
  225. case 'deletecomment':
  226. case 'trashcomment':
  227. case 'untrashcomment':
  228. case 'spamcomment':
  229. case 'unspamcomment':
  230. case 'approvecomment':
  231. case 'unapprovecomment':
  232. $comment_id = absint( $_REQUEST['c'] );
  233. if ( in_array( $action, array( 'approvecomment', 'unapprovecomment' ), true ) ) {
  234. check_admin_referer( 'approve-comment_' . $comment_id );
  235. } else {
  236. check_admin_referer( 'delete-comment_' . $comment_id );
  237. }
  238. $noredir = isset( $_REQUEST['noredir'] );
  239. $comment = get_comment( $comment_id );
  240. if ( ! $comment ) {
  241. comment_footer_die( __( 'Invalid comment ID.' ) . sprintf( ' <a href="%s">' . __( 'Go back' ) . '</a>.', 'edit-comments.php' ) );
  242. }
  243. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
  244. comment_footer_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) );
  245. }
  246. if ( wp_get_referer() && ! $noredir && false === strpos( wp_get_referer(), 'comment.php' ) ) {
  247. $redir = wp_get_referer();
  248. } elseif ( wp_get_original_referer() && ! $noredir ) {
  249. $redir = wp_get_original_referer();
  250. } elseif ( in_array( $action, array( 'approvecomment', 'unapprovecomment' ), true ) ) {
  251. $redir = admin_url( 'edit-comments.php?p=' . absint( $comment->comment_post_ID ) );
  252. } else {
  253. $redir = admin_url( 'edit-comments.php' );
  254. }
  255. $redir = remove_query_arg( array( 'spammed', 'unspammed', 'trashed', 'untrashed', 'deleted', 'ids', 'approved', 'unapproved' ), $redir );
  256. switch ( $action ) {
  257. case 'deletecomment':
  258. wp_delete_comment( $comment );
  259. $redir = add_query_arg( array( 'deleted' => '1' ), $redir );
  260. break;
  261. case 'trashcomment':
  262. wp_trash_comment( $comment );
  263. $redir = add_query_arg(
  264. array(
  265. 'trashed' => '1',
  266. 'ids' => $comment_id,
  267. ),
  268. $redir
  269. );
  270. break;
  271. case 'untrashcomment':
  272. wp_untrash_comment( $comment );
  273. $redir = add_query_arg( array( 'untrashed' => '1' ), $redir );
  274. break;
  275. case 'spamcomment':
  276. wp_spam_comment( $comment );
  277. $redir = add_query_arg(
  278. array(
  279. 'spammed' => '1',
  280. 'ids' => $comment_id,
  281. ),
  282. $redir
  283. );
  284. break;
  285. case 'unspamcomment':
  286. wp_unspam_comment( $comment );
  287. $redir = add_query_arg( array( 'unspammed' => '1' ), $redir );
  288. break;
  289. case 'approvecomment':
  290. wp_set_comment_status( $comment, 'approve' );
  291. $redir = add_query_arg( array( 'approved' => 1 ), $redir );
  292. break;
  293. case 'unapprovecomment':
  294. wp_set_comment_status( $comment, 'hold' );
  295. $redir = add_query_arg( array( 'unapproved' => 1 ), $redir );
  296. break;
  297. }
  298. wp_redirect( $redir );
  299. die;
  300. case 'editedcomment':
  301. $comment_id = absint( $_POST['comment_ID'] );
  302. $comment_post_id = absint( $_POST['comment_post_ID'] );
  303. check_admin_referer( 'update-comment_' . $comment_id );
  304. $updated = edit_comment();
  305. if ( is_wp_error( $updated ) ) {
  306. wp_die( $updated->get_error_message() );
  307. }
  308. $location = ( empty( $_POST['referredby'] ) ? "edit-comments.php?p=$comment_post_id" : $_POST['referredby'] ) . '#comment-' . $comment_id;
  309. /**
  310. * Filters the URI the user is redirected to after editing a comment in the admin.
  311. *
  312. * @since 2.1.0
  313. *
  314. * @param string $location The URI the user will be redirected to.
  315. * @param int $comment_id The ID of the comment being edited.
  316. */
  317. $location = apply_filters( 'comment_edit_redirect', $location, $comment_id );
  318. wp_redirect( $location );
  319. exit;
  320. default:
  321. wp_die( __( 'Unknown action.' ) );
  322. } // End switch.
  323. require_once ABSPATH . 'wp-admin/admin-footer.php';