Skip to content

Commit bbef0fd

Browse files
committed
fix(inactive-users): clear stale last_seen meta on login in REPORT mode
1 parent 4769333 commit bbef0fd

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

modules/inactive-users/class-inactive-users.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,14 @@ public static function record_activity() {
144144
return;
145145
}
146146

147-
if ( self::is_block_action_enabled() && self::is_considered_inactive( $user_id ) ) {
148-
// User needs to be unblocked first
149-
return;
147+
if ( self::is_considered_inactive( $user_id ) ) {
148+
if ( self::is_block_action_enabled() ) {
149+
// User needs to be unblocked first.
150+
return;
151+
}
152+
153+
// In REPORT mode, clear the stale last seen meta so the user starts fresh.
154+
delete_user_meta( $user_id, self::LAST_SEEN_META_KEY );
150155
}
151156

152157
// phpcs:ignore WordPressVIPMinimum.Performance.LowExpiryCacheTime.CacheTimeUndetermined

tests/phpunit/test-inactive-users.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,15 +1041,16 @@ public function test_single_site_unblock_action() {
10411041
}
10421042

10431043
/**
1044-
* Test that record_activity() updates last seen meta for an inactive user in REPORT mode.
1044+
* Test that record_activity() clears stale last seen meta and records fresh
1045+
* activity for an inactive user in REPORT mode.
10451046
*
10461047
* This is a regression test for the bug where REPORT mode created a permanent
10471048
* inactive flag: the early-return in record_activity() only checked
10481049
* is_considered_inactive() without also checking is_block_action_enabled(),
10491050
* so once a user was flagged inactive in REPORT mode, their activity was
10501051
* never recorded again — making the flag permanent.
10511052
*/
1052-
public function test_record_activity_updates_last_seen_in_report_mode_for_inactive_user() {
1053+
public function test_record_activity_clears_meta_and_records_activity_in_report_mode() {
10531054
// Create anonymous subclass to set mode to REPORT.
10541055
$inactive_users_class = new class() extends Inactive_Users {
10551056
public static function reset_for_test() {
@@ -1078,12 +1079,13 @@ public static function reset_for_test() {
10781079
wp_set_current_user( $user_id );
10791080
wp_cache_delete( $user_id, Inactive_Users::LAST_SEEN_CACHE_GROUP );
10801081

1081-
// Call record_activity() — in REPORT mode this should NOT early-return.
1082+
// Call record_activity() — in REPORT mode this should clear stale meta and record fresh activity.
10821083
Inactive_Users::record_activity();
10831084

1084-
// The last seen timestamp should have been updated.
1085+
// The user should no longer be considered inactive.
10851086
$new_timestamp = get_user_meta( $user_id, Inactive_Users::LAST_SEEN_META_KEY, true );
1086-
$this->assertGreaterThan( $old_timestamp, (int) $new_timestamp, 'record_activity() should update last_seen in REPORT mode even for inactive users' );
1087+
$this->assertGreaterThan( $old_timestamp, (int) $new_timestamp, 'record_activity() should record fresh last_seen in REPORT mode for inactive users' );
1088+
$this->assertFalse( Inactive_Users::is_considered_inactive( $user_id ), 'User should no longer be considered inactive after record_activity() in REPORT mode' );
10871089

10881090
// Clean up.
10891091
wp_delete_user( $user_id );

0 commit comments

Comments
 (0)