Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
dbcd32c
Output script modules with fetchpriority=low
westonruter May 19, 2025
5207c27
Fix duplicate array keys in data_special_chars_script_encoding
westonruter May 19, 2025
088366d
Add ability to set fetch priority for script modules
westonruter May 19, 2025
9eab85d
Add fetchpriority support for non-module scripts
westonruter May 19, 2025
d9c4037
Set fetchpriority=low on comment-reply script
westonruter May 19, 2025
4f82a4f
Use auto as default fetchpriority for script modules
westonruter May 19, 2025
f2cd9be
Avoid printing fetchpriority attribute when auto
westonruter May 19, 2025
ff4fd36
fixup! Use auto as default fetchpriority for script modules
westonruter May 19, 2025
9edbe4c
Use HTML Tag Processor to parse import map
westonruter May 19, 2025
021fe74
Ensure parity in args between class methods and global function aliases
westonruter May 19, 2025
34ef7fa
Use fetch priority low by default for Interactivity API view script m…
westonruter May 20, 2025
e54274a
Add missing since tag
westonruter May 21, 2025
cc1d909
Account for full block.json schema when checking for interactivity
westonruter May 21, 2025
4197eb2
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Jun 20, 2025
74a49e1
Remove PHPStan annotations for commit
westonruter Jun 20, 2025
75fb879
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Jul 29, 2025
0e9994a
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Aug 26, 2025
06e9cac
Remove TODO comments which have been filed in https://github.com/Word…
westonruter Aug 26, 2025
f91c61d
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Sep 1, 2025
4596644
Add validation for fetchpriority set on scripts
westonruter Sep 2, 2025
2172e9e
Allow empty string for set_fetchpriority(), return bool, and add tests
westonruter Sep 2, 2025
535669d
Add missing type for args param
westonruter Sep 2, 2025
fe366e2
Improve typing for is_delayed_stragegy
westonruter Sep 2, 2025
f704f5e
Merge branch 'trunk' of https://github.com/WordPress/wordpress-develo…
westonruter Sep 2, 2025
38150dc
Replace n.e.x.t with 6.9.0
westonruter Sep 2, 2025
f05ca72
Ensure reflection property is accessible
westonruter Sep 2, 2025
607ce0c
Use static data provider methods
westonruter Sep 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add validation for fetchpriority set on scripts
Co-authored-by: sirreal <[email protected]>
  • Loading branch information
westonruter and sirreal committed Sep 2, 2025
commit 4596644185bd4e692ade9d0dc9ce8ef05d69b15f
43 changes: 42 additions & 1 deletion src/wp-includes/class-wp-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public function do_item( $handle, $group = false ) {
if ( $intended_strategy ) {
$attr['data-wp-strategy'] = $intended_strategy;
}
if ( isset( $obj->extra['fetchpriority'] ) && 'auto' !== $obj->extra['fetchpriority'] ) {
if ( isset( $obj->extra['fetchpriority'] ) && 'auto' !== $obj->extra['fetchpriority'] && $this->is_valid_fetchpriority( $obj->extra['fetchpriority'] ) ) {
$attr['fetchpriority'] = $obj->extra['fetchpriority'];
}
$tag = $translations . $ie_conditional_prefix . $before_script;
Expand Down Expand Up @@ -834,6 +834,35 @@ public function add_data( $handle, $key, $value ) {
);
return false;
}
} elseif ( 'fetchpriority' === $key ) {
if ( empty( $value ) ) {
$value = 'auto';
}
if ( ! $this->is_valid_fetchpriority( $value ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: $fetchpriority, 2: $handle */
__( 'Invalid fetchpriority `%1$s` defined for `%2$s` during script registration.' ),
is_string( $value ) ? $value : gettype( $value ),
$handle
),
'n.e.x.t'
);
return false;
} elseif ( ! $this->registered[ $handle ]->src ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: 1: $fetchpriority, 2: $handle */
__( 'Cannot supply a fetchpriority `%1$s` for script `%2$s` because it is an alias (it lacks a `src` value).' ),
is_string( $value ) ? $value : gettype( $value ),
$handle
),
'n.e.x.t'
);
return false;
}
}
return parent::add_data( $handle, $key, $value );
}
Expand Down Expand Up @@ -883,6 +912,18 @@ private function is_delayed_strategy( $strategy ) {
);
}

/**
* Checks if the provided fetchpriority is valid.
*
* @since n.e.x.t
*
* @param string|mixed $priority Fetch priority.
* @return bool Whether valid fetchpriority.
*/
private function is_valid_fetchpriority( $priority ): bool {
return in_array( $priority, array( 'auto', 'low', 'high' ), true );
}

/**
* Gets the best eligible loading strategy for a script.
*
Expand Down
102 changes: 102 additions & 0 deletions tests/phpunit/tests/dependencies/scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,108 @@ public function test_defer_with_async_dependent() {
$this->assertEqualHTML( $expected, $output, '<body>', 'Scripts registered as defer but that have dependents that are async are expected to have said dependents deferred.' );
}

/**
* Data provider for test_fetchpriority_values.
*
* @return array<string, array{fetchpriority: string}>
*/
public function data_provider_fetchpriority_values(): array {
return array(
'auto' => array( 'fetchpriority' => 'auto' ),
'low' => array( 'fetchpriority' => 'low' ),
'high' => array( 'fetchpriority' => 'high' ),
);
}

/**
* Tests that valid fetchpriority values are correctly added to script data.
*
* @ticket 61734
*
* @covers ::wp_register_script
* @covers WP_Scripts::add_data
* @covers ::wp_script_add_data
*
* @dataProvider data_provider_fetchpriority_values
*
* @param string $fetchpriority The fetchpriority value to test.
*/
public function test_fetchpriority_values( string $fetchpriority ) {
wp_register_script( 'test-script', '/test-script.js', array(), null, array( 'fetchpriority' => $fetchpriority ) );
$this->assertArrayHasKey( 'fetchpriority', wp_scripts()->registered['test-script']->extra );
$this->assertSame( $fetchpriority, wp_scripts()->registered['test-script']->extra['fetchpriority'] );

wp_register_script( 'test-script-2', '/test-script-2.js' );
$this->assertTrue( wp_script_add_data( 'test-script-2', 'fetchpriority', $fetchpriority ) );
$this->assertArrayHasKey( 'fetchpriority', wp_scripts()->registered['test-script-2']->extra );
$this->assertSame( $fetchpriority, wp_scripts()->registered['test-script-2']->extra['fetchpriority'] );
}

/**
* Tests that an empty fetchpriority is treated the same as auto.
*
* @ticket 61734
*
* @covers ::wp_register_script
* @covers WP_Scripts::add_data
*/
public function test_empty_fetchpriority_value() {
wp_register_script( 'unset', '/joke.js', array(), null, array( 'fetchpriority' => 'low' ) );
$this->assertSame( 'low', wp_scripts()->registered['unset']->extra['fetchpriority'] );
$this->assertTrue( wp_script_add_data( 'unset', 'fetchpriority', null ) );
$this->assertSame( 'auto', wp_scripts()->registered['unset']->extra['fetchpriority'] );
}

/**
* Tests that an invalid fetchpriority causes a _doing_it_wrong() warning.
*
* @ticket 61734
*
* @covers ::wp_register_script
* @covers WP_Scripts::add_data
*
* @expectedIncorrectUsage WP_Scripts::add_data
*/
public function test_invalid_fetchpriority_value() {
wp_register_script( 'joke', '/joke.js', array(), null, array( 'fetchpriority' => 'silly' ) );
$this->assertArrayNotHasKey( 'fetchpriority', wp_scripts()->registered['joke']->extra );
$this->assertArrayHasKey( 'WP_Scripts::add_data', $this->caught_doing_it_wrong );
$this->assertStringContainsString( 'Invalid fetchpriority `silly`', $this->caught_doing_it_wrong['WP_Scripts::add_data'] );
}

/**
* Tests that an invalid fetchpriority causes a _doing_it_wrong() warning.
*
* @ticket 61734
*
* @covers ::wp_register_script
* @covers WP_Scripts::add_data
*
* @expectedIncorrectUsage WP_Scripts::add_data
*/
public function test_invalid_fetchpriority_value_type() {
wp_register_script( 'bad', '/bad.js' );
$this->assertFalse( wp_script_add_data( 'bad', 'fetchpriority', array( 'THIS IS SO WRONG!!!' ) ) );
$this->assertArrayNotHasKey( 'fetchpriority', wp_scripts()->registered['bad']->extra );
$this->assertArrayHasKey( 'WP_Scripts::add_data', $this->caught_doing_it_wrong );
$this->assertStringContainsString( 'Invalid fetchpriority `array`', $this->caught_doing_it_wrong['WP_Scripts::add_data'] );
}

/**
* Tests that adding fetchpriority causes a _doing_it_wrong() warning on a script alias.
*
* @ticket 61734
*
* @covers ::wp_register_script
* @covers WP_Scripts::add_data
*
* @expectedIncorrectUsage WP_Scripts::add_data
*/
public function test_invalid_fetchpriority_on_alias() {
wp_register_script( 'alias', false, array(), null, array( 'fetchpriority' => 'low' ) );
$this->assertArrayNotHasKey( 'fetchpriority', wp_scripts()->registered['alias']->extra );
}

/**
* Tests that scripts registered as defer become blocking when their dependents chain are all blocking.
*
Expand Down