Make WordPress Core

Changeset 48521


Ignore:
Timestamp:
07/21/2020 12:27:46 AM (4 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.

Location:
trunk
Files:
2 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' );
  • trunk/tests/phpunit/tests/admin/includesListTable.php

    r48165 r48521  
    283283
    284284    /**
     285     * @ticket 40188
     286     */
     287    public function test_filter_button_should_not_be_shown_if_there_are_no_comments() {
     288        $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
     289
     290        ob_start();
     291        $table->extra_tablenav( 'top' );
     292        $output = ob_get_clean();
     293
     294        $this->assertNotContains( 'id="post-query-submit"', $output );
     295    }
     296
     297    /**
     298     * @ticket 40188
     299     */
     300    public function test_filter_button_should_be_shown_if_there_are_comments() {
     301        $post_id    = self::factory()->post->create();
     302        $comment_id = self::factory()->comment->create(
     303            array(
     304                'comment_post_ID'  => $post_id,
     305                'comment_approved' => '1',
     306            )
     307        );
     308
     309        $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
     310        $table->prepare_items();
     311
     312        ob_start();
     313        $table->extra_tablenav( 'top' );
     314        $output = ob_get_clean();
     315
     316        $this->assertContains( 'id="post-query-submit"', $output );
     317    }
     318
     319    /**
     320     * @ticket 40188
     321     */
     322    public function test_filter_comment_status_dropdown_should_be_shown_if_there_are_comments() {
     323        $post_id    = self::factory()->post->create();
     324        $comment_id = self::factory()->comment->create(
     325            array(
     326                'comment_post_ID'  => $post_id,
     327                'comment_approved' => '1',
     328            )
     329        );
     330
     331        $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
     332        $table->prepare_items();
     333
     334        ob_start();
     335        $table->extra_tablenav( 'top' );
     336        $output = ob_get_clean();
     337
     338        $this->assertContains( 'id="filter-by-comment-type"', $output );
     339        $this->assertContains( "<option value='comment'>", $output );
     340    }
     341
     342    /**
    285343     * @ticket 38341
    286344     */
Note: See TracChangeset for help on using the changeset viewer.