Users: Share current user instance across functions.

Share the `WP_User` instance for the current user between the functions `get_userdata()` and `wp_get_current_user()`. Both functions return the `$current_user` global for the current user.

Force refresh the `$current_user` global within `clean_user_cache()` by immediately re-calling `wp_set_current_user()` with the current user's ID. This ensures any changes to the current user's permissions or other settings are reflected in the global. As a side-effect this immediately rewarms the current user's cache.

Props chaion07, chriscct7, donmhico, hellofromtonya, lukecarbis, peterwilsoncc, rmccue, TimothyBlynJacobs.
Fixes #28020.

Built from https://develop.svn.wordpress.org/trunk@50790


git-svn-id: http://core.svn.wordpress.org/trunk@50399 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson
2021-04-26 01:03:59 +00:00
parent 868ad5a313
commit de519f9dfe
3 changed files with 20 additions and 1 deletions

View File

@@ -91,18 +91,25 @@ if ( ! function_exists( 'get_user_by' ) ) :
*
* @since 2.8.0
* @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
* @since 5.8.0 Returns the global `$current_user` if it's the user being fetched.
*
* @param string $field The field to retrieve the user with. id | ID | slug | email | login.
* @param int|string $value A value for $field. A user ID, slug, email address, or login name.
* @return WP_User|false WP_User object on success, false on failure.
*/
function get_user_by( $field, $value ) {
global $current_user;
$userdata = WP_User::get_data_by( $field, $value );
if ( ! $userdata ) {
return false;
}
if ( $current_user instanceof WP_User && $current_user->ID === (int) $userdata->ID ) {
return $current_user;
}
$user = new WP_User;
$user->init( $userdata );