WordPress.org

Make WordPress Core

Ticket #9568: 9568.4.diff

File 9568.4.diff, 9.7 KB (added by Denis-de-Bernardy, 9 years ago)

as 9568.3.diff with a different approach for extracting a user_nicename from the email

  • wp-login.php

     
    205205        $user_email = apply_filters( 'user_registration_email', $user_email );
    206206
    207207        // Check the username
    208         if ( $user_login == '' )
    209                 $errors->add('empty_username', __('<strong>ERROR</strong>: Please enter a username.'));
    210         elseif ( !validate_username( $user_login ) ) {
    211                 $errors->add('invalid_username', __('<strong>ERROR</strong>: This username is invalid.  Please enter a valid username.'));
    212                 $user_login = '';
    213         } elseif ( username_exists( $user_login ) )
    214                 $errors->add('username_exists', __('<strong>ERROR</strong>: This username is already registered, please choose another one.'));
     208        if ( !empty($user_login) ) {
     209                if ( !validate_username( $user_login ) ) {
     210                        $errors->add('invalid_username', __('<strong>ERROR</strong>: This username is invalid.  Please enter a valid username.'));
     211                        $user_login = '';
     212                } elseif ( username_exists( $user_login ) )
     213                        $errors->add('username_exists', __('<strong>ERROR</strong>: This username is already registered, please choose another one.'));
     214        }
    215215
    216216        // Check the e-mail address
    217217        if ($user_email == '') {
     
    222222        } elseif ( email_exists( $user_email ) )
    223223                $errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'));
    224224
     225        if ( !$errors->get_error_code() && empty($user_login) )
     226                $user_login = $user_email;
     227
    225228        do_action('register_post', $user_login, $user_email, $errors);
    226229
    227230        $errors = apply_filters( 'registration_errors', $errors );
     
    373376
    374377<form name="registerform" id="registerform" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post">
    375378        <p>
    376                 <label><?php _e('Username') ?><br />
     379                <label><?php _e('Username (optional)') ?><br />
    377380                <input type="text" name="user_login" id="user_login" class="input" value="<?php echo attr(stripslashes($user_login)); ?>" size="20" tabindex="10" /></label>
    378381        </p>
    379382        <p>
     
    466469<?php if ( !isset($_GET['checkemail']) || !in_array( $_GET['checkemail'], array('confirm', 'newpass') ) ) : ?>
    467470<form name="loginform" id="loginform" action="<?php echo site_url('wp-login.php', 'login_post') ?>" method="post">
    468471        <p>
    469                 <label><?php _e('Username') ?><br />
     472                <label><?php _e('Username or Email') ?><br />
    470473                <input type="text" name="log" id="user_login" class="input" value="<?php echo attr($user_login); ?>" size="20" tabindex="10" /></label>
    471474        </p>
    472475        <p>
  • wp-includes/registration.php

     
    119119        $user_login = sanitize_user($user_login, true);
    120120        $user_login = apply_filters('pre_user_login', $user_login);
    121121
    122         if ( empty($user_nicename) )
    123                 $user_nicename = sanitize_title( $user_login );
     122        if ( empty($user_nicename) ) {
     123                if ( $user_login == $user_email ) {
     124                        $user_nicename = preg_replace("/@.*/", '', $user_email);
     125                } else {
     126                        $user_nicename = $user_login;
     127                }
     128                $user_nicename = sanitize_title( $user_nicename );
     129        }
    124130        $user_nicename = apply_filters('pre_user_nicename', $user_nicename);
    125131
    126132        if ( empty($user_url) )
     
    176182        if ( empty($user_registered) )
    177183                $user_registered = gmdate('Y-m-d H:i:s');
    178184
    179         $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND ID != %d LIMIT 1" , $user_nicename, (int) $ID));
     185        $user_nicename = wp_unique_user_nicename($user_nicename, $ID);
    180186
    181         if ($user_nicename_check) {
    182                 $suffix = 2;
    183                 while ($user_nicename_check) {
    184                         $alt_user_nicename = $user_nicename . "-$suffix";
    185                         $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND ID != %d LIMIT 1" , $alt_user_nicename, (int) $ID));
    186                         $suffix++;
    187                 }
    188                 $user_nicename = $alt_user_nicename;
    189         }
    190 
    191         $data = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
     187        $data = compact( 'user_login', 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
    192188        $data = stripslashes_deep( $data );
    193189
    194190        if ( $update ) {
    195191                $wpdb->update( $wpdb->users, $data, compact( 'ID' ) );
    196192                $user_id = (int) $ID;
    197193        } else {
    198                 $wpdb->insert( $wpdb->users, $data + compact( 'user_login' ) );
     194                $wpdb->insert( $wpdb->users, $data );
    199195                $user_id = (int) $wpdb->insert_id;
    200196        }
    201197
     
    266262                $userdata['user_pass'] = wp_hash_password($userdata['user_pass']);
    267263        }
    268264
     265        // Keep trace of current user
     266        $current_user = wp_get_current_user();
     267       
    269268        // Merge old and new fields with new fields overwriting old ones.
    270269        $userdata = array_merge($user, $userdata);
    271270        $user_id = wp_insert_user($userdata);
    272271
    273272        // Update the cookies if the password changed.
    274         $current_user = wp_get_current_user();
    275273        if ( $current_user->id == $ID ) {
    276                 if ( isset($plaintext_pass) ) {
     274                if ( isset($plaintext_pass) || $user['user_login'] != $userdata['user_login'] ) {
    277275                        wp_clear_auth_cookie();
    278276                        wp_set_auth_cookie($ID);
    279277                }
     
    308306        return wp_insert_user($userdata);
    309307}
    310308
    311 ?>
     309/**
     310 * Returns a unique nicename for a user
     311 *
     312 * @since 2.8
     313 * @see wp_insert_user()
     314 *
     315 * @param string $user_nicename The user's nicename.
     316 * @param string $user_id The user's ID.
     317 * @return string The user's nicename.
     318 */
     319function wp_unique_user_nicename($user_nicename, $user_id) {
     320        global $wpdb;
     321       
     322        $user_id = (int) $user_id;
     323        $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND ID != %d LIMIT 1" , $user_nicename, $user_id));
     324
     325        if ( $user_nicename_check ) {
     326                $suffix = 2;
     327                while ( $user_nicename_check ) {
     328                        $alt_user_nicename = $user_nicename . "-$suffix";
     329                        $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND ID != %d LIMIT 1" , $alt_user_nicename, $user_id));
     330                        $suffix++;
     331                }
     332                $user_nicename = $alt_user_nicename;
     333        }
     334       
     335        return $user_nicename;
     336}
     337?>
     338 No newline at end of file
  • wp-admin/includes/user.php

     
    129129
    130130        $errors = new WP_Error();
    131131
    132         /* checking that username has been typed */
    133         if ( $user->user_login == '' )
    134                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ));
    135 
    136132        /* checking the password has been typed twice */
    137133        do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 ));
    138134
     
    159155        if (!empty ( $pass1 ))
    160156                $user->user_pass = $pass1;
    161157
    162         if ( !$update && !validate_username( $user->user_login ) )
    163                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid. Please enter a valid username.' ));
     158        if ( !$update && !empty($user->user_login) ) {
     159                if ( !validate_username( $user->user_login ) )
     160                        $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid. Please enter a valid username.' ));
     161                if ( username_exists( $user->user_login ) )
     162                        $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ));
     163        }
    164164
    165         if (!$update && username_exists( $user->user_login ))
    166                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ));
    167 
    168165        /* checking e-mail address */
    169166        if ( empty ( $user->user_email ) ) {
    170167                $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please enter an e-mail address.' ), array( 'form-field' => 'email' ) );
     
    179176        if ( $errors->get_error_codes() )
    180177                return $errors;
    181178
     179        if ( $update && $userdata->user_login == $userdata->user_email ) {
     180                $user->user_login = $user->user_email;
     181        } elseif ( !$update && empty($user->user_login) ) {
     182                $user->user_login = $user->user_email;
     183        }
     184
    182185        if ( $update ) {
    183186                $user_id = wp_update_user( get_object_vars( $user ));
    184187        } else {
  • wp-admin/user-new.php

     
    8989}
    9090?>
    9191<table class="form-table">
    92         <tr class="form-field form-required">
    93                 <th scope="row"><label for="user_login"><?php _e('Username (required)') ?></label><input name="action" type="hidden" id="action" value="adduser" /></th>
    94                 <td ><input name="user_login" type="text" id="user_login" value="<?php echo attr($new_user_login); ?>" aria-required="true" /></td>
     92        <tr class="form-field">
     93                <th scope="row"><label for="user_login"><?php _e('Username') ?></label><input name="action" type="hidden" id="action" value="adduser" /></th>
     94                <td ><input name="user_login" type="text" id="user_login" value="<?php echo attr($new_user_login); ?>" /></td>
    9595        </tr>
    9696        <tr class="form-field">
    9797                <th scope="row"><label for="first_name"><?php _e('First Name') ?> </label></th>
  • wp-admin/user-edit.php

     
    246246<h3><?php _e('Name') ?></h3>
    247247
    248248<table class="form-table">
     249<?php if ( $profileuser->user_login != $profileuser->user_email ) : ?>
    249250        <tr>
    250251                <th><label for="user_login"><?php _e('Username'); ?></label></th>
    251252                <td><input type="text" name="user_login" id="user_login" value="<?php echo attr($profileuser->user_login); ?>" disabled="disabled" class="regular-text" /> <?php _e('Your username cannot be changed.'); ?></td>
    252253        </tr>
     254<?php endif; ?>
    253255
    254256<?php if ( !$is_profile_page ): ?>
    255257<tr><th><label for="role"><?php _e('Role:') ?></label></th>