From 08dfd2b5501ebb7abbaa716bf0ac4749202b8127 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Tue, 6 Dec 2022 13:52:02 +0000 Subject: [PATCH] Users: Clear the user_meta cache when `clean_user_cache` function is called. Unlike other clean cache functions like `clean_post_cache`, `clean_user_cache` did not also clear user meta caches. This is inconsistent and can result in some strange side effects. Update the `clean_user_cache` function to also clear user meta caches when called. Props dd32, spacedmonkey, peterwilsoncc. Fixes #54316. git-svn-id: https://develop.svn.wordpress.org/trunk@54940 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/user.php | 3 + tests/phpunit/tests/meta/updateMetadata.php | 79 +++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index cc128c26e4..595c4c8447 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -1873,6 +1873,7 @@ function update_user_caches( $user ) { * * @since 3.0.0 * @since 4.4.0 'clean_user_cache' action was added. + * @since 6.2.0 User metadata caches are now cleared. * * @param WP_User|int $user User object or ID to be cleaned from the cache */ @@ -1893,6 +1894,8 @@ function clean_user_cache( $user ) { wp_cache_delete( $user->user_email, 'useremail' ); } + wp_cache_delete( $user->ID, 'user_meta' ); + /** * Fires immediately after the given user's cache is cleaned. * diff --git a/tests/phpunit/tests/meta/updateMetadata.php b/tests/phpunit/tests/meta/updateMetadata.php index 5e376425b6..7f5aff32cd 100644 --- a/tests/phpunit/tests/meta/updateMetadata.php +++ b/tests/phpunit/tests/meta/updateMetadata.php @@ -27,4 +27,83 @@ class Tests_Meta_UpdateMetadata extends WP_UnitTestCase { $found = get_metadata( 'post', 123, 'foo\foo', true ); $this->assertSame( 'baz', $found ); } + + /** + * @ticket 54316 + * + * @group user + * + * @covers ::clean_user_cache + * + * @global wpdb $wpdb WordPress database abstraction object. + */ + public function test_clear_user_metadata_caches() { + global $wpdb; + + $user_id = self::factory()->user->create(); + + update_metadata( 'user', $user_id, 'key', 'value1' ); + + $found = get_metadata( 'user', $user_id, 'key', true ); + $this->assertSame( 'value1', $found ); + + // Simulate updating the DB from outside of WordPress. + $wpdb->update( + $wpdb->usermeta, + array( + 'meta_value' => 'value2', + ), + array( + 'user_id' => $user_id, + 'meta_key' => 'key', + ) + ); + + // Clear the user caches. + clean_user_cache( $user_id ); + + // Verify metadata cache was cleared. + $found = get_metadata( 'user', $user_id, 'key', true ); + $this->assertSame( 'value2', $found ); + } + + /** + * @ticket 54316 + * + * @group user + * + * @covers ::clean_user_cache + * + * @global wpdb $wpdb WordPress database abstraction object. + */ + public function test_clear_post_metadata_caches() { + global $wpdb; + + $post_id = self::factory()->post->create(); + + update_metadata( 'post', $post_id, 'key', 'value1' ); + + $found = get_metadata( 'post', $post_id, 'key', true ); + $this->assertSame( 'value1', $found ); + + // Simulate updating the DB from outside of WordPress. + $wpdb->update( + $wpdb->postmeta, + array( + 'meta_value' => 'value2', + ), + array( + 'post_id' => $post_id, + 'meta_key' => 'key', + ) + ); + + // Clear the post caches. + clean_post_cache( $post_id ); + + // Verify metadata cache was cleared. + $found = get_metadata( 'post', $post_id, 'key', true ); + $this->assertSame( 'value2', $found ); + } + }