Make WordPress Core


Ignore:
Timestamp:
08/24/2011 07:32:59 PM (10 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.