WordPress.org

Make WordPress Core

Ticket #7977: wp-import-usability.diff

File wp-import-usability.diff, 4.1 KB (added by tott, 9 years ago)

usability improvements diff against rev 9997

  • wp-includes/user.php

     
    226226 * @uses $blog_id The Blog id of the blog for those that use more than one blog
    227227 *
    228228 * @param int $id Blog ID.
     229 * @param string $sort_field optional Sort Field. user_login, display_name, user_email, user_id
    229230 * @return array List of users that are part of that Blog ID
    230231 */
    231 function get_users_of_blog( $id = '' ) {
    232         global $wpdb, $blog_id;
     232function get_users_of_blog( $id = '', $sort_field = 'user_id' ) {
     233        global $wpdb;
     234    $possible_sort_fields = array( 'user_login', 'display_name', 'user_email', 'user_id' );
    233235        if ( empty($id) )
    234                 $id = (int) $blog_id;
    235         $users = $wpdb->get_results( "SELECT user_id, user_login, display_name, user_email, meta_value FROM $wpdb->users, $wpdb->usermeta WHERE " . $wpdb->users . ".ID = " . $wpdb->usermeta . ".user_id AND meta_key = '" . $wpdb->prefix . "capabilities' ORDER BY {$wpdb->usermeta}.user_id" );
     236                $id = (int) $wpdb->blogid;
     237   
     238    if ( ! in_array( $sort_field, $possible_sort_fields ) )
     239        $sort_field = 'user_id';
     240
     241    switch( $sort_field ) {
     242    case 'user_email':
     243        $order = "{$wpdb->users}.user_email";
     244        break;
     245    case 'user_login':
     246        $order = "{$wpdb->users}.user_login";
     247        break;
     248    case 'display_name':
     249        $order = "{$wpdb->users}.display_name";
     250        break;
     251    case 'user_id':
     252        $order = "{$wpdb->usermeta}.user_id";
     253        break;
     254    }
     255        $users = $wpdb->get_results( "SELECT user_id, user_login, display_name, user_email, meta_value FROM $wpdb->users, $wpdb->usermeta WHERE {$wpdb->users}.user_login != '' && {$wpdb->users}.ID = {$wpdb->usermeta}.user_id AND meta_key = '{$wpdb->prefix}capabilities' ORDER BY {$order}" );
    236256        return $users;
    237257}
    238258
  • wp-admin/import/wordpress.php

     
    239239
    240240        }
    241241
     242   function suggest_user( $author_in, $users ) {
     243        $shortest = -1;
     244        $shortestavg = array();
     245
     246        $threshold = floor( ( strlen( $author_in ) / 100 ) * 25 ); // 25 % of the strlen are valid
     247        foreach ( $users as $user) {
     248            $levs[] = levenshtein( $author_in, $user->display_name );
     249            $levs[] = levenshtein( $author_in, $user->user_login );
     250            arsort( $levs );
     251            $lev = array_pop( $levs );
     252            if ( 0 == $lev ) {
     253                $closest = $user->user_id;
     254                $shortest = 0;
     255                break;
     256            }
     257           
     258            if ( ( $lev <= $shortest || $shortest < 0 ) && $lev <= $threshold ) {
     259                $closest  = $user->user_id;
     260                $shortest = $lev;
     261            }
     262
     263            $shortestavg[] = $lev;
     264
     265        }
     266
     267        // in case all usernames have a common pattern
     268        if ( $shortest > ( array_sum( $shortestavg ) / count( $shortestavg ) ) )
     269            return false;
     270
     271        return $closest;       
     272    }
     273   
    242274        function users_form($n, $author) {
    243275
    244276                if ( $this->allow_create_users() ) {
     
    251283                // keep track of $n => $author name
    252284                echo '<input type="hidden" name="author_in['.intval($n).']" value="'.htmlspecialchars($author).'" />';
    253285
    254                 $users = get_users_of_blog();
    255 ?><select name="user_select[<?php echo $n; ?>]">
     286                $users = get_users_of_blog( null, 'display_name' );
     287    ?>
     288    <select name="user_select[<?php echo $n; ?>]">
    256289        <option value="0"><?php _e('- Select -'); ?></option>
    257290        <?php
    258                 foreach ($users as $user) {
    259                         echo '<option value="'.$user->user_id.'">'.$user->user_login.'</option>';
     291        foreach ($users as $user) {
     292            if ( $this->suggest_user( $author, $users ) == $user->user_id )
     293                $selected = ' selected="selected"';
     294            else
     295                $selected = '';
     296                        echo '<option value="' . $user->user_id . '"'. $selected . '>' . $user->user_login . ' (' . $user->display_name . ') </option>';
    260297                }
    261 ?>
     298    ?>
    262299        </select>
    263300        <?php
    264301        }