[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/ -> comment.php (source)

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


Generated : Thu Apr 16 08:20:10 2026 Cross-referenced by PHPXref