From aa7d2e2b175328a5b67906a128a2cfd2c76b3375 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 6 Oct 2026 22:20:20 +0300 Subject: [PATCH 1/9] Comments: Hide zero-count status filters on the Comments list table WP_Posts_List_Table::get_views() hides any status filter link (other than "All") whose post count is zero. WP_Comments_List_Table::get_views() had no equivalent check, so "Pending", "Approved", "Spam", "Trash", and "Mine" were always shown even with a count of (0), cluttering the screen with filters that return no results. Apply the same logic: skip adding a status link when its count is empty, except for "All" which always stays visible. This reverses the behavior asserted by test_get_views_should_return_views_by_default (added for #42066), which is removed since it codified the opposite, now-undesired behavior. No replacement test is added in this change. Co-Authored-By: Claude Sonnet 5 --- .../includes/class-wp-comments-list-table.php | 5 +++++ .../tests/admin/wpCommentsListTable.php | 19 ------------------- 2 files changed, 5 insertions(+), 19 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 2b927a7f81a6a..42ca66841ea00 100644 --- a/src/wp-admin/includes/class-wp-comments-list-table.php +++ b/src/wp-admin/includes/class-wp-comments-list-table.php @@ -327,6 +327,11 @@ protected function get_views() { $num_comments->$status = 10; } + // Don't show statuses (other than "All") with zero comments, as on the Posts list table. + if ( 'all' !== $status && empty( $num_comments->$status ) ) { + continue; + } + $link = add_query_arg( 'comment_status', $status, $link ); if ( $post_id ) { diff --git a/tests/phpunit/tests/admin/wpCommentsListTable.php b/tests/phpunit/tests/admin/wpCommentsListTable.php index 47b9bec57f140..d7419eae5a2e3 100644 --- a/tests/phpunit/tests/admin/wpCommentsListTable.php +++ b/tests/phpunit/tests/admin/wpCommentsListTable.php @@ -195,25 +195,6 @@ public function test_sortable_columns_with_current_ordering() { $this->assertStringContainsString( 'column-date sorted desc', $output, 'Mismatch of CSS classes for the comment date column.' ); } - /** - * @ticket 42066 - * - * @covers WP_Comments_List_Table::get_views - */ - public function test_get_views_should_return_views_by_default() { - $this->table->prepare_items(); - - $expected = array( - 'all' => 'All (0)', - 'mine' => 'Mine (0)', - 'moderated' => 'Pending (0)', - 'approved' => 'Approved (0)', - 'spam' => 'Spam (0)', - 'trash' => 'Trash (0)', - ); - $this->assertSame( $expected, $this->table->get_views() ); - } - /** * Verify that the comments table never shows the note comment_type. * From ccd1d782f6b9f675b84b7a559d954eefd5f7e059 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Tue, 6 Oct 2026 22:32:15 +0300 Subject: [PATCH 2/9] Comments: Add tests for hiding zero-count status filters Covers WP_Comments_List_Table::get_views() hiding "Pending", "Approved", "Spam", and "Trash" links when their count is zero, while keeping "All" visible, and showing a status link again once its count becomes nonzero. Co-Authored-By: Claude Sonnet 5 --- .../tests/admin/wpCommentsListTable.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/phpunit/tests/admin/wpCommentsListTable.php b/tests/phpunit/tests/admin/wpCommentsListTable.php index d7419eae5a2e3..5abab8190564e 100644 --- a/tests/phpunit/tests/admin/wpCommentsListTable.php +++ b/tests/phpunit/tests/admin/wpCommentsListTable.php @@ -195,6 +195,43 @@ public function test_sortable_columns_with_current_ordering() { $this->assertStringContainsString( 'column-date sorted desc', $output, 'Mismatch of CSS classes for the comment date column.' ); } + /** + * @covers WP_Comments_List_Table::get_views + */ + public function test_get_views_should_hide_empty_status_links() { + $this->table->prepare_items(); + + $views = $this->table->get_views(); + + $this->assertArrayHasKey( 'all', $views, 'The "All" view should always be shown.' ); + $this->assertArrayNotHasKey( 'moderated', $views, 'Views with a zero count should be hidden.' ); + $this->assertArrayNotHasKey( 'approved', $views, 'Views with a zero count should be hidden.' ); + $this->assertArrayNotHasKey( 'spam', $views, 'Views with a zero count should be hidden.' ); + $this->assertArrayNotHasKey( 'trash', $views, 'Views with a zero count should be hidden.' ); + } + + /** + * @covers WP_Comments_List_Table::get_views + */ + public function test_get_views_should_show_status_link_with_nonzero_count() { + $post_id = self::factory()->post->create(); + self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_approved' => '1', + ) + ); + + $this->table->prepare_items(); + + $views = $this->table->get_views(); + + $this->assertArrayHasKey( 'approved', $views, 'A view with a nonzero count should be shown.' ); + $this->assertArrayNotHasKey( 'moderated', $views, 'Views with a zero count should still be hidden.' ); + $this->assertArrayNotHasKey( 'spam', $views, 'Views with a zero count should still be hidden.' ); + $this->assertArrayNotHasKey( 'trash', $views, 'Views with a zero count should still be hidden.' ); + } + /** * Verify that the comments table never shows the note comment_type. * From c26158156bcb70872d9a8ee5b6d516dc34d15629 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 7 Oct 2026 09:39:48 +0300 Subject: [PATCH 3/9] Comments: Keep Pending/Approved always visible and never hide the active view Addresses review feedback on the zero-count filtering added in the previous commit. Comments is a moderation queue, not a plain list like Posts, so: - "Pending" and "Approved" now always stay visible regardless of count. A "(0)" on Pending is reassuring confirmation of an empty queue, not clutter, and hiding it could read as moderation being broken. - The currently active $comment_status is never hidden, even at zero count, so an admin who lands on e.g. ?comment_status=moderated (from a stale notification) or ?comment_status=spam (after "Empty Spam") always has a visible, "current" tab showing what they're filtered to. "Mine", "Spam", and "Trash" are still hidden when empty and not the active view. Updated the existing get_views() tests for the new rules and added a regression test for the active-view exception. Co-Authored-By: Claude Sonnet 5 --- .../includes/class-wp-comments-list-table.php | 14 +++++++++-- .../tests/admin/wpCommentsListTable.php | 23 ++++++++++++++++--- 2 files changed, 32 insertions(+), 5 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 42ca66841ea00..8b72032617665 100644 --- a/src/wp-admin/includes/class-wp-comments-list-table.php +++ b/src/wp-admin/includes/class-wp-comments-list-table.php @@ -327,8 +327,18 @@ protected function get_views() { $num_comments->$status = 10; } - // Don't show statuses (other than "All") with zero comments, as on the Posts list table. - if ( 'all' !== $status && empty( $num_comments->$status ) ) { + /* + * Don't show "Mine", "Spam", or "Trash" when they have zero comments, to avoid + * cluttering the moderation queue with filters that return no results. "All", + * "Pending", and "Approved" always stay visible: pending is an active task queue + * where "(0)" is reassuring confirmation, and approved is the default working view. + * The currently requested view is never hidden, regardless of its count, so the + * admin always has a visual indicator of what's being filtered (e.g. landing on + * ?comment_status=moderated from a notification after it's already been cleared). + */ + $always_visible = array( 'all', 'moderated', 'approved' ); + + if ( ! in_array( $status, $always_visible, true ) && empty( $num_comments->$status ) && $status !== $comment_status ) { continue; } diff --git a/tests/phpunit/tests/admin/wpCommentsListTable.php b/tests/phpunit/tests/admin/wpCommentsListTable.php index 5abab8190564e..a3736be0390dc 100644 --- a/tests/phpunit/tests/admin/wpCommentsListTable.php +++ b/tests/phpunit/tests/admin/wpCommentsListTable.php @@ -204,8 +204,9 @@ public function test_get_views_should_hide_empty_status_links() { $views = $this->table->get_views(); $this->assertArrayHasKey( 'all', $views, 'The "All" view should always be shown.' ); - $this->assertArrayNotHasKey( 'moderated', $views, 'Views with a zero count should be hidden.' ); - $this->assertArrayNotHasKey( 'approved', $views, 'Views with a zero count should be hidden.' ); + $this->assertArrayHasKey( 'moderated', $views, '"Pending" should always be shown, even at zero.' ); + $this->assertArrayHasKey( 'approved', $views, '"Approved" should always be shown, even at zero.' ); + $this->assertArrayNotHasKey( 'mine', $views, 'Views with a zero count should be hidden.' ); $this->assertArrayNotHasKey( 'spam', $views, 'Views with a zero count should be hidden.' ); $this->assertArrayNotHasKey( 'trash', $views, 'Views with a zero count should be hidden.' ); } @@ -227,11 +228,27 @@ public function test_get_views_should_show_status_link_with_nonzero_count() { $views = $this->table->get_views(); $this->assertArrayHasKey( 'approved', $views, 'A view with a nonzero count should be shown.' ); - $this->assertArrayNotHasKey( 'moderated', $views, 'Views with a zero count should still be hidden.' ); + $this->assertArrayHasKey( 'moderated', $views, '"Pending" should always be shown, even at zero.' ); $this->assertArrayNotHasKey( 'spam', $views, 'Views with a zero count should still be hidden.' ); $this->assertArrayNotHasKey( 'trash', $views, 'Views with a zero count should still be hidden.' ); } + /** + * @covers WP_Comments_List_Table::get_views + */ + public function test_get_views_should_not_hide_the_currently_active_status() { + $_REQUEST['comment_status'] = 'spam'; + + $this->table->prepare_items(); + + $views = $this->table->get_views(); + + $this->assertArrayHasKey( 'spam', $views, 'The currently active view must never be hidden, even at zero count.' ); + $this->assertStringContainsString( 'class="current"', $views['spam'], 'The currently active view should be marked current.' ); + + unset( $_REQUEST['comment_status'] ); + } + /** * Verify that the comments table never shows the note comment_type. * From f395aee2840faf8fbfb71feae9e7e2f001341478 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Wed, 7 Oct 2026 10:41:50 +0300 Subject: [PATCH 4/9] Comments: Update WP_Post_Comments_List_Table test for zero-count view hiding WP_Post_Comments_List_Table extends WP_Comments_List_Table and inherits its get_views(), so it was asserting the same superseded all-statuses-always-shown behavior (also added for #42066) that was already updated in wpCommentsListTable.php. Missed in the earlier commit since it lives in a separate test file. Co-Authored-By: Claude Sonnet 5 --- .../tests/admin/wpPostCommentsListTable.php | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/tests/phpunit/tests/admin/wpPostCommentsListTable.php b/tests/phpunit/tests/admin/wpPostCommentsListTable.php index 975bbf2a7c71e..db5080db32586 100644 --- a/tests/phpunit/tests/admin/wpPostCommentsListTable.php +++ b/tests/phpunit/tests/admin/wpPostCommentsListTable.php @@ -18,21 +18,18 @@ public function set_up() { } /** - * @ticket 42066 - * * @covers WP_Post_Comments_List_Table::get_views */ - public function test_get_views_should_return_views_by_default() { + public function test_get_views_should_hide_empty_status_links() { $this->table->prepare_items(); - $expected = array( - 'all' => 'All (0)', - 'mine' => 'Mine (0)', - 'moderated' => 'Pending (0)', - 'approved' => 'Approved (0)', - 'spam' => 'Spam (0)', - 'trash' => 'Trash (0)', - ); - $this->assertSame( $expected, $this->table->get_views() ); + $views = $this->table->get_views(); + + $this->assertArrayHasKey( 'all', $views, 'The "All" view should always be shown.' ); + $this->assertArrayHasKey( 'moderated', $views, '"Pending" should always be shown, even at zero.' ); + $this->assertArrayHasKey( 'approved', $views, '"Approved" should always be shown, even at zero.' ); + $this->assertArrayNotHasKey( 'mine', $views, 'Views with a zero count should be hidden.' ); + $this->assertArrayNotHasKey( 'spam', $views, 'Views with a zero count should be hidden.' ); + $this->assertArrayNotHasKey( 'trash', $views, 'Views with a zero count should be hidden.' ); } } From f420d1d8bd3962e4d11283a467941ff3bf2ca8d6 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Thu, 8 Oct 2026 21:36:54 +0300 Subject: [PATCH 5/9] Comments: Show a status link immediately when it goes from zero to non-zero via AJAX Status links (e.g. "Trash", "Spam") are now hidden entirely when their count is zero (see WP_Comments_List_Table::get_views()). edit-comments.js only updates the *text* of existing count spans after an AJAX moderation action, so when a status link was never rendered in the first place, incrementing its (non-existent) count text does nothing: trashing the first comment on a site with no prior trashed comments left no way to reach the Trash view without a full page reload. _wp_ajax_delete_comment_response() now re-renders the comment status view tabs (WP_Comments_List_Table::views()), using the requesting page's own comment_status/p context (recovered from the posted _url), and includes the fresh HTML in the AJAX response's supplemental data. edit-comments.js replaces the whole .subsubsub list with it, after the existing incremental count updates (since it supersedes them), so a status that just crossed zero appears or disappears immediately. Added a test asserting the "Trash" link is present in the AJAX response after trashing the only trashable comment; confirmed it fails without the fix and passes with it. Co-Authored-By: Claude Sonnet 5 --- src/js/_enqueues/admin/edit-comments.js | 12 ++++++ src/wp-admin/includes/ajax-actions.php | 33 +++++++++++++++ .../tests/ajax/wpAjaxDeleteComment.php | 41 +++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/src/js/_enqueues/admin/edit-comments.js b/src/js/_enqueues/admin/edit-comments.js index 7e89f1c0dd7cb..94c0bd3e4e1cf 100644 --- a/src/js/_enqueues/admin/edit-comments.js +++ b/src/js/_enqueues/admin/edit-comments.js @@ -642,6 +642,18 @@ window.setCommentsList = function() { updateCountText( 'span.trash-count', trashDiff ); } + /* + * Status links (e.g. "Trash") are hidden entirely when their count is + * zero, so the count updates above have nothing to show/hide. Replace + * the whole view tabs list with a freshly rendered, authoritative copy + * from the server so a status that just went from zero to non-zero + * (or vice versa) appears or disappears immediately. This must run + * after the count updates above, since it supersedes them. + */ + if ( response.supplemental && response.supplemental.comment_status_links ) { + $( '.subsubsub' ).replaceWith( response.supplemental.comment_status_links ); + } + if ( ( ( 'trash' === settings.data.comment_status ) && !getCount( $( 'span.trash-count' ) ) ) || ( ( 'spam' === settings.data.comment_status ) && !getCount( $( 'span.spam-count' ) ) ) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 26c8f6b6c0c95..ee2f135b13367 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -570,6 +570,38 @@ function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) { $comment = get_comment( $comment_id ); $counts = wp_count_comments(); + /* + * Re-render the comment status view tabs so that a status link which just + * went from zero to non-zero comments (e.g. "Trash" after trashing the + * first comment) shows up immediately, instead of only after a full page + * reload. Status links are hidden entirely when their count is zero (see + * WP_Comments_List_Table::get_views()), so updating a count in place via + * JS has nothing to update if the link was never rendered. + */ + $parsed_url = parse_url( $url ); + $query_vars = array(); + + if ( ! empty( $parsed_url['query'] ) ) { + parse_str( $parsed_url['query'], $query_vars ); + } + + $valid_comment_statuses = array( 'all', 'mine', 'moderated', 'approved', 'spam', 'trash' ); + $current_comment_status = ! empty( $query_vars['comment_status'] ) ? $query_vars['comment_status'] : 'all'; + + if ( ! in_array( $current_comment_status, $valid_comment_statuses, true ) ) { + $current_comment_status = 'all'; + } + + $wp_list_table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); + + global $post_id, $comment_status; + $post_id = ! empty( $query_vars['p'] ) ? (int) $query_vars['p'] : 0; + $comment_status = $current_comment_status; + + ob_start(); + $wp_list_table->views(); + $comment_status_links = ob_get_clean(); + $response = new WP_Ajax_Response( array( 'what' => 'comment', @@ -589,6 +621,7 @@ function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) { _n( '%s Comment in moderation', '%s Comments in moderation', $counts->moderated ), number_format_i18n( $counts->moderated ) ), + 'comment_status_links' => $comment_status_links, ), ) ); diff --git a/tests/phpunit/tests/ajax/wpAjaxDeleteComment.php b/tests/phpunit/tests/ajax/wpAjaxDeleteComment.php index 26b14dd221b79..0ff56fe0aec27 100644 --- a/tests/phpunit/tests/ajax/wpAjaxDeleteComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxDeleteComment.php @@ -216,6 +216,47 @@ public function _test_with_bad_id( $comment, $action ) { } } + /** + * Trashing the only trashable comment should make the previously-hidden + * "Trash" status link appear in the same response, rather than only + * after a full page reload. + * + * @covers ::_wp_ajax_delete_comment_response + */ + public function test_trashing_only_trashed_comment_includes_trash_link_in_response() { + $this->_setRole( 'administrator' ); + + $comment_id = self::factory()->comment->create( + array( + 'comment_post_ID' => self::$post_id, + 'comment_approved' => '1', + ) + ); + + $_POST['id'] = $comment_id; + $_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_' . $comment_id ); + $_POST['trash'] = '1'; + $_POST['_total'] = '1'; + $_POST['_per_page'] = '20'; + $_POST['_page'] = '1'; + $_POST['_url'] = admin_url( 'edit-comments.php' ); + + try { + $this->_handleAjax( 'delete-comment' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA ); + $comment_status_links = (string) $xml->response[0]->comment[0]->supplemental[0]->comment_status_links[0]; + + $this->assertStringContainsString( + 'comment_status=trash', + $comment_status_links, + 'The "Trash" status link should appear once a comment has actually been trashed.' + ); + } + /** * Tests doubling the action (e.g. trash a trashed comment). * From ae53ce56b7c572286ffda840c53079e61b2923af Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Fri, 9 Oct 2026 09:47:35 +0300 Subject: [PATCH 6/9] Comments: Hide empty status views via CSS instead of re-rendering over AJAX Per review feedback, replace the previous AJAX approach to keeping status view tabs ("Mine", "Spam", "Trash") in sync after a moderation action. Re-rendering WP_Comments_List_Table::views() server-side and replacing the whole .subsubsub node over AJAX (previous commit) works in isolation, but destroys any DOM nodes, event handlers, or data a plugin may have attached to the existing view links on every single moderation action - an architectural regression for plugin extensibility. Instead: - WP_Comments_List_Table::get_views() no longer omits empty, non-active status entries from its returned array; every status key is always present now, and get_views() instead records which ones are empty in a new $empty_views property. - A new WP_Comments_List_Table::views() override (the base WP_List_Table::views() doesn't support this) adds an `is-empty-view` class to those views'
  • elements, hidden via a new CSS rule, rather than omitting their markup. - edit-comments.js's delAfter handler toggles that class on the relevant
  • (via a new toggleEmptyView() helper) after a spam/trash count changes, instead of replacing the view tabs' HTML wholesale. The currently active view is still never hidden, even at zero count. - Reverted the AJAX supplemental data / full-HTML-replace added to _wp_ajax_delete_comment_response() and edit-comments.js in the previous commit. Updated the get_views()/views() tests in wpCommentsListTable.php and wpPostCommentsListTable.php for the new always-present-keys / CSS-class-based behavior, and removed the AJAX response test that asserted the reverted approach. Co-Authored-By: Claude Sonnet 5 --- src/js/_enqueues/admin/edit-comments.js | 41 +++++++---- src/wp-admin/css/list-tables.css | 9 +++ src/wp-admin/includes/ajax-actions.php | 33 --------- .../includes/class-wp-comments-list-table.php | 68 ++++++++++++++++--- .../tests/admin/wpCommentsListTable.php | 53 ++++++++++----- .../tests/admin/wpPostCommentsListTable.php | 28 ++++++-- .../tests/ajax/wpAjaxDeleteComment.php | 41 ----------- 7 files changed, 153 insertions(+), 120 deletions(-) diff --git a/src/js/_enqueues/admin/edit-comments.js b/src/js/_enqueues/admin/edit-comments.js index 94c0bd3e4e1cf..af775129130d8 100644 --- a/src/js/_enqueues/admin/edit-comments.js +++ b/src/js/_enqueues/admin/edit-comments.js @@ -11,7 +11,7 @@ * @param {JQueryStatic} $ The jQuery object. */ (function($) { -var getCount, updateCount, updateCountText, updatePending, updateApproved, +var getCount, updateCount, updateCountText, updatePending, updateApproved, toggleEmptyView, updateHtmlTitle, updateDashboardText, updateInModerationText, adminTitle = document.title, isDashboard = $('#dashboard_right_now').length, titleDiv, titleRegEx, @@ -136,6 +136,31 @@ var getCount, updateCount, updateCountText, updatePending, updateApproved, }); }; + /** + * Shows or re-hides a comment status view tab based on its current count. + * + * Status views with zero comments are rendered with an `is-empty-view` + * class (see WP_Comments_List_Table::views()) that hides them via CSS, + * rather than being omitted from the DOM, so that any handlers or data a + * plugin has attached to them survive. This keeps that class in sync + * after an AJAX moderation action changes a count, without replacing any + * markup. The currently active view is never hidden, even at zero count. + * + * @since 7.2.0 + * @access private + * + * @param {string} status The comment status view to check (e.g. 'spam', 'trash'). + * + * @return {void} + */ + toggleEmptyView = function( status ) { + var view = $( '.subsubsub li.' + status ), + count = getCount( view.find( 'span.' + status + '-count' ) ), + isCurrent = view.find( 'a.current' ).length > 0; + + view.toggleClass( 'is-empty-view', 0 === count && ! isCurrent ); + }; + /** * Updates a text about comment count on the dashboard. * @@ -636,22 +661,12 @@ window.setCommentsList = function() { if ( spamDiff ) { updateCountText( 'span.spam-count', spamDiff ); + toggleEmptyView( 'spam' ); } if ( trashDiff ) { updateCountText( 'span.trash-count', trashDiff ); - } - - /* - * Status links (e.g. "Trash") are hidden entirely when their count is - * zero, so the count updates above have nothing to show/hide. Replace - * the whole view tabs list with a freshly rendered, authoritative copy - * from the server so a status that just went from zero to non-zero - * (or vice versa) appears or disappears immediately. This must run - * after the count updates above, since it supersedes them. - */ - if ( response.supplemental && response.supplemental.comment_status_links ) { - $( '.subsubsub' ).replaceWith( response.supplemental.comment_status_links ); + toggleEmptyView( 'trash' ); } if ( diff --git a/src/wp-admin/css/list-tables.css b/src/wp-admin/css/list-tables.css index f6105ad5a59fb..a2ad93949b387 100644 --- a/src/wp-admin/css/list-tables.css +++ b/src/wp-admin/css/list-tables.css @@ -89,6 +89,15 @@ border-top-color: #3858e9; } +/* + * Comment status views ("Mine", "Spam", "Trash") with zero comments start + * CSS-hidden rather than omitted from the DOM, so edit-comments.js can + * reveal or re-hide them after an AJAX action by toggling this class. + */ +.subsubsub li.is-empty-view { + display: none; +} + /* @todo: consider to use a single rule for these counters and the admin menu counters. */ .column-response .post-com-count-pending, .column-comments .post-com-count-pending { diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index ee2f135b13367..26c8f6b6c0c95 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -570,38 +570,6 @@ function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) { $comment = get_comment( $comment_id ); $counts = wp_count_comments(); - /* - * Re-render the comment status view tabs so that a status link which just - * went from zero to non-zero comments (e.g. "Trash" after trashing the - * first comment) shows up immediately, instead of only after a full page - * reload. Status links are hidden entirely when their count is zero (see - * WP_Comments_List_Table::get_views()), so updating a count in place via - * JS has nothing to update if the link was never rendered. - */ - $parsed_url = parse_url( $url ); - $query_vars = array(); - - if ( ! empty( $parsed_url['query'] ) ) { - parse_str( $parsed_url['query'], $query_vars ); - } - - $valid_comment_statuses = array( 'all', 'mine', 'moderated', 'approved', 'spam', 'trash' ); - $current_comment_status = ! empty( $query_vars['comment_status'] ) ? $query_vars['comment_status'] : 'all'; - - if ( ! in_array( $current_comment_status, $valid_comment_statuses, true ) ) { - $current_comment_status = 'all'; - } - - $wp_list_table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); - - global $post_id, $comment_status; - $post_id = ! empty( $query_vars['p'] ) ? (int) $query_vars['p'] : 0; - $comment_status = $current_comment_status; - - ob_start(); - $wp_list_table->views(); - $comment_status_links = ob_get_clean(); - $response = new WP_Ajax_Response( array( 'what' => 'comment', @@ -621,7 +589,6 @@ function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) { _n( '%s Comment in moderation', '%s Comments in moderation', $counts->moderated ), number_format_i18n( $counts->moderated ) ), - 'comment_status_links' => $comment_status_links, ), ) ); 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 8b72032617665..a7437cf2bbf80 100644 --- a/src/wp-admin/includes/class-wp-comments-list-table.php +++ b/src/wp-admin/includes/class-wp-comments-list-table.php @@ -24,6 +24,16 @@ class WP_Comments_List_Table extends WP_List_Table { private $user_can; + /** + * Status keys of views that should be rendered CSS-hidden by views(). + * + * Populated by get_views() and consumed by the views() override below. + * + * @since 7.2.0 + * @var string[] + */ + private $empty_views = array(); + /** * Constructor. * @@ -250,8 +260,9 @@ public function no_items() { protected function get_views() { global $post_id, $comment_status, $comment_type; - $status_links = array(); - $num_comments = ( $post_id ) ? wp_count_comments( $post_id ) : wp_count_comments(); + $status_links = array(); + $this->empty_views = array(); + $num_comments = ( $post_id ) ? wp_count_comments( $post_id ) : wp_count_comments(); $statuses = array( /* translators: %s: Number of comments. */ @@ -328,18 +339,21 @@ protected function get_views() { } /* - * Don't show "Mine", "Spam", or "Trash" when they have zero comments, to avoid - * cluttering the moderation queue with filters that return no results. "All", - * "Pending", and "Approved" always stay visible: pending is an active task queue - * where "(0)" is reassuring confirmation, and approved is the default working view. - * The currently requested view is never hidden, regardless of its count, so the - * admin always has a visual indicator of what's being filtered (e.g. landing on - * ?comment_status=moderated from a notification after it's already been cleared). + * "Mine", "Spam", and "Trash" are marked for CSS-only hiding when they have + * zero comments, to avoid cluttering the moderation queue with filters that + * return no results, while keeping their markup (and any event handlers a + * plugin may have attached to it) in the DOM. "All", "Pending", and + * "Approved" always stay visible: pending is an active task queue where + * "(0)" is reassuring confirmation, and approved is the default working + * view. The currently requested view is never marked hidden, even at zero + * count, so the admin always has a visual indicator of what's being + * filtered (e.g. landing on ?comment_status=moderated from a notification + * after it's already been cleared). See self::views(). */ $always_visible = array( 'all', 'moderated', 'approved' ); if ( ! in_array( $status, $always_visible, true ) && empty( $num_comments->$status ) && $status !== $comment_status ) { - continue; + $this->empty_views[] = $status; } $link = add_query_arg( 'comment_status', $status, $link ); @@ -380,6 +394,40 @@ protected function get_views() { return apply_filters( 'comment_status_links', $this->get_views_links( $status_links ) ); } + /** + * Displays the list of comment status views available on this table. + * + * Overrides WP_List_Table::views() only to add an `is-empty-view` class to + * views get_views() flagged as having zero comments, rather than omitting + * their markup entirely. Keeping every view in the DOM at all times (merely + * hidden via CSS when empty) means edit-comments.js can reveal or re-hide a + * view after an AJAX moderation action by toggling that class, without ever + * having to replace markup a plugin may have already attached handlers or + * data to. + * + * @since 7.2.0 + */ + public function views() { + $views = $this->get_views(); + + /** This filter is documented in wp-admin/includes/class-wp-list-table.php */ + $views = apply_filters( "views_{$this->screen->id}", $views ); + + if ( empty( $views ) ) { + return; + } + + $this->screen->render_screen_reader_content( 'heading_views' ); + + echo "
      \n"; + foreach ( $views as $class => $view ) { + $li_class = in_array( $class, $this->empty_views, true ) ? "$class is-empty-view" : $class; + $views[ $class ] = "\t
    • $view"; + } + echo implode( " |
    • \n", $views ) . "\n"; + echo '
    '; + } + /** * Gets the available bulk actions for the comments list. * diff --git a/tests/phpunit/tests/admin/wpCommentsListTable.php b/tests/phpunit/tests/admin/wpCommentsListTable.php index a3736be0390dc..e006744c6b4fe 100644 --- a/tests/phpunit/tests/admin/wpCommentsListTable.php +++ b/tests/phpunit/tests/admin/wpCommentsListTable.php @@ -198,53 +198,74 @@ public function test_sortable_columns_with_current_ordering() { /** * @covers WP_Comments_List_Table::get_views */ - public function test_get_views_should_hide_empty_status_links() { + public function test_get_views_should_always_include_every_status_key() { $this->table->prepare_items(); $views = $this->table->get_views(); - $this->assertArrayHasKey( 'all', $views, 'The "All" view should always be shown.' ); - $this->assertArrayHasKey( 'moderated', $views, '"Pending" should always be shown, even at zero.' ); - $this->assertArrayHasKey( 'approved', $views, '"Approved" should always be shown, even at zero.' ); - $this->assertArrayNotHasKey( 'mine', $views, 'Views with a zero count should be hidden.' ); - $this->assertArrayNotHasKey( 'spam', $views, 'Views with a zero count should be hidden.' ); - $this->assertArrayNotHasKey( 'trash', $views, 'Views with a zero count should be hidden.' ); + foreach ( array( 'all', 'mine', 'moderated', 'approved', 'spam', 'trash' ) as $status ) { + $this->assertArrayHasKey( $status, $views, "The \"$status\" view should always be present in get_views(), even at zero, so its markup (and anything a plugin attached to it) stays in the DOM." ); + } } /** * @covers WP_Comments_List_Table::get_views + * @covers WP_Comments_List_Table::views */ - public function test_get_views_should_show_status_link_with_nonzero_count() { + public function test_views_should_mark_empty_status_links_as_hidden() { + $this->table->prepare_items(); + + $output = get_echo( array( $this->table, 'views' ) ); + + $this->assertStringNotContainsString( "class='all is-empty-view'", $output, 'The "All" view should never be marked hidden.' ); + $this->assertStringNotContainsString( "class='moderated is-empty-view'", $output, '"Pending" should never be marked hidden, even at zero.' ); + $this->assertStringNotContainsString( "class='approved is-empty-view'", $output, '"Approved" should never be marked hidden, even at zero.' ); + $this->assertStringContainsString( "class='mine is-empty-view'", $output, '"Mine" should be marked hidden at zero.' ); + $this->assertStringContainsString( "class='spam is-empty-view'", $output, '"Spam" should be marked hidden at zero.' ); + $this->assertStringContainsString( "class='trash is-empty-view'", $output, '"Trash" should be marked hidden at zero.' ); + } + + /** + * @covers WP_Comments_List_Table::get_views + * @covers WP_Comments_List_Table::views + */ + public function test_views_should_unhide_status_link_with_nonzero_count() { + $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + wp_set_current_user( $user_id ); + $post_id = self::factory()->post->create(); self::factory()->comment->create( array( 'comment_post_ID' => $post_id, 'comment_approved' => '1', + 'user_id' => $user_id, ) ); $this->table->prepare_items(); - $views = $this->table->get_views(); + $output = get_echo( array( $this->table, 'views' ) ); - $this->assertArrayHasKey( 'approved', $views, 'A view with a nonzero count should be shown.' ); - $this->assertArrayHasKey( 'moderated', $views, '"Pending" should always be shown, even at zero.' ); - $this->assertArrayNotHasKey( 'spam', $views, 'Views with a zero count should still be hidden.' ); - $this->assertArrayNotHasKey( 'trash', $views, 'Views with a zero count should still be hidden.' ); + $this->assertStringNotContainsString( "class='mine is-empty-view'", $output, 'A view with a nonzero count should not be marked hidden.' ); + $this->assertStringContainsString( "class='spam is-empty-view'", $output, 'Views with a zero count should still be marked hidden.' ); + $this->assertStringContainsString( "class='trash is-empty-view'", $output, 'Views with a zero count should still be marked hidden.' ); } /** * @covers WP_Comments_List_Table::get_views + * @covers WP_Comments_List_Table::views */ - public function test_get_views_should_not_hide_the_currently_active_status() { + public function test_views_should_not_hide_the_currently_active_status() { $_REQUEST['comment_status'] = 'spam'; $this->table->prepare_items(); - $views = $this->table->get_views(); + $views = $this->table->get_views(); + $output = get_echo( array( $this->table, 'views' ) ); - $this->assertArrayHasKey( 'spam', $views, 'The currently active view must never be hidden, even at zero count.' ); + $this->assertArrayHasKey( 'spam', $views ); $this->assertStringContainsString( 'class="current"', $views['spam'], 'The currently active view should be marked current.' ); + $this->assertStringNotContainsString( "class='spam is-empty-view'", $output, 'The currently active view must never be marked hidden, even at zero count.' ); unset( $_REQUEST['comment_status'] ); } diff --git a/tests/phpunit/tests/admin/wpPostCommentsListTable.php b/tests/phpunit/tests/admin/wpPostCommentsListTable.php index db5080db32586..95f07f20d6933 100644 --- a/tests/phpunit/tests/admin/wpPostCommentsListTable.php +++ b/tests/phpunit/tests/admin/wpPostCommentsListTable.php @@ -20,16 +20,30 @@ public function set_up() { /** * @covers WP_Post_Comments_List_Table::get_views */ - public function test_get_views_should_hide_empty_status_links() { + public function test_get_views_should_always_include_every_status_key() { $this->table->prepare_items(); $views = $this->table->get_views(); - $this->assertArrayHasKey( 'all', $views, 'The "All" view should always be shown.' ); - $this->assertArrayHasKey( 'moderated', $views, '"Pending" should always be shown, even at zero.' ); - $this->assertArrayHasKey( 'approved', $views, '"Approved" should always be shown, even at zero.' ); - $this->assertArrayNotHasKey( 'mine', $views, 'Views with a zero count should be hidden.' ); - $this->assertArrayNotHasKey( 'spam', $views, 'Views with a zero count should be hidden.' ); - $this->assertArrayNotHasKey( 'trash', $views, 'Views with a zero count should be hidden.' ); + foreach ( array( 'all', 'mine', 'moderated', 'approved', 'spam', 'trash' ) as $status ) { + $this->assertArrayHasKey( $status, $views, "The \"$status\" view should always be present in get_views(), even at zero." ); + } + } + + /** + * @covers WP_Post_Comments_List_Table::get_views + * @covers WP_Post_Comments_List_Table::views + */ + public function test_views_should_mark_empty_status_links_as_hidden() { + $this->table->prepare_items(); + + $output = get_echo( array( $this->table, 'views' ) ); + + $this->assertStringNotContainsString( "class='all is-empty-view'", $output, 'The "All" view should never be marked hidden.' ); + $this->assertStringNotContainsString( "class='moderated is-empty-view'", $output, '"Pending" should never be marked hidden, even at zero.' ); + $this->assertStringNotContainsString( "class='approved is-empty-view'", $output, '"Approved" should never be marked hidden, even at zero.' ); + $this->assertStringContainsString( "class='mine is-empty-view'", $output, '"Mine" should be marked hidden at zero.' ); + $this->assertStringContainsString( "class='spam is-empty-view'", $output, '"Spam" should be marked hidden at zero.' ); + $this->assertStringContainsString( "class='trash is-empty-view'", $output, '"Trash" should be marked hidden at zero.' ); } } diff --git a/tests/phpunit/tests/ajax/wpAjaxDeleteComment.php b/tests/phpunit/tests/ajax/wpAjaxDeleteComment.php index 0ff56fe0aec27..26b14dd221b79 100644 --- a/tests/phpunit/tests/ajax/wpAjaxDeleteComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxDeleteComment.php @@ -216,47 +216,6 @@ public function _test_with_bad_id( $comment, $action ) { } } - /** - * Trashing the only trashable comment should make the previously-hidden - * "Trash" status link appear in the same response, rather than only - * after a full page reload. - * - * @covers ::_wp_ajax_delete_comment_response - */ - public function test_trashing_only_trashed_comment_includes_trash_link_in_response() { - $this->_setRole( 'administrator' ); - - $comment_id = self::factory()->comment->create( - array( - 'comment_post_ID' => self::$post_id, - 'comment_approved' => '1', - ) - ); - - $_POST['id'] = $comment_id; - $_POST['_ajax_nonce'] = wp_create_nonce( 'delete-comment_' . $comment_id ); - $_POST['trash'] = '1'; - $_POST['_total'] = '1'; - $_POST['_per_page'] = '20'; - $_POST['_page'] = '1'; - $_POST['_url'] = admin_url( 'edit-comments.php' ); - - try { - $this->_handleAjax( 'delete-comment' ); - } catch ( WPAjaxDieContinueException $e ) { - unset( $e ); - } - - $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA ); - $comment_status_links = (string) $xml->response[0]->comment[0]->supplemental[0]->comment_status_links[0]; - - $this->assertStringContainsString( - 'comment_status=trash', - $comment_status_links, - 'The "Trash" status link should appear once a comment has actually been trashed.' - ); - } - /** * Tests doubling the action (e.g. trash a trashed comment). * From 2e8e00acaef26c63551e63565244dbba5ec2f200 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Fri, 9 Oct 2026 09:55:36 +0300 Subject: [PATCH 7/9] Comments: Fix dangling separator after a hidden trailing status view views() attached each view's " | " separator as the last thing inside the *preceding* view's own
  • , mirroring the parent class. That relied on every view actually being visible, which no longer holds now that empty views stay in the DOM (hidden via is-empty-view) instead of being omitted. A visible view followed only by hidden ones (e.g. "Approved" when "Spam" and "Trash" are both empty) still rendered its own trailing "|", with nothing visible after it to pair with. Attach the separator as a leading marker on each view instead, skipped for hidden views and for the first view that actually renders visible, so a run of hidden views never leaves an orphaned separator next to them regardless of which views are empty. Co-Authored-By: Claude Sonnet 5 --- .../includes/class-wp-comments-list-table.php | 24 ++++++++++++++++--- 1 file changed, 21 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 a7437cf2bbf80..9b0c39cb59b70 100644 --- a/src/wp-admin/includes/class-wp-comments-list-table.php +++ b/src/wp-admin/includes/class-wp-comments-list-table.php @@ -420,11 +420,29 @@ public function views() { $this->screen->render_screen_reader_content( 'heading_views' ); echo "
      \n"; + + /* + * Unlike the parent class, a view hidden via is-empty-view stays in the + * markup, so the " | " separator between views can't be attached to + * whichever one happens to precede it in the array (it may be the one + * getting hidden, or everything after it might be). Instead, prefix it + * to each view except the first one actually rendered visible, so a + * run of hidden views never leaves a dangling separator next to them. + */ + $rendered_a_visible_view = false; + foreach ( $views as $class => $view ) { - $li_class = in_array( $class, $this->empty_views, true ) ? "$class is-empty-view" : $class; - $views[ $class ] = "\t
    • $view"; + $is_hidden = in_array( $class, $this->empty_views, true ); + $li_class = $is_hidden ? "$class is-empty-view" : $class; + $separator = ( ! $is_hidden && $rendered_a_visible_view ) ? '| ' : ''; + + echo "\t
    • $separator$view
    • \n"; + + if ( ! $is_hidden ) { + $rendered_a_visible_view = true; + } } - echo implode( " |\n", $views ) . "\n"; + echo '
    '; } From b641312efc16133b5b5299967d2e15d2398a1c89 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Fri, 9 Oct 2026 10:01:40 +0300 Subject: [PATCH 8/9] Comments: Generate the view separator via CSS instead of baking it into HTML The previous fix attached the " | " separator as a leading marker on each view, computed from which views were empty at render time. That only holds at the moment of rendering: when edit-comments.js later reveals a previously-hidden view by removing its is-empty-view class (e.g. "Trash" becoming non-empty after a comment is trashed), that view's HTML never had a separator baked into it to begin with, so it renders flush against the preceding view with no separator between them. Stop writing the separator as literal text entirely and generate it with CSS instead (`::before` on every non-first
  • , scoped to the Comments screen so other list tables - which still embed the separator as literal text and don't use is-empty-view - aren't affected). A hidden view's own separator is part of its own box, so it hides and reappears together with the view itself automatically, regardless of which direction (PHP's initial render or JS's later toggle) changes its visibility. Co-Authored-By: Claude Sonnet 5 --- src/wp-admin/css/list-tables.css | 12 ++++++++ .../includes/class-wp-comments-list-table.php | 30 ++++++------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/wp-admin/css/list-tables.css b/src/wp-admin/css/list-tables.css index a2ad93949b387..721b2b7ecdd7f 100644 --- a/src/wp-admin/css/list-tables.css +++ b/src/wp-admin/css/list-tables.css @@ -98,6 +98,18 @@ display: none; } +/* + * The " | " separator between comment status views is generated here, + * rather than written as literal text server-side, so it shows or hides + * itself together with its own view regardless of which direction + * edit-comments.js later toggles that view's visibility in. Scoped to the + * Comments screen specifically: every other list table still renders its + * separator as literal text and doesn't use is-empty-view. + */ +body.edit-comments-php .subsubsub li:not(:first-child)::before { + content: "| "; +} + /* @todo: consider to use a single rule for these counters and the admin menu counters. */ .column-response .post-com-count-pending, .column-comments .post-com-count-pending { 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 9b0c39cb59b70..607d1a81c058d 100644 --- a/src/wp-admin/includes/class-wp-comments-list-table.php +++ b/src/wp-admin/includes/class-wp-comments-list-table.php @@ -405,6 +405,13 @@ protected function get_views() { * having to replace markup a plugin may have already attached handlers or * data to. * + * Unlike the parent class, the " | " separator between views isn't written + * as literal text here at all: baking it into the HTML for whichever views + * happen to be visible at render time can't react to a view's visibility + * later changing on the client (toggling is-empty-view). It's generated by + * CSS instead (see list-tables.css), so it shows or hides itself in lockstep + * with its own view, in whichever direction that changes. + * * @since 7.2.0 */ public function views() { @@ -420,29 +427,10 @@ public function views() { $this->screen->render_screen_reader_content( 'heading_views' ); echo "
      \n"; - - /* - * Unlike the parent class, a view hidden via is-empty-view stays in the - * markup, so the " | " separator between views can't be attached to - * whichever one happens to precede it in the array (it may be the one - * getting hidden, or everything after it might be). Instead, prefix it - * to each view except the first one actually rendered visible, so a - * run of hidden views never leaves a dangling separator next to them. - */ - $rendered_a_visible_view = false; - foreach ( $views as $class => $view ) { - $is_hidden = in_array( $class, $this->empty_views, true ); - $li_class = $is_hidden ? "$class is-empty-view" : $class; - $separator = ( ! $is_hidden && $rendered_a_visible_view ) ? '| ' : ''; - - echo "\t
    • $separator$view
    • \n"; - - if ( ! $is_hidden ) { - $rendered_a_visible_view = true; - } + $li_class = in_array( $class, $this->empty_views, true ) ? "$class is-empty-view" : $class; + echo "\t
    • $view
    • \n"; } - echo '
    '; } From bb5b72120e58d8c9f0ab4fa6b659c6ec4dfdc4f4 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Fri, 9 Oct 2026 10:39:42 +0300 Subject: [PATCH 9/9] Comments, Administration: Extract WP_List_Table::print_views() for overriding WP_Comments_List_Table::views() duplicated nearly all of WP_List_Table::views() (fetching views, filtering, the empty check, the screen-reader heading, the
      wrapper) just to customize how individual
    • elements are printed (adding an is-empty-view class). Split that per-item printing out of WP_List_Table::views() into a new protected print_views() method, so a subclass that needs to customize it can override just that, instead of the whole method. Behavior for every other list table is unchanged - this only moves existing code into its own method. WP_Comments_List_Table now only overrides print_views(). Co-Authored-By: Claude Sonnet 5 --- .../includes/class-wp-comments-list-table.php | 47 ++++++++----------- src/wp-admin/includes/class-wp-list-table.php | 18 ++++++- 2 files changed, 36 insertions(+), 29 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 607d1a81c058d..3f987527d11da 100644 --- a/src/wp-admin/includes/class-wp-comments-list-table.php +++ b/src/wp-admin/includes/class-wp-comments-list-table.php @@ -395,43 +395,34 @@ protected function get_views() { } /** - * Displays the list of comment status views available on this table. + * Prints the `
    • ` elements for the list of comment status views. * - * Overrides WP_List_Table::views() only to add an `is-empty-view` class to - * views get_views() flagged as having zero comments, rather than omitting - * their markup entirely. Keeping every view in the DOM at all times (merely - * hidden via CSS when empty) means edit-comments.js can reveal or re-hide a - * view after an AJAX moderation action by toggling that class, without ever - * having to replace markup a plugin may have already attached handlers or - * data to. + * Overrides WP_List_Table::print_views() only to add an `is-empty-view` + * class to views get_views() flagged as having zero comments, rather than + * omitting their markup entirely. Keeping every view in the DOM at all + * times (merely hidden via CSS when empty) means edit-comments.js can + * reveal or re-hide a view after an AJAX moderation action by toggling + * that class, without ever having to replace markup a plugin may have + * already attached handlers or data to. * - * Unlike the parent class, the " | " separator between views isn't written - * as literal text here at all: baking it into the HTML for whichever views - * happen to be visible at render time can't react to a view's visibility - * later changing on the client (toggling is-empty-view). It's generated by - * CSS instead (see list-tables.css), so it shows or hides itself in lockstep - * with its own view, in whichever direction that changes. + * Unlike the parent class, the " | " separator between views isn't + * written as literal text here at all: baking it into the HTML for + * whichever views happen to be visible at render time can't react to a + * view's visibility later changing on the client (toggling + * is-empty-view). It's generated by CSS instead (see list-tables.css), + * so it shows or hides itself in lockstep with its own view, in + * whichever direction that changes. * * @since 7.2.0 + * + * @param string[] $views An array of available list table views, as + * returned by get_views() and already filtered. */ - public function views() { - $views = $this->get_views(); - - /** This filter is documented in wp-admin/includes/class-wp-list-table.php */ - $views = apply_filters( "views_{$this->screen->id}", $views ); - - if ( empty( $views ) ) { - return; - } - - $this->screen->render_screen_reader_content( 'heading_views' ); - - echo "
        \n"; + protected function print_views( $views ) { foreach ( $views as $class => $view ) { $li_class = in_array( $class, $this->empty_views, true ) ? "$class is-empty-view" : $class; echo "\t
      • $view
      • \n"; } - echo '
      '; } /** diff --git a/src/wp-admin/includes/class-wp-list-table.php b/src/wp-admin/includes/class-wp-list-table.php index a63719d1d3bb2..d0ef21a9fb73f 100644 --- a/src/wp-admin/includes/class-wp-list-table.php +++ b/src/wp-admin/includes/class-wp-list-table.php @@ -557,11 +557,27 @@ public function views() { $this->screen->render_screen_reader_content( 'heading_views' ); echo "
        \n"; + $this->print_views( $views ); + echo '
      '; + } + + /** + * Prints the `
    • ` elements for the list of views. + * + * Split out from views() so subclasses that need to customize how + * individual views are rendered (e.g. adding a class to some of them) + * can override just this part, instead of the whole method. + * + * @since 7.2.0 + * + * @param string[] $views An array of available list table views, as + * returned by get_views() and already filtered. + */ + protected function print_views( $views ) { foreach ( $views as $class => $view ) { $views[ $class ] = "\t
    • $view"; } echo implode( " |
    • \n", $views ) . "\n"; - echo '
    '; } /**