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


Generated : Sat Oct 24 08:20:34 2026 Cross-referenced by PHPXref