From 11d556e42b53d572813600584fcb26a4df083bae Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 17 Mar 2017 16:53:08 +0000 Subject: [PATCH] List Tables: Hide 'Empty Trash' and 'Empty Spam' buttons when view is already empty. Props ivantedja, Presskopp, printsachen1, Jaydeep Rami, mathieuhays, cazm. Fixes #38341. git-svn-id: https://develop.svn.wordpress.org/trunk@40297 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-wp-comments-list-table.php | 2 +- .../includes/class-wp-media-list-table.php | 2 +- .../includes/class-wp-posts-list-table.php | 2 +- src/wp-admin/js/edit-comments.js | 7 +++++ .../phpunit/tests/admin/includesListTable.php | 27 +++++++++++++++++++ 5 files changed, 37 insertions(+), 3 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 8f53ae4be7..fa41a7203c 100644 --- a/src/wp-admin/includes/class-wp-comments-list-table.php +++ b/src/wp-admin/includes/class-wp-comments-list-table.php @@ -354,7 +354,7 @@ class WP_Comments_List_Table extends WP_List_Table { submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) ); } - if ( ( 'spam' === $comment_status || 'trash' === $comment_status ) && current_user_can( 'moderate_comments' ) ) { + if ( ( 'spam' === $comment_status || 'trash' === $comment_status ) && current_user_can( 'moderate_comments' ) && $this->has_items() ) { wp_nonce_field( 'bulk-destroy', '_destroy_nonce' ); $title = ( 'spam' === $comment_status ) ? esc_attr__( 'Empty Spam' ) : esc_attr__( 'Empty Trash' ); submit_button( $title, 'apply', 'delete_all', false ); diff --git a/src/wp-admin/includes/class-wp-media-list-table.php b/src/wp-admin/includes/class-wp-media-list-table.php index 97b166e59c..805f4017d3 100644 --- a/src/wp-admin/includes/class-wp-media-list-table.php +++ b/src/wp-admin/includes/class-wp-media-list-table.php @@ -177,7 +177,7 @@ class WP_Media_List_Table extends WP_List_Table { submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) ); } - if ( $this->is_trash && current_user_can( 'edit_others_posts' ) ) { + if ( $this->is_trash && current_user_can( 'edit_others_posts' ) && $this->has_items() ) { submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false ); } ?> diff --git a/src/wp-admin/includes/class-wp-posts-list-table.php b/src/wp-admin/includes/class-wp-posts-list-table.php index 668c3d20a4..535a2dd0c8 100644 --- a/src/wp-admin/includes/class-wp-posts-list-table.php +++ b/src/wp-admin/includes/class-wp-posts-list-table.php @@ -489,7 +489,7 @@ class WP_Posts_List_Table extends WP_List_Table { } } - if ( $this->is_trash && current_user_can( get_post_type_object( $this->screen->post_type )->cap->edit_others_posts ) ) { + if ( $this->is_trash && current_user_can( get_post_type_object( $this->screen->post_type )->cap->edit_others_posts ) && $this->has_items() ) { submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false ); } ?> diff --git a/src/wp-admin/js/edit-comments.js b/src/wp-admin/js/edit-comments.js index 3de031022c..9e0fc09602 100644 --- a/src/wp-admin/js/edit-comments.js +++ b/src/wp-admin/js/edit-comments.js @@ -461,6 +461,13 @@ setCommentsList = function() { updateCountText( 'span.trash-count', trashDiff ); } + if ( + ( ( 'trash' === settings.data.comment_status ) && !getCount( $( 'span.trash-count' ) ) ) || + ( ( 'spam' === settings.data.comment_status ) && !getCount( $( 'span.spam-count' ) ) ) + ) { + $( '#delete_all' ).hide(); + } + if ( ! isDashboard ) { total = totalInput.val() ? parseInt( totalInput.val(), 10 ) : 0; if ( $(settings.target).parent().is('span.undo') ) diff --git a/tests/phpunit/tests/admin/includesListTable.php b/tests/phpunit/tests/admin/includesListTable.php index b0be74f28c..cf40457cdf 100644 --- a/tests/phpunit/tests/admin/includesListTable.php +++ b/tests/phpunit/tests/admin/includesListTable.php @@ -235,4 +235,31 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase { $this->assertNotContains( 'id="cat"', $output ); } + + /** + * @ticket 38341 + */ + public function test_empty_trash_button_should_not_be_shown_if_there_are_no_posts() { + // Set post type to a non-existent one. + $this->table->screen->post_type = 'foo'; + + ob_start(); + $this->table->extra_tablenav( 'top' ); + $output = ob_get_clean(); + + $this->assertNotContains( 'id="delete_all"', $output ); + } + + /** + * @ticket 38341 + */ + public function test_empty_trash_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="delete_all"', $output ); + } }