Make WordPress Core


Ignore:
Timestamp:
03/15/2024 11:27:12 AM (3 years 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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                // Filter the version to include the ID in the final URL, to test the filter, this should affect the tests below.
     567                add_filter(
     568                        'script_module_loader_src',
     569                        function ( $src, $id ) {
     570                                return add_query_arg( 'script_module_id', urlencode( $id ), $src );
     571                        },
     572                        10,
     573                        2
     574                );
     575
     576                $result = $get_src->invoke( $this->script_modules, 'module_without_version' );
     577                $this->assertEquals( 'http://example.com/module.js?script_module_id=module_without_version', $result );
     578
     579                $result = $get_src->invoke( $this->script_modules, 'module_with_existing_query_string' );
     580                $this->assertEquals( 'http://example.com/module.js?foo=bar&ver=1.0&script_module_id=module_with_existing_query_string', $result );
    557581        }
    558582
Note: See TracChangeset for help on using the changeset viewer.