WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/04/2013 08:58:41 AM (8 years ago)
Author:
SergeyBiryukov
Message:

Move check_password_reset_key(), reset_password(), and register_new_user() from wp-login.php to wp-includes/user.php, to make them reusable. props beaulebens for initial patch. fixes #20279.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-login.php

    r25203 r25231  
    268268
    269269    return true;
    270 }
    271 
    272 /**
    273  * Retrieves a user row based on password reset key and login
    274  *
    275  * @uses $wpdb WordPress Database object
    276  *
    277  * @param string $key Hash to validate sending user's password
    278  * @param string $login The user login
    279  * @return object|WP_Error User's database row on success, error object for invalid keys
    280  */
    281 function check_password_reset_key($key, $login) {
    282     global $wpdb;
    283 
    284     $key = preg_replace('/[^a-z0-9]/i', '', $key);
    285 
    286     if ( empty( $key ) || !is_string( $key ) )
    287         return new WP_Error('invalid_key', __('Invalid key'));
    288 
    289     if ( empty($login) || !is_string($login) )
    290         return new WP_Error('invalid_key', __('Invalid key'));
    291 
    292     $user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_activation_key = %s AND user_login = %s", $key, $login));
    293 
    294     if ( empty( $user ) )
    295         return new WP_Error('invalid_key', __('Invalid key'));
    296 
    297     return $user;
    298 }
    299 
    300 /**
    301  * Handles resetting the user's password.
    302  *
    303  * @param object $user The user
    304  * @param string $new_pass New password for the user in plaintext
    305  */
    306 function reset_password( $user, $new_pass ) {
    307     do_action( 'password_reset', $user, $new_pass );
    308 
    309     wp_set_password( $new_pass, $user->ID );
    310     update_user_option( $user->ID, 'default_password_nag', false, true );
    311 
    312     wp_password_change_notification( $user );
    313 }
    314 
    315 /**
    316  * Handles registering a new user.
    317  *
    318  * @param string $user_login User's username for logging in
    319  * @param string $user_email User's email address to send password and add
    320  * @return int|WP_Error Either user's ID or error on failure.
    321  */
    322 function register_new_user( $user_login, $user_email ) {
    323     $errors = new WP_Error();
    324 
    325     $sanitized_user_login = sanitize_user( $user_login );
    326     $user_email = apply_filters( 'user_registration_email', $user_email );
    327 
    328     // Check the username
    329     if ( $sanitized_user_login == '' ) {
    330         $errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
    331     } elseif ( ! validate_username( $user_login ) ) {
    332         $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
    333         $sanitized_user_login = '';
    334     } elseif ( username_exists( $sanitized_user_login ) ) {
    335         $errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ) );
    336     }
    337 
    338     // Check the e-mail address
    339     if ( $user_email == '' ) {
    340         $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.' ) );
    341     } elseif ( ! is_email( $user_email ) ) {
    342         $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ) );
    343         $user_email = '';
    344     } elseif ( email_exists( $user_email ) ) {
    345         $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
    346     }
    347 
    348     do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
    349 
    350     $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
    351 
    352     if ( $errors->get_error_code() )
    353         return $errors;
    354 
    355     $user_pass = wp_generate_password( 12, false);
    356     $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
    357     if ( ! $user_id || is_wp_error( $user_id ) ) {
    358         $errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn&#8217;t register you&hellip; please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
    359         return $errors;
    360     }
    361 
    362     update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.
    363 
    364     wp_new_user_notification( $user_id, $user_pass );
    365 
    366     return $user_id;
    367270}
    368271
Note: See TracChangeset for help on using the changeset viewer.