Make WordPress Core

Opened 3 weeks ago

Closed 3 weeks ago

#64628 closed defect (bug) (fixed)

wp_set_current_user() is type strict determing current user

Reported by: peterwilsoncc's profile peterwilsoncc Owned by: peterwilsoncc's profile peterwilsoncc
Milestone: 7.0 Priority: normal
Severity: normal Version:
Component: Users Keywords: has-patch has-unit-tests
Focuses: performance Cc:

Description

The wp_set_current_user() function returns early if the function is called with the current user ID to avoid the work of switching users when it's not required.

However, the function uses a type strict comparison when determining if the user ID is the same, so wp_set_current_user( (int) get_current_user_id() ) will return early, whereas wp_set_current_user( (string) get_current_user_id() ) will proceed through the set up of the new current user.

Introduced in r57882 for #60700.

This is a minor fix (tests and PR incoming) so I've put it on the 7.0 milestone.

Change History (2)

This ticket was mentioned in PR #10905 on WordPress/wordpress-develop by @peterwilsoncc.


3 weeks ago
#1

  • Keywords has-patch has-unit-tests added

Ensures the user switching does not occur when attempting to switch to the same user with the user ID cast to a string.

Trac ticket: https://core.trac.wordpress.org/ticket/64628

## Use of AI Tools

Nil.

#2 @peterwilsoncc
3 weeks ago

  • Owner set to peterwilsoncc
  • Resolution set to fixed
  • Status changed from new to closed

In 61633:

Users: Ensure switching to current user doesn't reinstantiate current user.

Prevent wp_set_current_user() from reinstantiating the current user when the user ID is passed as a string, eg wp_set_current_user( (string) get_current_user_id() ).

This restores the function's previous behaviour of returning early in the event the IDs loosely match.

Follow up to r57882.

Props westonruter, peterwilsoncc.
Fixes #64628.

Note: See TracTickets for help on using tickets.