Make WordPress Core

Changeset 60062


Ignore:
Timestamp:
03/21/2025 01:54:38 PM (4 weeks ago)
Author:
audrasjb
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r59966 r60062  
    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(
  • trunk/tests/phpunit/tests/general/template.php

    r56635 r60062  
    1818    public $custom_logo_id;
    1919    public $custom_logo_url;
     20
     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;
    2034
    2135    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     
    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
     
    524554
    525555    /**
     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
     688    /**
    526689     * @ticket 40969
    527690     *
Note: See TracChangeset for help on using the changeset viewer.