+/**
+ * Handles registering a new user.
+ *
+ * @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.' ) );
+ }
+
+ // Check the e-mail address
+ if ( $user_email == '' ) {
+ $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail 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.
+
+ wp_new_user_notification( $user_id, $user_pass );
+
+ return $user_id;
+}
+
+/**
+ * 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();
+}