Skip to content

Commit 2102b68

Browse files
committed
Toolbar: Fix display name live preview and "Howdy" color.
When the display name is changed on the profile page, correctly update the user menu in the admin bar: don't overwrite the "Edit Profile" item, do update the aria-label attribute, and of course update the rendered username. Add class `display-name` to the greeting wrapper to better preserve back-compat with custom admin color schemes that only target `#wp-admin-bar-user-info .display-name`. Follow-up to r64502 and its revert, r64504. Props fushar, tyxla. Fixes #66222. git-svn-id: https://develop.svn.wordpress.org/trunk@64519 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1e91058 commit 2102b68

5 files changed

Lines changed: 27 additions & 18 deletions

File tree

‎src/js/_enqueues/admin/user-profile.js‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,9 +494,14 @@
494494

495495
$( function() {
496496
var $colorpicker, $stylesheet, user_id, current_user_id,
497-
select = $( '#display_name' ),
498-
username = $( '#user_login' ).val(),
499-
greeting = $( '#wp-admin-bar-my-account' ).find( '.display-name' );
497+
select = $( '#display_name' ),
498+
username = $( '#user_login' ).val(),
499+
myAccount = $( '#wp-admin-bar-my-account' ),
500+
displayNames = myAccount.find( '.user-display-name' ),
501+
usernameLine = myAccount.find( '#wp-admin-bar-user-info .username' ),
502+
menus = myAccount.children( '.ab-sub-wrapper' ).children( '[role="menu"]' ).filter( function() {
503+
return this.getAttribute( 'aria-label' ) === displayNames.first().text();
504+
} );
500505

501506
$( '#pass1' ).val( '' ).on( 'input' + ' pwupdate', check_pass_strength );
502507
$('#pass-strength-result').show();
@@ -549,7 +554,11 @@
549554

550555
var display_name = this.value.trim() || username;
551556

552-
greeting.text( display_name );
557+
displayNames.text( display_name );
558+
menus.attr( 'aria-label', display_name );
559+
560+
// Only show the username when it differs from the display name.
561+
usernameLine.prop( 'hidden', display_name === username );
553562
} );
554563
}
555564

‎src/wp-admin/css/colors/_admin.scss‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,13 +521,10 @@ ul#adminmenu > li.current > a.current::after {
521521
background-color: variables.$adminbar-avatar-frame;
522522
}
523523

524-
#wpadminbar #wp-admin-bar-user-info .howdy,
525524
#wpadminbar #wp-admin-bar-user-info .display-name {
526525
color: variables.$menu-text;
527526
}
528527

529-
#wpadminbar #wp-admin-bar-user-info a:hover .howdy,
530-
#wpadminbar #wp-admin-bar-user-info a:focus .howdy,
531528
#wpadminbar #wp-admin-bar-user-info a:hover .display-name,
532529
#wpadminbar #wp-admin-bar-user-info a:focus .display-name {
533530
color: variables.$menu-submenu-focus-text;

‎src/wp-includes/admin-bar.php‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ function wp_admin_bar_my_account_item( $wp_admin_bar ) {
286286
array(
287287
'id' => 'my-account',
288288
'parent' => 'top-secondary',
289-
'title' => '<span class="display-name">' . $display_name . '</span>' . $avatar,
289+
'title' => '<span class="display-name user-display-name">' . $display_name . '</span>' . $avatar,
290290
'href' => $profile_url,
291291
'meta' => array(
292292
'class' => empty( $avatar ) ? '' : 'with-avatar',
@@ -335,14 +335,13 @@ function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
335335
}
336336

337337
/* translators: %s: Current user's display name. */
338-
$howdy = sprintf( __( 'Howdy, %s' ), '<span class="display-name">' . $display_name . '</span>' );
338+
$howdy = sprintf( __( 'Howdy, %s' ), "<span class='user-display-name'>" . $display_name . '</span>' );
339339

340340
$user_info = get_avatar( $user_id, 64 );
341-
$user_info .= '<span class="howdy">' . $howdy . '</span>';
341+
$user_info .= "<span class='display-name howdy'>" . $howdy . '</span>';
342342

343-
if ( $display_name !== $current_user->user_login ) {
344-
$user_info .= "<span class='username'>{$current_user->user_login}</span>";
345-
}
343+
$username_hidden = ( $display_name === $current_user->user_login ) ? ' hidden' : '';
344+
$user_info .= "<span class='username'{$username_hidden}>{$current_user->user_login}</span>";
346345

347346
if ( false !== $profile_url ) {
348347
$user_info .= "<span class='display-name edit-profile'>" . __( 'Edit Profile' ) . '</span>';

‎src/wp-includes/css/admin-bar.css‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,14 +461,17 @@ html:lang(he-il) .rtl #wpadminbar * {
461461
height: 18px;
462462
}
463463

464-
#wpadminbar #wp-admin-bar-user-info .howdy,
465464
#wpadminbar #wp-admin-bar-user-info .display-name,
466465
#wpadminbar #wp-admin-bar-user-info .username {
467466
display: block;
468467
}
469468

470-
#wpadminbar #wp-admin-bar-user-info .howdy .display-name {
471-
display: inline;
469+
#wpadminbar #wp-admin-bar-user-info .username[hidden] {
470+
display: none;
471+
}
472+
473+
#wpadminbar #wp-admin-bar-user-info .howdy .user-display-name {
474+
font: inherit;
472475
}
473476

474477
#wpadminbar #wp-admin-bar-user-info .username {
@@ -1044,7 +1047,6 @@ html:lang(he-il) .rtl #wpadminbar * {
10441047
margin: 0;
10451048
}
10461049

1047-
#wpadminbar #wp-admin-bar-user-info .howdy,
10481050
#wpadminbar #wp-admin-bar-user-info .display-name {
10491051
height: auto;
10501052
font-size: 16px;

‎tests/phpunit/tests/adminbar.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ public function test_my_account_has_greeting_in_user_info_only() {
935935
$this->assertSame( 'John Doe', $node_my_account->meta['menu_title'] );
936936

937937
$this->assertSame( 'Howdy, John Doe' . $current_user->user_login . 'Edit Profile', wp_strip_all_tags( $node_user_info->title ) );
938+
$this->assertStringContainsString( "<span class='username'>", $node_user_info->title );
938939
}
939940

940941
/**
@@ -958,7 +959,8 @@ public function test_my_account_falls_back_to_user_login_when_display_name_is_em
958959
$this->assertSame( $current_user->user_login, wp_strip_all_tags( $node_my_account->title ) );
959960
$this->assertSame( $current_user->user_login, $node_my_account->meta['menu_title'] );
960961

961-
$this->assertSame( 'Howdy, ' . $current_user->user_login . 'Edit Profile', wp_strip_all_tags( $node_user_info->title ) );
962+
$this->assertSame( 'Howdy, ' . $current_user->user_login . $current_user->user_login . 'Edit Profile', wp_strip_all_tags( $node_user_info->title ) );
963+
$this->assertStringContainsString( "<span class='username' hidden>", $node_user_info->title );
962964
}
963965

964966
/**

0 commit comments

Comments
 (0)