[ Index ]

PHP Cross Reference of WordPress

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


Generated: Fri Apr 19 01:00:02 2024 Cross-referenced by PHPXref 0.7.1