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


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref