diff --git src/wp-includes/pluggable.php src/wp-includes/pluggable.php
index 4edb9929f6..50832db27e 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' ) ) : |
90 | 93 | * |
91 | 94 | * @since 2.8.0 |
92 | 95 | * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter. |
| 96 | * @since 5.3 Return the global $current_user if it's the user being fetched. |
93 | 97 | * |
94 | 98 | * @param string $field The field to retrieve the user with. id | ID | slug | email | login. |
95 | 99 | * @param int|string $value A value for $field. A user ID, slug, email address, or login name. |
96 | 100 | * @return WP_User|false WP_User object on success, false on failure. |
97 | 101 | */ |
98 | 102 | function get_user_by( $field, $value ) { |
| 103 | global $current_user; |
| 104 | |
99 | 105 | $userdata = WP_User::get_data_by( $field, $value ); |
100 | 106 | |
101 | 107 | if ( ! $userdata ) { |
102 | 108 | return false; |
103 | 109 | } |
| 110 | |
| 111 | if ( ! empty( $current_user ) |
| 112 | && $current_user instanceof WP_User |
| 113 | && $current_user->ID == $userdata->ID ) { |
| 114 | |
| 115 | return $current_user; |
| 116 | |
| 117 | } |
104 | 118 | |
105 | 119 | $user = new WP_User; |
106 | 120 | $user->init( $userdata ); |
… |
… |
if ( ! function_exists( 'wp_validate_auth_cookie' ) ) : |
637 | 651 | return false; |
638 | 652 | } |
639 | 653 | |
640 | | $user = get_user_by( 'login', $username ); |
| 654 | $user = get_user_by( 'login', $username, false ); |
641 | 655 | if ( ! $user ) { |
642 | 656 | /** |
643 | 657 | * Fires if a bad username is entered in the user authentication process. |
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 | } |