+/**
+ * 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;
+}
+
+/**
+ * Sanitize user field based on context.
+ *
+ * Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and 'js'. The
+ * 'display' context is used by default. 'attribute' and 'js' contexts are treated like 'display'
+ * when calling filters.
+ *
+ * @since 2.3.0
+ * @uses apply_filters() Calls 'edit_$field' and '{$field_no_prefix}_edit_pre' passing $value and
+ * $user_id if $context == 'edit' and field name prefix == 'user_'.
+ *
+ * @uses apply_filters() Calls 'edit_user_$field' passing $value and $user_id if $context == 'db'.
+ * @uses apply_filters() Calls 'pre_$field' passing $value if $context == 'db' and field name prefix == 'user_'.
+ * @uses apply_filters() Calls '{$field}_pre' passing $value if $context == 'db' and field name prefix != 'user_'.
+ *
+ * @uses apply_filters() Calls '$field' passing $value, $user_id and $context if $context == anything
+ * other than 'raw', 'edit' and 'db' and field name prefix == 'user_'.
+ * @uses apply_filters() Calls 'user_$field' passing $value if $context == anything other than 'raw',
+ * 'edit' and 'db' and field name prefix != 'user_'.
+ *
+ * @param string $field The user Object field name.
+ * @param mixed $value The user Object value.
+ * @param int $user_id user ID.
+ * @param string $context How to sanitize user fields. Looks for 'raw', 'edit', 'db', 'display',
+ * 'attribute' and 'js'.
+ * @return mixed Sanitized value.
+ */
+function sanitize_user_field($field, $value, $user_id, $context) {
+ $int_fields = array('ID');
+ if ( in_array($field, $int_fields) )
+ $value = (int) $value;
+
+ if ( 'raw' == $context )
+ return $value;
+
+ if ( !is_string($value) && !is_numeric($value) )
+ return $value;
+
+ $prefixed = false;
+ if ( false !== strpos($field, 'user_') ) {
+ $prefixed = true;
+ $field_no_prefix = str_replace('user_', '', $field);
+ }
+
+ if ( 'edit' == $context ) {
+ if ( $prefixed ) {
+ $value = apply_filters("edit_{$field}", $value, $user_id);
+ } else {
+ $value = apply_filters("edit_user_{$field}", $value, $user_id);
+ }
+
+ if ( 'description' == $field )
+ $value = esc_html( $value ); // textarea_escaped?
+ else
+ $value = esc_attr($value);
+ } else if ( 'db' == $context ) {
+ if ( $prefixed ) {
+ $value = apply_filters("pre_{$field}", $value);
+ } else {
+ $value = apply_filters("pre_user_{$field}", $value);
+ }
+ } else {
+ // Use display filters by default.
+ if ( $prefixed )
+ $value = apply_filters($field, $value, $user_id, $context);
+ else
+ $value = apply_filters("user_{$field}", $value, $user_id, $context);
+ }
+
+ if ( 'user_url' == $field )
+ $value = esc_url($value);
+
+ if ( 'attribute' == $context )
+ $value = esc_attr($value);
+ else if ( 'js' == $context )
+ $value = esc_js($value);
+
+ return $value;
+}
+
+/**
+ * Update all user caches
+ *
+ * @since 3.0.0
+ *
+ * @param object $user User object to be cached
+ */
+function update_user_caches(&$user) {
+ wp_cache_add($user->ID, $user, 'users');
+ wp_cache_add($user->user_login, $user->ID, 'userlogins');
+ wp_cache_add($user->user_email, $user->ID, 'useremail');
+ wp_cache_add($user->user_nicename, $user->ID, 'userslugs');
+}
+
+/**
+ * Clean all user caches
+ *
+ * @since 3.0.0
+ *
+ * @param int $id User ID
+ */
+function clean_user_cache($id) {
+ $user = new WP_User($id);
+
+ wp_cache_delete($id, 'users');
+ wp_cache_delete($user->user_login, 'userlogins');
+ wp_cache_delete($user->user_email, 'useremail');
+ wp_cache_delete($user->user_nicename, 'userslugs');
+ wp_cache_delete('blogs_of_user-' . $id, 'users');
+}
+
+/**
+ * Checks whether the given username exists.
+ *
+ * @since 2.0.0
+ *
+ * @param string $username Username.
+ * @return null|int The user's ID on success, and null on failure.
+ */
+function username_exists( $username ) {
+ if ( $user = get_userdatabylogin( $username ) ) {
+ return $user->ID;
+ } else {
+ return null;
+ }
+}
+
+/**
+ * Checks whether the given email exists.
+ *
+ * @since 2.1.0
+ * @uses $wpdb
+ *
+ * @param string $email Email.
+ * @return bool|int The user's ID on success, and false on failure.
+ */
+function email_exists( $email ) {
+ if ( $user = get_user_by_email($email) )
+ return $user->ID;
+
+ return false;
+}
+
+/**
+ * Checks whether an username is valid.
+ *
+ * @since 2.0.1
+ * @uses apply_filters() Calls 'validate_username' hook on $valid check and $username as parameters
+ *
+ * @param string $username Username.
+ * @return bool Whether username given is valid
+ */
+function validate_username( $username ) {
+ $sanitized = sanitize_user( $username, true );
+ $valid = ( $sanitized == $username );
+ return apply_filters( 'validate_username', $valid, $username );
+}
+
+/**
+ * Insert an user into the database.
+ *
+ * Can update a current user or insert a new user based on whether the user's ID
+ * is present.
+ *
+ * Can be used to update the user's info (see below), set the user's role, and
+ * set the user's preference on whether they want the rich editor on.
+ *
+ * Most of the $userdata array fields have filters associated with the values.
+ * The exceptions are 'rich_editing', 'role', 'jabber', 'aim', 'yim',
+ * 'user_registered', and 'ID'. The filters have the prefix 'pre_user_' followed
+ * by the field name. An example using 'description' would have the filter
+ * called, 'pre_user_description' that can be hooked into.
+ *
+ * The $userdata array can contain the following fields:
+ * 'ID' - An integer that will be used for updating an existing user.
+ * 'user_pass' - A string that contains the plain text password for the user.
+ * 'user_login' - A string that contains the user's username for logging in.
+ * 'user_nicename' - A string that contains a nicer looking name for the user.
+ * The default is the user's username.
+ * 'user_url' - A string containing the user's URL for the user's web site.
+ * 'user_email' - A string containing the user's email address.
+ * 'display_name' - A string that will be shown on the site. Defaults to user's
+ * username. It is likely that you will want to change this, for both
+ * appearance and security through obscurity (that is if you don't use and
+ * delete the default 'admin' user).
+ * 'nickname' - The user's nickname, defaults to the user's username.
+ * 'first_name' - The user's first name.
+ * 'last_name' - The user's last name.
+ * 'description' - A string containing content about the user.
+ * 'rich_editing' - A string for whether to enable the rich editor. False
+ * if not empty.
+ * 'user_registered' - The date the user registered. Format is 'Y-m-d H:i:s'.
+ * 'role' - A string used to set the user's role.
+ * 'jabber' - User's Jabber account.
+ * 'aim' - User's AOL IM account.
+ * 'yim' - User's Yahoo IM account.
+ *
+ * @since 2.0.0
+ * @uses $wpdb WordPress database layer.
+ * @uses apply_filters() Calls filters for most of the $userdata fields with the prefix 'pre_user'. See note above.
+ * @uses do_action() Calls 'profile_update' hook when updating giving the user's ID
+ * @uses do_action() Calls 'user_register' hook when creating a new user giving the user's ID
+ *
+ * @param array $userdata An array of user data.
+ * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not be created.
+ */
+function wp_insert_user($userdata) {
+ global $wpdb;
+
+ extract($userdata, EXTR_SKIP);
+
+ // Are we updating or creating?
+ if ( !empty($ID) ) {
+ $ID = (int) $ID;
+ $update = true;
+ $old_user_data = get_userdata($ID);
+ } else {
+ $update = false;
+ // Hash the password
+ $user_pass = wp_hash_password($user_pass);
+ }
+
+ $user_login = sanitize_user($user_login, true);
+ $user_login = apply_filters('pre_user_login', $user_login);
+
+ //Remove any non-printable chars from the login string to see if we have ended up with an empty username
+ $user_login = trim($user_login);
+
+ if ( empty($user_login) )
+ return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') );
+
+ if ( !$update && username_exists( $user_login ) )
+ return new WP_Error('existing_user_login', __('This username is already registered.') );
+
+ if ( empty($user_nicename) )
+ $user_nicename = sanitize_title( $user_login );
+ $user_nicename = apply_filters('pre_user_nicename', $user_nicename);
+
+ if ( empty($user_url) )
+ $user_url = '';
+ $user_url = apply_filters('pre_user_url', $user_url);
+
+ if ( empty($user_email) )
+ $user_email = '';
+ $user_email = apply_filters('pre_user_email', $user_email);
+
+ if ( !$update && ! defined( 'WP_IMPORTING' ) && email_exists($user_email) )
+ return new WP_Error('existing_user_email', __('This email address is already registered.') );
+
+ if ( empty($display_name) )
+ $display_name = $user_login;
+ $display_name = apply_filters('pre_user_display_name', $display_name);
+
+ if ( empty($nickname) )
+ $nickname = $user_login;
+ $nickname = apply_filters('pre_user_nickname', $nickname);
+
+ if ( empty($first_name) )
+ $first_name = '';
+ $first_name = apply_filters('pre_user_first_name', $first_name);
+
+ if ( empty($last_name) )
+ $last_name = '';
+ $last_name = apply_filters('pre_user_last_name', $last_name);
+
+ if ( empty($description) )
+ $description = '';
+ $description = apply_filters('pre_user_description', $description);
+
+ if ( empty($rich_editing) )
+ $rich_editing = 'true';
+
+ if ( empty($comment_shortcuts) )
+ $comment_shortcuts = 'false';
+
+ if ( empty($admin_color) )
+ $admin_color = 'fresh';
+ $admin_color = preg_replace('|[^a-z0-9 _.\-@]|i', '', $admin_color);
+
+ if ( empty($use_ssl) )
+ $use_ssl = 0;
+
+ if ( empty($user_registered) )
+ $user_registered = gmdate('Y-m-d H:i:s');
+
+ if ( empty($show_admin_bar_front) )
+ $show_admin_bar_front = 'true';
+
+ if ( empty($show_admin_bar_admin) )
+ $show_admin_bar_admin = is_multisite() ? 'true' : 'false';
+
+ $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $user_nicename, $user_login));
+
+ if ( $user_nicename_check ) {
+ $suffix = 2;
+ while ($user_nicename_check) {
+ $alt_user_nicename = $user_nicename . "-$suffix";
+ $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $alt_user_nicename, $user_login));
+ $suffix++;
+ }
+ $user_nicename = $alt_user_nicename;
+ }
+
+ $data = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
+ $data = stripslashes_deep( $data );
+
+ if ( $update ) {
+ $wpdb->update( $wpdb->users, $data, compact( 'ID' ) );
+ $user_id = (int) $ID;
+ } else {
+ $wpdb->insert( $wpdb->users, $data + compact( 'user_login' ) );
+ $user_id = (int) $wpdb->insert_id;
+ }
+
+ update_user_meta( $user_id, 'first_name', $first_name );
+ update_user_meta( $user_id, 'last_name', $last_name );
+ update_user_meta( $user_id, 'nickname', $nickname );
+ update_user_meta( $user_id, 'description', $description );
+ update_user_meta( $user_id, 'rich_editing', $rich_editing );
+ update_user_meta( $user_id, 'comment_shortcuts', $comment_shortcuts );
+ update_user_meta( $user_id, 'admin_color', $admin_color );
+ update_user_meta( $user_id, 'use_ssl', $use_ssl );
+ update_user_meta( $user_id, 'show_admin_bar_front', $show_admin_bar_front );
+ update_user_meta( $user_id, 'show_admin_bar_admin', $show_admin_bar_admin );
+
+ $user = new WP_User($user_id);
+
+ foreach ( _wp_get_user_contactmethods( $user ) as $method => $name ) {
+ if ( empty($$method) )
+ $$method = '';
+
+ update_user_meta( $user_id, $method, $$method );
+ }
+
+ if ( isset($role) )
+ $user->set_role($role);
+ elseif ( !$update )
+ $user->set_role(get_option('default_role'));
+
+ wp_cache_delete($user_id, 'users');
+ wp_cache_delete($user_login, 'userlogins');
+
+ if ( $update )
+ do_action('profile_update', $user_id, $old_user_data);
+ else
+ do_action('user_register', $user_id);
+
+ return $user_id;
+}
+
+/**
+ * Update an user in the database.
+ *
+ * It is possible to update a user's password by specifying the 'user_pass'
+ * value in the $userdata parameter array.
+ *
+ * If $userdata does not contain an 'ID' key, then a new user will be created
+ * and the new user's ID will be returned.
+ *
+ * If current user's password is being updated, then the cookies will be
+ * cleared.
+ *
+ * @since 2.0.0
+ * @see wp_insert_user() For what fields can be set in $userdata
+ * @uses wp_insert_user() Used to update existing user or add new one if user doesn't exist already
+ *
+ * @param array $userdata An array of user data.
+ * @return int The updated user's ID.
+ */
+function wp_update_user($userdata) {
+ $ID = (int) $userdata['ID'];
+
+ // First, get all of the original fields
+ $user = get_userdata($ID);
+
+ // Escape data pulled from DB.
+ $user = add_magic_quotes(get_object_vars($user));
+
+ // If password is changing, hash it now.
+ if ( ! empty($userdata['user_pass']) ) {
+ $plaintext_pass = $userdata['user_pass'];
+ $userdata['user_pass'] = wp_hash_password($userdata['user_pass']);
+ }
+
+ wp_cache_delete($user[ 'user_email' ], 'useremail');
+
+ // Merge old and new fields with new fields overwriting old ones.
+ $userdata = array_merge($user, $userdata);
+ $user_id = wp_insert_user($userdata);
+
+ // Update the cookies if the password changed.
+ $current_user = wp_get_current_user();
+ if ( $current_user->id == $ID ) {
+ if ( isset($plaintext_pass) ) {
+ wp_clear_auth_cookie();
+ wp_set_auth_cookie($ID);
+ }
+ }
+
+ return $user_id;
+}
+
+/**
+ * A simpler way of inserting an user into the database.
+ *
+ * Creates a new user with just the username, password, and email. For a more
+ * detail creation of a user, use wp_insert_user() to specify more infomation.
+ *
+ * @since 2.0.0
+ * @see wp_insert_user() More complete way to create a new user
+ *
+ * @param string $username The user's username.
+ * @param string $password The user's password.
+ * @param string $email The user's email (optional).
+ * @return int The new user's ID.
+ */
+function wp_create_user($username, $password, $email = '') {
+ $user_login = esc_sql( $username );
+ $user_email = esc_sql( $email );
+ $user_pass = $password;
+
+ $userdata = compact('user_login', 'user_email', 'user_pass');
+ return wp_insert_user($userdata);
+}
+
+
+/**
+ * Set up the default contact methods
+ *
+ * @access private
+ * @since
+ *
+ * @param object $user User data object (optional)
+ * @return array $user_contactmethods Array of contact methods and their labels.
+ */
+function _wp_get_user_contactmethods( $user = null ) {
+ $user_contactmethods = array(
+ 'aim' => __('AIM'),
+ 'yim' => __('Yahoo IM'),
+ 'jabber' => __('Jabber / Google Talk')
+ );
+ return apply_filters( 'user_contactmethods', $user_contactmethods, $user );
+}
+