Make WordPress Core


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

    r47938 r50790  
    325325        }
    326326
     327        /**
     328         * @ticket 28020
     329         */
     330        public function test_get_user_by_should_return_same_instance_as_wp_get_current_user() {
     331                // Create a test user
     332                $new_user = self::factory()->user->create( array( 'role' => 'subscriber' ) );
     333
     334                // Set the test user as the current user
     335                $current_user = wp_set_current_user( $new_user );
     336
     337                // Get the test user using get_user_by()
     338                $from_get_user_by = get_user_by( 'id', $new_user );
     339
     340                $this->assertSame( $current_user, $from_get_user_by );
     341        }
    327342}
Note: See TracChangeset for help on using the changeset viewer.