-/**
- * Add user meta data as properties to given user object.
- *
- * The finished user data is cached, but the cache is not used to fill in the
- * user data for the given object. Once the function has been used, the cache
- * should be used to retrieve user data. The intention is if the current data
- * had been cached already, there would be no need to call this function.
- *
- * @access private
- * @since 2.5.0
- * @uses $wpdb WordPress database object for queries
- *
- * @param object $user The user data object.
- */
-function _fill_user( &$user ) {
- $metavalues = get_user_metavalues(array($user->ID));
- _fill_single_user($user, $metavalues[$user->ID]);
-}
-
-/**
- * Perform the query to get the $metavalues array(s) needed by _fill_user and _fill_many_users
- *
- * @since 3.0.0
- * @param array $ids User ID numbers list.
- * @return array of arrays. The array is indexed by user_id, containing $metavalues object arrays.
- */
-function get_user_metavalues($ids) {
- $objects = array();
-
- $ids = array_map('intval', $ids);
- foreach ( $ids as $id )
- $objects[$id] = array();
-
- $metas = update_meta_cache('user', $ids);
-
- foreach ( $metas as $id => $meta ) {
- foreach ( $meta as $key => $metavalues ) {
- foreach ( $metavalues as $value ) {
- $objects[$id][] = (object)array( 'user_id' => $id, 'meta_key' => $key, 'meta_value' => $value);
- }
- }
- }
-
- return $objects;
-}
-
-/**
- * Unserialize user metadata, fill $user object, then cache everything.
- *
- * @since 3.0.0
- * @param object $user The User object.
- * @param array $metavalues An array of objects provided by get_user_metavalues()
- */
-function _fill_single_user( &$user, &$metavalues ) {
- global $wpdb;
-
- foreach ( $metavalues as $meta ) {
- $value = maybe_unserialize($meta->meta_value);
- // Keys used as object vars cannot have dashes.
- $key = str_replace('-', '', $meta->meta_key);
- $user->{$key} = $value;
- }
-
- $level = $wpdb->prefix . 'user_level';
- if ( isset( $user->{$level} ) )
- $user->user_level = $user->{$level};
-
- // For backwards compat.
- if ( isset($user->first_name) )
- $user->user_firstname = $user->first_name;
- if ( isset($user->last_name) )
- $user->user_lastname = $user->last_name;
- if ( isset($user->description) )
- $user->user_description = $user->description;
-
- update_user_caches($user);
-}
-
-/**
- * Take an array of user objects, fill them with metas, and cache them.
- *
- * @since 3.0.0
- * @param array $users User objects
- */
-function _fill_many_users( &$users ) {
- $ids = array();
- foreach( $users as $user_object ) {
- $ids[] = $user_object->ID;
- }
-
- $metas = get_user_metavalues($ids);
-
- foreach ( $users as $user_object ) {
- if ( isset($metas[$user_object->ID]) ) {
- _fill_single_user($user_object, $metas[$user_object->ID]);
- }
- }
-}
-
-/**
- * Sanitize every user field.
- *
- * If the context is 'raw', then the user object or array will get minimal santization of the int fields.
- *
- * @since 2.3.0
- * @uses sanitize_user_field() Used to sanitize the fields.
- *
- * @param object|array $user The User Object or Array
- * @param string $context Optional, default is 'display'. How to sanitize user fields.
- * @return object|array The now sanitized User Object or Array (will be the same type as $user)
- */
-function sanitize_user_object($user, $context = 'display') {
- if ( is_object($user) ) {
- if ( !isset($user->ID) )
- $user->ID = 0;
- if ( isset($user->data) )
- $vars = get_object_vars( $user->data );
- else
- $vars = get_object_vars($user);
- foreach ( array_keys($vars) as $field ) {
- if ( is_string($user->$field) || is_numeric($user->$field) )
- $user->$field = sanitize_user_field($field, $user->$field, $user->ID, $context);
- }
- $user->filter = $context;
- } else {
- if ( !isset($user['ID']) )
- $user['ID'] = 0;
- foreach ( array_keys($user) as $field )
- $user[$field] = sanitize_user_field($field, $user[$field], $user['ID'], $context);
- $user['filter'] = $context;
- }
-
- return $user;
-}
-