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.


git-svn-id: https://develop.svn.wordpress.org/trunk@50790 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson
2021-04-26 01:02:34 +00:00
parent c44e1d6fe4
commit ecd93af74a
3 changed files with 34 additions and 0 deletions

View File

@@ -1561,10 +1561,15 @@ function update_user_caches( $user ) {
*
* @since 3.0.0
* @since 4.4.0 'clean_user_cache' action was added.
* @since 5.8.0 Refreshes the global user instance if cleaning the user cache for the current user.
*
* @global WP_User $current_user The current user object which holds the user data.
*
* @param WP_User|int $user User object or ID to be cleaned from the cache
*/
function clean_user_cache( $user ) {
global $current_user;
if ( is_numeric( $user ) ) {
$user = new WP_User( $user );
}
@@ -1587,6 +1592,13 @@ function clean_user_cache( $user ) {
* @param WP_User $user User object.
*/
do_action( 'clean_user_cache', $user->ID, $user );
// Refresh the global user instance if the cleaning current user.
if ( get_current_user_id() === (int) $user->ID ) {
$user_id = (int) $user->ID;
$current_user = null;
wp_set_current_user( $user_id, '' );
}
}
/**