Changeset 18597 for trunk/wp-includes/user.php
- Timestamp:
- 08/24/2011 07:32:59 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/user.php
r18560 r18597 255 255 _deprecated_argument( __FUNCTION__, '3.0' ); 256 256 257 if ( empty( $user) ) {257 if ( empty( $user ) ) 258 258 $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 ); 271 269 else 272 270 $result = false; … … 681 679 682 680 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 ) ) 685 683 return false; 686 684 687 685 $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 ) { 690 688 if ( $prefix_length && substr($key, 0, $prefix_length) != $wpdb->base_prefix ) 691 689 continue; … … 794 792 * is true. 795 793 */ 796 function get_user_meta($user_id, $key , $single = false) {794 function get_user_meta($user_id, $key = '', $single = false) { 797 795 return get_metadata('user', $user_id, $key, $single); 798 796 } … … 1044 1042 1045 1043 /** 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 the1049 * user data for the given object. Once the function has been used, the cache1050 * should be used to retrieve user data. The intention is if the current data1051 * had been cached already, there would be no need to call this function.1052 *1053 * @access private1054 * @since 2.5.01055 * @uses $wpdb WordPress database object for queries1056 *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_users1066 *1067 * @since 3.0.01068 * @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.01095 * @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.01127 * @param array $users User objects1128 */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.01150 * @uses sanitize_user_field() Used to sanitize the fields.1151 *1152 * @param object|array $user The User Object or Array1153 * @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 else1163 $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 /**1181 1044 * Sanitize user field based on context. 1182 1045 * … … 1265 1128 * @param object $user User object to be cached 1266 1129 */ 1267 function update_user_caches( &$user) {1130 function update_user_caches($user) { 1268 1131 wp_cache_add($user->ID, $user, 'users'); 1269 1132 wp_cache_add($user->user_login, $user->ID, 'userlogins'); … … 1280 1143 */ 1281 1144 function clean_user_cache($id) { 1282 $user = new WP_User($id);1145 $user = WP_User::get_data_by( 'id', $id ); 1283 1146 1284 1147 wp_cache_delete($id, 'users'); … … 1391 1254 $ID = (int) $ID; 1392 1255 $update = true; 1393 $old_user_data = get_userdata($ID);1256 $old_user_data = WP_User::get_data_by( 'id', $ID ); 1394 1257 } else { 1395 1258 $update = false; … … 1549 1412 1550 1413 // First, get all of the original fields 1551 $user = get_userdata($ID);1414 $user = WP_User::get_data_by('id', $ID); 1552 1415 1553 1416 // Escape data pulled from DB.
Note: See TracChangeset
for help on using the changeset viewer.