diff --git a/src/wp-includes/user-functions.php b/src/wp-includes/user-functions.php index fd73875958..9e95bee6c7 100644 --- a/src/wp-includes/user-functions.php +++ b/src/wp-includes/user-functions.php @@ -1102,9 +1102,18 @@ function sanitize_user_field($field, $value, $user_id, $context) { * * @since 3.0.0 * - * @param object $user User object to be cached + * @param object|WP_User $user User object to be cached + * @return bool|null Returns false on failure. */ -function update_user_caches($user) { +function update_user_caches( $user ) { + if ( $user instanceof WP_User ) { + if ( ! $user->exists() ) { + return false; + } + + $user = $user->data; + } + wp_cache_add($user->ID, $user, 'users'); wp_cache_add($user->user_login, $user->ID, 'userlogins'); wp_cache_add($user->user_email, $user->ID, 'useremail'); diff --git a/tests/phpunit/tests/user/updateUserCaches.php b/tests/phpunit/tests/user/updateUserCaches.php index 98d14a6248..f4b0856834 100644 --- a/tests/phpunit/tests/user/updateUserCaches.php +++ b/tests/phpunit/tests/user/updateUserCaches.php @@ -50,4 +50,21 @@ class Tests_User_UpdateUserCaches extends WP_UnitTestCase { $this->assertEquals( 12345, wp_cache_get( 'bar', 'userslugs' ) ); } + + /** + * @ticket 24635 + */ + public function test_should_store_raw_data_in_users_bucket_when_passed_a_wp_user_object() { + global $wpdb; + + $u = $this->factory->user->create(); + $raw_userdata = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE ID = %d", $u ) ); + $user_object = new WP_User( $u ); + + update_user_caches( $user_object ); + + $cached = wp_cache_get( $u, 'users' ); + $this->assertFalse( $cached instanceof WP_User ); + $this->assertEquals( $raw_userdata, $cached ); + } }