Skip to content

Commit d2fc75d

Browse files
committed
Toolbar: A11y: Add skip link to bypass the toolbar.
When the admin bar is rendered in the footer, a link is added to skip to the toolbar. However, if it is rendered at `wp_body_open`, it injects significant repeated content at the beginning of the page with no bypass mechanism. Add a skip link that targets a point after the end of the admin bar when it is rendered during `wp_body_open`, allowing users to bypass the navigation, supporting WCAG 2.4.1: Bypass Blocks. Developed in #14410. Props travisjeans, rishabhwp, ravi patel, joedolson. Fixes #66331. git-svn-id: https://develop.svn.wordpress.org/trunk@65325 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 12b8e41 commit d2fc75d

2 files changed

Lines changed: 101 additions & 1 deletion

File tree

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ final protected function _bind() {
460460

461461
/**
462462
* @since 3.3.0
463+
* @since 7.2.0 Added a link to skip the toolbar when it is rendered at the start of the page.
463464
*
464465
* @param object $root
465466
*/
@@ -473,10 +474,19 @@ final protected function _render( $root ) {
473474
$class .= ' mobile';
474475
}
475476

477+
/*
478+
* On the front end, add a link to reach the toolbar when it is rendered in the footer
479+
* by themes without 'wp_body_open' support, or to skip it when it precedes the content.
480+
*/
481+
$skip_to_toolbar = ! is_admin() && ! did_action( 'wp_body_open' );
482+
$skip_past_toolbar = ! is_admin() && did_action( 'wp_body_open' ) && ! doing_action( 'wp_footer' );
483+
476484
?>
477485
<div id="wpadminbar" class="<?php echo $class; ?>">
478-
<?php if ( ! is_admin() && ! did_action( 'wp_body_open' ) ) { ?>
486+
<?php if ( $skip_to_toolbar ) { ?>
479487
<a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e( 'Skip to toolbar' ); ?></a>
488+
<?php } elseif ( $skip_past_toolbar ) { ?>
489+
<a class="screen-reader-shortcut" href="#wp-toolbar-skip-target"><?php _e( 'Skip toolbar' ); ?></a>
480490
<?php } ?>
481491
<div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e( 'Toolbar' ); ?>">
482492
<?php
@@ -485,6 +495,9 @@ final protected function _render( $root ) {
485495
}
486496
?>
487497
</div>
498+
<?php if ( $skip_past_toolbar ) { ?>
499+
<div id="wp-toolbar-skip-target"></div>
500+
<?php } ?>
488501
</div>
489502

490503
<?php

‎tests/phpunit/tests/adminbar.php‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,93 @@ public function test_my_account_escapes_display_name_and_user_login() {
990990
$this->assertStringContainsString( '&lt;b&gt;login&lt;/b&gt;', $node_user_info->title );
991991
}
992992

993+
/**
994+
* Tests that the toolbar has a link to skip it when rendered on 'wp_body_open'.
995+
*
996+
* @ticket 66331
997+
*
998+
* @covers WP_Admin_Bar::render
999+
*/
1000+
public function test_render_outputs_skip_toolbar_link_when_rendered_on_wp_body_open() {
1001+
$admin_bar_html = $this->render_admin_bar_on_action( 'wp_body_open' );
1002+
1003+
$processor = new WP_HTML_Tag_Processor( $admin_bar_html );
1004+
$processor->next_tag( array( 'tag_name' => 'A' ) );
1005+
$this->assertSame( '#wp-toolbar-skip-target', $processor->get_attribute( 'href' ), 'The "Skip toolbar" link should be the first link in the toolbar.' );
1006+
$this->assertStringContainsString( '>Skip toolbar</a>', $admin_bar_html, 'The "Skip toolbar" link text should be output.' );
1007+
1008+
$this->assertGreaterThan(
1009+
strrpos( $admin_bar_html, 'ab-item' ),
1010+
strpos( $admin_bar_html, '<div id="wp-toolbar-skip-target"></div>' ),
1011+
'The "Skip toolbar" link target should follow all toolbar items.'
1012+
);
1013+
}
1014+
1015+
/**
1016+
* Tests that the toolbar has no link to skip it when it doesn't precede the page content.
1017+
*
1018+
* @ticket 66331
1019+
*
1020+
* @covers WP_Admin_Bar::render
1021+
*
1022+
* @dataProvider data_render_does_not_output_skip_toolbar_link
1023+
*
1024+
* @param bool $is_admin Whether the toolbar is rendered in the admin.
1025+
* @param bool $fire_wp_body_open Whether the 'wp_body_open' action fires before the toolbar is rendered.
1026+
* @param string $hook_name The action on which the toolbar is rendered.
1027+
*/
1028+
public function test_render_does_not_output_skip_toolbar_link( $is_admin, $fire_wp_body_open, $hook_name ) {
1029+
if ( $is_admin ) {
1030+
set_current_screen( 'dashboard' );
1031+
}
1032+
1033+
if ( $fire_wp_body_open ) {
1034+
remove_all_actions( 'wp_body_open' );
1035+
do_action( 'wp_body_open' );
1036+
}
1037+
1038+
$admin_bar_html = $this->render_admin_bar_on_action( $hook_name );
1039+
1040+
$this->assertStringNotContainsString( 'wp-toolbar-skip-target', $admin_bar_html, 'The "Skip toolbar" link and its target should not be output.' );
1041+
}
1042+
1043+
/**
1044+
* Data provider.
1045+
*
1046+
* @return array[]
1047+
*/
1048+
public static function data_render_does_not_output_skip_toolbar_link() {
1049+
return array(
1050+
'theme without wp_body_open support' => array( false, false, 'wp_footer' ),
1051+
'toolbar moved to the footer' => array( false, true, 'wp_footer' ),
1052+
'admin' => array( true, true, 'in_admin_header' ),
1053+
);
1054+
}
1055+
1056+
/**
1057+
* Renders a toolbar with a single node while the given action is running.
1058+
*
1059+
* @param string $hook_name The action on which to render the toolbar.
1060+
* @return string The toolbar HTML.
1061+
*/
1062+
private function render_admin_bar_on_action( $hook_name ) {
1063+
$admin_bar = new WP_Admin_Bar();
1064+
$admin_bar->add_node( array( 'id' => 'test-node' ) );
1065+
1066+
$admin_bar_html = '';
1067+
1068+
remove_all_actions( $hook_name );
1069+
add_action(
1070+
$hook_name,
1071+
static function () use ( $admin_bar, &$admin_bar_html ) {
1072+
$admin_bar_html = get_echo( array( $admin_bar, 'render' ) );
1073+
}
1074+
);
1075+
do_action( $hook_name );
1076+
1077+
return $admin_bar_html;
1078+
}
1079+
9931080
/**
9941081
* This test ensures that WP_Admin_Bar::$proto is not defined (including magic methods).
9951082
*

0 commit comments

Comments
 (0)