Make WordPress Core

Changeset 57840

Timestamp:
03/15/2024 11:27:12 AM (4 months ago)
Author:
swissspidy
Message:

Script Loader: Add new script_module_loader_src filter for the script module src.

Ensures parity with the script_loader_src filter for regular scripts, allowing the URL to be filtered, for example to load them from a CDN or alter query parameters.

Props dd32, peterwilsoncc, westonruter.
Fixes #60742.

Location:
trunk
Files:
2 edited

Legend:

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

    r57644 r57840  
    192192                array(
    193193                    'type' => 'module',
    194                     'src'  => $this->get_versioned_src( $script_module ),
     194                    'src'  => $this->get_ ),
    195195                    'id'   => $id . '-js-module',
    196196                )
     
    213213                echo sprintf(
    214214                    '<link rel="modulepreload" href="%s" id="%s">',
    215                     esc_url( $this->get_versioned_src( $script_module ) ),
     215                    esc_url( $this->get_ ) ),
    216216                    esc_attr( $id . '-js-modulepreload' )
    217217                );
     
    265265        $imports = array();
    266266        foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ) ) as $id => $script_module ) {
    267             $imports[ $id ] = $this->get_versioned_src( $script_module );
     267            $imports[ $id ] = $this->get_ );
    268268        }
    269269        return array( 'imports' => $imports );
     
    332332     * @since 6.5.0
    333333     *
    334      * @param array $script_module The script module.
     334     * @param .
    335335     * @return string The script module src with a version if relevant.
    336336     */
    337     private function get_versioned_src( array $script_module ): string {
    338         $args = array();
     337    private function get_src( string $id ): string {
     338        if ( ! isset( $this->registered[ $id ] ) ) {
     339            return '';
     340        }
     341
     342        $script_module = $this->registered[ $id ];
     343        $src           = $script_module['src'];
     344
    339345        if ( false === $script_module['version'] ) {
    340             $args['ver'] = get_bloginfo( 'version' );
     346            $ );
    341347        } elseif ( null !== $script_module['version'] ) {
    342             $args['ver'] = $script_module['version'];
    343         }
    344         if ( $args ) {
    345             return add_query_arg( $args, $script_module['src'] );
    346         }
    347         return $script_module['src'];
     348            $src = add_query_arg( 'ver', $script_module['version'], $src );
     349        }
     350
     351        /**
     352         * Filters the script module source.
     353         *
     354         * @since 6.5.0
     355         *
     356         * @param string $src Module source url.
     357         * @param string $id  Module identifier.
     358         */
     359        $src = apply_filters( 'script_module_loader_src', $src, $id );
     360
     361        return $src;
    348362    }
    349363}
  • trunk/tests/phpunit/tests/script-modules/wpScriptModules.php

    r57593 r57840  
    513513
    514514    /**
    515      * Tests the functionality of the `get_versioned_src` method to ensure
     515     * Tests the functionality of the `get_src` method to ensure
    516516     * proper URLs with version strings are returned.
    517517     *
    518518     * @ticket 56313
    519519     *
    520      * @covers ::get_versioned_src()
    521      */
    522     public function test_get_versioned_src() {
    523         $get_versioned_src = new ReflectionMethod( $this->script_modules, 'get_versioned_src' );
    524         $get_versioned_src->setAccessible( true );
    525 
    526         $module_with_version = array(
    527             'src'     => 'http://example.com/module.js',
    528             'version' => '1.0',
    529         );
    530 
    531         $result = $get_versioned_src->invoke( $this->script_modules, $module_with_version );
     520     * @covers ::get_src()
     521     */
     522    public function test_get_src() {
     523        $get_src = new ReflectionMethod( $this->script_modules, 'get_src' );
     524        $get_src->setAccessible( true );
     525
     526        $this->script_modules->register(
     527            'module_with_version',
     528            'http://example.com/module.js',
     529            array(),
     530            '1.0'
     531        );
     532
     533        $result = $get_src->invoke( $this->script_modules, 'module_with_version' );
    532534        $this->assertEquals( 'http://example.com/module.js?ver=1.0', $result );
    533535
    534         $module_without_version = array(
    535             'src'     => 'http://example.com/module.js',
    536             'version' => null,
    537         );
    538 
    539         $result = $get_versioned_src->invoke( $this->script_modules, $module_without_version );
     536        $this->script_modules->register(
     537            'module_without_version',
     538            'http://example.com/module.js',
     539            array(),
     540            null
     541        );
     542
     543        $result = $get_src->invoke( $this->script_modules, 'module_without_version' );
    540544        $this->assertEquals( 'http://example.com/module.js', $result );
    541545
    542         $module_with_wp_version = array(
    543             'src'     => 'http://example.com/module.js',
    544             'version' => false,
    545         );
    546 
    547         $result = $get_versioned_src->invoke( $this->script_modules, $module_with_wp_version );
     546        $this->script_modules->register(
     547            'module_with_wp_version',
     548            'http://example.com/module.js',
     549            array(),
     550            false
     551        );
     552
     553        $result = $get_src->invoke( $this->script_modules, 'module_with_wp_version' );
    548554        $this->assertEquals( 'http://example.com/module.js?ver=' . get_bloginfo( 'version' ), $result );
    549555
    550         $module_with_existing_query_string = array(
    551             'src'     => 'http://example.com/module.js?foo=bar',
    552             'version' => '1.0',
    553         );
    554 
    555         $result = $get_versioned_src->invoke( $this->script_modules, $module_with_existing_query_string );
     556        $this->script_modules->register(
     557            'module_with_existing_query_string',
     558            'http://example.com/module.js?foo=bar',
     559            array(),
     560            '1.0'
     561        );
     562
     563        $result = $get_src->invoke( $this->script_modules, 'module_with_existing_query_string' );
    556564        $this->assertEquals( 'http://example.com/module.js?foo=bar&ver=1.0', $result );
     565
     566
     567
     568
     569
     570
     571
     572
     573
     574
     575
     576
     577
     578
     579
     580
    557581    }
    558582
Note: See TracChangeset for help on using the changeset viewer.