Make WordPress Core


Ignore:
Timestamp:
12/06/2015 08:56:11 PM (9 years ago)
Author:
boonebgorges
Message:

Show user_login in Dashboard user dropdowns.

User dropdowns in wp-admin have traditionally shown the users' display names.
However, this causes ambiguity when users share display names. To correct this,
we now show the unique user_login in parentheses after the display name.

The new display_name_with_login value for the show parameter of
wp_dropdown_users() enables this functionality. The default value of show
has not been changed, for backward compatibility, but all instances of
wp_dropdown_users() in core wp-admin have been switched.

This changeset also reduces some duplicated logic when assembling a user list
when include_selected is true.

Props krogsgard, boonebgorges.
Fixes #31251.

File:
1 edited

Legend:

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

    r35772 r35790  
    871871 *
    872872 * @since 2.3.0
     873 * @since 4.5.0 Added the 'display_name_with_login' value for 'show'.
    873874 *
    874875 * @global int  $blog_id
     
    897898 *     @type bool|int     $multi                   Whether to skip the ID attribute on the 'select' element.
    898899 *                                                 Accepts 1|true or 0|false. Default 0|false.
    899  *     @type string       $show                    User table column to display. If the selected item is empty
     900 *     @type string       $show                    User data to display. If the selected item is empty
    900901 *                                                 then the 'user_login' will be displayed in parentheses.
    901  *                                                 Accepts user fields. Default 'display_name'.
     902 *                                                 Accepts any user field, or 'display_name_with_login' to show
     903 *                                                 the display name with user_login in parentheses.
     904 *                                                 Default 'display_name'.
    902905 *     @type int|bool     $echo                    Whether to echo or return the drop-down. Accepts 1|true (echo)
    903906 *                                                 or 0|false (return). Default 1|true.
     
    928931
    929932    $r = wp_parse_args( $args, $defaults );
    930     $show = $r['show'];
     933
     934    $query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who' ) );
     935
     936    $fields = array( 'ID', 'user_login' );
     937
     938    $show = ! empty( $r['show'] ) ? $r['show'] : 'display_name';
     939    if ( 'display_name_with_login' === $show ) {
     940        $fields[] = 'display_name';
     941    } else {
     942        $fields[] = $show;
     943    }
     944
     945    $query_args['fields'] = $fields;
     946
    931947    $show_option_all = $r['show_option_all'];
    932948    $show_option_none = $r['show_option_none'];
    933949    $option_none_value = $r['option_none_value'];
    934 
    935     $query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who' ) );
    936     $query_args['fields'] = array( 'ID', 'user_login', $show );
    937950
    938951    /**
     
    967980        }
    968981
    969         $found_selected = false;
     982        if ( $r['include_selected'] && ( $r['selected'] > 0 ) ) {
     983            $found_selected = false;
     984            $r['selected'] = (int) $r['selected'];
     985            foreach ( (array) $users as $user ) {
     986                $user->ID = (int) $user->ID;
     987                if ( $user->ID === $r['selected'] ) {
     988                    $found_selected = true;
     989                }
     990            }
     991
     992            if ( ! $found_selected ) {
     993                $users[] = get_userdata( $r['selected'] );
     994            }
     995        }
     996
    970997        foreach ( (array) $users as $user ) {
    971             $user->ID = (int) $user->ID;
     998            if ( 'display_name_with_login' === $show ) {
     999                /* translators: 1: display name, 2: user_login */
     1000                $display = sprintf( _x( '%1$s (%2$s)', 'user dropdown' ), $user->display_name, $user->user_login );
     1001            } elseif ( ! empty( $user->$show ) ) {
     1002                $display = $user->$show;
     1003            } else {
     1004                $display = '(' . $user->user_login . ')';
     1005            }
     1006
    9721007            $_selected = selected( $user->ID, $r['selected'], false );
    973             if ( $_selected ) {
    974                 $found_selected = true;
    975             }
    976             $display = ! empty( $user->$show ) ? $user->$show : '('. $user->user_login . ')';
    977             $output .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
    978         }
    979 
    980         if ( $r['include_selected'] && ! $found_selected && ( $r['selected'] > 0 ) ) {
    981             $user = get_userdata( $r['selected'] );
    982             $_selected = selected( $user->ID, $r['selected'], false );
    983             $display = ! empty( $user->$show ) ? $user->$show : '('. $user->user_login . ')';
    9841008            $output .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
    9851009        }
Note: See TracChangeset for help on using the changeset viewer.