Repository navigation
Comments: Update the "Mine" view count after AJAX moderation actions - #14114
AbdiTolesa wants to merge 2 commits into
Conversation
After approving, unapproving, spamming, trashing, or deleting a comment via AJAX on the Comments screen, edit-comments.js live-updates the "All", "Pending", "Approved", "Spam", and "Trash" view tab counts in the DOM. The "Mine" count was never touched, so it stayed stale until a full page reload, unlike every other view. "Mine" shares the same status scope as "All" (approved + pending), just filtered to the current user's own comments, so it needs the same pendingDiff/approvedDiff updates "All" gets, applied only when the affected comment belongs to the current user. To determine that client-side: - WP_Comments_List_Table::single_row() now renders a data-comment-user-id attribute on each comment row. - The admin-comments script is localized with the current user's ID (adminCommentsSettings.currentUserId). - edit-comments.js compares the two to decide whether to also bump span.mine-count alongside span.all-count. Added a test asserting single_row() renders the new attribute for the comment's author. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
SainathPoojary
left a comment
There was a problem hiding this comment.
Thanks for working on this fix! The overall approach looks good and the unit test covers the new attribute nicely.
I left one comment regarding type casting adminCommentsSettings.currentUserId to ensure the strict equality check passes as expected.
| unapproved = commentRow.hasClass( 'unapproved' ), | ||
| spammed = commentRow.hasClass( 'spam' ), | ||
| trashed = commentRow.hasClass( 'trash' ), | ||
| isMine = !! adminCommentsSettings.currentUserId && |
There was a problem hiding this comment.
Since adminCommentsSettings.currentUserId is passed via wp_localize_script(), it arrives as a string. Because we're using strict equality ===, this comparison evaluates to false even when the IDs match (e.g., 7 === "7").
Could we wrap adminCommentsSettings.currentUserId in parseInt(..., 10) as well?
There was a problem hiding this comment.
@SainathPoojary Thanks for bringing that up! The concern is addressed now.
wp_localize_script() (via WP_Scripts::localize()) casts every scalar value to a string before JSON-encoding it, so adminCommentsSettings.currentUserId arrives in JS as a string (e.g. "7"), not a number. The isMine check compared it with === against a parseInt()'d number, which is always false for a string/number pair, so the "Mine" count was never actually updated via AJAX. Wrap adminCommentsSettings.currentUserId in parseInt(..., 10) as well so both sides of the comparison are numbers. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Trac ticket: https://core.trac.wordpress.org/ticket/66272
Summary
On the Comments screen, performing an AJAX moderation action (Approve, Unapprove, Spam, Trash, Delete Permanently, or Undo) live-updates several view tab counts in the DOM without a full page reload —
edit-comments.js'sdelAfterhandler bumpsspan.all-count,span.pending-count(viaupdatePending),span.approved-count(viaupdateApproved),span.spam-count, andspan.trash-count.The
span.mine-countbadge is never touched by this code, so after e.g. deleting one of your own comments via AJAX, the "Mine (N)" tab keeps showing the old count until the page is fully reloaded — inconsistent with every other tab."Mine" uses the same status scope as "All" (approved + pending comments; see
WP_Comments_List_Table::get_views(), which queries with nostatusarg, defaulting to'all'), just filtered to the current user's own comments. So it needs the samependingDiff/approvedDiffupdates thatall-countgets, applied only when the affected comment belongs to the current user.Changes
WP_Comments_List_Table::single_row()now renders adata-comment-user-idattribute on each comment<tr>.admin-commentsscript is localized with the current user's ID (adminCommentsSettings.currentUserId).edit-comments.jscompares the acted-upon row'sdata-comment-user-idto the current user ID and, when they match, also updatesspan.mine-countalongsidespan.all-countfor bothpendingDiffandapprovedDiff.test_single_row_includes_comment_user_id_for_mine_count_trackingtowpCommentsListTable.php.Test plan
php -lon the changed PHP files;node --checkandnpx eslinton the changed JS file (no errors).Tests_Admin_wpCommentsListTableandTests_Admin_wpPostCommentsListTable— 12 tests total, all passing.class-wp-comments-list-table.php(nodata-comment-user-idattribute) and passes with the fix.span.mine-countafter an AJAX action) isn't covered by PHPUnit — manually verified the logic by tracingdelAfterinedit-comments.js, but a browser/QUnit check of the actual tab count updating live would be good before merging.Update: fix a type-mismatch bug caught in review
wp_localize_script()(viaWP_Scripts::localize()) casts every scalar value to a string before JSON-encoding it, soadminCommentsSettings.currentUserIdactually arrives in JS as a string (e.g."7"), not a number. TheisMinecheck compared it with===against aparseInt()'d number, which is alwaysfalsefor a string/number pair — so the "Mine" count was never actually updated, regardless of ownership. Fixed by wrappingadminCommentsSettings.currentUserIdinparseInt(..., 10)too, so both sides of the comparison are numbers. Confirmed this matchesWP_Scripts::localize()'s actual behavior by reading its source ((string) $valuecast beforewp_json_encode()).