Skip to content

Commit 4d19161

Browse files
committed
General: Improve aria-current management in get_custom_logo().
This changeset fixes a edge case in `get_custom_logo()` where a page was set for the homepage without any front page in Settings > Reading and the `aria-current` attribute wasn't present on the logo link. Props bschneidewind, audrasjb, siliconforks, sabernhardt, faisal03, shailu25, peterwilsoncc. Fixes #62879. git-svn-id: https://develop.svn.wordpress.org/trunk@60062 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1563bf7 commit 4d19161

2 files changed

Lines changed: 164 additions & 1 deletion

File tree

src/wp-includes/general-template.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ function get_custom_logo( $blog_id = 0 ) {
11181118
$image
11191119
);
11201120
} else {
1121-
$aria_current = is_front_page() && ! is_paged() ? ' aria-current="page"' : '';
1121+
$aria_current = ! is_paged() && ( is_front_page() || is_home() && ( (int) get_option( 'page_for_posts' ) !== get_queried_object_id() ) ) ? ' aria-current="page"' : '';
11221122

11231123
$html = sprintf(
11241124
'<a href="%1$s" class="custom-logo-link" rel="home"%2$s>%3$s</a>',

tests/phpunit/tests/general/template.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ class Tests_General_Template extends WP_UnitTestCase {
1818
public $custom_logo_id;
1919
public $custom_logo_url;
2020

21+
/**
22+
* Blog page used by aria tests.
23+
*
24+
* @var int
25+
*/
26+
public static $blog_page_id;
27+
28+
/**
29+
* Home page used by aria tests.
30+
*
31+
* @var int
32+
*/
33+
public static $home_page_id;
34+
2135
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
2236
/*
2337
* Declare theme support for custom logo.
@@ -33,6 +47,22 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
3347
* remove_filter( 'pre_set_theme_mod_custom_logo', '_sync_custom_logo_to_site_logo' );
3448
*/
3549
add_theme_support( 'custom-logo' );
50+
51+
self::$blog_page_id = self::factory()->post->create(
52+
array(
53+
'post_type' => 'page',
54+
'post_title' => 'Blog',
55+
'page_name' => 'blog',
56+
)
57+
);
58+
59+
self::$home_page_id = self::factory()->post->create(
60+
array(
61+
'post_type' => 'page',
62+
'post_title' => 'Home',
63+
'page_name' => 'home',
64+
)
65+
);
3666
}
3767

3868
public static function wpTearDownAfterClass() {
@@ -522,6 +552,139 @@ public function test_get_custom_logo_preserves_switched_state() {
522552
$this->assertSame( $expected, $result );
523553
}
524554

555+
/**
556+
* Test the aria attribute for the custom logo on the front page set to the blog.
557+
*
558+
* @ticket 62879
559+
*
560+
* @covers ::get_custom_logo
561+
*
562+
* @dataProvider data_get_custom_logo_aria_current_attribute_blog_front_page
563+
*
564+
* @param string $url The URL to visit.
565+
* @param bool $attribute_expected Whether the aria-current attribute is expected.
566+
*/
567+
public function test_get_custom_logo_aria_current_attribute_blog_front_page( $url, $attribute_expected ) {
568+
// Set the custom logo.
569+
$this->set_custom_logo();
570+
$this->go_to( $url );
571+
572+
$this->assertNotEmpty( get_custom_logo(), 'Custom logo is expected to be set' );
573+
574+
if ( $attribute_expected ) {
575+
$this->assertStringContainsString( 'aria-current="page"', get_custom_logo(), 'Custom logo is expected to contain aria-current attribute' );
576+
} else {
577+
$this->assertStringNotContainsString( 'aria-current="page"', get_custom_logo(), 'Custom logo is expected to contain aria-current attribute' );
578+
}
579+
}
580+
581+
/**
582+
* Data provider for the test_get_custom_logo_aria_current_attribute_blog_front_page.
583+
*
584+
* @return array[]
585+
*/
586+
public function data_get_custom_logo_aria_current_attribute_blog_front_page() {
587+
return array(
588+
'Front page' => array( home_url(), true ),
589+
'Blog post' => array( home_url( '/?p=1' ), false ),
590+
'Sample page' => array( home_url( '/?page_id=2' ), false ),
591+
);
592+
}
593+
594+
/**
595+
* Test the aria attribute for the custom logo on the front page set to the blog.
596+
*
597+
* @ticket 62879
598+
*
599+
* @covers ::get_custom_logo
600+
*
601+
* @dataProvider data_get_custom_logo_aria_current_attribute_blog_set_to_page_without_front_page_defined
602+
* @param string $url The URL to visit.
603+
* @param bool $attribute_expected Whether the aria-current attribute is expected.
604+
*/
605+
public function test_get_custom_logo_aria_current_attribute_blog_set_to_page_without_front_page_defined( $url, $attribute_expected ) {
606+
// Set up pretty permalinks.
607+
update_option( 'permalink_structure', '/%postname%/' );
608+
609+
// Set posts to show on a static page.
610+
update_option( 'show_on_front', 'page' );
611+
update_option( 'page_for_posts', self::$blog_page_id );
612+
613+
// Set the custom logo.
614+
$this->set_custom_logo();
615+
$this->go_to( $url );
616+
617+
$this->assertNotEmpty( get_custom_logo(), 'Custom logo is expected to be set' );
618+
619+
if ( $attribute_expected ) {
620+
$this->assertStringContainsString( 'aria-current="page"', get_custom_logo(), 'Custom logo is expected to contain aria-current attribute' );
621+
} else {
622+
$this->assertStringNotContainsString( 'aria-current="page"', get_custom_logo(), 'Custom logo is expected to contain aria-current attribute' );
623+
}
624+
}
625+
626+
/**
627+
* Data provider for the test_get_custom_logo_aria_current_attribute_blog_set_to_page_without_front_page_defined.
628+
*
629+
* @return array[]
630+
*/
631+
public function data_get_custom_logo_aria_current_attribute_blog_set_to_page_without_front_page_defined() {
632+
return array(
633+
'Front page' => array( home_url(), true ),
634+
'Blog index' => array( home_url( '/blog/' ), true ),
635+
'Blog post' => array( home_url( '/?p=1' ), false ),
636+
'Sample page' => array( home_url( '/?page_id=2' ), false ),
637+
);
638+
}
639+
640+
/**
641+
* Test the aria attribute for the custom logo on the front page set to the blog.
642+
*
643+
* @ticket 62879
644+
*
645+
* @covers ::get_custom_logo
646+
*
647+
* @dataProvider data_get_custom_logo_aria_current_attribute_blog_set_to_page_with_front_page_defined
648+
*
649+
* @param string $url The URL to visit.
650+
* @param bool $attribute_expected Whether the aria-current attribute is expected.
651+
*/
652+
public function test_get_custom_logo_aria_current_attribute_blog_set_to_page_with_front_page_defined( $url, $attribute_expected ) {
653+
// Set up pretty permalinks.
654+
update_option( 'permalink_structure', '/%postname%/' );
655+
656+
// Set posts to show on a static page, show static page on front.
657+
update_option( 'show_on_front', 'page' );
658+
update_option( 'page_for_posts', self::$blog_page_id );
659+
update_option( 'page_on_front', self::$home_page_id );
660+
661+
// Set the custom logo.
662+
$this->set_custom_logo();
663+
$this->go_to( $url );
664+
665+
$this->assertNotEmpty( get_custom_logo(), 'Custom logo is expected to be set' );
666+
667+
if ( $attribute_expected ) {
668+
$this->assertStringContainsString( 'aria-current="page"', get_custom_logo(), 'Custom logo is expected to contain aria-current attribute' );
669+
} else {
670+
$this->assertStringNotContainsString( 'aria-current="page"', get_custom_logo(), 'Custom logo is expected to contain aria-current attribute' );
671+
}
672+
}
673+
674+
/**
675+
* Data provider for the test_get_custom_logo_aria_current_attribute_blog_set_to_page_with_front_page_defined.
676+
*
677+
* @return array[]
678+
*/
679+
public function data_get_custom_logo_aria_current_attribute_blog_set_to_page_with_front_page_defined() {
680+
return array(
681+
'Front page' => array( home_url(), true ),
682+
'Blog index' => array( home_url( '/blog/' ), true ),
683+
'Blog post' => array( home_url( '/?p=1' ), false ),
684+
'Sample page' => array( home_url( '/?page_id=2' ), false ),
685+
);
686+
}
687+
525688
/**
526689
* @ticket 40969
527690
*

0 commit comments

Comments
 (0)