+/**
+ * Retrieves a user row based on password reset key and login
+ *
+ * A key is considered 'expired' if it exactly matches the value of the
+ * user_activation_key field, rather than being matched after going through the
+ * hashing process. This field is now hashed; old values are no longer accepted
+ * but have a different WP_Error code so good user feedback can be provided.
+ *
+ * @since 3.1.0
+ *
+ * @global wpdb $wpdb WordPress database object for queries.
+ * @global PasswordHash $wp_hasher Portable PHP password hashing framework instance.
+ *
+ * @param string $key Hash to validate sending user's password.
+ * @param string $login The user login.
+ * @return WP_User|WP_Error WP_User object on success, WP_Error object for invalid or expired keys.
+ */
+function check_password_reset_key($key, $login) {
+ global $wpdb, $wp_hasher;
+
+ $key = preg_replace('/[^a-z0-9]/i', '', $key);
+
+ if ( empty( $key ) || !is_string( $key ) )
+ return new WP_Error('invalid_key', __('Invalid key'));
+
+ if ( empty($login) || !is_string($login) )
+ return new WP_Error('invalid_key', __('Invalid key'));
+
+ $row = $wpdb->get_row( $wpdb->prepare( "SELECT ID, user_activation_key FROM $wpdb->users WHERE user_login = %s", $login ) );
+ if ( ! $row )
+ return new WP_Error('invalid_key', __('Invalid key'));
+
+ if ( empty( $wp_hasher ) ) {
+ require_once ABSPATH . WPINC . '/class-phpass.php';
+ $wp_hasher = new PasswordHash( 8, true );
+ }
+
+ /**
+ * Filter the expiration time of password reset keys.
+ *
+ * @since 4.3.0
+ *
+ * @param int $expiration The expiration time in seconds.
+ */
+ $expiration_duration = apply_filters( 'password_reset_expiration', DAY_IN_SECONDS );
+
+ if ( false !== strpos( $row->user_activation_key, ':' ) ) {
+ list( $pass_request_time, $pass_key ) = explode( ':', $row->user_activation_key, 2 );
+ $expiration_time = $pass_request_time + $expiration_duration;
+ } else {
+ $pass_key = $row->user_activation_key;
+ $expiration_time = false;
+ }
+
+ if ( ! $pass_key ) {
+ return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
+ }
+
+ $hash_is_correct = $wp_hasher->CheckPassword( $key, $pass_key );
+
+ if ( $hash_is_correct && $expiration_time && time() < $expiration_time ) {
+ return get_userdata( $row->ID );
+ } elseif ( $hash_is_correct && $expiration_time ) {
+ // Key has an expiration time that's passed
+ return new WP_Error( 'expired_key', __( 'Invalid key' ) );
+ }
+
+ if ( hash_equals( $row->user_activation_key, $key ) || ( $hash_is_correct && ! $expiration_time ) ) {
+ $return = new WP_Error( 'expired_key', __( 'Invalid key' ) );
+ $user_id = $row->ID;
+
+ /**
+ * Filter the return value of check_password_reset_key() when an
+ * old-style key is used.
+ *
+ * @since 3.7.0 Previously plain-text keys were stored in the database.
+ * @since 4.3.0 Previously key hashes were stored without an expiration time.
+ *
+ * @param WP_Error $return A WP_Error object denoting an expired key.
+ * Return a WP_User object to validate the key.
+ * @param int $user_id The matched user ID.
+ */
+ return apply_filters( 'password_reset_key_expired', $return, $user_id );
+ }
+
+ return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
+}
+
+/**
+ * Handles resetting the user's password.
+ *
+ * @since 2.5.0
+ *
+ * @param object $user The user
+ * @param string $new_pass New password for the user in plaintext
+ */
+function reset_password( $user, $new_pass ) {
+ /**
+ * Fires before the user's password is reset.
+ *
+ * @since 1.5.0
+ *
+ * @param object $user The user.
+ * @param string $new_pass New user password.
+ */
+ do_action( 'password_reset', $user, $new_pass );
+
+ wp_set_password( $new_pass, $user->ID );
+ update_user_option( $user->ID, 'default_password_nag', false, true );
+
+ /**
+ * Fires after the user's password is reset.
+ *
+ * @since 4.4.0
+ *
+ * @param object $user The user.
+ * @param string $new_pass New user password.
+ */
+ do_action( 'after_password_reset', $user, $new_pass );
+}
+
+/**
+ * Handles registering a new user.
+ *
+ * @since 2.5.0
+ *
+ * @param string $user_login User's username for logging in
+ * @param string $user_email User's email address to send password and add
+ * @return int|WP_Error Either user's ID or error on failure.
+ */
+function register_new_user( $user_login, $user_email ) {
+ $errors = new WP_Error();
+
+ $sanitized_user_login = sanitize_user( $user_login );
+ /**
+ * Filter the email address of a user being registered.
+ *
+ * @since 2.1.0
+ *
+ * @param string $user_email The email address of the new user.
+ */
+ $user_email = apply_filters( 'user_registration_email', $user_email );
+
+ // Check the username
+ if ( $sanitized_user_login == '' ) {
+ $errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
+ } elseif ( ! validate_username( $user_login ) ) {
+ $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
+ $sanitized_user_login = '';
+ } elseif ( username_exists( $sanitized_user_login ) ) {
+ $errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ) );
+
+ } else {
+ /** This filter is documented in wp-includes/user.php */
+ $illegal_user_logins = array_map( 'strtolower', (array) apply_filters( 'illegal_user_logins', array() ) );
+ if ( in_array( strtolower( $sanitized_user_login ), $illegal_user_logins ) ) {
+ $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: Sorry, that username is not allowed.' ) );
+ }
+ }
+
+ // Check the email address
+ if ( $user_email == '' ) {
+ $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your email address.' ) );
+ } elseif ( ! is_email( $user_email ) ) {
+ $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn’t correct.' ) );
+ $user_email = '';
+ } elseif ( email_exists( $user_email ) ) {
+ $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
+ }
+
+ /**
+ * Fires when submitting registration form data, before the user is created.
+ *
+ * @since 2.1.0
+ *
+ * @param string $sanitized_user_login The submitted username after being sanitized.
+ * @param string $user_email The submitted email.
+ * @param WP_Error $errors Contains any errors with submitted username and email,
+ * e.g., an empty field, an invalid username or email,
+ * or an existing username or email.
+ */
+ do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
+
+ /**
+ * Filter the errors encountered when a new user is being registered.
+ *
+ * The filtered WP_Error object may, for example, contain errors for an invalid
+ * or existing username or email address. A WP_Error object should always returned,
+ * but may or may not contain errors.
+ *
+ * If any errors are present in $errors, this will abort the user's registration.
+ *
+ * @since 2.1.0
+ *
+ * @param WP_Error $errors A WP_Error object containing any errors encountered
+ * during registration.
+ * @param string $sanitized_user_login User's username after it has been sanitized.
+ * @param string $user_email User's email.
+ */
+ $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
+
+ if ( $errors->get_error_code() )
+ return $errors;
+
+ $user_pass = wp_generate_password( 12, false );
+ $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
+ if ( ! $user_id || is_wp_error( $user_id ) ) {
+ $errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn’t register you… please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
+ return $errors;
+ }
+
+ update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.
+
+ /**
+ * Fires after a new user registration has been recorded.
+ *
+ * @since 4.4.0
+ *
+ * @param int $user_id ID of the newly registered user.
+ */
+ do_action( 'register_new_user', $user_id );
+
+ return $user_id;
+}
+
+/**
+ * Initiate email notifications related to the creation of new users.
+ *
+ * Notifications are sent both to the site admin and to the newly created user.
+ *
+ * @since 4.4.0
+ *
+ * @param int $user_id ID of the newly created user.
+ * @param string $notify Optional. Type of notification that should happen. Accepts 'admin' or an empty string
+ * (admin only), or 'both' (admin and user). Default 'both'.
+ */
+function wp_send_new_user_notifications( $user_id, $notify = 'both' ) {
+ wp_new_user_notification( $user_id, null, $notify );
+}
+
+/**
+ * Retrieve the current session token from the logged_in cookie.
+ *
+ * @since 4.0.0
+ *
+ * @return string Token.
+ */
+function wp_get_session_token() {
+ $cookie = wp_parse_auth_cookie( '', 'logged_in' );
+ return ! empty( $cookie['token'] ) ? $cookie['token'] : '';
+}
+
+/**
+ * Retrieve a list of sessions for the current user.
+ *
+ * @since 4.0.0
+ * @return array Array of sessions.
+ */
+function wp_get_all_sessions() {
+ $manager = WP_Session_Tokens::get_instance( get_current_user_id() );
+ return $manager->get_all();
+}
+
+/**
+ * Remove the current session token from the database.
+ *
+ * @since 4.0.0
+ */
+function wp_destroy_current_session() {
+ $token = wp_get_session_token();
+ if ( $token ) {
+ $manager = WP_Session_Tokens::get_instance( get_current_user_id() );
+ $manager->destroy( $token );
+ }
+}
+
+/**
+ * Remove all but the current session token for the current user for the database.
+ *
+ * @since 4.0.0
+ */
+function wp_destroy_other_sessions() {
+ $token = wp_get_session_token();
+ if ( $token ) {
+ $manager = WP_Session_Tokens::get_instance( get_current_user_id() );
+ $manager->destroy_others( $token );
+ }
+}
+
+/**
+ * Remove all session tokens for the current user from the database.
+ *
+ * @since 4.0.0
+ */
+function wp_destroy_all_sessions() {
+ $manager = WP_Session_Tokens::get_instance( get_current_user_id() );
+ $manager->destroy_all();
+}
+
+/**
+ * Get the user IDs of all users with no role on this site.
+ *
+ * This function returns an empty array when used on Multisite.
+ *
+ * @since 4.4.0
+ *
+ * @return array Array of user IDs.
+ */
+function wp_get_users_with_no_role() {
+ global $wpdb;
+
+ if ( is_multisite() ) {
+ return array();
+ }
+
+ $prefix = $wpdb->get_blog_prefix();
+ $regex = implode( '|', wp_roles()->get_names() );
+ $regex = preg_replace( '/[^a-zA-Z_\|-]/', '', $regex );
+ $users = $wpdb->get_col( $wpdb->prepare( "
+ SELECT user_id
+ FROM $wpdb->usermeta
+ WHERE meta_key = '{$prefix}capabilities'
+ AND meta_value NOT REGEXP %s
+ ", $regex ) );
+
+ return $users;
+}
+
+/**
+ * Retrieves the current user object.
+ *
+ * Will set the current user, if the current user is not set. The current user
+ * will be set to the logged-in person. If no user is logged-in, then it will
+ * set the current user to 0, which is invalid and won't have any permissions.
+ *
+ * This function is used by the pluggable functions wp_get_current_user() and
+ * get_currentuserinfo(), the latter of which is deprecated but used for backward
+ * compatibility.
+ *
+ * @since 4.5.0
+ * @access private
+ *
+ * @see wp_get_current_user()
+ * @global WP_User $current_user Checks if the current user is set.
+ *
+ * @return WP_User Current WP_User instance.
+ */
+function _wp_get_current_user() {
+ global $current_user;
+
+ if ( ! empty( $current_user ) ) {
+ if ( $current_user instanceof WP_User ) {
+ return $current_user;
+ }
+
+ // Upgrade stdClass to WP_User
+ if ( is_object( $current_user ) && isset( $current_user->ID ) ) {
+ $cur_id = $current_user->ID;
+ $current_user = null;
+ wp_set_current_user( $cur_id );
+ return $current_user;
+ }
+
+ // $current_user has a junk value. Force to WP_User with ID 0.
+ $current_user = null;
+ wp_set_current_user( 0 );
+ return $current_user;
+ }
+
+ if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) {
+ wp_set_current_user( 0 );
+ return $current_user;
+ }
+
+ /**
+ * Filter the current user.
+ *
+ * The default filters use this to determine the current user from the
+ * request's cookies, if available.
+ *
+ * Returning a value of false will effectively short-circuit setting
+ * the current user.
+ *
+ * @since 3.9.0
+ *
+ * @param int|bool $user_id User ID if one has been determined, false otherwise.
+ */
+ $user_id = apply_filters( 'determine_current_user', false );
+ if ( ! $user_id ) {
+ wp_set_current_user( 0 );
+ return $current_user;
+ }
+
+ wp_set_current_user( $user_id );
+
+ return $current_user;
+}