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


Generated : Thu Dec 3 08:20:02 2026 Cross-referenced by PHPXref