Make WordPress Core


Ignore:
Timestamp:
07/21/2020 12:27:46 AM (6 years ago)
Author:
whyisjake
Message:

Comments: Don't show the filter/pagination actions if there are no comments to list.

It doesn't make sense to be able to filter the comments list table when there are are no (trashed/spam) comments available.

Fixes #40188.
Props swissspidy, Jim_Panse, menakas, akbarhusen429, dinhtungdu, birgire, SergeyBiryukov, davidbaumwald, rebasaurus, whyisjake.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-comments-list-table.php

    r48450 r48521  
    388388                        $has_items = $this->has_items();
    389389                }
    390                 ?>
    391                 <div class="alignleft actions">
    392                 <?php
     390                echo '<div class="alignleft actions">';
    393391                if ( 'top' === $which ) {
    394                         ?>
    395         <label class="screen-reader-text" for="filter-by-comment-type"><?php _e( 'Filter by comment type' ); ?></label>
    396         <select id="filter-by-comment-type" name="comment_type">
    397                 <option value=""><?php _e( 'All comment types' ); ?></option>
    398                         <?php
    399                                 /**
    400                                  * Filters the comment types dropdown menu.
    401                                  *
    402                                  * @since 2.7.0
    403                                  *
    404                                  * @param string[] $comment_types An array of comment types. Accepts 'Comments', 'Pings'.
    405                                  */
    406                                 $comment_types = apply_filters(
    407                                         'admin_comment_types_dropdown',
    408                                         array(
    409                                                 'comment' => __( 'Comments' ),
    410                                                 'pings'   => __( 'Pings' ),
    411                                         )
    412                                 );
    413 
    414                         foreach ( $comment_types as $type => $label ) {
    415                                 echo "\t" . '<option value="' . esc_attr( $type ) . '"' . selected( $comment_type, $type, false ) . ">$label</option>\n";
    416                         }
    417                         ?>
    418         </select>
    419                         <?php
     392                        ob_start();
     393
     394                        $this->comment_status_dropdown( $comment_type );
    420395                        /**
    421396                         * Fires just before the Filter submit button for comment types.
    … …  
    424399                         */
    425400                        do_action( 'restrict_manage_comments' );
    426                         submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
     401
     402                        $output = ob_get_clean();
     403
     404                        if ( ! empty( $output ) && $this->has_items() ) {
     405                                echo $output;
     406                                submit_button( esc_html__( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
     407                        }
    427408                }
    428409
    … …  
    479460
    480461                return $columns;
     462        }
     463
     464        /**
     465         * Displays a comment status drop-down for filtering on the Comments list table.
     466         *
     467         * @since 5.5.0
     468         *
     469         * @param string $comment_type The current comment type slug.
     470         */
     471        protected function comment_status_dropdown( $comment_type ) {
     472                /**
     473                 * Filters the comment types dropdown menu.
     474                 *
     475                 * @since 2.7.0
     476                 *
     477                 * @param array $comment_types An array of comment types. Accepts 'Comments', 'Pings'.
     478                 */
     479                $comment_types = apply_filters(
     480                        'admin_comment_types_dropdown',
     481                        array(
     482                                'comment' => esc_html__( 'Comments' ),
     483                                'pings'   => esc_html__( 'Pings' ),
     484                        )
     485                );
     486
     487                if ( $comment_types && is_array( $comment_types ) ) {
     488                        printf( '<label class="screen-reader-text" for="filter-by-comment-type">%s</label>', esc_html__( 'Filter by comment type' ) );
     489
     490                        echo '<select id="filter-by-comment-type" name="comment_type">';
     491
     492                        printf( "\t<option value=''>%s</option>", esc_html__( 'All comment types' ) );
     493
     494                        foreach ( $comment_types as $type => $label ) {
     495                                if ( get_comments(
     496                                        array(
     497                                                'number' => 1,
     498                                                'type'   => $type,
     499                                        )
     500                                ) ) {
     501                                        printf(
     502                                                "\t<option value='%s'%s>%s</option>\n",
     503                                                esc_attr( $type ),
     504                                                selected( $comment_type, $type, false ),
     505                                                esc_html( $label )
     506                                        );
     507                                }
     508                        }
     509                                echo '</select>';
     510                }
    481511        }
    482512
    … …  
    512542        public function display() {
    513543                wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' );
    514 
    515                 $this->display_tablenav( 'top' );
     544                static $has_items;
     545
     546                if ( ! isset( $has_items ) ) {
     547                        $has_items = $this->has_items();
     548                        if ( $has_items ) {
     549                                $this->display_tablenav( 'top' );
     550                        }
     551                }
    516552
    517553                $this->screen->render_screen_reader_content( 'heading_list' );
Note: See TracChangeset for help on using the changeset viewer.