[wp-trac] [WordPress Trac] #54984: wp_update_user doesn't work properly with current user instance

WordPress Trac noreply at wordpress.org
Thu Mar 10 03:12:36 UTC 2022


#54984: wp_update_user doesn't work properly with current user instance
--------------------------+---------------------
 Reporter:  oztaser       |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  6.0
Component:  Users         |     Version:
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+---------------------

Comment (by peterwilsoncc):

 Thanks for providing an mu-plugin as a proof of concept, @oztaser, it was
 a great help.

 The bug seems to be caused by the combination of two things:
 * objects are passed by reference
 * since 5.8 WP has used the same user object for the current user whenever
 possible.

 The result is that in this block of code, the old user data object is no
 such thing: it's simply a reference to the modified object that has been
 passed to the function.


 {{{#!php
 <?php

 // In `wp_update_user()`

 // First, get all of the original fields.
 $user_obj = get_userdata( $user_id );
 if ( ! $user_obj ) {
         return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' )
 );
 }
 }}}

 [https://github.com/WordPress/WordPress/blob/fcf2500f49341dfce84c3ac68d963a6a92874f64
 /wp-includes/user.php#L2350-L2354 reference]

 ----

 In [attachment:"54984.diff"] I've worked around this by creating a new
 `WP_User` object for the purpose of obtaining the old data.

 However, I am not sure this is an ideal solution as `get_userdata()` and
 related functions are pluggable so there is no way of knowing if the data
 returned by `new WP_User( $user_id )` is what is returned by
 `get_userdata()`.

 A technique to force an uncached copy of the user data is what is needed
 to fix this though.

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


More information about the wp-trac mailing list