Skip to content

Commit eb33c60

Browse files
committed
Toolbar: Accessibility: Keyboard navigation for screen readers.
Change the admin toolbar to have `role="menu"` and support opening for screen readers. Remove screen reader only log out link and collapse duplicate profile links into one link. This is an imperfect solution to a complex problem in the adminbar, but the lack of screen reader access to submenus is a major accessibility problem, and this fix provides access, even if the mechanism is imperfect. Screen reader log out added in [21452]. Props abletec, Cheffheid, sabernhardt, alexstine, joedolson, afercia, sparklingrobots, danieltj, swissspidy, netweb, dionysous. Fixes #34668, #43633. git-svn-id: https://develop.svn.wordpress.org/trunk@57708 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 2ba8f94 commit eb33c60

5 files changed

Lines changed: 61 additions & 46 deletions

File tree

src/js/_enqueues/lib/admin-bar.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
topMenuItems = adminBar.querySelectorAll( 'li.menupop' );
3333
allMenuItems = adminBar.querySelectorAll( '.ab-item' );
34-
adminBarLogout = document.getElementById( 'wp-admin-bar-logout' );
34+
adminBarLogout = document.querySelector( '#wp-admin-bar-logout a' );
3535
adminBarSearchForm = document.getElementById( 'adminbarsearch' );
3636
shortlink = document.getElementById( 'wp-admin-bar-get-shortlink' );
3737
skipLink = adminBar.querySelector( '.screen-reader-shortcut' );
@@ -149,7 +149,8 @@
149149
function toggleHoverIfEnter( event ) {
150150
var wrapper;
151151

152-
if ( event.which !== 13 ) {
152+
// Follow link if pressing Ctrl and/or Shift with Enter (opening in a new tab or window).
153+
if ( event.which !== 13 || event.ctrlKey || event.shiftKey ) {
153154
return;
154155
}
155156

@@ -336,6 +337,11 @@
336337

337338
element.className += className;
338339
}
340+
341+
var menuItemToggle = element.querySelector( 'a' );
342+
if ( className === 'hover' && menuItemToggle && menuItemToggle.hasAttribute( 'aria-expanded' ) ) {
343+
menuItemToggle.setAttribute( 'aria-expanded', 'true' );
344+
}
339345
}
340346

341347
/**
@@ -366,6 +372,11 @@
366372

367373
element.className = classes.replace( /^[\s]+|[\s]+$/g, '' );
368374
}
375+
376+
var menuItemToggle = element.querySelector( 'a' );
377+
if ( className === 'hover' && menuItemToggle && menuItemToggle.hasAttribute( 'aria-expanded' ) ) {
378+
menuItemToggle.setAttribute( 'aria-expanded', 'false' );
379+
}
369380
}
370381

371382
/**

src/wp-includes/admin-bar.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ function wp_admin_bar_wp_menu( $wp_admin_bar ) {
139139
__( 'About WordPress' ) .
140140
'</span>',
141141
'href' => $about_url,
142+
'meta' => array(
143+
'menu_title' => __( 'About WordPress' ),
144+
),
142145
);
143146

144147
// Set tabindex="0" to make sub menus accessible when no URL is available.
@@ -282,7 +285,8 @@ function wp_admin_bar_my_account_item( $wp_admin_bar ) {
282285
'title' => $howdy . $avatar,
283286
'href' => $profile_url,
284287
'meta' => array(
285-
'class' => $class,
288+
'class' => $class,
289+
'menu_title' => sprintf( __( 'Howdy, %s' ), $current_user->display_name ),
286290
),
287291
)
288292
);
@@ -325,29 +329,17 @@ function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
325329
$user_info .= "<span class='username'>{$current_user->user_login}</span>";
326330
}
327331

332+
$user_info .= "<span class='edit-profile'>" . __( 'Edit Profile' ) . '</span>';
333+
328334
$wp_admin_bar->add_node(
329335
array(
330336
'parent' => 'user-actions',
331337
'id' => 'user-info',
332338
'title' => $user_info,
333339
'href' => $profile_url,
334-
'meta' => array(
335-
'tabindex' => -1,
336-
),
337340
)
338341
);
339342

340-
if ( false !== $profile_url ) {
341-
$wp_admin_bar->add_node(
342-
array(
343-
'parent' => 'user-actions',
344-
'id' => 'edit-profile',
345-
'title' => __( 'Edit Profile' ),
346-
'href' => $profile_url,
347-
)
348-
);
349-
}
350-
351343
$wp_admin_bar->add_node(
352344
array(
353345
'parent' => 'user-actions',
@@ -397,6 +389,9 @@ function wp_admin_bar_site_menu( $wp_admin_bar ) {
397389
'id' => 'site-name',
398390
'title' => $title,
399391
'href' => ( is_admin() || ! current_user_can( 'read' ) ) ? home_url( '/' ) : admin_url(),
392+
'meta' => array(
393+
'menu_title' => $title,
394+
),
400395
)
401396
);
402397

@@ -994,6 +989,9 @@ function wp_admin_bar_new_content_menu( $wp_admin_bar ) {
994989
'id' => 'new-content',
995990
'title' => $title,
996991
'href' => admin_url( current( array_keys( $actions ) ) ),
992+
'meta' => array(
993+
'menu_title' => _x( 'New', 'admin bar menu group label' ),
994+
),
997995
)
998996
);
999997

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public function remove_menu( $id ) {
107107
*
108108
* @since 3.1.0
109109
* @since 4.5.0 Added the ability to pass 'lang' and 'dir' meta data.
110+
* @since 6.5.0 Added the ability to pass 'menu_title' for an ARIA menu name.
110111
*
111112
* @param array $args {
112113
* Arguments for adding a node.
@@ -117,7 +118,7 @@ public function remove_menu( $id ) {
117118
* @type string $href Optional. Link for the item.
118119
* @type bool $group Optional. Whether or not the node is a group. Default false.
119120
* @type array $meta Meta data including the following keys: 'html', 'class', 'rel', 'lang', 'dir',
120-
* 'onclick', 'target', 'title', 'tabindex'. Default empty.
121+
* 'onclick', 'target', 'title', 'tabindex', 'menu_title'. Default empty.
121122
* }
122123
*/
123124
public function add_node( $args ) {
@@ -478,9 +479,6 @@ final protected function _render( $root ) {
478479
}
479480
?>
480481
</div>
481-
<?php if ( is_user_logged_in() ) : ?>
482-
<a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e( 'Log Out' ); ?></a>
483-
<?php endif; ?>
484482
</div>
485483

486484
<?php
@@ -505,10 +503,12 @@ final protected function _render_container( $node ) {
505503

506504
/**
507505
* @since 3.3.0
506+
* @since 6.5.0 Added `$menu_title` parameter to allow an ARIA menu name.
508507
*
509508
* @param object $node
509+
* @param string|bool $menu_title The accessible name of this aria menu or false if not provided.
510510
*/
511-
final protected function _render_group( $node ) {
511+
final protected function _render_group( $node, $menu_title = false ) {
512512
if ( 'container' === $node->type ) {
513513
$this->_render_container( $node );
514514
return;
@@ -523,7 +523,11 @@ final protected function _render_group( $node ) {
523523
$class = '';
524524
}
525525

526-
echo "<ul id='" . esc_attr( 'wp-admin-bar-' . $node->id ) . "'$class>";
526+
if ( empty( $menu_title ) ) {
527+
echo "<ul role='menu' id='" . esc_attr( 'wp-admin-bar-' . $node->id ) . "'$class>";
528+
} else {
529+
echo "<ul role='menu' aria-label='" . esc_attr( $menu_title ) . "' id='" . esc_attr( 'wp-admin-bar-' . $node->id ) . "'$class>";
530+
}
527531
foreach ( $node->children as $item ) {
528532
$this->_render_item( $item );
529533
}
@@ -546,15 +550,16 @@ final protected function _render_item( $node ) {
546550
$is_top_secondary_item = 'top-secondary' === $node->parent;
547551

548552
// Allow only numeric values, then casted to integers, and allow a tabindex value of `0` for a11y.
549-
$tabindex = ( isset( $node->meta['tabindex'] ) && is_numeric( $node->meta['tabindex'] ) ) ? (int) $node->meta['tabindex'] : '';
550-
$aria_attributes = ( '' !== $tabindex ) ? ' tabindex="' . $tabindex . '"' : '';
553+
$tabindex = ( isset( $node->meta['tabindex'] ) && is_numeric( $node->meta['tabindex'] ) ) ? (int) $node->meta['tabindex'] : '';
554+
$aria_attributes = ( '' !== $tabindex ) ? ' tabindex="' . $tabindex . '"' : '';
555+
$aria_attributes .= ' role="menuitem"';
551556

552557
$menuclass = '';
553558
$arrow = '';
554559

555560
if ( $is_parent ) {
556561
$menuclass = 'menupop ';
557-
$aria_attributes .= ' aria-haspopup="true"';
562+
$aria_attributes .= ' aria-expanded="false"';
558563
}
559564

560565
if ( ! empty( $node->meta['class'] ) ) {
@@ -603,7 +608,11 @@ final protected function _render_item( $node ) {
603608
if ( $is_parent ) {
604609
echo '<div class="ab-sub-wrapper">';
605610
foreach ( $node->children as $group ) {
606-
$this->_render_group( $group );
611+
if ( empty( $node->meta['menu_title'] ) ) {
612+
$this->_render_group( $group, false );
613+
} else {
614+
$this->_render_group( $group, $node->meta['menu_title'] );
615+
}
607616
}
608617
echo '</div>';
609618
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,11 @@ html:lang(he-il) .rtl #wpadminbar * {
447447
background: none;
448448
}
449449

450+
#wpadminbar #wp-admin-bar-user-info a {
451+
display: grid;
452+
row-gap: 12px;
453+
}
454+
450455
#wp-admin-bar-user-info .avatar {
451456
position: absolute;
452457
left: -72px;

tests/phpunit/tests/adminbar.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,13 @@ public function test_admin_bar_contains_correct_links_for_users_with_no_role() {
9797
$node_site_name = $wp_admin_bar->get_node( 'site-name' );
9898
$node_my_account = $wp_admin_bar->get_node( 'my-account' );
9999
$node_user_info = $wp_admin_bar->get_node( 'user-info' );
100-
$node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' );
101100

102101
// Site menu points to the home page instead of the admin URL.
103102
$this->assertSame( home_url( '/' ), $node_site_name->href );
104103

105104
// No profile links as the user doesn't have any permissions on the site.
106105
$this->assertFalse( $node_my_account->href );
107106
$this->assertFalse( $node_user_info->href );
108-
$this->assertNull( $node_edit_profile );
109107
}
110108

111109
/**
@@ -119,10 +117,9 @@ public function test_admin_bar_contains_correct_links_for_users_with_role() {
119117

120118
$wp_admin_bar = $this->get_standard_admin_bar();
121119

122-
$node_site_name = $wp_admin_bar->get_node( 'site-name' );
123-
$node_my_account = $wp_admin_bar->get_node( 'my-account' );
124-
$node_user_info = $wp_admin_bar->get_node( 'user-info' );
125-
$node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' );
120+
$node_site_name = $wp_admin_bar->get_node( 'site-name' );
121+
$node_my_account = $wp_admin_bar->get_node( 'my-account' );
122+
$node_user_info = $wp_admin_bar->get_node( 'user-info' );
126123

127124
// Site menu points to the admin URL.
128125
$this->assertSame( admin_url( '/' ), $node_site_name->href );
@@ -132,7 +129,6 @@ public function test_admin_bar_contains_correct_links_for_users_with_role() {
132129
// Profile URLs point to profile.php.
133130
$this->assertSame( $profile_url, $node_my_account->href );
134131
$this->assertSame( $profile_url, $node_user_info->href );
135-
$this->assertSame( $profile_url, $node_edit_profile->href );
136132
}
137133

138134
/**
@@ -162,7 +158,6 @@ public function test_admin_bar_contains_correct_links_for_users_with_no_role_on_
162158
$node_site_name = $wp_admin_bar->get_node( 'site-name' );
163159
$node_my_account = $wp_admin_bar->get_node( 'my-account' );
164160
$node_user_info = $wp_admin_bar->get_node( 'user-info' );
165-
$node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' );
166161

167162
// Get primary blog.
168163
$primary = get_active_blog_for_user( self::$editor_id );
@@ -179,7 +174,6 @@ public function test_admin_bar_contains_correct_links_for_users_with_no_role_on_
179174
// Profile URLs should go to the user's primary blog.
180175
$this->assertSame( $primary_profile_url, $node_my_account->href );
181176
$this->assertSame( $primary_profile_url, $node_user_info->href );
182-
$this->assertSame( $primary_profile_url, $node_edit_profile->href );
183177

184178
restore_current_blog();
185179
}
@@ -218,7 +212,6 @@ public function test_admin_bar_contains_correct_links_for_users_with_no_role_on_
218212
$node_site_name = $wp_admin_bar->get_node( 'site-name' );
219213
$node_my_account = $wp_admin_bar->get_node( 'my-account' );
220214
$node_user_info = $wp_admin_bar->get_node( 'user-info' );
221-
$node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' );
222215

223216
// Get primary blog.
224217
$primary = get_active_blog_for_user( self::$no_role_id );
@@ -234,7 +227,6 @@ public function test_admin_bar_contains_correct_links_for_users_with_no_role_on_
234227
// Profile URLs should go to the user's primary blog.
235228
$this->assertSame( $user_profile_url, $node_my_account->href );
236229
$this->assertSame( $user_profile_url, $node_user_info->href );
237-
$this->assertSame( $user_profile_url, $node_edit_profile->href );
238230

239231
restore_current_blog();
240232
}
@@ -284,63 +276,63 @@ public function data_admin_bar_nodes_with_tabindex_meta() {
284276
array(
285277
'id' => 'test-node',
286278
),
287-
'<div class="ab-item ab-empty-item">',
279+
'<div class="ab-item ab-empty-item" role="menuitem">',
288280
),
289281
array(
290282
// Empty string.
291283
array(
292284
'id' => 'test-node',
293285
'meta' => array( 'tabindex' => '' ),
294286
),
295-
'<div class="ab-item ab-empty-item">',
287+
'<div class="ab-item ab-empty-item" role="menuitem">',
296288
),
297289
array(
298290
// Integer 1 as string.
299291
array(
300292
'id' => 'test-node',
301293
'meta' => array( 'tabindex' => '1' ),
302294
),
303-
'<div class="ab-item ab-empty-item" tabindex="1">',
295+
'<div class="ab-item ab-empty-item" tabindex="1" role="menuitem">',
304296
),
305297
array(
306298
// Integer -1 as string.
307299
array(
308300
'id' => 'test-node',
309301
'meta' => array( 'tabindex' => '-1' ),
310302
),
311-
'<div class="ab-item ab-empty-item" tabindex="-1">',
303+
'<div class="ab-item ab-empty-item" tabindex="-1" role="menuitem">',
312304
),
313305
array(
314306
// Integer 0 as string.
315307
array(
316308
'id' => 'test-node',
317309
'meta' => array( 'tabindex' => '0' ),
318310
),
319-
'<div class="ab-item ab-empty-item" tabindex="0">',
311+
'<div class="ab-item ab-empty-item" tabindex="0" role="menuitem">',
320312
),
321313
array(
322314
// Integer, 0.
323315
array(
324316
'id' => 'test-node',
325317
'meta' => array( 'tabindex' => 0 ),
326318
),
327-
'<div class="ab-item ab-empty-item" tabindex="0">',
319+
'<div class="ab-item ab-empty-item" tabindex="0" role="menuitem">',
328320
),
329321
array(
330322
// Integer, 2.
331323
array(
332324
'id' => 'test-node',
333325
'meta' => array( 'tabindex' => 2 ),
334326
),
335-
'<div class="ab-item ab-empty-item" tabindex="2">',
327+
'<div class="ab-item ab-empty-item" tabindex="2" role="menuitem">',
336328
),
337329
array(
338330
// Boolean, false.
339331
array(
340332
'id' => 'test-node',
341333
'meta' => array( 'tabindex' => false ),
342334
),
343-
'<div class="ab-item ab-empty-item">',
335+
'<div class="ab-item ab-empty-item" role="menuitem">',
344336
),
345337
);
346338
}

0 commit comments

Comments
 (0)