WordPress.org

Make WordPress Core


Ignore:
Timestamp:
04/26/2021 01:02:34 AM (3 months 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/pluggable.php

    r50781 r50790  
    9292     * @since 2.8.0
    9393     * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
     94     * @since 5.8.0 Returns the global `$current_user` if it's the user being fetched.
    9495     *
    9596     * @param string     $field The field to retrieve the user with. id | ID | slug | email | login.
     
    9899     */
    99100    function get_user_by( $field, $value ) {
     101        global $current_user;
     102
    100103        $userdata = WP_User::get_data_by( $field, $value );
    101104
    102105        if ( ! $userdata ) {
    103106            return false;
     107        }
     108
     109        if ( $current_user instanceof WP_User && $current_user->ID === (int) $userdata->ID ) {
     110            return $current_user;
    104111        }
    105112
Note: See TracChangeset for help on using the changeset viewer.