Make WordPress Core

Changeset 18597


Ignore:
Timestamp:
08/24/2011 07:32:59 PM (13 years ago)
Author:
ryan
Message:

Introduce metadata_exists(), WP_User::get_data_by(), WP_User::get(), WP_User::has_prop(). Don't fill user objects with meta. Eliminate data duplication in cache and memory. Props scribu. see #15458

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/class-wp-users-list-table.php

    r18179 r18597  
    220220        if ( !( is_object( $user_object ) && is_a( $user_object, 'WP_User' ) ) )
    221221            $user_object = new WP_User( (int) $user_object );
    222         $user_object = sanitize_user_object( $user_object, 'display' );
     222        $user_object->filter = 'display';
    223223        $email = $user_object->user_email;
    224224
  • trunk/wp-admin/includes/user.php

    r17032 r18597  
    227227    $user = new WP_User( $user_id );
    228228
    229     $user_contactmethods = _wp_get_user_contactmethods( $user );
    230     foreach ($user_contactmethods as $method => $name) {
    231         if ( empty( $user->{$method} ) )
    232             $user->{$method} = '';
    233     }
    234 
    235     if ( empty($user->description) )
    236         $user->description = '';
    237 
    238     $user = sanitize_user_object($user, 'edit');
     229    $user->filter = 'edit';
    239230
    240231    return $user;
  • trunk/wp-includes/capabilities.php

    r18515 r18597  
    421421    var $filter = null;
    422422
    423     /**
    424      * Constructor - Sets up the object properties.
    425      *
    426      * Retrieves the userdata and then assigns all of the data keys to direct
    427      * properties of the object. Calls {@link WP_User::_init_caps()} after
    428      * setting up the object's user data properties.
    429      *
    430      * @since 2.0.0
    431      * @access public
    432      *
    433      * @param int|string $id User's ID or username
    434      * @param int $name Optional. User's username
     423    private static $back_compat_keys = array(
     424        'user_firstname' => 'first_name',
     425        'user_lastname' => 'last_name',
     426        'user_description' => 'description'
     427    );
     428
     429    /**
     430     * Constructor
     431     *
     432     * Retrieves the userdata and passes it to {@link WP_User::init()}.
     433     *
     434     * @since 2.0.0
     435     * @access public
     436     *
     437     * @param int|string $id User's ID
     438     * @param string $name Optional. User's username
    435439     * @param int $blog_id Optional Blog ID, defaults to current blog.
    436440     * @return WP_User
    437441     */
    438     function __construct( $id, $name = '', $blog_id = '' ) {
    439         if ( empty( $id ) && empty( $name ) )
    440             return;
    441 
     442    function __construct( $id = 0, $name = '', $blog_id = '' ) {
    442443        if ( ! is_numeric( $id ) ) {
    443444            $name = $id;
     
    445446        }
    446447
    447         if ( ! empty( $id ) )
    448             $this->data = get_userdata( $id );
     448        if ( $id )
     449            $data = self::get_data_by( 'id', $id );
    449450        else
    450             $this->data = get_user_by('login', $name );
    451 
    452         if ( empty( $this->data->ID ) )
    453             return;
    454 
    455         $this->ID = $this->data->ID;
     451            $data = self::get_data_by( 'login', $name );
     452
     453        if ( $data )
     454            $this->init( $data, $blog_id );
     455    }
     456
     457    /**
     458     * Sets up object properties, including capabilities.
     459     *
     460     * @param object $data User DB row object
     461     * @param int $blog_id Optional. The blog id to initialize for
     462     */
     463    function init( $data, $blog_id = '' ) {
     464        $this->data = $data;
     465        $this->ID = (int) $data->ID;
     466
    456467        $this->for_blog( $blog_id );
     468    }
     469
     470    /**
     471     * Return only the main user fields
     472     *
     473     * @since 3.3.0
     474     *
     475     * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
     476     * @param string|int $value The field value
     477     */
     478    static function get_data_by( $field, $value ) {
     479        global $wpdb;
     480
     481        if ( 'id' == $field )
     482            $value = absint( $value );
     483        else
     484            $value = trim( $value );
     485
     486        if ( !$value )
     487            return false;
     488
     489        switch ( $field ) {
     490            case 'id':
     491                $user_id = $value;
     492                $db_field = 'ID';
     493                break;
     494            case 'slug':
     495                $user_id = wp_cache_get($value, 'userslugs');
     496                $db_field = 'user_nicename';
     497                break;
     498            case 'email':
     499                $user_id = wp_cache_get($value, 'useremail');
     500                $db_field = 'user_email';
     501                break;
     502            case 'login':
     503                $value = sanitize_user( $value );
     504                $user_id = wp_cache_get($value, 'userlogins');
     505                $db_field = 'user_login';
     506                break;
     507            default:
     508                return false;
     509        }
     510
     511        if ( false !== $user_id ) {
     512            if ( $user = wp_cache_get( $user_id, 'users' ) )
     513                return $user;
     514        }
     515
     516        if ( !$user = $wpdb->get_row( $wpdb->prepare(
     517            "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
     518        ) ) )
     519            return false;
     520
     521        update_user_caches( $user );
     522
     523        return $user;
    457524    }
    458525
     
    467534            $key = 'ID';
    468535        }
    469         return isset( $this->data->$key );
     536
     537        if ( isset( $this->data->$key ) )
     538            return true;
     539
     540        if ( isset( self::$back_compat_keys[ $key ] ) )
     541            $key = self::$back_compat_keys[ $key ];
     542
     543        return metadata_exists( 'user', $this->ID, $key );
    470544    }
    471545
     
    481555        }
    482556
    483         return $this->data->$key;
     557        if ( isset( $this->data->$key ) ) {
     558            $value = $this->data->$key;
     559        } else {
     560            if ( isset( self::$back_compat_keys[ $key ] ) )
     561                $key = self::$back_compat_keys[ $key ];
     562            $value = get_user_meta( $this->ID, $key, true );
     563        }
     564
     565        if ( $this->filter ) {
     566            $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
     567        }
     568
     569        return $value;
    484570    }
    485571
     
    500586
    501587    /**
     588     * Retrieve the value of a property or meta key.
     589     *
     590     * Retrieves from the users and usermeta table.
     591     *
     592     * @since 3.3.0
     593     *
     594     * @param string $key Property
     595     */
     596    function get( $key ) {
     597        return $this->__get( $key );
     598    }
     599
     600    /**
     601     * Determine whether a property or meta key is set
     602     *
     603     * Consults the users and usermeta tables.
     604     *
     605     * @since 3.3.0
     606     *
     607     * @param string $key Property
     608     */
     609    function has_prop( $key ) {
     610        return $this->__isset( $key );
     611    }
     612
     613    /**
    502614     * Set up capability object properties.
    503615     *
     
    520632            $this->cap_key = $cap_key;
    521633
    522         $this->caps = &$this->data->{$this->cap_key};
     634        $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
     635
    523636        if ( ! is_array( $this->caps ) )
    524637            $this->caps = array();
     
    663776    function update_user_level_from_caps() {
    664777        global $wpdb;
    665         $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
     778        $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
    666779        update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
    667780    }
     
    10671180    $args = array_merge( array( $capability ), $args );
    10681181
    1069     return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
     1182    return call_user_func_array( array( $current_user, 'has_cap' ), $args );
    10701183}
    10711184
  • trunk/wp-includes/deprecated.php

    r18498 r18597  
    26282628function wp_default_editor() {
    26292629    _deprecated_function( __FUNCTION__, '3.3' );
    2630    
     2630
    26312631    global $wp_editor;
    26322632    if ( !is_a($wp_editor, 'WP_Editor') ) {
     
    26342634        $wp_editor = new WP_Editor;
    26352635    }
    2636    
     2636
    26372637    return $wp_editor->wp_default_editor();
    26382638}
     
    26422642 *
    26432643 * @since 2.1.0
    2644  * @deprecated 3.3 
     2644 * @deprecated 3.3
    26452645 *
    26462646 * @param string $content Textarea content.
     
    26512651 */
    26522652function the_editor($content, $id = 'content', $prev_id = 'title', $media_buttons = true, $tab_index = 2, $extended = true) {
    2653    
     2653
    26542654    wp_editor( $content, $id, array( 'media_buttons' => $media_buttons ) );
    26552655    return;
    26562656}
    26572657
     2658/**
     2659 * Perform the query to get the $metavalues array(s) needed by _fill_user and _fill_many_users
     2660 *
     2661 * @since 3.0.0
     2662 * @param array $ids User ID numbers list.
     2663 * @return array of arrays. The array is indexed by user_id, containing $metavalues object arrays.
     2664 */
     2665function get_user_metavalues($ids) {
     2666    _deprecated_function( __FUNCTION__, '3.3' );
     2667
     2668    $objects = array();
     2669
     2670    $ids = array_map('intval', $ids);
     2671    foreach ( $ids as $id )
     2672        $objects[$id] = array();
     2673
     2674    $metas = update_meta_cache('user', $ids);
     2675
     2676    foreach ( $metas as $id => $meta ) {
     2677        foreach ( $meta as $key => $metavalues ) {
     2678            foreach ( $metavalues as $value ) {
     2679                $objects[$id][] = (object)array( 'user_id' => $id, 'meta_key' => $key, 'meta_value' => $value);
     2680            }
     2681        }
     2682    }
     2683
     2684    return $objects;
     2685}
     2686
     2687/**
     2688 * Sanitize every user field.
     2689 *
     2690 * If the context is 'raw', then the user object or array will get minimal santization of the int fields.
     2691 *
     2692 * @since 2.3.0
     2693 * @deprecated 3.3.0
     2694 * @uses sanitize_user_field() Used to sanitize the fields.
     2695 *
     2696 * @param object|array $user The User Object or Array
     2697 * @param string $context Optional, default is 'display'. How to sanitize user fields.
     2698 * @return object|array The now sanitized User Object or Array (will be the same type as $user)
     2699 */
     2700function sanitize_user_object($user, $context = 'display') {
     2701    _deprecated_function( __FUNCTION__, '3.3' );
     2702
     2703    if ( is_object($user) ) {
     2704        if ( !isset($user->ID) )
     2705            $user->ID = 0;
     2706        if ( !is_a( $user, 'WP_User' ) ) {
     2707            $vars = get_object_vars($user);
     2708            foreach ( array_keys($vars) as $field ) {
     2709                if ( is_string($user->$field) || is_numeric($user->$field) )
     2710                    $user->$field = sanitize_user_field($field, $user->$field, $user->ID, $context);
     2711            }
     2712        }
     2713        $user->filter = $context;
     2714    } else {
     2715        if ( !isset($user['ID']) )
     2716            $user['ID'] = 0;
     2717        foreach ( array_keys($user) as $field )
     2718            $user[$field] = sanitize_user_field($field, $user[$field], $user['ID'], $context);
     2719        $user['filter'] = $context;
     2720    }
     2721
     2722    return $user;
     2723}
  • trunk/wp-includes/meta.php

    r18500 r18597  
    7474
    7575    wp_cache_delete($object_id, $meta_type . '_meta');
    76     // users cache stores usermeta that must be cleared.
    77     if ( 'user' == $meta_type )
    78         clean_user_cache($object_id);
    7976
    8077    do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );
     
    150147
    151148    do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
    152    
     149
    153150    if ( 'post' == $meta_type )
    154         do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );   
     151        do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
    155152
    156153    $wpdb->update( $table, $data, $where );
    157154
    158155    wp_cache_delete($object_id, $meta_type . '_meta');
    159     // users cache stores usermeta that must be cleared.
    160     if ( 'user' == $meta_type )
    161         clean_user_cache($object_id);
    162156
    163157    do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
    164    
     158
    165159    if ( 'post' == $meta_type )
    166         do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );     
     160        do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
    167161
    168162    return true;
     
    211205    $_meta_value = $meta_value;
    212206    $meta_value = maybe_serialize( $meta_value );
    213        
     207
    214208    $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
    215209
     
    228222
    229223    do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
    230    
     224
    231225    if ( 'post' == $meta_type )
    232226        do_action( 'delete_postmeta', $meta_ids );
     
    247241    }
    248242
    249     // users cache stores usermeta that must be cleared.
    250     if ( 'user' == $meta_type )
    251         clean_user_cache($object_id);
    252 
    253243    do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
    254    
     244
    255245    if ( 'post' == $meta_type )
    256246        do_action( 'deleted_postmeta', $meta_ids );
     
    311301
    312302/**
     303 * Determine if a meta key is set for a given object
     304 *
     305 * @since 3.3.0
     306 *
     307 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
     308 * @param int $object_id ID of the object metadata is for
     309 * @param string $meta_key Metadata key.
     310 * @return boolean true of the key is set, false if not.
     311 */
     312function metadata_exists( $meta_type, $object_id, $meta_key ) {
     313    if ( ! $meta_type )
     314        return false;
     315
     316    if ( ! $object_id = absint( $object_id ) )
     317        return false;
     318
     319    $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true );
     320    if ( null !== $check )
     321        return true;
     322
     323    $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
     324
     325    if ( !$meta_cache ) {
     326        $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
     327        $meta_cache = $meta_cache[$object_id];
     328    }
     329
     330    if ( isset( $meta_cache[ $meta_key ] ) )
     331        return true;
     332
     333    return false;
     334}
     335
     336/**
    313337 * Get meta data by meta ID
    314338 *
     
    407431        if ( 'post' == $meta_type )
    408432            do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
    409        
     433
    410434        // Run the update query, all fields in $data are %s, $where is a %d.
    411435        $result = (bool) $wpdb->update( $table, $data, $where, '%s', '%d' );
     
    413437        // Clear the caches.
    414438        wp_cache_delete($object_id, $meta_type . '_meta');
    415        
    416         // Users cache stores usermeta that must be cleared.
    417         if ( 'user' == $meta_type )
    418             clean_user_cache($object_id);
    419439
    420440        do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
     
    425445        return $result;
    426446    }
    427    
     447
    428448    // And if the meta was not found.
    429449    return false;
     
    444464function delete_metadata_by_mid( $meta_type, $meta_id ) {
    445465    global $wpdb;
    446    
     466
    447467    // Make sure everything is valid.
    448468    if ( ! $meta_type )
     
    454474    if ( ! $table = _get_meta_table( $meta_type ) )
    455475        return false;
    456    
     476
    457477    // object and id columns
    458478    $column = esc_sql($meta_type . '_id');
     
    474494        wp_cache_delete($object_id, $meta_type . '_meta');
    475495
    476         // Users cache stores usermeta that must be cleared.
    477         if ( 'user' == $meta_type )
    478             clean_user_cache($object_id);
    479 
    480496        do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
    481497
     
    486502
    487503    }
    488    
     504
    489505    // Meta id was not found.
    490506    return false;
     
    819835/**
    820836 * Register meta key
    821  * 
     837 *
    822838 * @since 3.3.0
    823839 *
  • trunk/wp-includes/pluggable.php

    r18558 r18597  
    9999 *
    100100 * @param int $user_id User ID
    101  * @return bool|object False on failure, User DB row object
     101 * @return bool|object False on failure, WP_User object on success
    102102 */
    103103function get_userdata( $user_id ) {
     104    return get_user_by( 'id', $user_id );
     105}
     106endif;
     107
     108if ( !function_exists('get_user_by') ) :
     109/**
     110 * Retrieve user info by a given field
     111 *
     112 * @since 2.8.0
     113 *
     114 * @param string $field The field to retrieve the user with.  id | slug | email | login
     115 * @param int|string $value A value for $field.  A user ID, slug, email address, or login name.
     116 * @return bool|object False on failure, WP_User object on success
     117 */
     118function get_user_by( $field, $value ) {
     119    $userdata = WP_User::get_data_by( $field, $value );
     120
     121    if ( !$userdata )
     122        return false;
     123
     124    $user = new WP_User;
     125    $user->init( $userdata );
     126
     127    return $user;
     128}
     129endif;
     130
     131if ( !function_exists('cache_users') ) :
     132/**
     133 * Retrieve info for user lists to prevent multiple queries by get_userdata()
     134 *
     135 * @since 3.0.0
     136 *
     137 * @param array $user_ids User ID numbers list
     138 */
     139function cache_users( $user_ids ) {
    104140    global $wpdb;
    105141
    106     if ( ! is_numeric( $user_id ) )
    107         return false;
    108 
    109     $user_id = absint( $user_id );
    110     if ( ! $user_id )
    111         return false;
    112 
    113     $user = wp_cache_get( $user_id, 'users' );
    114 
    115     if ( $user )
    116         return $user;
    117 
    118     if ( ! $user = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE ID = %d LIMIT 1", $user_id ) ) )
    119         return false;
    120 
    121     _fill_user( $user );
    122 
    123     return $user;
    124 }
    125 endif;
    126 
    127 if ( !function_exists('cache_users') ) :
    128 /**
    129  * Retrieve info for user lists to prevent multiple queries by get_userdata()
    130  *
    131  * @since 3.0.0
    132  *
    133  * @param array $users User ID numbers list
    134  */
    135 function cache_users( $users ) {
    136     global $wpdb;
    137 
    138142    $clean = array();
    139     foreach($users as $id) {
     143    foreach ( $user_ids as $id ) {
    140144        $id = (int) $id;
    141         if (wp_cache_get($id, 'users')) {
    142             // seems to be cached already
    143         } else {
     145        if ( !wp_cache_get( $id, 'users' ) ) {
    144146            $clean[] = $id;
    145147        }
    146148    }
    147149
    148     if ( 0 == count($clean) )
     150    if ( empty( $clean ) )
    149151        return;
    150152
    151     $list = implode(',', $clean);
    152 
    153     $results = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE ID IN ($list)");
    154 
    155     _fill_many_users($results);
    156 }
    157 endif;
    158 
    159 if ( !function_exists('get_user_by') ) :
    160 /**
    161  * Retrieve user info by a given field
    162  *
    163  * @since 2.8.0
    164  *
    165  * @param string $field The field to retrieve the user with.  id | slug | email | login
    166  * @param int|string $value A value for $field.  A user ID, slug, email address, or login name.
    167  * @return bool|object False on failure, User DB row object
    168  */
    169 function get_user_by($field, $value) {
    170     global $wpdb;
    171 
    172     switch ($field) {
    173         case 'id':
    174             return get_userdata($value);
    175             break;
    176         case 'slug':
    177             $user_id = wp_cache_get($value, 'userslugs');
    178             $field = 'user_nicename';
    179             break;
    180         case 'email':
    181             $user_id = wp_cache_get($value, 'useremail');
    182             $field = 'user_email';
    183             break;
    184         case 'login':
    185             $value = sanitize_user( $value );
    186             $user_id = wp_cache_get($value, 'userlogins');
    187             $field = 'user_login';
    188             break;
    189         default:
    190             return false;
    191     }
    192 
    193      if ( false !== $user_id )
    194         return get_userdata($user_id);
    195 
    196     if ( !$user = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->users WHERE $field = %s", $value) ) )
    197         return false;
    198 
    199     _fill_user($user);
    200 
    201     return $user;
     153    $list = implode( ',', $clean );
     154
     155    $users = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($list)" );
     156
     157    $ids = array();
     158    foreach ( $users as $user ) {
     159        update_user_caches( $user );
     160        $ids[] = $user->ID;
     161    }
     162    update_meta_cache( 'user', $ids );
    202163}
    203164endif;
  • trunk/wp-includes/user.php

    r18560 r18597  
    255255        _deprecated_argument( __FUNCTION__, '3.0' );
    256256
    257     if ( empty($user) ) {
     257    if ( empty( $user ) )
    258258        $user = wp_get_current_user();
    259         $user = $user->ID;
    260     }
    261 
    262     $user = get_userdata($user);
    263 
    264     // Keys used as object vars cannot have dashes.
    265     $key = str_replace('-', '', $option);
    266 
    267     if ( isset( $user->{$wpdb->prefix . $key} ) ) // Blog specific
    268         $result = $user->{$wpdb->prefix . $key};
    269     elseif ( isset( $user->{$key} ) ) // User specific and cross-blog
    270         $result = $user->{$key};
     259    else
     260        $user = new WP_User( $user );
     261
     262    if ( ! isset( $user->ID ) )
     263        return false;
     264
     265    if ( $user->has_prop( $wpdb->prefix . $option ) ) // Blog specific
     266        $result = $user->get( $wpdb->prefix . $option );
     267    elseif ( $user->has_prop( $option ) ) // User specific and cross-blog
     268        $result = $user->get( $option );
    271269    else
    272270        $result = false;
     
    681679
    682680    if ( false === $blogs ) {
    683         $user = get_userdata( (int) $id );
    684         if ( !$user )
     681        $userkeys = array_keys( get_user_meta( (int) $id ) );
     682        if ( empty( $userkeys ) )
    685683            return false;
    686684
    687685        $blogs = $match = array();
    688         $prefix_length = strlen($wpdb->base_prefix);
    689         foreach ( (array) $user as $key => $value ) {
     686        $prefix_length = strlen( $wpdb->base_prefix );
     687        foreach ( $userkeys as $key ) {
    690688            if ( $prefix_length && substr($key, 0, $prefix_length) != $wpdb->base_prefix )
    691689                continue;
     
    794792 *  is true.
    795793 */
    796 function get_user_meta($user_id, $key, $single = false) {
     794function get_user_meta($user_id, $key = '', $single = false) {
    797795    return get_metadata('user', $user_id, $key, $single);
    798796}
     
    10441042
    10451043/**
    1046  * Add user meta data as properties to given user object.
    1047  *
    1048  * The finished user data is cached, but the cache is not used to fill in the
    1049  * user data for the given object. Once the function has been used, the cache
    1050  * should be used to retrieve user data. The intention is if the current data
    1051  * had been cached already, there would be no need to call this function.
    1052  *
    1053  * @access private
    1054  * @since 2.5.0
    1055  * @uses $wpdb WordPress database object for queries
    1056  *
    1057  * @param object $user The user data object.
    1058  */
    1059 function _fill_user( &$user ) {
    1060     $metavalues = get_user_metavalues(array($user->ID));
    1061     _fill_single_user($user, $metavalues[$user->ID]);
    1062 }
    1063 
    1064 /**
    1065  * Perform the query to get the $metavalues array(s) needed by _fill_user and _fill_many_users
    1066  *
    1067  * @since 3.0.0
    1068  * @param array $ids User ID numbers list.
    1069  * @return array of arrays. The array is indexed by user_id, containing $metavalues object arrays.
    1070  */
    1071 function get_user_metavalues($ids) {
    1072     $objects = array();
    1073 
    1074     $ids = array_map('intval', $ids);
    1075     foreach ( $ids as $id )
    1076         $objects[$id] = array();
    1077 
    1078     $metas = update_meta_cache('user', $ids);
    1079 
    1080     foreach ( $metas as $id => $meta ) {
    1081         foreach ( $meta as $key => $metavalues ) {
    1082             foreach ( $metavalues as $value ) {
    1083                 $objects[$id][] = (object)array( 'user_id' => $id, 'meta_key' => $key, 'meta_value' => $value);
    1084             }
    1085         }
    1086     }
    1087 
    1088     return $objects;
    1089 }
    1090 
    1091 /**
    1092  * Unserialize user metadata, fill $user object, then cache everything.
    1093  *
    1094  * @since 3.0.0
    1095  * @param object $user The User object.
    1096  * @param array $metavalues An array of objects provided by get_user_metavalues()
    1097  */
    1098 function _fill_single_user( &$user, &$metavalues ) {
    1099     global $wpdb;
    1100 
    1101     foreach ( $metavalues as $meta ) {
    1102         $value = maybe_unserialize($meta->meta_value);
    1103         // Keys used as object vars cannot have dashes.
    1104         $key = str_replace('-', '', $meta->meta_key);
    1105         $user->{$key} = $value;
    1106     }
    1107 
    1108     $level = $wpdb->prefix . 'user_level';
    1109     if ( isset( $user->{$level} ) )
    1110         $user->user_level = $user->{$level};
    1111 
    1112     // For backwards compat.
    1113     if ( isset($user->first_name) )
    1114         $user->user_firstname = $user->first_name;
    1115     if ( isset($user->last_name) )
    1116         $user->user_lastname = $user->last_name;
    1117     if ( isset($user->description) )
    1118         $user->user_description = $user->description;
    1119 
    1120     update_user_caches($user);
    1121 }
    1122 
    1123 /**
    1124  * Take an array of user objects, fill them with metas, and cache them.
    1125  *
    1126  * @since 3.0.0
    1127  * @param array $users User objects
    1128  */
    1129 function _fill_many_users( &$users ) {
    1130     $ids = array();
    1131     foreach( $users as $user_object ) {
    1132         $ids[] = $user_object->ID;
    1133     }
    1134 
    1135     $metas = get_user_metavalues($ids);
    1136 
    1137     foreach ( $users as $user_object ) {
    1138         if ( isset($metas[$user_object->ID]) ) {
    1139             _fill_single_user($user_object, $metas[$user_object->ID]);
    1140         }
    1141     }
    1142 }
    1143 
    1144 /**
    1145  * Sanitize every user field.
    1146  *
    1147  * If the context is 'raw', then the user object or array will get minimal santization of the int fields.
    1148  *
    1149  * @since 2.3.0
    1150  * @uses sanitize_user_field() Used to sanitize the fields.
    1151  *
    1152  * @param object|array $user The User Object or Array
    1153  * @param string $context Optional, default is 'display'. How to sanitize user fields.
    1154  * @return object|array The now sanitized User Object or Array (will be the same type as $user)
    1155  */
    1156 function sanitize_user_object($user, $context = 'display') {
    1157     if ( is_object($user) ) {
    1158         if ( !isset($user->ID) )
    1159             $user->ID = 0;
    1160         if ( isset($user->data) )
    1161             $vars = get_object_vars( $user->data );
    1162         else
    1163             $vars = get_object_vars($user);
    1164         foreach ( array_keys($vars) as $field ) {
    1165             if ( is_string($user->$field) || is_numeric($user->$field) )
    1166                 $user->$field = sanitize_user_field($field, $user->$field, $user->ID, $context);
    1167         }
    1168         $user->filter = $context;
    1169     } else {
    1170         if ( !isset($user['ID']) )
    1171             $user['ID'] = 0;
    1172         foreach ( array_keys($user) as $field )
    1173             $user[$field] = sanitize_user_field($field, $user[$field], $user['ID'], $context);
    1174         $user['filter'] = $context;
    1175     }
    1176 
    1177     return $user;
    1178 }
    1179 
    1180 /**
    11811044 * Sanitize user field based on context.
    11821045 *
     
    12651128 * @param object $user User object to be cached
    12661129 */
    1267 function update_user_caches(&$user) {
     1130function update_user_caches($user) {
    12681131    wp_cache_add($user->ID, $user, 'users');
    12691132    wp_cache_add($user->user_login, $user->ID, 'userlogins');
     
    12801143 */
    12811144function clean_user_cache($id) {
    1282     $user = new WP_User($id);
     1145    $user = WP_User::get_data_by( 'id', $id );
    12831146
    12841147    wp_cache_delete($id, 'users');
     
    13911254        $ID = (int) $ID;
    13921255        $update = true;
    1393         $old_user_data = get_userdata($ID);
     1256        $old_user_data = WP_User::get_data_by( 'id', $ID );
    13941257    } else {
    13951258        $update = false;
     
    15491412
    15501413    // First, get all of the original fields
    1551     $user = get_userdata($ID);
     1414    $user = WP_User::get_data_by('id', $ID);
    15521415
    15531416    // Escape data pulled from DB.
Note: See TracChangeset for help on using the changeset viewer.