Make WordPress Core


Ignore:
Timestamp:
01/11/2026 06:34:57 AM (2 months ago)
Author:
westonruter
Message:

Themes: Use WP_HTML_Tag_Processor to insert the block template skip link instead of JavaScript.

  • The skip link now works when JavaScript is turned off.
  • By removing the script, the amount of JavaScript sent to the client is reduced for a very marginal performance improvement.
  • A new wp-block-template-skip-link stylesheet is registered, with minification and path data for inlining.
  • The CSS for the skip link now has an RTL version generated, although it is not yet served when the styles are inlined. See #61625.
  • The wp_enqueue_block_template_skip_link() function now exclusively enqueues the stylesheet since the script is removed.
  • For backwards-compatibility, the skip link will continue to be omitted if the_block_template_skip_link() is unhooked from the wp_footer action or wp_enqueue_block_template_skip_link() is unhooked from wp_enqueue_scripts.

Developed in https://github.com/WordPress/wordpress-develop/pull/10676

Follow-up to [56932], [51003].

Props rutviksavsani, westonruter, dmsnell, whiteshadow01, Slieptsov.
See #59505, #53176.
Fixes #64361.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/block-template.php

    r61090 r61469  
    313313
    314314    /**
     315     * Tests that `get_the_block_template_html()` adds a skip link when a MAIN element is present.
     316     *
     317     * @ticket 64361
     318     * @covers ::get_the_block_template_html
     319     */
     320    public function test_get_the_block_template_html_adds_skip_link_when_main_present() {
     321        global $_wp_current_template_id, $_wp_current_template_content;
     322
     323        $_wp_current_template_id      = get_stylesheet() . '//index';
     324        $_wp_current_template_content = '<main>Content</main>';
     325
     326        $processor = new WP_HTML_Tag_Processor( get_the_block_template_html() );
     327        $this->assertTrue(
     328            $processor->next_tag(
     329                array(
     330                    'tag_name'   => 'A',
     331                    'class_name' => 'skip-link',
     332                )
     333            ),
     334            'Expected skip link was not added to the block template HTML.'
     335        );
     336        $this->assertSame( 'wp-skip-link', $processor->get_attribute( 'id' ), 'Unexpected ID on skip link.' );
     337        $this->assertTrue( $processor->has_class( 'screen-reader-text' ), 'Expected "screen-reader-text" class on skip link.' );
     338    }
     339
     340    /**
     341     * Tests that `get_the_block_template_html()` does not add a skip link when the skip-link action is unhooked.
     342     *
     343     * @ticket 64361
     344     * @covers ::get_the_block_template_html
     345     *
     346     * @dataProvider data_provider_skip_link_actions
     347     */
     348    public function test_get_the_block_template_html_does_not_add_skip_link_when_action_unhooked( string $action, string $callback ) {
     349        global $_wp_current_template_id, $_wp_current_template_content;
     350
     351        $_wp_current_template_id      = get_stylesheet() . '//index';
     352        $_wp_current_template_content = '<main>Content</main>';
     353
     354        remove_action( $action, $callback );
     355
     356        $processor = new WP_HTML_Tag_Processor( get_the_block_template_html() );
     357        $this->assertFalse(
     358            $processor->next_tag(
     359                array(
     360                    'tag_name'   => 'A',
     361                    'class_name' => 'skip-link',
     362                )
     363            ),
     364            'Unexpected skip link was added to the block template HTML when the action was unhooked.'
     365        );
     366    }
     367
     368    /**
     369     * Data provider for test_get_the_block_template_html_does_not_add_skip_link_when_action_unhooked.
     370     *
     371     * @return array<string, array<string, string>>
     372     */
     373    public function data_provider_skip_link_actions(): array {
     374        return array(
     375            'the_block_template_skip_link'        => array(
     376                'action'   => 'wp_footer',
     377                'callback' => 'the_block_template_skip_link',
     378            ),
     379            'wp_enqueue_block_template_skip_link' => array(
     380                'action'   => 'wp_enqueue_scripts',
     381                'callback' => 'wp_enqueue_block_template_skip_link',
     382            ),
     383        );
     384    }
     385
     386    /**
    315387     * @ticket 58319
    316388     *
Note: See TracChangeset for help on using the changeset viewer.