[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> class-wp-comments-list-table.php (source)

   1  <?php
   2  /**
   3   * List Table API: WP_Comments_List_Table class
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   * @since 3.1.0
   8   */
   9  
  10  /**
  11   * Core class used to implement displaying comments in a list table.
  12   *
  13   * @since 3.1.0
  14   *
  15   * @see WP_List_Table
  16   */
  17  class WP_Comments_List_Table extends WP_List_Table {
  18  
  19      public $checkbox = true;
  20  
  21      public $pending_count = array();
  22  
  23      public $extra_items;
  24  
  25      private $user_can;
  26  
  27      /**
  28       * Constructor.
  29       *
  30       * @since 3.1.0
  31       *
  32       * @see WP_List_Table::__construct() for more information on default arguments.
  33       *
  34       * @global int $post_id The ID of the post to show comments for.
  35       *
  36       * @param array $args An associative array of arguments.
  37       */
  38  	public function __construct( $args = array() ) {
  39          global $post_id;
  40  
  41          $post_id = isset( $_REQUEST['p'] ) ? absint( $_REQUEST['p'] ) : 0;
  42  
  43          if ( get_option( 'show_avatars' ) ) {
  44              add_filter( 'comment_author', array( $this, 'floated_admin_avatar' ), 10, 2 );
  45          }
  46  
  47          parent::__construct(
  48              array(
  49                  'plural'   => 'comments',
  50                  'singular' => 'comment',
  51                  'ajax'     => true,
  52                  'screen'   => $args['screen'] ?? null,
  53              )
  54          );
  55      }
  56  
  57      /**
  58       * Adds avatars to comment author names.
  59       *
  60       * @since 3.1.0
  61       *
  62       * @param string $name       Comment author name.
  63       * @param int    $comment_id Comment ID.
  64       * @return string Avatar with the user name.
  65       */
  66  	public function floated_admin_avatar( $name, $comment_id ) {
  67          $comment = get_comment( $comment_id );
  68          $avatar  = get_avatar( $comment, 32, 'mystery' );
  69          return "$avatar $name";
  70      }
  71  
  72      /**
  73       * Checks if the user can edit posts.
  74       *
  75       * @return bool Whether the user can edit posts.
  76       */
  77  	public function ajax_user_can() {
  78          return current_user_can( 'edit_posts' );
  79      }
  80  
  81      /**
  82       * Prepares the comments list items.
  83       *
  84       * @global string $mode           Current list table display mode.
  85       * @global int    $post_id        Current post ID filter.
  86       * @global string $comment_status Comment status filter.
  87       * @global string $comment_type   Comment type filter.
  88       * @global string $search         Search term.
  89       */
  90  	public function prepare_items() {
  91          global $mode, $post_id, $comment_status, $comment_type, $search;
  92  
  93          if ( ! empty( $_REQUEST['mode'] ) ) {
  94              $mode = 'excerpt' === $_REQUEST['mode'] ? 'excerpt' : 'list';
  95              set_user_setting( 'posts_list_mode', $mode );
  96          } else {
  97              $mode = get_user_setting( 'posts_list_mode', 'list' );
  98          }
  99  
 100          $comment_status = $_REQUEST['comment_status'] ?? 'all';
 101  
 102          if ( ! in_array( $comment_status, array( 'all', 'mine', 'moderated', 'approved', 'spam', 'trash' ), true ) ) {
 103              $comment_status = 'all';
 104          }
 105  
 106          $comment_type = '';
 107  
 108          if ( ! empty( $_REQUEST['comment_type'] ) && 'note' !== $_REQUEST['comment_type'] ) {
 109              $comment_type = $_REQUEST['comment_type'];
 110          }
 111  
 112          $search = $_REQUEST['s'] ?? '';
 113  
 114          $post_type = ( isset( $_REQUEST['post_type'] ) ) ? sanitize_key( $_REQUEST['post_type'] ) : '';
 115  
 116          $user_id = $_REQUEST['user_id'] ?? '';
 117  
 118          $orderby = $_REQUEST['orderby'] ?? '';
 119          $order   = $_REQUEST['order'] ?? '';
 120  
 121          $comments_per_page = $this->get_per_page( $comment_status );
 122  
 123          $doing_ajax = wp_doing_ajax();
 124  
 125          if ( isset( $_REQUEST['number'] ) ) {
 126              $number = (int) $_REQUEST['number'];
 127          } else {
 128              $number = $comments_per_page + min( 8, $comments_per_page ); // Grab a few extra.
 129          }
 130  
 131          $page = $this->get_pagenum();
 132  
 133          if ( isset( $_REQUEST['start'] ) ) {
 134              $start = $_REQUEST['start'];
 135          } else {
 136              $start = ( $page - 1 ) * $comments_per_page;
 137          }
 138  
 139          if ( $doing_ajax && isset( $_REQUEST['offset'] ) ) {
 140              $start += $_REQUEST['offset'];
 141          }
 142  
 143          $status_map = array(
 144              'mine'      => '',
 145              'moderated' => 'hold',
 146              'approved'  => 'approve',
 147              'all'       => '',
 148          );
 149  
 150          $args = array(
 151              'status'                    => $status_map[ $comment_status ] ?? $comment_status,
 152              'search'                    => $search,
 153              'user_id'                   => $user_id,
 154              'offset'                    => $start,
 155              'number'                    => $number,
 156              'post_id'                   => $post_id,
 157              'type'                      => $comment_type,
 158              'type__not_in'              => array( 'note' ),
 159              'orderby'                   => $orderby,
 160              'order'                     => $order,
 161              'post_type'                 => $post_type,
 162              'update_comment_post_cache' => true,
 163          );
 164  
 165          /**
 166           * Filters the arguments for the comment query in the comments list table.
 167           *
 168           * @since 5.1.0
 169           *
 170           * @param array $args An array of get_comments() arguments.
 171           */
 172          $args = apply_filters( 'comments_list_table_query_args', $args );
 173  
 174          $_comments = get_comments( $args );
 175  
 176          if ( is_array( $_comments ) ) {
 177              $this->items       = array_slice( $_comments, 0, $comments_per_page );
 178              $this->extra_items = array_slice( $_comments, $comments_per_page );
 179  
 180              $_comment_post_ids = array_unique( wp_list_pluck( $_comments, 'comment_post_ID' ) );
 181  
 182              $this->pending_count = get_pending_comments_num( $_comment_post_ids );
 183          }
 184  
 185          $total_comments = get_comments(
 186              array_merge(
 187                  $args,
 188                  array(
 189                      'count'   => true,
 190                      'offset'  => 0,
 191                      'number'  => 0,
 192                      'orderby' => 'none',
 193                  )
 194              )
 195          );
 196  
 197          $this->set_pagination_args(
 198              array(
 199                  'total_items' => $total_comments,
 200                  'per_page'    => $comments_per_page,
 201              )
 202          );
 203      }
 204  
 205      /**
 206       * Gets the number of comments to display per page.
 207       *
 208       * @param string $comment_status Comment status.
 209       * @return int Comments per page.
 210       */
 211  	public function get_per_page( $comment_status = 'all' ) {
 212          $comments_per_page = $this->get_items_per_page( 'edit_comments_per_page' );
 213  
 214          /**
 215           * Filters the number of comments listed per page in the comments list table.
 216           *
 217           * @since 2.6.0
 218           *
 219           * @param int    $comments_per_page The number of comments to list per page.
 220           * @param string $comment_status    The comment status name. Default 'All'.
 221           */
 222          return apply_filters( 'comments_per_page', $comments_per_page, $comment_status );
 223      }
 224  
 225      /**
 226       * Displays a message when no comments are found.
 227       * @global string $comment_status The current comment status filter.
 228       */
 229  	public function no_items() {
 230          global $comment_status;
 231  
 232          if ( 'moderated' === $comment_status ) {
 233              _e( 'No comments awaiting moderation.' );
 234          } elseif ( 'trash' === $comment_status ) {
 235              _e( 'No comments found in Trash.' );
 236          } else {
 237              _e( 'No comments found.' );
 238          }
 239      }
 240  
 241      /**
 242       * Returns an array of comment status links.
 243       *
 244       * @global int    $post_id        The ID of the post to show comments for.
 245       * @global string $comment_status The current comment status.
 246       * @global string $comment_type   The current comment type.
 247       *
 248       * @return array<string, string> Comment status HTML links keyed by view.
 249       */
 250  	protected function get_views() {
 251          global $post_id, $comment_status, $comment_type;
 252  
 253          $status_links = array();
 254          $num_comments = ( $post_id ) ? wp_count_comments( $post_id ) : wp_count_comments();
 255  
 256          $statuses = array(
 257              /* translators: %s: Number of comments. */
 258              'all'       => _nx_noop(
 259                  'All <span class="count">(%s)</span>',
 260                  'All <span class="count">(%s)</span>',
 261                  'comments'
 262              ), // Singular not used.
 263  
 264              /* translators: %s: Number of comments. */
 265              'mine'      => _nx_noop(
 266                  'Mine <span class="count">(%s)</span>',
 267                  'Mine <span class="count">(%s)</span>',
 268                  'comments'
 269              ),
 270  
 271              /* translators: %s: Number of comments. */
 272              'moderated' => _nx_noop(
 273                  'Pending <span class="count">(%s)</span>',
 274                  'Pending <span class="count">(%s)</span>',
 275                  'comments'
 276              ),
 277  
 278              /* translators: %s: Number of comments. */
 279              'approved'  => _nx_noop(
 280                  'Approved <span class="count">(%s)</span>',
 281                  'Approved <span class="count">(%s)</span>',
 282                  'comments'
 283              ),
 284  
 285              /* translators: %s: Number of comments. */
 286              'spam'      => _nx_noop(
 287                  'Spam <span class="count">(%s)</span>',
 288                  'Spam <span class="count">(%s)</span>',
 289                  'comments'
 290              ),
 291  
 292              /* translators: %s: Number of comments. */
 293              'trash'     => _nx_noop(
 294                  'Trash <span class="count">(%s)</span>',
 295                  'Trash <span class="count">(%s)</span>',
 296                  'comments'
 297              ),
 298          );
 299  
 300          if ( ! EMPTY_TRASH_DAYS ) {
 301              unset( $statuses['trash'] );
 302          }
 303  
 304          $link = admin_url( 'edit-comments.php' );
 305  
 306          if ( ! empty( $comment_type ) && 'all' !== $comment_type ) {
 307              $link = add_query_arg( 'comment_type', $comment_type, $link );
 308          }
 309  
 310          foreach ( $statuses as $status => $label ) {
 311              if ( 'mine' === $status ) {
 312                  $current_user_id    = get_current_user_id();
 313                  $num_comments->mine = get_comments(
 314                      array(
 315                          'post_id' => $post_id ? $post_id : 0,
 316                          'user_id' => $current_user_id,
 317                          'count'   => true,
 318                          'orderby' => 'none',
 319                      )
 320                  );
 321                  $link               = add_query_arg( 'user_id', $current_user_id, $link );
 322              } else {
 323                  $link = remove_query_arg( 'user_id', $link );
 324              }
 325  
 326              if ( ! isset( $num_comments->$status ) ) {
 327                  $num_comments->$status = 10;
 328              }
 329  
 330              $link = add_query_arg( 'comment_status', $status, $link );
 331  
 332              if ( $post_id ) {
 333                  $link = add_query_arg( 'p', absint( $post_id ), $link );
 334              }
 335  
 336              /*
 337              // I toyed with this, but decided against it. Leaving it in here in case anyone thinks it is a good idea. ~ Mark
 338              if ( !empty( $_REQUEST['s'] ) )
 339                  $link = add_query_arg( 's', esc_attr( wp_unslash( $_REQUEST['s'] ) ), $link );
 340              */
 341  
 342              $status_links[ $status ] = array(
 343                  'url'     => esc_url( $link ),
 344                  'label'   => sprintf(
 345                      translate_nooped_plural( $label, $num_comments->$status ),
 346                      sprintf(
 347                          '<span class="%s-count">%s</span>',
 348                          ( 'moderated' === $status ) ? 'pending' : $status,
 349                          number_format_i18n( $num_comments->$status )
 350                      )
 351                  ),
 352                  'current' => $status === $comment_status,
 353              );
 354          }
 355  
 356          /**
 357           * Filters the comment status links.
 358           *
 359           * @since 2.5.0
 360           * @since 5.1.0 The 'Mine' link was added.
 361           *
 362           * @param string[] $status_links An associative array of fully-formed comment status links. Includes 'All', 'Mine',
 363           *                              'Pending', 'Approved', 'Spam', and 'Trash'.
 364           */
 365          return apply_filters( 'comment_status_links', $this->get_views_links( $status_links ) );
 366      }
 367  
 368      /**
 369       * Gets the available bulk actions for the comments list.
 370       *
 371       * @global string $comment_status Current comment status filter.
 372       *
 373       * @return array<string, string> Bulk action labels keyed by action name.
 374       */
 375  	protected function get_bulk_actions() {
 376          global $comment_status;
 377  
 378          if ( ! current_user_can( 'moderate_comments' ) ) {
 379              return array(); // Return an empty array if the user doesn't have permission
 380          }
 381  
 382          $actions = array();
 383  
 384          if ( in_array( $comment_status, array( 'all', 'approved' ), true ) ) {
 385              $actions['unapprove'] = __( 'Unapprove' );
 386          }
 387  
 388          if ( in_array( $comment_status, array( 'all', 'moderated' ), true ) ) {
 389              $actions['approve'] = __( 'Approve' );
 390          }
 391  
 392          if ( in_array( $comment_status, array( 'all', 'moderated', 'approved', 'trash' ), true ) ) {
 393              $actions['spam'] = _x( 'Mark as spam', 'comment' );
 394          }
 395  
 396          if ( 'trash' === $comment_status ) {
 397              $actions['untrash'] = __( 'Restore' );
 398          } elseif ( 'spam' === $comment_status ) {
 399              $actions['unspam'] = _x( 'Not spam', 'comment' );
 400          }
 401  
 402          if ( in_array( $comment_status, array( 'trash', 'spam' ), true ) || ! EMPTY_TRASH_DAYS ) {
 403              $actions['delete'] = __( 'Delete permanently' );
 404          } else {
 405              $actions['trash'] = __( 'Move to Trash' );
 406          }
 407  
 408          return $actions;
 409      }
 410  
 411      /**
 412       * Displays extra controls between bulk actions and pagination.
 413       *
 414       * @global string $comment_status Current comment status.
 415       * @global string $comment_type   Current comment type.
 416       *
 417       * @param string $which The location of the extra table nav markup: Either 'top' or 'bottom'.
 418       */
 419  	protected function extra_tablenav( $which ) {
 420          global $comment_status, $comment_type;
 421          static $has_items;
 422  
 423          if ( ! isset( $has_items ) ) {
 424              $has_items = $this->has_items();
 425          }
 426  
 427          echo '<div class="alignleft actions">';
 428  
 429          if ( 'top' === $which ) {
 430              ob_start();
 431  
 432              $this->comment_type_dropdown( $comment_type );
 433  
 434              /**
 435               * Fires just before the Filter submit button for comment types.
 436               *
 437               * @since 3.5.0
 438               */
 439              do_action( 'restrict_manage_comments' );
 440  
 441              $output = ob_get_clean();
 442  
 443              if ( ! empty( $output ) && $this->has_items() ) {
 444                  echo $output;
 445                  submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
 446              }
 447          }
 448  
 449          if ( ( 'spam' === $comment_status || 'trash' === $comment_status ) && $has_items
 450              && current_user_can( 'moderate_comments' )
 451          ) {
 452              wp_nonce_field( 'bulk-destroy', '_destroy_nonce' );
 453              $title = ( 'spam' === $comment_status ) ? esc_attr__( 'Empty Spam' ) : esc_attr__( 'Empty Trash' );
 454              submit_button( $title, 'apply', 'delete_all', false );
 455          }
 456  
 457          /**
 458           * Fires after the Filter submit button for comment types.
 459           *
 460           * @since 2.5.0
 461           * @since 5.6.0 The `$which` parameter was added.
 462           *
 463           * @param string $comment_status The comment status name. Default 'All'.
 464           * @param string $which          The location of the extra table nav markup: Either 'top' or 'bottom'.
 465           */
 466          do_action( 'manage_comments_nav', $comment_status, $which );
 467  
 468          echo '</div>';
 469      }
 470  
 471      /**
 472       * Gets the current action selected from the bulk actions dropdown.
 473       *
 474       * @return string|false Current action or false if none.
 475       */
 476  	public function current_action() {
 477          if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) {
 478              return 'delete_all';
 479          }
 480  
 481          return parent::current_action();
 482      }
 483  
 484      /**
 485       * Gets the list of columns.
 486       *
 487       * @global int $post_id The ID of the post comments are being shown for.
 488       *
 489       * @return string[] Array of column titles keyed by their column name.
 490       */
 491  	public function get_columns() {
 492          global $post_id;
 493  
 494          $columns = array();
 495  
 496          if ( $this->checkbox ) {
 497              $columns['cb'] = '<input type="checkbox" />';
 498          }
 499  
 500          $columns['author']  = __( 'Author' );
 501          $columns['comment'] = _x( 'Comment', 'column name' );
 502  
 503          if ( ! $post_id ) {
 504              /* translators: Column name or table row header. */
 505              $columns['response'] = __( 'In response to' );
 506          }
 507  
 508          $columns['date'] = _x( 'Submitted on', 'column name' );
 509  
 510          return $columns;
 511      }
 512  
 513      /**
 514       * Displays a comment type drop-down for filtering on the Comments list table.
 515       *
 516       * @since 5.5.0
 517       * @since 5.6.0 Renamed from `comment_status_dropdown()` to `comment_type_dropdown()`.
 518       *
 519       * @param string $comment_type The current comment type slug.
 520       */
 521  	protected function comment_type_dropdown( $comment_type ) {
 522          /**
 523           * Filters the comment types shown in the drop-down menu on the Comments list table.
 524           *
 525           * @since 2.7.0
 526           *
 527           * @param string[] $comment_types Array of comment type labels keyed by their name.
 528           */
 529          $comment_types = apply_filters(
 530              'admin_comment_types_dropdown',
 531              array(
 532                  'comment' => __( 'Comments' ),
 533                  'pings'   => __( 'Pings' ),
 534              )
 535          );
 536  
 537          if ( $comment_types && is_array( $comment_types ) ) {
 538              printf(
 539                  '<label class="screen-reader-text" for="filter-by-comment-type">%s</label>',
 540                  /* translators: Hidden accessibility text. */
 541                  __( 'Filter by comment type' )
 542              );
 543  
 544              echo '<select id="filter-by-comment-type" name="comment_type">';
 545  
 546              printf( "\t<option value=''>%s</option>", __( 'All comment types' ) );
 547  
 548              foreach ( $comment_types as $type => $label ) {
 549                  if ( get_comments(
 550                      array(
 551                          'count'   => true,
 552                          'orderby' => 'none',
 553                          'type'    => $type,
 554                      )
 555                  ) ) {
 556                      printf(
 557                          "\t<option value='%s'%s>%s</option>\n",
 558                          esc_attr( $type ),
 559                          selected( $comment_type, $type, false ),
 560                          esc_html( $label )
 561                      );
 562                  }
 563              }
 564  
 565              echo '</select>';
 566          }
 567      }
 568  
 569      /**
 570       * Gets a list of sortable columns.
 571       *
 572       * @return array<string, string|array> The sortable columns.
 573       */
 574  	protected function get_sortable_columns() {
 575          return array(
 576              'author'   => array( 'comment_author', false, __( 'Author' ), __( 'Table ordered by Comment Author.' ) ),
 577              'response' => array( 'comment_post_ID', false, _x( 'In Response To', 'column name' ), __( 'Table ordered by Post Replied To.' ) ),
 578              'date'     => 'comment_date',
 579          );
 580      }
 581  
 582      /**
 583       * Gets the name of the default primary column.
 584       *
 585       * @since 4.3.0
 586       *
 587       * @return string Name of the default primary column, in this case, 'comment'.
 588       */
 589  	protected function get_default_primary_column_name() {
 590          return 'comment';
 591      }
 592  
 593      /**
 594       * Displays the comments table.
 595       *
 596       * Overrides the parent display() method to render extra comments.
 597       *
 598       * @since 3.1.0
 599       */
 600  	public function display() {
 601          wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' );
 602          static $has_items;
 603  
 604          if ( ! isset( $has_items ) ) {
 605              $has_items = $this->has_items();
 606  
 607              if ( $has_items ) {
 608                  $this->display_tablenav( 'top' );
 609              }
 610          }
 611  
 612          $this->screen->render_screen_reader_content( 'heading_list' );
 613  
 614          ?>
 615  <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
 616          <?php
 617          if ( ! isset( $_GET['orderby'] ) ) {
 618              // In the initial view, Comments are ordered by comment's date but there's no column for that.
 619              echo '<caption class="screen-reader-text">' .
 620              /* translators: Hidden accessibility text. */
 621              __( 'Ordered by Comment Date, descending.' ) .
 622              '</caption>';
 623          } else {
 624              $this->print_table_description();
 625          }
 626          ?>
 627      <thead>
 628      <tr>
 629          <?php $this->print_column_headers(); ?>
 630      </tr>
 631      </thead>
 632  
 633      <tbody id="the-comment-list" data-wp-lists="list:comment">
 634          <?php $this->display_rows_or_placeholder(); ?>
 635      </tbody>
 636  
 637      <tbody id="the-extra-comment-list" data-wp-lists="list:comment" style="display: none;">
 638          <?php
 639              /*
 640               * Back up the items to restore after printing the extra items markup.
 641               * The extra items may be empty, which will prevent the table nav from displaying later.
 642               */
 643              $items       = $this->items;
 644              $this->items = $this->extra_items;
 645              $this->display_rows_or_placeholder();
 646              $this->items = $items;
 647          ?>
 648      </tbody>
 649  
 650      <tfoot>
 651      <tr>
 652          <?php $this->print_column_headers( false ); ?>
 653      </tr>
 654      </tfoot>
 655  
 656  </table>
 657          <?php
 658  
 659          $this->display_tablenav( 'bottom' );
 660      }
 661  
 662      /**
 663       * Generates content for a single row of the table.
 664       *
 665       * @global WP_Post    $post    Global post object.
 666       * @global WP_Comment $comment Global comment object.
 667       *
 668       * @param WP_Comment $item The comment object.
 669       */
 670  	public function single_row( $item ) {
 671          global $post, $comment;
 672  
 673          // Restores the more descriptive, specific name for use within this method.
 674          $comment = $item;
 675  
 676          if ( $comment->comment_post_ID > 0 ) {
 677              $post = get_post( $comment->comment_post_ID );
 678          }
 679  
 680          $edit_post_cap = $post ? 'edit_post' : 'edit_posts';
 681  
 682          if ( ! current_user_can( $edit_post_cap, $comment->comment_post_ID )
 683              && ( post_password_required( $comment->comment_post_ID )
 684                  || ! current_user_can( 'read_post', $comment->comment_post_ID ) )
 685          ) {
 686              // The user has no access to the post and thus cannot see the comments.
 687              return false;
 688          }
 689  
 690          $the_comment_class = wp_get_comment_status( $comment );
 691  
 692          if ( ! $the_comment_class ) {
 693              $the_comment_class = '';
 694          }
 695  
 696          $the_comment_class = implode( ' ', get_comment_class( $the_comment_class, $comment, $comment->comment_post_ID ) );
 697  
 698          $this->user_can = current_user_can( 'edit_comment', $comment->comment_ID );
 699  
 700          echo "<tr id='comment-$comment->comment_ID' class='$the_comment_class'>";
 701          $this->single_row_columns( $comment );
 702          echo "</tr>\n";
 703  
 704          unset( $GLOBALS['post'], $GLOBALS['comment'] );
 705      }
 706  
 707      /**
 708       * Generates and displays row actions links.
 709       *
 710       * @since 4.3.0
 711       * @since 5.9.0 Renamed `$comment` to `$item` to match parent class for PHP 8 named parameter support.
 712       *
 713       * @global string $comment_status Status for the current listed comments.
 714       *
 715       * @param WP_Comment $item        The comment object.
 716       * @param string     $column_name Current column name.
 717       * @param string     $primary     Primary column name.
 718       * @return string Row actions output for comments. An empty string
 719       *                if the current column is not the primary column,
 720       *                or if the current user cannot edit the comment.
 721       */
 722  	protected function handle_row_actions( $item, $column_name, $primary ) {
 723          global $comment_status;
 724  
 725          if ( $primary !== $column_name ) {
 726              return '';
 727          }
 728  
 729          if ( ! $this->user_can ) {
 730              return '';
 731          }
 732  
 733          // Restores the more descriptive, specific name for use within this method.
 734          $comment = $item;
 735  
 736          $the_comment_status = wp_get_comment_status( $comment );
 737  
 738          $output = '';
 739  
 740          $approve_nonce = esc_html( '_wpnonce=' . wp_create_nonce( 'approve-comment_' . $comment->comment_ID ) );
 741          $del_nonce     = esc_html( '_wpnonce=' . wp_create_nonce( 'delete-comment_' . $comment->comment_ID ) );
 742  
 743          $action_string = 'comment.php?action=%s&c=' . $comment->comment_ID . '&%s';
 744  
 745          $approve_url   = sprintf( $action_string, 'approvecomment', $approve_nonce );
 746          $unapprove_url = sprintf( $action_string, 'unapprovecomment', $approve_nonce );
 747          $spam_url      = sprintf( $action_string, 'spamcomment', $del_nonce );
 748          $unspam_url    = sprintf( $action_string, 'unspamcomment', $del_nonce );
 749          $trash_url     = sprintf( $action_string, 'trashcomment', $del_nonce );
 750          $untrash_url   = sprintf( $action_string, 'untrashcomment', $del_nonce );
 751          $delete_url    = sprintf( $action_string, 'deletecomment', $del_nonce );
 752  
 753          // Preorder it: Approve | Reply | Quick Edit | Edit | Spam | Trash.
 754          $actions = array(
 755              'approve'   => '',
 756              'unapprove' => '',
 757              'reply'     => '',
 758              'quickedit' => '',
 759              'edit'      => '',
 760              'spam'      => '',
 761              'unspam'    => '',
 762              'trash'     => '',
 763              'untrash'   => '',
 764              'delete'    => '',
 765          );
 766  
 767          // Not looking at all comments.
 768          if ( $comment_status && 'all' !== $comment_status ) {
 769              if ( 'approved' === $the_comment_status ) {
 770                  $actions['unapprove'] = sprintf(
 771                      '<a href="%s" data-wp-lists="%s" class="vim-u vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
 772                      esc_url( $unapprove_url ),
 773                      "delete:the-comment-list:comment-{$comment->comment_ID}:e7e7d3:action=dim-comment&amp;new=unapproved",
 774                      esc_attr__( 'Unapprove this comment' ),
 775                      __( 'Unapprove' )
 776                  );
 777              } elseif ( 'unapproved' === $the_comment_status ) {
 778                  $actions['approve'] = sprintf(
 779                      '<a href="%s" data-wp-lists="%s" class="vim-a vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
 780                      esc_url( $approve_url ),
 781                      "delete:the-comment-list:comment-{$comment->comment_ID}:e7e7d3:action=dim-comment&amp;new=approved",
 782                      esc_attr__( 'Approve this comment' ),
 783                      __( 'Approve' )
 784                  );
 785              }
 786          } else {
 787              $actions['approve'] = sprintf(
 788                  '<a href="%s" data-wp-lists="%s" class="vim-a aria-button-if-js" aria-label="%s">%s</a>',
 789                  esc_url( $approve_url ),
 790                  "dim:the-comment-list:comment-{$comment->comment_ID}:unapproved:e7e7d3:e7e7d3:new=approved",
 791                  esc_attr__( 'Approve this comment' ),
 792                  __( 'Approve' )
 793              );
 794  
 795              $actions['unapprove'] = sprintf(
 796                  '<a href="%s" data-wp-lists="%s" class="vim-u aria-button-if-js" aria-label="%s">%s</a>',
 797                  esc_url( $unapprove_url ),
 798                  "dim:the-comment-list:comment-{$comment->comment_ID}:unapproved:e7e7d3:e7e7d3:new=unapproved",
 799                  esc_attr__( 'Unapprove this comment' ),
 800                  __( 'Unapprove' )
 801              );
 802          }
 803  
 804          if ( 'spam' !== $the_comment_status ) {
 805              $actions['spam'] = sprintf(
 806                  '<a href="%s" data-wp-lists="%s" class="vim-s vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
 807                  esc_url( $spam_url ),
 808                  "delete:the-comment-list:comment-{$comment->comment_ID}::spam=1",
 809                  esc_attr__( 'Mark this comment as spam' ),
 810                  /* translators: "Mark as spam" link. */
 811                  _x( 'Spam', 'verb' )
 812              );
 813          } elseif ( 'spam' === $the_comment_status ) {
 814              $actions['unspam'] = sprintf(
 815                  '<a href="%s" data-wp-lists="%s" class="vim-z vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
 816                  esc_url( $unspam_url ),
 817                  "delete:the-comment-list:comment-{$comment->comment_ID}:66cc66:unspam=1",
 818                  esc_attr__( 'Restore this comment from the spam' ),
 819                  _x( 'Not Spam', 'comment' )
 820              );
 821          }
 822  
 823          if ( 'trash' === $the_comment_status ) {
 824              $actions['untrash'] = sprintf(
 825                  '<a href="%s" data-wp-lists="%s" class="vim-z vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
 826                  esc_url( $untrash_url ),
 827                  "delete:the-comment-list:comment-{$comment->comment_ID}:66cc66:untrash=1",
 828                  esc_attr__( 'Restore this comment from the Trash' ),
 829                  __( 'Restore' )
 830              );
 831          }
 832  
 833          if ( 'spam' === $the_comment_status || 'trash' === $the_comment_status || ! EMPTY_TRASH_DAYS ) {
 834              $actions['delete'] = sprintf(
 835                  '<a href="%s" data-wp-lists="%s" class="delete vim-d vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
 836                  esc_url( $delete_url ),
 837                  "delete:the-comment-list:comment-{$comment->comment_ID}::delete=1",
 838                  esc_attr__( 'Delete this comment permanently' ),
 839                  __( 'Delete Permanently' )
 840              );
 841          } else {
 842              $actions['trash'] = sprintf(
 843                  '<a href="%s" data-wp-lists="%s" class="delete vim-d vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
 844                  esc_url( $trash_url ),
 845                  "delete:the-comment-list:comment-{$comment->comment_ID}::trash=1",
 846                  esc_attr__( 'Move this comment to the Trash' ),
 847                  _x( 'Trash', 'verb' )
 848              );
 849          }
 850  
 851          if ( 'spam' !== $the_comment_status && 'trash' !== $the_comment_status ) {
 852              $actions['edit'] = sprintf(
 853                  '<a href="%s" aria-label="%s">%s</a>',
 854                  "comment.php?action=editcomment&amp;c={$comment->comment_ID}",
 855                  esc_attr__( 'Edit this comment' ),
 856                  __( 'Edit' )
 857              );
 858  
 859              $format = '<button type="button" data-comment-id="%d" data-post-id="%d" data-action="%s" class="%s button-link" aria-expanded="false" aria-label="%s">%s</button>';
 860  
 861              $actions['quickedit'] = sprintf(
 862                  $format,
 863                  $comment->comment_ID,
 864                  $comment->comment_post_ID,
 865                  'edit',
 866                  'vim-q comment-inline',
 867                  esc_attr__( 'Quick edit this comment inline' ),
 868                  __( 'Quick&nbsp;Edit' )
 869              );
 870  
 871              $actions['reply'] = sprintf(
 872                  $format,
 873                  $comment->comment_ID,
 874                  $comment->comment_post_ID,
 875                  'replyto',
 876                  'vim-r comment-inline',
 877                  esc_attr__( 'Reply to this comment' ),
 878                  __( 'Reply' )
 879              );
 880          }
 881  
 882          /**
 883           * Filters the action links displayed for each comment in the Comments list table.
 884           *
 885           * @since 2.6.0
 886           *
 887           * @param string[]   $actions An array of comment actions. Default actions include:
 888           *                            'Approve', 'Unapprove', 'Edit', 'Reply', 'Spam',
 889           *                            'Delete', and 'Trash'.
 890           * @param WP_Comment $comment The comment object.
 891           */
 892          $actions = apply_filters( 'comment_row_actions', array_filter( $actions ), $comment );
 893  
 894          $always_visible = false;
 895  
 896          $mode = get_user_setting( 'posts_list_mode', 'list' );
 897  
 898          if ( 'excerpt' === $mode ) {
 899              $always_visible = true;
 900          }
 901  
 902          $output .= '<div class="' . ( $always_visible ? 'row-actions visible' : 'row-actions' ) . '">';
 903  
 904          $i = 0;
 905  
 906          foreach ( $actions as $action => $link ) {
 907              ++$i;
 908  
 909              if ( ( ( 'approve' === $action || 'unapprove' === $action ) && 2 === $i )
 910                  || 1 === $i
 911              ) {
 912                  $separator = '';
 913              } else {
 914                  $separator = ' | ';
 915              }
 916  
 917              // Reply and quickedit need a hide-if-no-js span when not added with Ajax.
 918              if ( ( 'reply' === $action || 'quickedit' === $action ) && ! wp_doing_ajax() ) {
 919                  $action .= ' hide-if-no-js';
 920              } elseif ( ( 'untrash' === $action && 'trash' === $the_comment_status )
 921                  || ( 'unspam' === $action && 'spam' === $the_comment_status )
 922              ) {
 923                  if ( '1' === get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true ) ) {
 924                      $action .= ' approve';
 925                  } else {
 926                      $action .= ' unapprove';
 927                  }
 928              }
 929  
 930              $output .= "<span class='$action'>{$separator}{$link}</span>";
 931          }
 932  
 933          $output .= '</div>';
 934  
 935          $output .= '<button type="button" class="toggle-row"><span class="screen-reader-text">' .
 936              /* translators: Hidden accessibility text. */
 937              __( 'Show more details' ) .
 938          '</span></button>';
 939  
 940          return $output;
 941      }
 942  
 943      /**
 944       * Outputs the checkbox column.
 945       *
 946       * @since 5.9.0 Renamed `$comment` to `$item` to match parent class for PHP 8 named parameter support.
 947       *
 948       * @param WP_Comment $item The comment object.
 949       */
 950  	public function column_cb( $item ) {
 951          // Restores the more descriptive, specific name for use within this method.
 952          $comment = $item;
 953  
 954          if ( $this->user_can ) {
 955              ?>
 956          <input id="cb-select-<?php echo $comment->comment_ID; ?>" type="checkbox" name="delete_comments[]" value="<?php echo $comment->comment_ID; ?>" />
 957          <label for="cb-select-<?php echo $comment->comment_ID; ?>">
 958              <span class="screen-reader-text">
 959              <?php
 960              /* translators: Hidden accessibility text. */
 961              _e( 'Select comment' );
 962              ?>
 963              </span>
 964          </label>
 965              <?php
 966          }
 967      }
 968  
 969      /**
 970       * Outputs the comment column.
 971       *
 972       * @param WP_Comment $comment The comment object.
 973       */
 974  	public function column_comment( $comment ) {
 975          echo '<div class="comment-author">';
 976              $this->column_author( $comment );
 977          echo '</div>';
 978  
 979          if ( $comment->comment_parent ) {
 980              $parent = get_comment( $comment->comment_parent );
 981  
 982              if ( $parent ) {
 983                  $parent_link = esc_url( get_comment_link( $parent ) );
 984                  $name        = get_comment_author( $parent );
 985                  printf(
 986                      /* translators: %s: Comment link. */
 987                      __( 'In reply to %s.' ),
 988                      '<a href="' . $parent_link . '">' . $name . '</a>'
 989                  );
 990              }
 991          }
 992  
 993          comment_text( $comment );
 994  
 995          if ( $this->user_can ) {
 996              /** This filter is documented in wp-admin/includes/comment.php */
 997              $comment_content = apply_filters( 'comment_edit_pre', $comment->comment_content );
 998              ?>
 999          <div id="inline-<?php echo $comment->comment_ID; ?>" class="hidden">
1000              <textarea class="comment" rows="1" cols="1"><?php echo esc_textarea( $comment_content ); ?></textarea>
1001              <div class="author-email"><?php echo esc_html( $comment->comment_author_email ); ?></div>
1002              <div class="author"><?php echo esc_html( $comment->comment_author ); ?></div>
1003              <div class="author-url"><?php echo esc_url( $comment->comment_author_url ); ?></div>
1004              <div class="comment_status"><?php echo $comment->comment_approved; ?></div>
1005          </div>
1006              <?php
1007          }
1008      }
1009  
1010      /**
1011       * Outputs the author column.
1012       *
1013       * @global string $comment_status The current comment status.
1014       *
1015       * @param WP_Comment $comment The comment object.
1016       */
1017  	public function column_author( $comment ) {
1018          global $comment_status;
1019  
1020          $author_url = get_comment_author_url( $comment );
1021  
1022          $author_url_display = untrailingslashit( preg_replace( '|^http(s)?://(www\.)?|i', '', $author_url ) );
1023  
1024          if ( strlen( $author_url_display ) > 50 ) {
1025              $author_url_display = wp_html_excerpt( $author_url_display, 49, '&hellip;' );
1026          }
1027  
1028          echo '<strong>';
1029          comment_author( $comment );
1030          echo '</strong><br />';
1031  
1032          if ( ! empty( $author_url_display ) ) {
1033              // Print link to author URL, and disallow referrer information (without using target="_blank").
1034              printf(
1035                  '<a href="%s" rel="noopener noreferrer">%s</a><br />',
1036                  esc_url( $author_url ),
1037                  esc_html( $author_url_display )
1038              );
1039          }
1040  
1041          if ( $this->user_can ) {
1042              if ( ! empty( $comment->comment_author_email ) ) {
1043                  /** This filter is documented in wp-includes/comment-template.php */
1044                  $email = apply_filters( 'comment_email', $comment->comment_author_email, $comment );
1045  
1046                  if ( ! empty( $email ) && '@' !== $email ) {
1047                      printf( '<a href="%1$s">%2$s</a><br />', esc_url( 'mailto:' . $email ), esc_html( $email ) );
1048                  }
1049              }
1050  
1051              $author_ip = get_comment_author_IP( $comment );
1052  
1053              if ( $author_ip ) {
1054                  $author_ip_url = add_query_arg(
1055                      array(
1056                          's'    => $author_ip,
1057                          'mode' => 'detail',
1058                      ),
1059                      admin_url( 'edit-comments.php' )
1060                  );
1061  
1062                  if ( 'spam' === $comment_status ) {
1063                      $author_ip_url = add_query_arg( 'comment_status', 'spam', $author_ip_url );
1064                  }
1065  
1066                  printf( '<a href="%1$s">%2$s</a>', esc_url( $author_ip_url ), esc_html( $author_ip ) );
1067              }
1068          }
1069      }
1070  
1071      /**
1072       * Outputs the date column.
1073       *
1074       * @param WP_Comment $comment The comment object.
1075       */
1076  	public function column_date( $comment ) {
1077          $submitted = sprintf(
1078              /* translators: 1: Comment date, 2: Comment time. */
1079              __( '%1$s at %2$s' ),
1080              /* translators: Comment date format. See https://www.php.net/manual/datetime.format.php */
1081              get_comment_date( __( 'Y/m/d' ), $comment ),
1082              /* translators: Comment time format. See https://www.php.net/manual/datetime.format.php */
1083              get_comment_date( __( 'g:i a' ), $comment )
1084          );
1085  
1086          echo '<div class="submitted-on">';
1087  
1088          if ( 'approved' === wp_get_comment_status( $comment ) && ! empty( $comment->comment_post_ID ) ) {
1089              printf(
1090                  '<a href="%s">%s</a>',
1091                  esc_url( get_comment_link( $comment ) ),
1092                  $submitted
1093              );
1094          } else {
1095              echo $submitted;
1096          }
1097  
1098          echo '</div>';
1099      }
1100  
1101      /**
1102       * Outputs the response column.
1103       *
1104       * @param WP_Comment $comment The comment object.
1105       */
1106  	public function column_response( $comment ) {
1107          $post = get_post();
1108  
1109          if ( ! $post ) {
1110              return;
1111          }
1112  
1113          if ( isset( $this->pending_count[ $post->ID ] ) ) {
1114              $pending_comments = $this->pending_count[ $post->ID ];
1115          } else {
1116              $_pending_count_temp              = get_pending_comments_num( array( $post->ID ) );
1117              $pending_comments                 = $_pending_count_temp[ $post->ID ];
1118              $this->pending_count[ $post->ID ] = $pending_comments;
1119          }
1120  
1121          if ( current_user_can( 'edit_post', $post->ID ) ) {
1122              $post_link  = "<a href='" . get_edit_post_link( $post->ID ) . "' class='comments-edit-item-link'>";
1123              $post_link .= esc_html( get_the_title( $post->ID ) ) . '</a>';
1124          } else {
1125              $post_link = esc_html( get_the_title( $post->ID ) );
1126          }
1127  
1128          echo '<div class="response-links">';
1129  
1130          if ( 'attachment' === $post->post_type ) {
1131              $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true );
1132              if ( $thumb ) {
1133                  echo $thumb;
1134              }
1135          }
1136  
1137          echo $post_link;
1138  
1139          $post_type_object = get_post_type_object( $post->post_type );
1140          echo "<a href='" . get_permalink( $post->ID ) . "' class='comments-view-item-link'>" . $post_type_object->labels->view_item . '</a>';
1141  
1142          echo '<span class="post-com-count-wrapper post-com-count-', $post->ID, '">';
1143          $this->comments_bubble( $post->ID, $pending_comments );
1144          echo '</span> ';
1145  
1146          echo '</div>';
1147      }
1148  
1149      /**
1150       * Outputs the default column.
1151       *
1152       * @since 5.9.0 Renamed `$comment` to `$item` to match parent class for PHP 8 named parameter support.
1153       *
1154       * @param WP_Comment $item        The comment object.
1155       * @param string     $column_name The custom column's name.
1156       */
1157  	public function column_default( $item, $column_name ) {
1158          // Restores the more descriptive, specific name for use within this method.
1159          $comment = $item;
1160  
1161          /**
1162           * Fires when the default column output is displayed for a single row.
1163           *
1164           * @since 2.8.0
1165           *
1166           * @param string $column_name The custom column's name.
1167           * @param string $comment_id  The comment ID as a numeric string.
1168           */
1169          do_action( 'manage_comments_custom_column', $column_name, $comment->comment_ID );
1170      }
1171  }


Generated : Sun Mar 8 08:20:03 2026 Cross-referenced by PHPXref