Make WordPress Core


Ignore:
Timestamp:
04/26/2021 01:02:34 AM (4 years ago)
Author:
peterwilsoncc
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r50641 r50790  
    15621562 * @since 3.0.0
    15631563 * @since 4.4.0 'clean_user_cache' action was added.
     1564 * @since 5.8.0 Refreshes the global user instance if cleaning the user cache for the current user.
     1565 *
     1566 * @global WP_User $current_user The current user object which holds the user data.
    15641567 *
    15651568 * @param WP_User|int $user User object or ID to be cleaned from the cache
    15661569 */
    15671570function clean_user_cache( $user ) {
     1571    global $current_user;
     1572
    15681573    if ( is_numeric( $user ) ) {
    15691574        $user = new WP_User( $user );
     
    15881593     */
    15891594    do_action( 'clean_user_cache', $user->ID, $user );
     1595
     1596    // Refresh the global user instance if the cleaning current user.
     1597    if ( get_current_user_id() === (int) $user->ID ) {
     1598        $user_id      = (int) $user->ID;
     1599        $current_user = null;
     1600        wp_set_current_user( $user_id, '' );
     1601    }
    15901602}
    15911603
Note: See TracChangeset for help on using the changeset viewer.