WordPress.org

Make WordPress Core

Changeset 34919


Ignore:
Timestamp:
10/07/2015 10:41:07 PM (6 years ago)
Author:
boonebgorges
Message:

Handle WP_User objects properly in update_user_caches().

We should not be storing the WP_User object in the cache, as it may contain
usermeta and other data that's cache elsewhere.

Props dd32.
See #24635.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user-functions.php

    r34912 r34919  
    11031103 * @since 3.0.0
    11041104 *
    1105  * @param object $user User object to be cached
    1106  */
    1107 function update_user_caches($user) {
     1105 * @param object|WP_User $user User object to be cached
     1106 * @return bool|null Returns false on failure.
     1107 */
     1108function update_user_caches( $user ) {
     1109    if ( $user instanceof WP_User ) {
     1110        if ( ! $user->exists() ) {
     1111            return false;
     1112        }
     1113
     1114        $user = $user->data;
     1115    }
     1116
    11081117    wp_cache_add($user->ID, $user, 'users');
    11091118    wp_cache_add($user->user_login, $user->ID, 'userlogins');
  • trunk/tests/phpunit/tests/user/updateUserCaches.php

    r34918 r34919  
    5151        $this->assertEquals( 12345, wp_cache_get( 'bar', 'userslugs' ) );
    5252    }
     53
     54    /**
     55     * @ticket 24635
     56     */
     57    public function test_should_store_raw_data_in_users_bucket_when_passed_a_wp_user_object() {
     58        global $wpdb;
     59
     60        $u = $this->factory->user->create();
     61        $raw_userdata = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE ID = %d", $u ) );
     62        $user_object = new WP_User( $u );
     63
     64        update_user_caches( $user_object );
     65
     66        $cached = wp_cache_get( $u, 'users' );
     67        $this->assertFalse( $cached instanceof WP_User );
     68        $this->assertEquals( $raw_userdata, $cached );
     69    }
    5370}
Note: See TracChangeset for help on using the changeset viewer.