Aucune description

comment.php 11KB

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