Make WordPress Core

Ticket #9568: 9568.6.diff

File 9568.6.diff, 9.8 KB (added by Denis-de-Bernardy, 16 years ago)
  • wp-login.php

     
    232232        $user_email = apply_filters( 'user_registration_email', $user_email );
    233233
    234234        // Check the username
    235         if ( $user_login == '' )
    236                 $errors->add('empty_username', __('<strong>ERROR</strong>: Please enter a username.'));
    237         elseif ( !validate_username( $user_login ) ) {
    238                 $errors->add('invalid_username', __('<strong>ERROR</strong>: This username is invalid.  Please enter a valid username.'));
    239                 $user_login = '';
    240         } elseif ( username_exists( $user_login ) )
    241                 $errors->add('username_exists', __('<strong>ERROR</strong>: This username is already registered, please choose another one.'));
     235        if ( !empty($user_login) ) {
     236                if ( !validate_username( $user_login ) ) {
     237                        $errors->add('invalid_username', __('<strong>ERROR</strong>: This username is invalid.  Please enter a valid username.'));
     238                        $user_login = '';
     239                } elseif ( username_exists( $user_login ) ) {
     240                        $errors->add('username_exists', __('<strong>ERROR</strong>: This username is already registered, please choose another one.'));
     241                }
     242        }
    242243
    243244        // Check the e-mail address
    244245        if ($user_email == '') {
     
    249250        } elseif ( email_exists( $user_email ) )
    250251                $errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'));
    251252
     253        if ( !$errors->get_error_code() && empty($user_login) )
     254                $user_login = $user_email;
     255
    252256        do_action('register_post', $user_login, $user_email, $errors);
    253257
    254258        $errors = apply_filters( 'registration_errors', $errors );
     
    407411
    408412<form name="registerform" id="registerform" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post">
    409413        <p>
    410                 <label><?php _e('Username') ?><br />
     414                <label><?php _e('Username (optional)') ?><br />
    411415                <input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr(stripslashes($user_login)); ?>" size="20" tabindex="10" /></label>
    412416        </p>
    413417        <p>
     
    501505<?php if ( !isset($_GET['checkemail']) || !in_array( $_GET['checkemail'], array('confirm', 'newpass') ) ) : ?>
    502506<form name="loginform" id="loginform" action="<?php echo site_url('wp-login.php', 'login_post') ?>" method="post">
    503507        <p>
    504                 <label><?php _e('Username') ?><br />
     508                <label><?php _e('Username or Email') ?><br />
    505509                <input type="text" name="log" id="user_login" class="input" value="<?php echo esc_attr($user_login); ?>" size="20" tabindex="10" /></label>
    506510        </p>
    507511        <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 user_login != %s LIMIT 1" , $user_nicename, $user_login));
    180 
    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 user_login != %s LIMIT 1" , $alt_user_nicename, $user_login));
    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' );
     185        $user_nicename = wp_unique_user_nicename($user_nicename, $ID);
     186       
     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
     
    264260                $userdata['user_pass'] = wp_hash_password($userdata['user_pass']);
    265261        }
    266262
     263        // Keep trace of current user
     264        $current_user = wp_get_current_user();
     265       
    267266        // Merge old and new fields with new fields overwriting old ones.
    268267        $userdata = array_merge($user, $userdata);
    269268        $user_id = wp_insert_user($userdata);
    270269
    271270        // Update the cookies if the password changed.
    272         $current_user = wp_get_current_user();
    273271        if ( $current_user->id == $ID ) {
    274                 if ( isset($plaintext_pass) ) {
     272                if ( isset($plaintext_pass) || $user['user_login'] != $userdata['user_login'] ) {
    275273                        wp_clear_auth_cookie();
    276274                        wp_set_auth_cookie($ID);
    277275                }
     
    306304        return wp_insert_user($userdata);
    307305}
    308306
    309 ?>
     307/**
     308 * Returns a unique nicename for a user
     309 *
     310 * @since 2.8
     311 * @see wp_insert_user()
     312 *
     313 * @param string $user_nicename The user's nicename.
     314 * @param string $user_id The user's ID.
     315 * @return string The user's nicename.
     316 */
     317
     318function wp_unique_user_nicename($user_nicename, $user_id) {
     319        global $wpdb;
     320
     321        $user_id = (int) $user_id;
     322        $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));
     323
     324        if ( $user_nicename_check ) {
     325                $suffix = 2;
     326                while ( $user_nicename_check ) {
     327                        $alt_user_nicename = $user_nicename . "-$suffix";
     328                        $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));
     329                        $suffix++;
     330                }
     331                $user_nicename = $alt_user_nicename;
     332        }
     333
     334        return $user_nicename;
     335}
     336?>
     337 No newline at end of file
  • wp-admin/includes/user.php

     
    133133
    134134        $errors = new WP_Error();
    135135
    136         /* checking that username has been typed */
    137         if ( $user->user_login == '' )
    138                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ));
    139 
    140136        /* checking the password has been typed twice */
    141137        do_action_ref_array( 'check_passwords', array ( $user->user_login, & $pass1, & $pass2 ));
    142138
     
    163159        if (!empty ( $pass1 ))
    164160                $user->user_pass = $pass1;
    165161
    166         if ( !$update && !validate_username( $user->user_login ) )
    167                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid. Please enter a valid username.' ));
     162        if ( !$update && !empty($user->user_login) ) {
     163                if ( !validate_username( $user->user_login ) )
     164                        $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid. Please enter a valid username.' ));
     165                if ( username_exists( $user->user_login ) )
     166                        $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ));
     167        }
    168168
    169         if (!$update && username_exists( $user->user_login ))
    170                 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ));
    171 
    172169        /* checking e-mail address */
    173170        if ( empty ( $user->user_email ) ) {
    174171                $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please enter an e-mail address.' ), array( 'form-field' => 'email' ) );
     
    184181        if ( $errors->get_error_codes() )
    185182                return $errors;
    186183
     184        if ( $update && $userdata->user_login == $userdata->user_email ) {
     185                $user->user_login = $user->user_email;
     186        } elseif ( !$update && empty($user->user_login) ) {
     187                $user->user_login = $user->user_email;
     188        }
     189
    187190        if ( $update ) {
    188191                $user_id = wp_update_user( get_object_vars( $user ));
    189192        } else {
  • wp-admin/user-new.php

     
    9393?>
    9494<table class="form-table">
    9595        <tr class="form-field form-required">
    96                 <th scope="row"><label for="user_login"><?php _e('Username'); ?> <span class="description"><?php _e('(required)'); ?></span></label>
     96                <th scope="row"><label for="user_login"><?php _e('Username'); ?></label>
    9797                <input name="action" type="hidden" id="action" value="adduser" /></th>
    98                 <td><input name="user_login" type="text" id="user_login" value="<?php echo esc_attr($new_user_login); ?>" aria-required="true" /></td>
     98                <td><input name="user_login" type="text" id="user_login" value="<?php echo esc_attr($new_user_login); ?>" /></td>
    9999        </tr>
    100100        <tr class="form-field">
    101101                <th scope="row"><label for="first_name"><?php _e('First Name') ?> </label></th>
  • wp-admin/user-edit.php

     
    183183<h3><?php _e('Name') ?></h3>
    184184
    185185<table class="form-table">
     186<?php if ( $profileuser->user_login != $profileuser->user_email ) : ?>
    186187        <tr>
    187188                <th><label for="user_login"><?php _e('Username'); ?></label></th>
    188189                <td><input type="text" name="user_login" id="user_login" value="<?php echo esc_attr($profileuser->user_login); ?>" disabled="disabled" class="regular-text" /> <span class="description"><?php _e('Your username cannot be changed.'); ?></span></td>
    189190        </tr>
     191<?php endif; ?>
    190192
    191193<?php if ( !IS_PROFILE_PAGE ): ?>
    192194<tr><th><label for="role"><?php _e('Role:') ?></label></th>