Accessibility: use `aria-current` for the paginated post links output by `wp_link_pages()`.

Continues the introduction in core of the aria-current attribute after [41683], [41359], and [41371].

- changes the `wp_link_pages()` (see the `nextpage` quicktag) output to use an `aria-current` attribute on the current item
- adds `post-nav-links` and `post-page-numbers` CSS classes to help themes style these links
- updates the related tests

Props antonioeatgoat, alexstine.
Fixes #41859.


git-svn-id: https://develop.svn.wordpress.org/trunk@42440 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrea Fercia 2018-01-12 20:16:56 +00:00
parent a8d74c4f4e
commit 1258d1d9d2
2 changed files with 15 additions and 9 deletions

View File

@ -862,6 +862,7 @@ function post_password_required( $post = null ) {
* Quicktag one or more times). This tag must be within The Loop.
*
* @since 1.2.0
* @since 5.0.0 Added the `aria_current` argument.
*
* @global int $page
* @global int $numpages
@ -877,6 +878,8 @@ function post_password_required( $post = null ) {
* Also prepended to the current item, which is not linked. Default empty.
* @type string $link_after HTML or text to append to each Pages link inside the `<a>` tag.
* Also appended to the current item, which is not linked. Default empty.
* @type string $aria_current The value for the aria-current attribute. Possible values are 'page',
* 'step', 'location', 'date', 'time', 'true', 'false'. Default is 'page'.
* @type string $next_or_number Indicates whether page numbers should be used. Valid values are number
* and next. Default is 'number'.
* @type string $separator Text between pagination links. Default is ' '.
@ -893,10 +896,11 @@ function wp_link_pages( $args = '' ) {
global $page, $numpages, $multipage, $more;
$defaults = array(
'before' => '<p>' . __( 'Pages:' ),
'before' => '<p class="post-nav-links">' . __( 'Pages:' ),
'after' => '</p>',
'link_before' => '',
'link_after' => '',
'aria_current' => 'page',
'next_or_number' => 'number',
'separator' => ' ',
'nextpagelink' => __( 'Next page' ),
@ -924,6 +928,8 @@ function wp_link_pages( $args = '' ) {
$link = $r['link_before'] . str_replace( '%', $i, $r['pagelink'] ) . $r['link_after'];
if ( $i != $page || ! $more && 1 == $page ) {
$link = _wp_link_page( $i ) . $link . '</a>';
} elseif ( $i === $page ) {
$link = '<span class="post-page-numbers current" aria-current="' . esc_attr( $r['aria_current'] ) . '">' . $link . '</span>';
}
/**
* Filters the HTML output of individual page number links.
@ -1017,7 +1023,7 @@ function _wp_link_page( $i ) {
$url = get_preview_post_link( $post, $query_args, $url );
}
return '<a href="' . esc_url( $url ) . '">';
return '<a href="' . esc_url( $url ) . '" class="post-page-numbers">';
}
//

View File

@ -14,16 +14,16 @@ class Tests_Post_Template extends WP_UnitTestCase {
setup_postdata( get_post( $post_id ) );
$permalink = sprintf( '<a href="%s">', get_permalink() );
$permalink = sprintf( '<a href="%s" class="post-page-numbers">', get_permalink() );
$page2 = _wp_link_page( 2 );
$page3 = _wp_link_page( 3 );
$expected = "<p>Pages: 1 {$page2}2</a> {$page3}3</a></p>";
$expected = '<p class="post-nav-links">Pages: <span class="post-page-numbers current" aria-current="page">1</span> ' . $page2 . '2</a> ' . $page3 . '3</a></p>';
$output = wp_link_pages( array( 'echo' => 0 ) );
$this->assertEquals( $expected, $output );
$before_after = " 1 {$page2}2</a> {$page3}3</a>";
$before_after = " <span class=\"post-page-numbers current\" aria-current=\"page\">1</span> {$page2}2</a> {$page3}3</a>";
$output = wp_link_pages(
array(
'echo' => 0,
@ -34,7 +34,7 @@ class Tests_Post_Template extends WP_UnitTestCase {
$this->assertEquals( $before_after, $output );
$separator = " 1{$page2}2</a>{$page3}3</a>";
$separator = " <span class=\"post-page-numbers current\" aria-current=\"page\">1</span>{$page2}2</a>{$page3}3</a>";
$output = wp_link_pages(
array(
'echo' => 0,
@ -46,7 +46,7 @@ class Tests_Post_Template extends WP_UnitTestCase {
$this->assertEquals( $separator, $output );
$link = " <em>1</em>{$page2}<em>2</em></a>{$page3}<em>3</em></a>";
$link = " <span class=\"post-page-numbers current\" aria-current=\"page\"><em>1</em></span>{$page2}<em>2</em></a>{$page3}<em>3</em></a>";
$output = wp_link_pages(
array(
'echo' => 0,
@ -107,7 +107,7 @@ class Tests_Post_Template extends WP_UnitTestCase {
$this->assertEquals( $next_prev_link, $output );
$GLOBALS['page'] = 1;
$separator = "<p>Pages: 1 | {$page2}2</a> | {$page3}3</a></p>";
$separator = "<p class=\"post-nav-links\">Pages: <span class=\"post-page-numbers current\" aria-current=\"page\">1</span> | {$page2}2</a> | {$page3}3</a></p>";
$output = wp_link_pages(
array(
'echo' => 0,
@ -117,7 +117,7 @@ class Tests_Post_Template extends WP_UnitTestCase {
$this->assertEquals( $separator, $output );
$pagelink = " Page 1 | {$page2}Page 2</a> | {$page3}Page 3</a>";
$pagelink = " <span class=\"post-page-numbers current\" aria-current=\"page\">Page 1</span> | {$page2}Page 2</a> | {$page3}Page 3</a>";
$output = wp_link_pages(
array(
'echo' => 0,