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
Allow empty string for set_fetchpriority(), return bool, and add tests
  • Loading branch information
westonruter committed Sep 2, 2025
commit 2172e9e32b528d1b6ab15a64d95872b31a4ca125
22 changes: 16 additions & 6 deletions src/wp-includes/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,12 @@ public function register( string $id, string $src, array $deps = array(), $versi
} else {
_doing_it_wrong(
__METHOD__,
/* translators: %s: Invalid fetchpriority. */
sprintf( __( 'Invalid fetchpriority: %s' ), $args['fetchpriority'] ),
sprintf(
/* translators: 1: $fetchpriority, 2: $id */
__( 'Invalid fetchpriority `%1$s` defined for `%2$s` during script registration.' ),
is_string( $args['fetchpriority'] ) ? $args['fetchpriority'] : gettype( $args['fetchpriority'] ),
$id
),
'n.e.x.t'
);
}
Expand All @@ -130,7 +134,7 @@ public function register( string $id, string $src, array $deps = array(), $versi
*
* @since n.e.x.t
*
* @param mixed $priority Fetch priority.
* @param string|mixed $priority Fetch priority.
* @return bool Whether valid fetchpriority.
*/
private function is_valid_fetchpriority( $priority ): bool {
Expand All @@ -144,10 +148,15 @@ private function is_valid_fetchpriority( $priority ): bool {
*
* @param string $id Script module identifier.
* @param 'auto'|'low'|'high' $priority Fetch priority for the script module.
* @return bool Whether setting the fetchpriority was successful.
*/
public function set_fetchpriority( string $id, string $priority ) {
public function set_fetchpriority( string $id, string $priority ): bool {
if ( ! isset( $this->registered[ $id ] ) ) {
return;
return false;
}

if ( '' === $priority ) {
$priority = 'auto';
}

if ( ! $this->is_valid_fetchpriority( $priority ) ) {
Expand All @@ -157,10 +166,11 @@ public function set_fetchpriority( string $id, string $priority ) {
sprintf( __( 'Invalid fetchpriority: %s' ), $priority ),
'n.e.x.t'
);
return;
return false;
}

$this->registered[ $id ]['fetchpriority'] = $priority;
return true;
}

/**
Expand Down
101 changes: 99 additions & 2 deletions tests/phpunit/tests/script-modules/wpScriptModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function test_comprehensive_methods( bool $use_global_function, bool $onl
// One Dependency.
$register( 'b-dep', '/b-dep.js' );
$register_and_enqueue( 'b', '/b.js', array( 'b-dep' ) );
wp_script_modules()->set_fetchpriority( 'b', 'low' );
$this->assertTrue( wp_script_modules()->set_fetchpriority( 'b', 'low' ) );

// Two dependencies with different formats and a false version.
$register( 'c-dep', '/c-static.js', array(), false, array( 'fetchpriority' => 'low' ) );
Expand Down Expand Up @@ -434,7 +434,7 @@ public function test_wp_enqueue_script_module() {
$this->script_modules->register( 'foo', '/foo.js' );
$this->script_modules->register( 'bar', '/bar.js', array(), false, array( 'fetchpriority' => 'high' ) );
$this->script_modules->register( 'baz', '/baz.js' );
$this->script_modules->set_fetchpriority( 'baz', 'low' );
$this->assertTrue( $this->script_modules->set_fetchpriority( 'baz', 'low' ) );
$this->script_modules->enqueue( 'foo' );
$this->script_modules->enqueue( 'bar' );
$this->script_modules->enqueue( 'baz' );
Expand Down Expand Up @@ -1253,6 +1253,103 @@ function ( $_ ) use ( $data ) {
$this->assertSame( '', $actual );
}

/**
* 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 the registered module.
*
* @ticket 61734
*
* @covers WP_Script_Modules::register
* @covers WP_Script_Modules::set_fetchpriority
*
* @dataProvider data_provider_fetchpriority_values
*
* @param string $fetchpriority The fetchpriority value to test.
*/
public function test_fetchpriority_values( string $fetchpriority ) {
$reflection_class = new ReflectionClass( $this->script_modules );
$registered_property = $reflection_class->getProperty( 'registered' );

$this->script_modules->register( 'test-script', '/test-script.js', array(), null, array( 'fetchpriority' => $fetchpriority ) );
$registered_modules = $registered_property->getValue( $this->script_modules );
$this->assertSame( $fetchpriority, $registered_modules['test-script']['fetchpriority'] );

$this->script_modules->register( 'test-script-2', '/test-script-2.js' );
$this->assertTrue( $this->script_modules->set_fetchpriority( 'test-script-2', $fetchpriority ) );
$registered_modules = $registered_property->getValue( $this->script_modules );
$this->assertSame( $fetchpriority, $registered_modules['test-script-2']['fetchpriority'] );

$this->assertTrue( $this->script_modules->set_fetchpriority( 'test-script-2', '' ) );
$registered_modules = $registered_property->getValue( $this->script_modules );
$this->assertSame( 'auto', $registered_modules['test-script-2']['fetchpriority'] );
}

/**
* Tests that a script module with an invalid fetchpriority value gets a value of auto.
*
* @ticket 61734
*
* @covers WP_Script_Modules::register
* @expectedIncorrectUsage WP_Script_Modules::register
*/
public function test_register_script_module_having_fetchpriority_with_invalid_value() {
$this->script_modules->register( 'foo', '/foo.js', array(), false, array( 'fetchpriority' => 'silly' ) );
$reflection_class = new ReflectionClass( $this->script_modules );
$registered_property = $reflection_class->getProperty( 'registered' );
$registered_modules = $registered_property->getValue( $this->script_modules );
$this->assertSame( 'auto', $registered_modules['foo']['fetchpriority'] );
$this->assertArrayHasKey( 'WP_Script_Modules::register', $this->caught_doing_it_wrong );
$this->assertStringContainsString( 'Invalid fetchpriority `silly`', $this->caught_doing_it_wrong['WP_Script_Modules::register'] );
}

/**
* Tests that a script module with an invalid fetchpriority value type gets a value of auto.
*
* @ticket 61734
*
* @covers WP_Script_Modules::register
* @expectedIncorrectUsage WP_Script_Modules::register
*/
public function test_register_script_module_having_fetchpriority_with_invalid_value_type() {
$this->script_modules->register( 'foo', '/foo.js', array(), false, array( 'fetchpriority' => array( 'WHY AM I NOT A STRING???' ) ) );
$reflection_class = new ReflectionClass( $this->script_modules );
$registered_property = $reflection_class->getProperty( 'registered' );
$registered_modules = $registered_property->getValue( $this->script_modules );
$this->assertSame( 'auto', $registered_modules['foo']['fetchpriority'] );
$this->assertArrayHasKey( 'WP_Script_Modules::register', $this->caught_doing_it_wrong );
$this->assertStringContainsString( 'Invalid fetchpriority `array`', $this->caught_doing_it_wrong['WP_Script_Modules::register'] );
}

/**
* Tests that a setting the fetchpriority for script module with an invalid value is ignored so that it remains auto.
*
* @ticket 61734
*
* @covers WP_Script_Modules::register
* @covers WP_Script_Modules::set_fetchpriority
* @expectedIncorrectUsage WP_Script_Modules::set_fetchpriority
*/
public function test_set_fetchpriority_with_invalid_value() {
$this->script_modules->register( 'foo', '/foo.js' );
$this->script_modules->set_fetchpriority( 'foo', 'silly' );
$reflection_class = new ReflectionClass( $this->script_modules );
$registered_property = $reflection_class->getProperty( 'registered' );
$registered_modules = $registered_property->getValue( $this->script_modules );
$this->assertSame( 'auto', $registered_modules['foo']['fetchpriority'] );
}

/**
* Data provider.
*
Expand Down