Make WordPress Core


Ignore:
Timestamp:
12/06/2022 01:52:02 PM (20 months ago)
Author:
spacedmonkey
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/meta/updateMetadata.php

    r46586 r54940  
    2828        $this->assertSame( 'baz', $found );
    2929    }
     30
     31    /**
     32     * @ticket 54316
     33     *
     34     * @group user
     35     *
     36     * @covers ::clean_user_cache
     37     *
     38     * @global wpdb $wpdb WordPress database abstraction object.
     39     */
     40    public function test_clear_user_metadata_caches() {
     41        global $wpdb;
     42
     43        $user_id = self::factory()->user->create();
     44
     45        update_metadata( 'user', $user_id, 'key', 'value1' );
     46
     47        $found = get_metadata( 'user', $user_id, 'key', true );
     48        $this->assertSame( 'value1', $found );
     49
     50        // Simulate updating the DB from outside of WordPress.
     51        $wpdb->update(
     52            $wpdb->usermeta,
     53            array(
     54                'meta_value' => 'value2',
     55            ),
     56            array(
     57                'user_id'  => $user_id,
     58                'meta_key' => 'key',
     59            )
     60        );
     61
     62        // Clear the user caches.
     63        clean_user_cache( $user_id );
     64
     65        // Verify metadata cache was cleared.
     66        $found = get_metadata( 'user', $user_id, 'key', true );
     67        $this->assertSame( 'value2', $found );
     68    }
     69
     70    /**
     71     * @ticket 54316
     72     *
     73     * @group user
     74     *
     75     * @covers ::clean_user_cache
     76     *
     77     * @global wpdb $wpdb WordPress database abstraction object.
     78     */
     79    public function test_clear_post_metadata_caches() {
     80        global $wpdb;
     81
     82        $post_id = self::factory()->post->create();
     83
     84        update_metadata( 'post', $post_id, 'key', 'value1' );
     85
     86        $found = get_metadata( 'post', $post_id, 'key', true );
     87        $this->assertSame( 'value1', $found );
     88
     89        // Simulate updating the DB from outside of WordPress.
     90        $wpdb->update(
     91            $wpdb->postmeta,
     92            array(
     93                'meta_value' => 'value2',
     94            ),
     95            array(
     96                'post_id'  => $post_id,
     97                'meta_key' => 'key',
     98            )
     99        );
     100
     101        // Clear the post caches.
     102        clean_post_cache( $post_id );
     103
     104        // Verify metadata cache was cleared.
     105        $found = get_metadata( 'post', $post_id, 'key', true );
     106        $this->assertSame( 'value2', $found );
     107    }
     108
    30109}
Note: See TracChangeset for help on using the changeset viewer.