diff --git src/wp-includes/pluggable.php src/wp-includes/pluggable.php
index 4edb9929f6..f85e013e55 100644
|
|
if ( ! function_exists( 'wp_set_current_user' ) ) : |
17 | 17 | * actions on users who aren't signed in. |
18 | 18 | * |
19 | 19 | * @since 2.0.3 |
| 20 | * @since 5.3 `$force` param was added. |
20 | 21 | * @global WP_User $current_user The current user object which holds the user data. |
21 | 22 | * |
22 | | * @param int $id User ID |
23 | | * @param string $name User's username |
| 23 | * @param int $id User ID |
| 24 | * @param string $name User's username |
| 25 | * @param bool $force Set to true to force the re-setup of the current user object |
24 | 26 | * @return WP_User Current user User object |
25 | 27 | */ |
26 | | function wp_set_current_user( $id, $name = '' ) { |
| 28 | function wp_set_current_user( $id, $name = '', $force = false ) { |
27 | 29 | global $current_user; |
28 | 30 | |
29 | 31 | // If `$id` matches the current user, there is nothing to do. |
… |
… |
if ( ! function_exists( 'wp_set_current_user' ) ) : |
31 | 33 | && ( $current_user instanceof WP_User ) |
32 | 34 | && ( $id == $current_user->ID ) |
33 | 35 | && ( null !== $id ) |
| 36 | && ( !$force ) |
34 | 37 | ) { |
35 | 38 | return $current_user; |
36 | 39 | } |
… |
… |
if ( ! function_exists( 'get_user_by' ) ) : |
102 | 105 | return false; |
103 | 106 | } |
104 | 107 | |
| 108 | if ( get_current_user_id() == $userdata->ID ) { |
| 109 | return wp_get_current_user(); |
| 110 | } |
| 111 | |
105 | 112 | $user = new WP_User; |
106 | 113 | $user->init( $userdata ); |
107 | 114 | |
diff --git src/wp-includes/user.php src/wp-includes/user.php
index a97f0fada8..83dd41fbb1 100644
|
|
function update_user_caches( $user ) { |
1360 | 1360 | * |
1361 | 1361 | * @since 3.0.0 |
1362 | 1362 | * @since 4.4.0 'clean_user_cache' action was added. |
| 1363 | * @since 5.3 Force re-setup if it is the current user's cache to be cleaned. |
1363 | 1364 | * |
1364 | 1365 | * @param WP_User|int $user User object or ID to be cleaned from the cache |
1365 | 1366 | */ |
… |
… |
function clean_user_cache( $user ) { |
1386 | 1387 | * @param WP_User $user User object. |
1387 | 1388 | */ |
1388 | 1389 | do_action( 'clean_user_cache', $user->ID, $user ); |
| 1390 | |
| 1391 | /** |
| 1392 | * Force re-setup of current user if it is user's cache to be cleaned. |
| 1393 | * |
| 1394 | * @since 5.3 |
| 1395 | */ |
| 1396 | if ( get_current_user_id() == $user->ID ) { |
| 1397 | wp_set_current_user( $user->ID, '', true ); |
| 1398 | } |
1389 | 1399 | } |
1390 | 1400 | |
1391 | 1401 | /** |
diff --git tests/phpunit/tests/pluggable.php tests/phpunit/tests/pluggable.php
index e711f3b8bc..ab55dd544d 100644
|
|
class Tests_Pluggable extends WP_UnitTestCase { |
128 | 128 | 'wp_set_current_user' => array( |
129 | 129 | 'id', |
130 | 130 | 'name' => '', |
| 131 | 'force' => false // ticket 28020 |
131 | 132 | ), |
132 | 133 | 'wp_get_current_user' => array(), |
133 | 134 | 'get_userdata' => array( 'user_id' ), |
… |
… |
class Tests_Pluggable extends WP_UnitTestCase { |
319 | 320 | return $signatures; |
320 | 321 | } |
321 | 322 | |
| 323 | /** |
| 324 | * @ticket 28020 |
| 325 | */ |
| 326 | public function test_get_user_by_should_return_same_instance_as_wp_get_current_user() { |
| 327 | |
| 328 | // Create a test user |
| 329 | $new_user = self::factory()->user->create( array( |
| 330 | 'role' => 'subscriber' |
| 331 | ) ); |
| 332 | |
| 333 | // Set the test user as the current user |
| 334 | wp_set_current_user( $new_user ); |
| 335 | |
| 336 | // Get the test user using get_user_by() |
| 337 | $from_get_user_by = get_user_by( 'id', $new_user ); |
| 338 | |
| 339 | // Set the test user role as editor |
| 340 | $from_get_user_by->set_role( 'administrator' ); |
| 341 | |
| 342 | // current_user_can() should reflect the same changes applied to the instance returned |
| 343 | // by get_user_by(). |
| 344 | $this->assertTrue( current_user_can( 'administrator') ); |
| 345 | |
| 346 | } |
| 347 | |
322 | 348 | } |