Make WordPress Core

Changeset 61073


Ignore:
Timestamp:
10/27/2025 04:10:24 PM (7 weeks ago)
Author:
westonruter
Message:

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 https://github.com/WordPress/wordpress-develop/pull/10403

Follow-up to [60999].

Props pbiron, westonruter, johnbillion.
See #63486, #60597.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-script-modules.php

    r60999 r61073  
    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 (
     
    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 ) {
     
    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 {
     
    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.
     
    593593        }
    594594
    595         return array_keys( $all_dependencies );
     595        return $all_dependencies;
    596596    }
    597597
  • trunk/tests/phpunit/tests/script-modules/wpScriptModules.php

    r61041 r61073  
    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
     
    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.
     
    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.
Note: See TracChangeset for help on using the changeset viewer.