From ded2547b57116df5cc5be0b154fbf3cfc2f92d3a Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sun, 6 Dec 2015 20:56:11 +0000 Subject: [PATCH] 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 --- src/wp-admin/export.php | 18 ++- src/wp-admin/includes/ajax-actions.php | 2 +- .../includes/class-wp-posts-list-table.php | 3 +- src/wp-admin/includes/meta-boxes.php | 3 +- src/wp-admin/users.php | 6 +- src/wp-includes/user.php | 58 ++++++--- tests/phpunit/tests/user/wpDropdownUsers.php | 113 ++++++++++++++++++ 7 files changed, 178 insertions(+), 25 deletions(-) create mode 100644 tests/phpunit/tests/user/wpDropdownUsers.php diff --git a/src/wp-admin/export.php b/src/wp-admin/export.php index 7f416c4656..6efb7a7510 100644 --- a/src/wp-admin/export.php +++ b/src/wp-admin/export.php @@ -177,8 +177,13 @@ function export_date_options( $post_type = 'post' ) {
  • @@ -214,8 +219,13 @@ function export_date_options( $post_type = 'post' ) {
  • diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 58734452f9..47a1fb0eb6 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -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, ); } diff --git a/src/wp-admin/includes/class-wp-posts-list-table.php b/src/wp-admin/includes/class-wp-posts-list-table.php index d52ebc9832..8a1cd65d18 100644 --- a/src/wp-admin/includes/class-wp-posts-list-table.php +++ b/src/wp-admin/includes/class-wp-posts-list-table.php @@ -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'] = __( '— No Change —' ); diff --git a/src/wp-admin/includes/meta-boxes.php b/src/wp-admin/includes/meta-boxes.php index 1627b1f577..f52af1470e 100644 --- a/src/wp-admin/includes/meta-boxes.php +++ b/src/wp-admin/includes/meta-boxes.php @@ -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', ) ); } diff --git a/src/wp-admin/users.php b/src/wp-admin/users.php index cdf0d8b372..44d860ecfa 100644 --- a/src/wp-admin/users.php +++ b/src/wp-admin/users.php @@ -270,7 +270,11 @@ case 'delete':
  • ' . __( 'Attribute all content to:' ) . ' '; - wp_dropdown_users( array( 'name' => 'reassign_user', 'exclude' => array_diff( $userids, array($current_user->ID) ) ) ); ?>
  • + wp_dropdown_users( array( + 'name' => 'reassign_user', + 'exclude' => array_diff( $userids, array( $current_user->ID ) ), + 'show' => 'display_name_with_login', + ) ); ?> $show_option_none\n"; } - $found_selected = false; - foreach ( (array) $users as $user ) { - $user->ID = (int) $user->ID; - $_selected = selected( $user->ID, $r['selected'], false ); - if ( $_selected ) { - $found_selected = true; + 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; + if ( $user->ID === $r['selected'] ) { + $found_selected = true; + } + } + + if ( ! $found_selected ) { + $users[] = get_userdata( $r['selected'] ); } - $display = ! empty( $user->$show ) ? $user->$show : '('. $user->user_login . ')'; - $output .= "\t\n"; } - if ( $r['include_selected'] && ! $found_selected && ( $r['selected'] > 0 ) ) { - $user = 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\n"; } diff --git a/tests/phpunit/tests/user/wpDropdownUsers.php b/tests/phpunit/tests/user/wpDropdownUsers.php new file mode 100644 index 0000000000..844c9532ed --- /dev/null +++ b/tests/phpunit/tests/user/wpDropdownUsers.php @@ -0,0 +1,113 @@ +factory->user->create( array( + 'user_login' => 'foo', + 'display_name' => 'Foo Person' + ) ); + + $found = wp_dropdown_users( array( + 'echo' => false + ) ); + + $expected = ""; + + $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 = ""; + + $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 = ""; + + $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 = ""; + + $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 ); + } +}