From 4ba60208d4bc72601134f17c74c85cf65ff4bdb6 Mon Sep 17 00:00:00 2001 From: Jake Spurlock Date: Tue, 21 Jul 2020 00:27:46 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@48521 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-wp-comments-list-table.php | 96 +++++++++++++------ .../phpunit/tests/admin/includesListTable.php | 58 +++++++++++ 2 files changed, 124 insertions(+), 30 deletions(-) diff --git a/src/wp-admin/includes/class-wp-comments-list-table.php b/src/wp-admin/includes/class-wp-comments-list-table.php index 5f0d373547..b76ad9162a 100644 --- a/src/wp-admin/includes/class-wp-comments-list-table.php +++ b/src/wp-admin/includes/class-wp-comments-list-table.php @@ -387,43 +387,24 @@ class WP_Comments_List_Table extends WP_List_Table { if ( ! isset( $has_items ) ) { $has_items = $this->has_items(); } - ?> -
- '; if ( 'top' === $which ) { - ?> - - - comment_status_dropdown( $comment_type ); /** * Fires just before the Filter submit button for comment types. * * @since 3.5.0 */ do_action( 'restrict_manage_comments' ); - submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) ); + + $output = ob_get_clean(); + + if ( ! empty( $output ) && $this->has_items() ) { + echo $output; + submit_button( esc_html__( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) ); + } } if ( ( 'spam' === $comment_status || 'trash' === $comment_status ) && current_user_can( 'moderate_comments' ) && $has_items ) { @@ -480,6 +461,55 @@ class WP_Comments_List_Table extends WP_List_Table { return $columns; } + /** + * Displays a comment status drop-down for filtering on the Comments list table. + * + * @since 5.5.0 + * + * @param string $comment_type The current comment type slug. + */ + protected function comment_status_dropdown( $comment_type ) { + /** + * Filters the comment types dropdown menu. + * + * @since 2.7.0 + * + * @param array $comment_types An array of comment types. Accepts 'Comments', 'Pings'. + */ + $comment_types = apply_filters( + 'admin_comment_types_dropdown', + array( + 'comment' => esc_html__( 'Comments' ), + 'pings' => esc_html__( 'Pings' ), + ) + ); + + if ( $comment_types && is_array( $comment_types ) ) { + printf( '', esc_html__( 'Filter by comment type' ) ); + + echo ''; + } + } + /** * @return array */ @@ -511,8 +541,14 @@ class WP_Comments_List_Table extends WP_List_Table { */ public function display() { wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' ); + static $has_items; - $this->display_tablenav( 'top' ); + if ( ! isset( $has_items ) ) { + $has_items = $this->has_items(); + if ( $has_items ) { + $this->display_tablenav( 'top' ); + } + } $this->screen->render_screen_reader_content( 'heading_list' ); diff --git a/tests/phpunit/tests/admin/includesListTable.php b/tests/phpunit/tests/admin/includesListTable.php index 745b224e9c..6ca5b61a4b 100644 --- a/tests/phpunit/tests/admin/includesListTable.php +++ b/tests/phpunit/tests/admin/includesListTable.php @@ -281,6 +281,64 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase { $this->assertNotContains( 'id="delete_all"', $output ); } + /** + * @ticket 40188 + */ + public function test_filter_button_should_not_be_shown_if_there_are_no_comments() { + $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); + + ob_start(); + $table->extra_tablenav( 'top' ); + $output = ob_get_clean(); + + $this->assertNotContains( 'id="post-query-submit"', $output ); + } + + /** + * @ticket 40188 + */ + public function test_filter_button_should_be_shown_if_there_are_comments() { + $post_id = self::factory()->post->create(); + $comment_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_approved' => '1', + ) + ); + + $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); + $table->prepare_items(); + + ob_start(); + $table->extra_tablenav( 'top' ); + $output = ob_get_clean(); + + $this->assertContains( 'id="post-query-submit"', $output ); + } + + /** + * @ticket 40188 + */ + public function test_filter_comment_status_dropdown_should_be_shown_if_there_are_comments() { + $post_id = self::factory()->post->create(); + $comment_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_approved' => '1', + ) + ); + + $table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); + $table->prepare_items(); + + ob_start(); + $table->extra_tablenav( 'top' ); + $output = ob_get_clean(); + + $this->assertContains( 'id="filter-by-comment-type"', $output ); + $this->assertContains( "