comment.php 11 KB

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