Make WordPress Core


Ignore:
Timestamp:
03/15/2024 11:27:12 AM (2 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/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_src( $id ),
    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_src( $id ) ),
    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_src( $id );
    268268                }
    269269                return array( 'imports' => $imports );
     
    332332         * @since 6.5.0
    333333         *
    334          * @param array $script_module The script module.
     334         * @param string $id The script module identifier.
    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                        $src = add_query_arg( 'ver', get_bloginfo( 'version' ), $src );
    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}
Note: See TracChangeset for help on using the changeset viewer.