Make WordPress Core

Ticket #15001: combine-user-error-checking.diff

File combine-user-error-checking.diff, 6.3 KB (added by wonderboymusic, 13 years ago)
  • wp-login.php

     
    297297 * @return int|WP_Error Either user's ID or error on failure.
    298298 */
    299299function register_new_user( $user_login, $user_email ) {
    300         $errors = new WP_Error();
     300        $data = check_user_for_errors( $user_login, $user_email );     
     301        // returns $errors, $user_login, $user_email
     302        extract( $data );
    301303
    302         $sanitized_user_login = sanitize_user( $user_login );
    303         $user_email = apply_filters( 'user_registration_email', $user_email );
     304        do_action( 'register_post', $user_login, $user_email, $errors );
    304305
    305         // Check the username
    306         if ( $sanitized_user_login == '' ) {
    307                 $errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
    308         } elseif ( ! validate_username( $user_login ) ) {
    309                 $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
    310                 $sanitized_user_login = '';
    311         } elseif ( username_exists( $sanitized_user_login ) ) {
    312                 $errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.' ) );
    313         }
     306        $errors = apply_filters( 'registration_errors', $errors, $user_login, $user_email );
    314307
    315         // Check the e-mail address
    316         if ( $user_email == '' ) {
    317                 $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.' ) );
    318         } elseif ( ! is_email( $user_email ) ) {
    319                 $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ) );
    320                 $user_email = '';
    321         } elseif ( email_exists( $user_email ) ) {
    322                 $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
    323         }
    324 
    325         do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
    326 
    327         $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
    328 
    329308        if ( $errors->get_error_code() )
    330309                return $errors;
    331310
    332311        $user_pass = wp_generate_password( 12, false);
    333         $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
     312        $user_id = wp_create_user( $user_login, $user_pass, $user_email );
    334313        if ( ! $user_id ) {
    335314                $errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
    336315                return $errors;
     
    734713<?php
    735714login_footer();
    736715break;
    737 } // end action switch
     716} // end action switch
     717 No newline at end of file
  • wp-includes/user.php

     
    12641264                $user_pass = wp_hash_password($user_pass);
    12651265        }
    12661266
    1267         $user_login = sanitize_user($user_login, true);
    1268         $user_login = apply_filters('pre_user_login', $user_login);
     1267        $data = check_user_for_errors( $user_login, $user_email, $update );     
     1268        extract( $data );
    12691269
    1270         //Remove any non-printable chars from the login string to see if we have ended up with an empty username
    1271         $user_login = trim($user_login);
    1272 
    1273         if ( empty($user_login) )
    1274                 return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') );
    1275 
    1276         if ( !$update && username_exists( $user_login ) )
    1277                 return new WP_Error('existing_user_login', __('This username is already registered.') );
    1278 
     1270        if ( $errors->get_error_code() )
     1271                return $errors;
     1272       
    12791273        if ( empty($user_nicename) )
    12801274                $user_nicename = sanitize_title( $user_login );
    12811275        $user_nicename = apply_filters('pre_user_nicename', $user_nicename);
     
    12841278                $user_url = '';
    12851279        $user_url = apply_filters('pre_user_url', $user_url);
    12861280
    1287         if ( empty($user_email) )
    1288                 $user_email = '';
    1289         $user_email = apply_filters('pre_user_email', $user_email);
    1290 
    1291         if ( !$update && ! defined( 'WP_IMPORTING' ) && email_exists($user_email) )
    1292                 return new WP_Error('existing_user_email', __('This email address is already registered.') );
    1293 
    12941281        if ( empty($display_name) )
    12951282                $display_name = $user_login;
    12961283        $display_name = apply_filters('pre_user_display_name', $display_name);
     
    14941481        );
    14951482        return apply_filters( 'user_contactmethods', $user_contactmethods, $user );
    14961483}
     1484
     1485/**
     1486 * Combines error_checking code from wp_insert_user and register_new_user
     1487 *
     1488 * @since 3.5.0
     1489 *
     1490 * @param string $user_login The user's login
     1491 * @param string $user_email The user's email address
     1492 * @param bool $update Whether the data is part of an update for an existing user
     1493 * @return array Data is meant to have extract() called on it
     1494 */
     1495function check_user_for_errors( $user_login, $user_email, $update = false ) {
     1496        $errors = new WP_Error();
     1497       
     1498        $user_login = apply_filters('pre_user_login', sanitize_user( $user_login, true ) );
     1499        $user_email = apply_filters( 'user_registration_email', $user_email );
     1500
     1501        $sanitized_user_login = trim( $user_login );   
     1502       
     1503        // Check the username
     1504        if ( empty( $sanitized_user_login ) ) {
     1505                $errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
     1506        } elseif ( ! validate_username( $user_login ) ) {
     1507                $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
     1508                $sanitized_user_login = '';
     1509        } elseif ( ! $update && username_exists( $sanitized_user_login ) ) {
     1510                $errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.' ) );
     1511        }       
     1512       
     1513        $sanitized_user_email = apply_filters( 'pre_user_email', $user_email );
     1514        // Check the e-mail address
     1515        if ( empty( $sanitized_user_email ) ) {
     1516                $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.' ) );
     1517        } elseif ( ! is_email( $sanitized_user_email ) ) {
     1518                $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ) );
     1519                $user_email = '';
     1520        } elseif ( ! $update && ! defined( 'WP_IMPORTING' ) && email_exists( $sanitized_user_email ) ) {
     1521                $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
     1522        }
     1523       
     1524        return array( 'errors' => $errors, 'user_email' => $sanitized_user_email, 'user_login' => $sanitized_user_login );
     1525}