+ $description = empty( $userdata['description'] ) ? '' : $userdata['description'];
+
+ /**
+ * Filters a user's description before the user is created or updated.
+ *
+ * @since 2.0.3
+ *
+ * @param string $description The user's description.
+ */
+ $meta['description'] = apply_filters( 'pre_user_description', $description );
+
+ $meta['rich_editing'] = empty( $userdata['rich_editing'] ) ? 'true' : $userdata['rich_editing'];
+
+ $meta['comment_shortcuts'] = empty( $userdata['comment_shortcuts'] ) || 'false' === $userdata['comment_shortcuts'] ? 'false' : 'true';
+
+ $admin_color = empty( $userdata['admin_color'] ) ? 'fresh' : $userdata['admin_color'];
+ $meta['admin_color'] = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $admin_color );
+
+ $meta['use_ssl'] = empty( $userdata['use_ssl'] ) ? 0 : $userdata['use_ssl'];
+
+ $user_registered = empty( $userdata['user_registered'] ) ? gmdate( 'Y-m-d H:i:s' ) : $userdata['user_registered'];
+
+ $meta['show_admin_bar_front'] = empty( $userdata['show_admin_bar_front'] ) ? 'true' : $userdata['show_admin_bar_front'];
+
+ $meta['locale'] = isset( $userdata['locale'] ) ? $userdata['locale'] : '';
+
+ $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) {
+ // user_nicename allows 50 chars. Subtract one for a hyphen, plus the length of the suffix.
+ $base_length = 49 - mb_strlen( $suffix );
+ $alt_user_nicename = mb_substr( $user_nicename, 0, $base_length ) . "-$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;
+ }
+
+ $compacted = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
+ $data = wp_unslash( $compacted );
+
+ if ( $update ) {
+ if ( $user_email !== $old_user_data->user_email ) {
+ $data['user_activation_key'] = '';
+ }
+ $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;
+ }
+
+ $user = new WP_User( $user_id );
+
+ /**
+ * Filters a user's meta values and keys before the user is created or updated.
+ *
+ * Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`.
+ *
+ * @since 4.4.0
+ *
+ * @param array $meta {
+ * Default meta values and keys for the user.
+ *
+ * @type string $nickname The user's nickname. Default is the user's username.
+ * @type string $first_name The user's first name.
+ * @type string $last_name The user's last name.
+ * @type string $description The user's description.
+ * @type bool $rich_editing Whether to enable the rich-editor for the user. False if not empty.
+ * @type bool $comment_shortcuts Whether to enable keyboard shortcuts for the user. Default false.
+ * @type string $admin_color The color scheme for a user's admin screen. Default 'fresh'.
+ * @type int|bool $use_ssl Whether to force SSL on the user's admin area. 0|false if SSL is
+ * not forced.
+ * @type bool $show_admin_bar_front Whether to show the admin bar on the front end for the user.
+ * Default true.
+ * }
+ * @param WP_User $user User object.
+ * @param bool $update Whether the user is being updated rather than created.
+ */
+ $meta = apply_filters( 'insert_user_meta', $meta, $user, $update );
+
+ // Update user meta.
+ foreach ( $meta as $key => $value ) {
+ update_user_meta( $user_id, $key, $value );
+ }
+
+ foreach ( wp_get_user_contact_methods( $user ) as $key => $value ) {
+ if ( isset( $userdata[ $key ] ) ) {
+ update_user_meta( $user_id, $key, $userdata[ $key ] );
+ }
+ }
+
+ if ( isset( $userdata['role'] ) ) {
+ $user->set_role( $userdata['role'] );
+ } elseif ( ! $update ) {
+ $user->set_role(get_option('default_role'));
+ }
+ wp_cache_delete( $user_id, 'users' );
+ wp_cache_delete( $user_login, 'userlogins' );
+
+ if ( $update ) {
+ /**
+ * Fires immediately after an existing user is updated.
+ *
+ * @since 2.0.0
+ *
+ * @param int $user_id User ID.
+ * @param object $old_user_data Object containing user's data prior to update.
+ */
+ do_action( 'profile_update', $user_id, $old_user_data );
+ } else {
+ /**
+ * Fires immediately after a new user is registered.
+ *
+ * @since 1.5.0
+ *
+ * @param int $user_id User ID.
+ */
+ do_action( 'user_register', $user_id );
+ }
+
+ return $user_id;
+}
+
+/**
+ * Update a 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 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.
+ *
+ * @param mixed $userdata An array of user data or a user object of type stdClass or WP_User.
+ * @return int|WP_Error The updated user's ID or a WP_Error object if the user could not be updated.
+ */
+function wp_update_user($userdata) {
+ if ( $userdata instanceof stdClass ) {
+ $userdata = get_object_vars( $userdata );
+ } elseif ( $userdata instanceof WP_User ) {
+ $userdata = $userdata->to_array();
+ }
+
+ $ID = isset( $userdata['ID'] ) ? (int) $userdata['ID'] : 0;
+ if ( ! $ID ) {
+ return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) );
+ }
+
+ // First, get all of the original fields
+ $user_obj = get_userdata( $ID );
+ if ( ! $user_obj ) {
+ return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) );
+ }
+
+ $user = $user_obj->to_array();
+
+ // Add additional custom fields
+ foreach ( _get_additional_user_keys( $user_obj ) as $key ) {
+ $user[ $key ] = get_user_meta( $ID, $key, true );
+ }
+
+ // Escape data pulled from DB.
+ $user = add_magic_quotes( $user );
+
+ if ( ! empty( $userdata['user_pass'] ) && $userdata['user_pass'] !== $user_obj->user_pass ) {
+ // If password is changing, hash it now
+ $plaintext_pass = $userdata['user_pass'];
+ $userdata['user_pass'] = wp_hash_password( $userdata['user_pass'] );
+
+ /**
+ * Filters whether to send the password change email.
+ *
+ * @since 4.3.0
+ *
+ * @see wp_insert_user() For `$user` and `$userdata` fields.
+ *
+ * @param bool $send Whether to send the email.
+ * @param array $user The original user array.
+ * @param array $userdata The updated user array.
+ *
+ */
+ $send_password_change_email = apply_filters( 'send_password_change_email', true, $user, $userdata );
+ }
+
+ if ( isset( $userdata['user_email'] ) && $user['user_email'] !== $userdata['user_email'] ) {
+ /**
+ * Filters whether to send the email change email.
+ *
+ * @since 4.3.0
+ *
+ * @see wp_insert_user() For `$user` and `$userdata` fields.
+ *
+ * @param bool $send Whether to send the email.
+ * @param array $user The original user array.
+ * @param array $userdata The updated user array.
+ *
+ */
+ $send_email_change_email = apply_filters( 'send_email_change_email', true, $user, $userdata );
+ }
+
+ wp_cache_delete( $user['user_email'], 'useremail' );
+ wp_cache_delete( $user['user_nicename'], 'userslugs' );
+
+ // Merge old and new fields with new fields overwriting old ones.
+ $userdata = array_merge( $user, $userdata );
+ $user_id = wp_insert_user( $userdata );
+
+ if ( ! is_wp_error( $user_id ) ) {
+
+ $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
+
+ $switched_locale = false;
+ if ( ! empty( $send_password_change_email ) || ! empty( $send_email_change_email ) ) {
+ $switched_locale = switch_to_locale( get_user_locale( $user_id ) );
+ }
+
+ if ( ! empty( $send_password_change_email ) ) {
+ /* translators: Do not translate USERNAME, ADMIN_EMAIL, EMAIL, SITENAME, SITEURL: those are placeholders. */
+ $pass_change_text = __( 'Hi ###USERNAME###,
+
+This notice confirms that your password was changed on ###SITENAME###.
+
+If you did not change your password, please contact the Site Administrator at
+###ADMIN_EMAIL###
+
+This email has been sent to ###EMAIL###
+
+Regards,
+All at ###SITENAME###
+###SITEURL###' );
+
+ $pass_change_email = array(
+ 'to' => $user['user_email'],
+ /* translators: User password change notification email subject. 1: Site name */
+ 'subject' => __( '[%s] Notice of Password Change' ),
+ 'message' => $pass_change_text,
+ 'headers' => '',
+ );
+
+ /**
+ * Filters the contents of the email sent when the user's password is changed.
+ *
+ * @since 4.3.0
+ *
+ * @param array $pass_change_email {
+ * Used to build wp_mail().
+ * @type string $to The intended recipients. Add emails in a comma separated string.
+ * @type string $subject The subject of the email.
+ * @type string $message The content of the email.
+ * The following strings have a special meaning and will get replaced dynamically:
+ * - ###USERNAME### The current user's username.
+ * - ###ADMIN_EMAIL### The admin email in case this was unexpected.
+ * - ###EMAIL### The old email.
+ * - ###SITENAME### The name of the site.
+ * - ###SITEURL### The URL to the site.
+ * @type string $headers Headers. Add headers in a newline (\r\n) separated string.
+ * }
+ * @param array $user The original user array.
+ * @param array $userdata The updated user array.
+ *
+ */
+ $pass_change_email = apply_filters( 'password_change_email', $pass_change_email, $user, $userdata );
+
+ $pass_change_email['message'] = str_replace( '###USERNAME###', $user['user_login'], $pass_change_email['message'] );
+ $pass_change_email['message'] = str_replace( '###ADMIN_EMAIL###', get_option( 'admin_email' ), $pass_change_email['message'] );
+ $pass_change_email['message'] = str_replace( '###EMAIL###', $user['user_email'], $pass_change_email['message'] );
+ $pass_change_email['message'] = str_replace( '###SITENAME###', $blog_name, $pass_change_email['message'] );
+ $pass_change_email['message'] = str_replace( '###SITEURL###', home_url(), $pass_change_email['message'] );
+
+ wp_mail( $pass_change_email['to'], sprintf( $pass_change_email['subject'], $blog_name ), $pass_change_email['message'], $pass_change_email['headers'] );
+ }
+
+ if ( ! empty( $send_email_change_email ) ) {
+ /* translators: Do not translate USERNAME, ADMIN_EMAIL, EMAIL, SITENAME, SITEURL: those are placeholders. */
+ $email_change_text = __( 'Hi ###USERNAME###,
+
+This notice confirms that your email was changed on ###SITENAME###.
+
+If you did not change your email, please contact the Site Administrator at
+###ADMIN_EMAIL###
+
+This email has been sent to ###EMAIL###
+
+Regards,
+All at ###SITENAME###
+###SITEURL###' );
+
+ $email_change_email = array(
+ 'to' => $user['user_email'],
+ /* translators: User email change notification email subject. 1: Site name */
+ 'subject' => __( '[%s] Notice of Email Change' ),
+ 'message' => $email_change_text,
+ 'headers' => '',
+ );
+
+ /**
+ * Filters the contents of the email sent when the user's email is changed.
+ *
+ * @since 4.3.0
+ *
+ * @param array $email_change_email {
+ * Used to build wp_mail().
+ * @type string $to The intended recipients.
+ * @type string $subject The subject of the email.
+ * @type string $message The content of the email.
+ * The following strings have a special meaning and will get replaced dynamically:
+ * - ###USERNAME### The current user's username.
+ * - ###ADMIN_EMAIL### The admin email in case this was unexpected.
+ * - ###EMAIL### The old email.
+ * - ###SITENAME### The name of the site.
+ * - ###SITEURL### The URL to the site.
+ * @type string $headers Headers.
+ * }
+ * @param array $user The original user array.
+ * @param array $userdata The updated user array.
+ */
+ $email_change_email = apply_filters( 'email_change_email', $email_change_email, $user, $userdata );
+
+ $email_change_email['message'] = str_replace( '###USERNAME###', $user['user_login'], $email_change_email['message'] );
+ $email_change_email['message'] = str_replace( '###ADMIN_EMAIL###', get_option( 'admin_email' ), $email_change_email['message'] );
+ $email_change_email['message'] = str_replace( '###EMAIL###', $user['user_email'], $email_change_email['message'] );
+ $email_change_email['message'] = str_replace( '###SITENAME###', $blog_name, $email_change_email['message'] );
+ $email_change_email['message'] = str_replace( '###SITEURL###', home_url(), $email_change_email['message'] );
+
+ wp_mail( $email_change_email['to'], sprintf( $email_change_email['subject'], $blog_name ), $email_change_email['message'], $email_change_email['headers'] );
+ }
+
+ if ( $switched_locale ) {
+ restore_previous_locale();
+ }
+ }
+
+ // 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();
+
+ // Here we calculate the expiration length of the current auth cookie and compare it to the default expiration.
+ // If it's greater than this, then we know the user checked 'Remember Me' when they logged in.
+ $logged_in_cookie = wp_parse_auth_cookie( '', 'logged_in' );
+ /** This filter is documented in wp-includes/pluggable.php */
+ $default_cookie_life = apply_filters( 'auth_cookie_expiration', ( 2 * DAY_IN_SECONDS ), $ID, false );
+ $remember = ( ( $logged_in_cookie['expiration'] - time() ) > $default_cookie_life );
+
+ wp_set_auth_cookie( $ID, $remember );
+ }
+ }
+
+ return $user_id;
+}
+
+/**
+ * A simpler way of inserting a user into the database.
+ *
+ * Creates a new user with just the username, password, and email. For more
+ * complex user creation use wp_insert_user() to specify more information.
+ *
+ * @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 Optional. The user's email. Default empty.
+ * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not
+ * be created.
+ */
+function wp_create_user($username, $password, $email = '') {
+ $user_login = wp_slash( $username );
+ $user_email = wp_slash( $email );
+ $user_pass = $password;
+
+ $userdata = compact('user_login', 'user_email', 'user_pass');
+ return wp_insert_user($userdata);
+}
+
+/**
+ * Returns a list of meta keys to be (maybe) populated in wp_update_user().
+ *
+ * The list of keys returned via this function are dependent on the presence
+ * of those keys in the user meta data to be set.
+ *
+ * @since 3.3.0
+ * @access private
+ *
+ * @param WP_User $user WP_User instance.
+ * @return array List of user keys to be populated in wp_update_user().
+ */
+function _get_additional_user_keys( $user ) {
+ $keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front', 'locale' );
+ return array_merge( $keys, array_keys( wp_get_user_contact_methods( $user ) ) );
+}
+
+/**
+ * Set up the user contact methods.
+ *
+ * Default contact methods were removed in 3.6. A filter dictates contact methods.
+ *
+ * @since 3.7.0
+ *
+ * @param WP_User $user Optional. WP_User object.
+ * @return array Array of contact methods and their labels.
+ */
+function wp_get_user_contact_methods( $user = null ) {
+ $methods = array();
+ if ( get_site_option( 'initial_db_version' ) < 23588 ) {
+ $methods = array(
+ 'aim' => __( 'AIM' ),
+ 'yim' => __( 'Yahoo IM' ),
+ 'jabber' => __( 'Jabber / Google Talk' )
+ );
+ }
+
+ /**
+ * Filters the user contact methods.
+ *
+ * @since 2.9.0
+ *
+ * @param array $methods Array of contact methods and their labels.
+ * @param WP_User $user WP_User object.
+ */
+ return apply_filters( 'user_contactmethods', $methods, $user );
+}
+
+/**
+ * The old private function for setting up user contact methods.
+ *
+ * Use wp_get_user_contact_methods() instead.
+ *
+ * @since 2.9.0
+ * @access private
+ *
+ * @param WP_User $user Optional. WP_User object. Default null.
+ * @return array Array of contact methods and their labels.
+ */
+function _wp_get_user_contactmethods( $user = null ) {
+ return wp_get_user_contact_methods( $user );
+}
+
+/**
+ * Gets the text suggesting how to create strong passwords.
+ *
+ * @since 4.1.0
+ *
+ * @return string The password hint text.
+ */
+function wp_get_password_hint() {
+ $hint = __( 'Hint: The password should be at least twelve characters long. To make it stronger, use upper and lower case letters, numbers, and symbols like ! " ? $ % ^ & ).' );
+
+ /**
+ * Filters the text describing the site's password complexity policy.
+ *
+ * @since 4.1.0
+ *
+ * @param string $hint The password hint text.
+ */
+ return apply_filters( 'password_hint', $hint );
+}
+
+/**
+ * Creates, stores, then returns a password reset key for user.
+ *
+ * @since 4.4.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ * @global PasswordHash $wp_hasher Portable PHP password hashing framework.
+ *
+ * @param WP_User $user User to retrieve password reset key for.
+ *
+ * @return string|WP_Error Password reset key on success. WP_Error on error.
+ */
+function get_password_reset_key( $user ) {
+ global $wpdb, $wp_hasher;
+
+ /**
+ * Fires before a new password is retrieved.
+ *
+ * Use the {@see 'retrieve_password'} hook instead.
+ *
+ * @since 1.5.0
+ * @deprecated 1.5.1 Misspelled. Use 'retrieve_password' hook instead.
+ *
+ * @param string $user_login The user login name.
+ */
+ do_action( 'retreive_password', $user->user_login );
+
+ /**
+ * Fires before a new password is retrieved.
+ *
+ * @since 1.5.1
+ *
+ * @param string $user_login The user login name.
+ */
+ do_action( 'retrieve_password', $user->user_login );
+
+ $allow = true;
+ if ( is_multisite() && is_user_spammy( $user ) ) {
+ $allow = false;
+ }
+
+ /**
+ * Filters whether to allow a password to be reset.
+ *
+ * @since 2.7.0
+ *
+ * @param bool $allow Whether to allow the password to be reset. Default true.
+ * @param int $user_data->ID The ID of the user attempting to reset a password.
+ */
+ $allow = apply_filters( 'allow_password_reset', $allow, $user->ID );
+
+ if ( ! $allow ) {
+ return new WP_Error( 'no_password_reset', __( 'Password reset is not allowed for this user' ) );
+ } elseif ( is_wp_error( $allow ) ) {
+ return $allow;
+ }
+
+ // Generate something random for a password reset key.
+ $key = wp_generate_password( 20, false );
+
+ /**
+ * Fires when a password reset key is generated.
+ *
+ * @since 2.5.0
+ *
+ * @param string $user_login The username for the user.
+ * @param string $key The generated password reset key.
+ */
+ do_action( 'retrieve_password_key', $user->user_login, $key );
+
+ // Now insert the key, hashed, into the DB.
+ if ( empty( $wp_hasher ) ) {
+ $wp_hasher = new PasswordHash( 8, true );
+ }
+ $hashed = time() . ':' . $wp_hasher->HashPassword( $key );
+ $key_saved = $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) );
+ if ( false === $key_saved ) {
+ return new WP_Error( 'no_password_key_update', __( 'Could not save password reset key to database.' ) );
+ }
+
+ return $key;