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.

git-svn-id: https://develop.svn.wordpress.org/trunk@35790 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-12-06 20:56:11 +00:00
parent c3b6753782
commit ded2547b57
7 changed files with 178 additions and 25 deletions

View File

@ -177,8 +177,13 @@ function export_date_options( $post_type = 'post' ) {
<label><span class="label-responsive"><?php _e( 'Authors:' ); ?></span>
<?php
$authors = $wpdb->get_col( "SELECT DISTINCT post_author FROM {$wpdb->posts} WHERE post_type = 'post'" );
wp_dropdown_users( array( 'include' => $authors, 'name' => 'post_author', 'multi' => true, 'show_option_all' => __('All') ) );
?>
wp_dropdown_users( array(
'include' => $authors,
'name' => 'post_author',
'multi' => true,
'show_option_all' => __( 'All' ),
'show' => 'display_name_with_login',
) ); ?>
</label>
</li>
<li>
@ -214,8 +219,13 @@ function export_date_options( $post_type = 'post' ) {
<label><span class="label-responsive"><?php _e( 'Authors:' ); ?></span>
<?php
$authors = $wpdb->get_col( "SELECT DISTINCT post_author FROM {$wpdb->posts} WHERE post_type = 'page'" );
wp_dropdown_users( array( 'include' => $authors, 'name' => 'page_author', 'multi' => true, 'show_option_all' => __('All') ) );
?>
wp_dropdown_users( array(
'include' => $authors,
'name' => 'page_author',
'multi' => true,
'show_option_all' => __( 'All' ),
'show' => 'display_name_with_login',
) ); ?>
</label>
</li>
<li>

View File

@ -289,7 +289,7 @@ function wp_ajax_autocomplete_user() {
foreach ( $users as $user ) {
$return[] = array(
/* translators: 1: user_login, 2: user_email */
'label' => sprintf( __( '%1$s (%2$s)' ), $user->user_login, $user->user_email ),
'label' => sprintf( _x( '%1$s (%2$s)', 'user autocomplete result' ), $user->user_login, $user->user_email ),
'value' => $user->$field,
);
}

View File

@ -1335,7 +1335,8 @@ class WP_Posts_List_Table extends WP_List_Table {
'name' => 'post_author',
'class'=> 'authors',
'multi' => 1,
'echo' => 0
'echo' => 0,
'show' => 'display_name_with_login',
);
if ( $bulk )
$users_opt['show_option_none'] = __( '&mdash; No Change &mdash;' );

View File

@ -742,7 +742,8 @@ function post_author_meta_box($post) {
'who' => 'authors',
'name' => 'post_author_override',
'selected' => empty($post->ID) ? $user_ID : $post->post_author,
'include_selected' => true
'include_selected' => true,
'show' => 'display_name_with_login',
) );
}

View File

@ -270,7 +270,11 @@ case 'delete':
<?php _e('Delete all content.'); ?></label></li>
<li><input type="radio" id="delete_option1" name="delete_option" value="reassign" />
<?php echo '<label for="delete_option1">' . __( 'Attribute all content to:' ) . '</label> ';
wp_dropdown_users( array( 'name' => 'reassign_user', 'exclude' => array_diff( $userids, array($current_user->ID) ) ) ); ?></li>
wp_dropdown_users( array(
'name' => 'reassign_user',
'exclude' => array_diff( $userids, array( $current_user->ID ) ),
'show' => 'display_name_with_login',
) ); ?></li>
</ul></fieldset>
<?php endif;
/**

View File

@ -870,6 +870,7 @@ function setup_userdata($for_user_id = '') {
* The available arguments are as follows:
*
* @since 2.3.0
* @since 4.5.0 Added the 'display_name_with_login' value for 'show'.
*
* @global int $blog_id
*
@ -896,9 +897,11 @@ function setup_userdata($for_user_id = '') {
* Default empty.
* @type bool|int $multi Whether to skip the ID attribute on the 'select' element.
* Accepts 1|true or 0|false. Default 0|false.
* @type string $show User table column to display. If the selected item is empty
* @type string $show User data to display. If the selected item is empty
* then the 'user_login' will be displayed in parentheses.
* Accepts user fields. Default 'display_name'.
* Accepts any user field, or 'display_name_with_login' to show
* the display name with user_login in parentheses.
* Default 'display_name'.
* @type int|bool $echo Whether to echo or return the drop-down. Accepts 1|true (echo)
* or 0|false (return). Default 1|true.
* @type int $selected Which user ID should be selected. Default 0.
@ -927,14 +930,24 @@ function wp_dropdown_users( $args = '' ) {
$defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0;
$r = wp_parse_args( $args, $defaults );
$show = $r['show'];
$query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who' ) );
$fields = array( 'ID', 'user_login' );
$show = ! empty( $r['show'] ) ? $r['show'] : 'display_name';
if ( 'display_name_with_login' === $show ) {
$fields[] = 'display_name';
} else {
$fields[] = $show;
}
$query_args['fields'] = $fields;
$show_option_all = $r['show_option_all'];
$show_option_none = $r['show_option_none'];
$option_none_value = $r['option_none_value'];
$query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who' ) );
$query_args['fields'] = array( 'ID', 'user_login', $show );
/**
* Filter the query arguments for the user drop-down.
*
@ -966,21 +979,32 @@ function wp_dropdown_users( $args = '' ) {
$output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$_selected>$show_option_none</option>\n";
}
if ( $r['include_selected'] && ( $r['selected'] > 0 ) ) {
$found_selected = false;
$r['selected'] = (int) $r['selected'];
foreach ( (array) $users as $user ) {
$user->ID = (int) $user->ID;
$_selected = selected( $user->ID, $r['selected'], false );
if ( $_selected ) {
if ( $user->ID === $r['selected'] ) {
$found_selected = true;
}
$display = ! empty( $user->$show ) ? $user->$show : '('. $user->user_login . ')';
$output .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
}
if ( $r['include_selected'] && ! $found_selected && ( $r['selected'] > 0 ) ) {
$user = get_userdata( $r['selected'] );
if ( ! $found_selected ) {
$users[] = get_userdata( $r['selected'] );
}
}
foreach ( (array) $users as $user ) {
if ( 'display_name_with_login' === $show ) {
/* translators: 1: display name, 2: user_login */
$display = sprintf( _x( '%1$s (%2$s)', 'user dropdown' ), $user->display_name, $user->user_login );
} elseif ( ! empty( $user->$show ) ) {
$display = $user->$show;
} else {
$display = '(' . $user->user_login . ')';
}
$_selected = selected( $user->ID, $r['selected'], false );
$display = ! empty( $user->$show ) ? $user->$show : '('. $user->user_login . ')';
$output .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
}

View File

@ -0,0 +1,113 @@
<?php
/**
* Test functions in wp-includes/user.php
*
* @group user
*/
class Tests_User_WpDropdownUsers extends WP_UnitTestCase {
/**
* @ticket 31251
*/
public function test_default_value_of_show_should_be_display_name() {
// create a user with a different display_name
$u = $this->factory->user->create( array(
'user_login' => 'foo',
'display_name' => 'Foo Person'
) );
$found = wp_dropdown_users( array(
'echo' => false
) );
$expected = "<option value='$u'>Foo Person</option>";
$this->assertContains( $expected, $found );
}
/**
* @ticket 31251
*/
public function test_show_should_display_display_name_show_is_specified_as_empty() {
// create a user with a different display_name
$u = $this->factory->user->create( array(
'user_login' => 'foo',
'display_name' => 'Foo Person'
) );
// Get the result of a non-default, but acceptable input for 'show' parameter to wp_dropdown_users().
$found = wp_dropdown_users( array(
'echo' => false,
'show' => ''
) );
$expected = "<option value='$u'>Foo Person</option>";
$this->assertContains( $expected, $found );
}
/**
* @ticket 31251
*/
public function test_show_should_display_user_property_when_the_value_of_show_is_a_valid_user_property() {
// create a user with a different display_name
$u = $this->factory->user->create( array(
'user_login' => 'foo',
'display_name' => 'Foo Person'
) );
// Get the result of a non-default, but acceptable input for 'show' parameter to wp_dropdown_users().
$found = wp_dropdown_users( array(
'echo' => false,
'show' => 'user_login'
) );
$expected = "<option value='$u'>foo</option>";
$this->assertContains( $expected, $found );
}
/**
* @ticket 31251
*/
public function test_show_display_name_with_login() {
// create a user with a different display_name
$u = $this->factory->user->create( array(
'user_login' => 'foo',
'display_name' => 'Foo Person'
) );
// Get the result of a non-default, but acceptable input for 'show' parameter to wp_dropdown_users().
$found = wp_dropdown_users( array(
'echo' => false,
'show' => 'display_name_with_login'
) );
$expected = "<option value='$u'>Foo Person (foo)</option>";
$this->assertContains( $expected, $found );
}
/**
* @ticket 31251
*/
public function test_include_selected() {
$users = self::factory()->user->create_many( 2 );
$found = wp_dropdown_users( array(
'echo' => false,
'include' => $users[0],
'selected' => $users[1],
'include_selected' => true,
'show' => 'user_login',
) );
$user1 = get_userdata( $users[1] );
$this->assertContains( $user1->user_login, $found );
}
}