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.
Built from https://develop.svn.wordpress.org/trunk@57840


git-svn-id: http://core.svn.wordpress.org/trunk@57341 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2024-03-15 11:29:07 +00:00
parent 6e516fe3cc
commit 038465b836
2 changed files with 27 additions and 13 deletions

View File

@ -191,7 +191,7 @@ class WP_Script_Modules {
wp_print_script_tag( wp_print_script_tag(
array( array(
'type' => 'module', 'type' => 'module',
'src' => $this->get_versioned_src( $script_module ), 'src' => $this->get_src( $id ),
'id' => $id . '-js-module', 'id' => $id . '-js-module',
) )
); );
@ -212,7 +212,7 @@ class WP_Script_Modules {
if ( true !== $script_module['enqueue'] ) { if ( true !== $script_module['enqueue'] ) {
echo sprintf( echo sprintf(
'<link rel="modulepreload" href="%s" id="%s">', '<link rel="modulepreload" href="%s" id="%s">',
esc_url( $this->get_versioned_src( $script_module ) ), esc_url( $this->get_src( $id ) ),
esc_attr( $id . '-js-modulepreload' ) esc_attr( $id . '-js-modulepreload' )
); );
} }
@ -264,7 +264,7 @@ class WP_Script_Modules {
private function get_import_map(): array { private function get_import_map(): array {
$imports = array(); $imports = array();
foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ) ) as $id => $script_module ) { foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ) ) as $id => $script_module ) {
$imports[ $id ] = $this->get_versioned_src( $script_module ); $imports[ $id ] = $this->get_src( $id );
} }
return array( 'imports' => $imports ); return array( 'imports' => $imports );
} }
@ -331,19 +331,33 @@ class WP_Script_Modules {
* *
* @since 6.5.0 * @since 6.5.0
* *
* @param array $script_module The script module. * @param string $id The script module identifier.
* @return string The script module src with a version if relevant. * @return string The script module src with a version if relevant.
*/ */
private function get_versioned_src( array $script_module ): string { private function get_src( string $id ): string {
$args = array(); if ( ! isset( $this->registered[ $id ] ) ) {
return '';
}
$script_module = $this->registered[ $id ];
$src = $script_module['src'];
if ( false === $script_module['version'] ) { if ( false === $script_module['version'] ) {
$args['ver'] = get_bloginfo( 'version' ); $src = add_query_arg( 'ver', get_bloginfo( 'version' ), $src );
} elseif ( null !== $script_module['version'] ) { } elseif ( null !== $script_module['version'] ) {
$args['ver'] = $script_module['version']; $src = add_query_arg( 'ver', $script_module['version'], $src );
} }
if ( $args ) {
return add_query_arg( $args, $script_module['src'] ); /**
} * Filters the script module source.
return $script_module['src']; *
* @since 6.5.0
*
* @param string $src Module source url.
* @param string $id Module identifier.
*/
$src = apply_filters( 'script_module_loader_src', $src, $id );
return $src;
} }
} }

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.6-alpha-57839'; $wp_version = '6.6-alpha-57840';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.