[wp-trac] [WordPress Trac] #28020: get_userdata and wp_get_current_user do not share WP_User instance

WordPress Trac noreply at wordpress.org
Thu Apr 29 20:08:59 UTC 2021


#28020: get_userdata and wp_get_current_user do not share WP_User instance
--------------------------------------------+----------------------------
 Reporter:  rmccue                          |       Owner:  peterwilsoncc
     Type:  defect (bug)                    |      Status:  closed
 Priority:  normal                          |   Milestone:  5.8
Component:  Users                           |     Version:
 Severity:  normal                          |  Resolution:  fixed
 Keywords:  has-patch has-unit-tests early  |     Focuses:  rest-api
--------------------------------------------+----------------------------

Comment (by hellofromTonya):

 **Manual Testing**

 === Steps to Reproduce and manually test:

 1. Added
 [https://gist.github.com/hellofromtonya/0845229fa9b7c26668cd2738e4150abd
 this testing script file] to the `wp-content/mu-plugins` folder
 2. Logged into the test site as an admin
 3. Created a test user and set access to Subscriber
 4. Viewed the site on the frontend to capture the results from the testing
 script

 === Reproduce original problem

 Was able to reproduce the problem.

 Results: 3 different objects for 1 user ❌

 {{{
 Current user => you
 Current user ID is: 1

 Has admin rights? 👍

 Object source           Object ID
 get_user_by()              832
 $GLOBALS['current_user']   540

 Changing to a different user
 Current user ID is: 2

 Has admin rights? 👎

 Object source            Object ID
 get_user_by()               307
 $GLOBALS['current_user'].   833
 get_userdata()              831

 Changing different user to be an admin
 Current user ID is: 2

 Has admin rights? 👎

 Object source              Object ID
 get_user_by()               307
 $GLOBALS['current_user']    833
 get_userdata()              831
 }}}

 Notice the object IDs are different, i.e. via
 [https://www.php.net/manual/en/function.spl-object-id.php spl_object_id]
 for the same user when getting from `get_user_user_by()`,
 `get_userdata()`, and the global `$GLOBALS['current_user']`. One user but
 3 different objects.


 === Testing with the fix applied

 Applied the fix and refreshed the test site.

 Results: 1 object per user. ✅

 {{{
 Current user => you
 Current user ID is: 1

 Has admin rights? 👍

 Object source           Object ID
 get_user_by()              540
 $GLOBALS['current_user']   540

 Changing to a different user
 Current user ID is: 2

 Has admin rights? 👎

 Object source            Object ID
 get_user_by()               829
 $GLOBALS['current_user']    829
 get_userdata()              829

 Changing different user to be an admin
 Current user ID is: 2

 Has admin rights? 👍

 Object source              Object ID
 get_user_by()               829
 $GLOBALS['current_user']    829
 get_userdata()              829
 }}}

 === Observations

 - Can reproduce the problem
 - Changeset [50790] fixes it

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/28020#comment:25>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list