diff --git a/src/js/_enqueues/admin/edit-comments.js b/src/js/_enqueues/admin/edit-comments.js
index 7e89f1c0dd7cb..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,10 +661,12 @@ window.setCommentsList = function() {
if ( spamDiff ) {
updateCountText( 'span.spam-count', spamDiff );
+ toggleEmptyView( 'spam' );
}
if ( trashDiff ) {
updateCountText( 'span.trash-count', trashDiff );
+ toggleEmptyView( 'trash' );
}
if (
diff --git a/src/wp-admin/css/list-tables.css b/src/wp-admin/css/list-tables.css
index f6105ad5a59fb..721b2b7ecdd7f 100644
--- a/src/wp-admin/css/list-tables.css
+++ b/src/wp-admin/css/list-tables.css
@@ -89,6 +89,27 @@
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;
+}
+
+/*
+ * 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 2b927a7f81a6a..3f987527d11da 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. */
@@ -327,6 +338,24 @@ protected function get_views() {
$num_comments->$status = 10;
}
+ /*
+ * "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 ) {
+ $this->empty_views[] = $status;
+ }
+
$link = add_query_arg( 'comment_status', $status, $link );
if ( $post_id ) {
@@ -365,6 +394,37 @@ protected function get_views() {
return apply_filters( 'comment_status_links', $this->get_views_links( $status_links ) );
}
+ /**
+ * Prints the `
` elements for the list of comment status views.
+ *
+ * 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.
+ *
+ * @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 ) {
+ $li_class = in_array( $class, $this->empty_views, true ) ? "$class is-empty-view" : $class;
+ echo "\t$view\n";
+ }
+ }
+
/**
* Gets the available bulk actions for the comments list.
*
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 '';
}
/**
diff --git a/tests/phpunit/tests/admin/wpCommentsListTable.php b/tests/phpunit/tests/admin/wpCommentsListTable.php
index 47b9bec57f140..e006744c6b4fe 100644
--- a/tests/phpunit/tests/admin/wpCommentsListTable.php
+++ b/tests/phpunit/tests/admin/wpCommentsListTable.php
@@ -196,22 +196,78 @@ public function test_sortable_columns_with_current_ordering() {
}
/**
- * @ticket 42066
- *
* @covers WP_Comments_List_Table::get_views
*/
- public function test_get_views_should_return_views_by_default() {
+ public function test_get_views_should_always_include_every_status_key() {
+ $this->table->prepare_items();
+
+ $views = $this->table->get_views();
+
+ 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_views_should_mark_empty_status_links_as_hidden() {
$this->table->prepare_items();
- $expected = array(
- 'all' => 'All (0)',
- 'mine' => 'Mine (0)',
- 'moderated' => 'Pending (0)',
- 'approved' => 'Approved (0)',
- 'spam' => 'Spam (0)',
- 'trash' => 'Trash (0)',
+ $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->assertSame( $expected, $this->table->get_views() );
+
+ $this->table->prepare_items();
+
+ $output = get_echo( array( $this->table, 'views' ) );
+
+ $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_views_should_not_hide_the_currently_active_status() {
+ $_REQUEST['comment_status'] = 'spam';
+
+ $this->table->prepare_items();
+
+ $views = $this->table->get_views();
+ $output = get_echo( array( $this->table, 'views' ) );
+
+ $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 975bbf2a7c71e..95f07f20d6933 100644
--- a/tests/phpunit/tests/admin/wpPostCommentsListTable.php
+++ b/tests/phpunit/tests/admin/wpPostCommentsListTable.php
@@ -18,21 +18,32 @@ 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_always_include_every_status_key() {
$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();
+
+ 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.' );
}
}