Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/js/_enqueues/admin/edit-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 (
Expand Down
21 changes: 21 additions & 0 deletions src/wp-admin/css/list-tables.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
64 changes: 62 additions & 2 deletions src/wp-admin/includes/class-wp-comments-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -365,6 +394,37 @@ protected function get_views() {
return apply_filters( 'comment_status_links', $this->get_views_links( $status_links ) );
}

/**
* Prints the `<li>` 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<li class='$li_class'>$view</li>\n";
}
}

/**
* Gets the available bulk actions for the comments list.
*
Expand Down
18 changes: 17 additions & 1 deletion src/wp-admin/includes/class-wp-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,11 +557,27 @@ public function views() {
$this->screen->render_screen_reader_content( 'heading_views' );

echo "<ul class='subsubsub'>\n";
$this->print_views( $views );
echo '</ul>';
}

/**
* Prints the `<li>` 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<li class='$class'>$view";
}
echo implode( " |</li>\n", $views ) . "</li>\n";
echo '</ul>';
}

/**
Expand Down
78 changes: 67 additions & 11 deletions tests/phpunit/tests/admin/wpCommentsListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/edit-comments.php?comment_status=all" class="current" aria-current="page">All <span class="count">(<span class="all-count">0</span>)</span></a>',
'mine' => '<a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/edit-comments.php?comment_status=mine&#038;user_id=0">Mine <span class="count">(<span class="mine-count">0</span>)</span></a>',
'moderated' => '<a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/edit-comments.php?comment_status=moderated">Pending <span class="count">(<span class="pending-count">0</span>)</span></a>',
'approved' => '<a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/edit-comments.php?comment_status=approved">Approved <span class="count">(<span class="approved-count">0</span>)</span></a>',
'spam' => '<a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/edit-comments.php?comment_status=spam">Spam <span class="count">(<span class="spam-count">0</span>)</span></a>',
'trash' => '<a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/edit-comments.php?comment_status=trash">Trash <span class="count">(<span class="trash-count">0</span>)</span></a>',
$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'] );
}

/**
Expand Down
35 changes: 23 additions & 12 deletions tests/phpunit/tests/admin/wpPostCommentsListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/edit-comments.php?comment_status=all" class="current" aria-current="page">All <span class="count">(<span class="all-count">0</span>)</span></a>',
'mine' => '<a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/edit-comments.php?comment_status=mine&#038;user_id=0">Mine <span class="count">(<span class="mine-count">0</span>)</span></a>',
'moderated' => '<a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/edit-comments.php?comment_status=moderated">Pending <span class="count">(<span class="pending-count">0</span>)</span></a>',
'approved' => '<a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/edit-comments.php?comment_status=approved">Approved <span class="count">(<span class="approved-count">0</span>)</span></a>',
'spam' => '<a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/edit-comments.php?comment_status=spam">Spam <span class="count">(<span class="spam-count">0</span>)</span></a>',
'trash' => '<a href="http://' . WP_TESTS_DOMAIN . '/wp-admin/edit-comments.php?comment_status=trash">Trash <span class="count">(<span class="trash-count">0</span>)</span></a>',
);
$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.' );
}
}
Loading