fix: render inactive-user badge without mutating WP_User#110
Open
sjinks wants to merge 1 commit into
Open
Conversation
The Users list "Inactive User" / "Blocked: Inactivity" badge was appended
directly to `WP_User->user_login` for every inactive item in
`$wp_list_table->items` on `admin_head-users.php`. Those objects are
shared by reference and are passed to the `user_row_actions` filter during
row rendering, so any consumer that reads `user_login` there received the
badge HTML instead of the real login. Co-Authors Plus looks up a linked
guest author by exact `user_login` match, so linked inactive users were
shown "Create Profile" instead of "Edit Profile".
Render the badge at the output layer instead: emit an inline script that
appends the badge to the rendered username cell, keyed on the user row,
and leave the `WP_User` objects untouched. The script resolves the row
across both the single-site (`#user-{ID}`) and network admin tables
(network rows have no row id, so it falls back to the row checkbox
`#blog_{ID}` and the edit-link href). The JSON payload is encoded with
JSON_HEX_* flags so a translated badge string cannot break out of the
inline script.
Tests: assert the badge is emitted via output (not via mutation), that
`user_login` is preserved byte-for-byte, that the payload is
`</script>`-safe, and that the network fallback selectors are present.
Refs: PLTFRM-2468
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes the inactive-user badge rendering in the WordPress Users list by avoiding mutation of shared WP_User objects (specifically user_login), and instead injecting the badge at the output layer via an inline script.
Changes:
- Refactors
Inactive_Users::modify_users_list_table_items()to collect inactive user IDs and emit a client-side script that appends the badge to.column-username. - Adds a hardened JSON payload for the inline script (using
wp_json_encodewithJSON_HEX_*flags) and guards against duplicate badge insertion. - Updates and expands PHPUnit coverage to validate output-based rendering, non-mutation regression behavior, script-safety, and network table selector fallbacks.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| modules/inactive-users/class-inactive-users.php | Stops mutating WP_User->user_login and injects the badge client-side via an inline script. |
| tests/phpunit/test-inactive-users.php | Updates tests to assert script output and adds regressions for non-mutation + script-safety + multisite fallbacks. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+332
to
+335
| var link = document.querySelector( 'a[href*="user_id=' + id + '"]' ); | ||
| if ( link && link.closest ) { | ||
| return link.closest( 'tr' ); | ||
| } |
Comment on lines
+269
to
+273
| $inactive_user = new stdClass(); | ||
| $inactive_user->ID = $this->user_id; | ||
| $inactive_user->user_login = 'zbroussard@salesforce.com'; | ||
|
|
||
| $wp_list_table->items = [ $inactive_user ]; |
Comment on lines
+281
to
+283
| // The login must be byte-for-byte preserved: no badge markup, no padding. | ||
| $this->assertSame( 'zbroussard@salesforce.com', $wp_list_table->items[0]->user_login ); | ||
| $this->assertStringNotContainsString( 'inactive-user-badge', $wp_list_table->items[0]->user_login ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The Users list "Inactive User" / "Blocked: Inactivity" badge was being appended directly to
WP_User->user_loginfor every inactive item in$wp_list_table->items, on theadmin_head-users.php/admin_head-users-network.phpactions.Those
WP_Userobjects are shared by reference and are passed to theuser_row_actionsfilter while WordPress renders each row. Any consumer that reads$user_object->user_loginthere received the badge HTML appended to the login instead of the real value. Co-Authors Plus looks up a linked guest author by an exactuser_loginmatch, so linked inactive users were shown its "Create Profile" action instead of "Edit Profile" (the linked-account lookup no longer matched).This change renders the badge at the output layer instead of mutating the model object:
modify_users_list_table_items()no longer touchesWP_User. It collects the IDs of inactive users and prints a small inline script.<span>to the rendered username cell (.column-username) for each inactive user, usingdocument.createElement+textContent(noinnerHTML), guarded against double-adding.id="user-{ID}"; network admin rows have no row id, so the script falls back to the row checkbox (#blog_{ID}) and finally the edit-link href (user_id={ID}).JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOTso a translated badge string can never break out of the inline<script>.The fix is entirely in this plugin; no change is needed in any consumer, whose
user_loginread is legitimate.Tradeoff: the badge is now applied client-side, so it does not appear with JavaScript disabled. The badge is purely informational — the inactivity enforcement (auth / application-password / REST blocking in BLOCK mode) is server-side and unchanged.
Changelog Description
Fixed
Pre-review checklist
Steps to Test
wp-admin/users.php.add_filter( 'coauthors_show_create_profile_user_link', '__return_true' );to make the action visible.users.php: the inactive, linked user now shows Edit Profile (previously Create Profile), and the badge still appears in the username column.users.phpand (multisite)users-network.php.user_row_actionsprobe loggingstrlen( $user_object->user_login )reports the clean login length for inactive users.