Make WordPress Core

Changeset 25231


Ignore:
Timestamp:
09/04/2013 08:58:41 AM (13 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.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r24850 r25231  
    15511551        return apply_filters( 'user_contactmethods', $user_contactmethods, $user );
    15521552}
     1553
     1554/**
     1555 * Retrieves a user row based on password reset key and login
     1556 *
     1557 * @uses $wpdb WordPress Database object
     1558 *
     1559 * @param string $key Hash to validate sending user's password
     1560 * @param string $login The user login
     1561 * @return object|WP_Error User's database row on success, error object for invalid keys
     1562 */
     1563function check_password_reset_key( $key, $login ) {
     1564        global $wpdb;
     1565
     1566        $key = preg_replace( '/[^a-z0-9]/i', '', $key );
     1567
     1568        if ( empty( $key ) || ! is_string( $key ) )
     1569                return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
     1570
     1571        if ( empty( $login ) || ! is_string( $login ) )
     1572                return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
     1573
     1574        $user = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE user_activation_key = %s AND user_login = %s", $key, $login ) );
     1575
     1576        if ( empty( $user ) )
     1577                return new WP_Error( 'invalid_key', __( 'Invalid key' ) );
     1578
     1579        return $user;
     1580}
     1581
     1582/**
     1583 * Handles resetting the user's password.
     1584 *
     1585 * @param object $user The user
     1586 * @param string $new_pass New password for the user in plaintext
     1587 */
     1588function reset_password( $user, $new_pass ) {
     1589        do_action( 'password_reset', $user, $new_pass );
     1590
     1591        wp_set_password( $new_pass, $user->ID );
     1592        update_user_option( $user->ID, 'default_password_nag', false, true );
     1593
     1594        wp_password_change_notification( $user );
     1595}
     1596
     1597/**
     1598 * Handles registering a new user.
     1599 *
     1600 * @param string $user_login User's username for logging in
     1601 * @param string $user_email User's email address to send password and add
     1602 * @return int|WP_Error Either user's ID or error on failure.
     1603 */
     1604function register_new_user( $user_login, $user_email ) {
     1605        $errors = new WP_Error();
     1606
     1607        $sanitized_user_login = sanitize_user( $user_login );
     1608        $user_email = apply_filters( 'user_registration_email', $user_email );
     1609
     1610        // Check the username
     1611        if ( $sanitized_user_login == '' ) {
     1612                $errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
     1613        } elseif ( ! validate_username( $user_login ) ) {
     1614                $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
     1615                $sanitized_user_login = '';
     1616        } elseif ( username_exists( $sanitized_user_login ) ) {
     1617                $errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ) );
     1618        }
     1619
     1620        // Check the e-mail address
     1621        if ( $user_email == '' ) {
     1622                $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.' ) );
     1623        } elseif ( ! is_email( $user_email ) ) {
     1624                $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ) );
     1625                $user_email = '';
     1626        } elseif ( email_exists( $user_email ) ) {
     1627                $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
     1628        }
     1629
     1630        do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
     1631
     1632        $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
     1633
     1634        if ( $errors->get_error_code() )
     1635                return $errors;
     1636
     1637        $user_pass = wp_generate_password( 12, false );
     1638        $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
     1639        if ( ! $user_id || is_wp_error( $user_id ) ) {
     1640                $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' ) ) );
     1641                return $errors;
     1642        }
     1643
     1644        update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.
     1645
     1646        wp_new_user_notification( $user_id, $user_pass );
     1647
     1648        return $user_id;
     1649}
  • 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.