Skip to content

Commit 8defb1f

Browse files
committed
Script Loader: Restore original return value for WP_Script_Modules::get_dependencies().
Even though this method is private, there are some usages of it in the ecosystem via the Reflection API. So this reverts the script module IDs `string[]` return value in favor of the original `array<string, array>` return value. This re-emphasizes the need for more public accessor methods being tracked in #60597. Developed in #10403 Follow-up to [60999]. Props pbiron, westonruter, johnbillion. See #63486, #60597. git-svn-id: https://develop.svn.wordpress.org/trunk@61073 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 15aa390 commit 8defb1f

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/wp-includes/class-wp-script-modules.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ public function print_head_enqueued_script_modules() {
398398
! $this->registered[ $id ]['in_footer']
399399
) {
400400
// If any dependency is set to be printed in footer, skip printing this module in head.
401-
$dependencies = $this->get_dependencies( array( $id ) );
401+
$dependencies = array_keys( $this->get_dependencies( array( $id ) ) );
402402
foreach ( $dependencies as $dependency_id ) {
403403
if (
404404
in_array( $dependency_id, $this->queue, true ) &&
@@ -528,7 +528,7 @@ public function print_import_map() {
528528
*/
529529
private function get_import_map(): array {
530530
$imports = array();
531-
foreach ( $this->get_dependencies( $this->queue ) as $id ) {
531+
foreach ( array_keys( $this->get_dependencies( $this->queue ) ) as $id ) {
532532
$src = $this->get_src( $id );
533533
if ( '' !== $src ) {
534534
$imports[ $id ] = $src;
@@ -566,7 +566,7 @@ private function get_marked_for_enqueue(): array {
566566
* @param string[] $ids The identifiers of the script modules for which to gather dependencies.
567567
* @param string[] $import_types Optional. Import types of dependencies to retrieve: 'static', 'dynamic', or both.
568568
* Default is both.
569-
* @return string[] List of IDs for script module dependencies.
569+
* @return array<string, array> List of dependencies, keyed by script module identifier.
570570
*/
571571
private function get_dependencies( array $ids, array $import_types = array( 'static', 'dynamic' ) ): array {
572572
$all_dependencies = array();
@@ -584,15 +584,15 @@ private function get_dependencies( array $ids, array $import_types = array( 'sta
584584
in_array( $dependency['import'], $import_types, true ) &&
585585
isset( $this->registered[ $dependency['id'] ] )
586586
) {
587-
$all_dependencies[ $dependency['id'] ] = true;
587+
$all_dependencies[ $dependency['id'] ] = $this->registered[ $dependency['id'] ];
588588

589589
// Add this dependency to the list to get dependencies for.
590590
$id_queue[] = $dependency['id'];
591591
}
592592
}
593593
}
594594

595-
return array_keys( $all_dependencies );
595+
return $all_dependencies;
596596
}
597597

598598
/**

tests/phpunit/tests/script-modules/wpScriptModules.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,10 @@ public function test_comprehensive_methods( bool $use_global_function, bool $onl
225225

226226
$reflection_class = new ReflectionClass( wp_script_modules() );
227227
$get_marked_for_enqueue = $reflection_class->getMethod( 'get_marked_for_enqueue' );
228+
$get_dependencies = $reflection_class->getMethod( 'get_dependencies' );
228229
if ( PHP_VERSION_ID < 80100 ) {
229230
$get_marked_for_enqueue->setAccessible( true );
231+
$get_dependencies->setAccessible( true );
230232
}
231233

232234
$register_and_enqueue = static function ( ...$args ) use ( $use_global_function, $only_enqueue ) {
@@ -254,12 +256,25 @@ public function test_comprehensive_methods( bool $use_global_function, bool $onl
254256
$this->assertSame( wp_script_modules()->get_queue(), array_keys( $marked_for_enqueue ), 'Expected get_queue() to match keys returned by get_marked_for_enqueue().' );
255257
$this->assertIsArray( $marked_for_enqueue['a'], 'Expected script module "a" to have an array entry.' );
256258
$this->assertSame( '/a.js', $marked_for_enqueue['a']['src'], 'Expected script module "a" to have the given src.' );
259+
$this->assertSame( array(), $get_dependencies->invoke( wp_script_modules(), array( 'a' ) ) );
257260

258261
// One Dependency.
259262
$register( 'b-dep', '/b-dep.js' );
260263
$register_and_enqueue( 'b', '/b.js', array( 'b-dep' ) );
261264
$this->assertTrue( wp_script_modules()->set_fetchpriority( 'b', 'low' ) );
262265
$this->assertSame( array( 'a', 'b' ), wp_script_modules()->get_queue() );
266+
$this->assertSame(
267+
array(
268+
'b-dep' => array(
269+
'src' => '/b-dep.js',
270+
'version' => false,
271+
'dependencies' => array(),
272+
'in_footer' => false,
273+
'fetchpriority' => 'auto',
274+
),
275+
),
276+
$get_dependencies->invoke( wp_script_modules(), array( 'b' ) )
277+
);
263278

264279
// Two dependencies with different formats and a false version.
265280
$register( 'c-dep', '/c-static.js', array(), false, array( 'fetchpriority' => 'low' ) );

0 commit comments

Comments
 (0)