From 8a78f7a9075b142fed13dabb8fd77c728b3f1729 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 15 Mar 2024 15:01:07 +0000 Subject: [PATCH] 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. Reviewed by swissspidy. Merges [57840] to the to the 6.5 branch. Props dd32, peterwilsoncc, westonruter. Fixes #60742. Built from https://develop.svn.wordpress.org/branches/6.5@57844 git-svn-id: http://core.svn.wordpress.org/branches/6.5@57345 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-script-modules.php | 38 +++++++++++++++++-------- wp-includes/version.php | 2 +- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/wp-includes/class-wp-script-modules.php b/wp-includes/class-wp-script-modules.php index 9bf6ce33e7..89d12a6b3f 100644 --- a/wp-includes/class-wp-script-modules.php +++ b/wp-includes/class-wp-script-modules.php @@ -191,7 +191,7 @@ class WP_Script_Modules { wp_print_script_tag( array( 'type' => 'module', - 'src' => $this->get_versioned_src( $script_module ), + 'src' => $this->get_src( $id ), 'id' => $id . '-js-module', ) ); @@ -212,7 +212,7 @@ class WP_Script_Modules { if ( true !== $script_module['enqueue'] ) { echo sprintf( '', - esc_url( $this->get_versioned_src( $script_module ) ), + esc_url( $this->get_src( $id ) ), esc_attr( $id . '-js-modulepreload' ) ); } @@ -264,7 +264,7 @@ class WP_Script_Modules { private function get_import_map(): array { $imports = array(); 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 ); } @@ -331,19 +331,33 @@ class WP_Script_Modules { * * @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. */ - private function get_versioned_src( array $script_module ): string { - $args = array(); + private function get_src( string $id ): string { + if ( ! isset( $this->registered[ $id ] ) ) { + return ''; + } + + $script_module = $this->registered[ $id ]; + $src = $script_module['src']; + if ( false === $script_module['version'] ) { - $args['ver'] = get_bloginfo( 'version' ); + $src = add_query_arg( 'ver', get_bloginfo( 'version' ), $src ); } 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'] ); - } - return $script_module['src']; + + /** + * Filters the script module source. + * + * @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; } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 73c6a9d539..91c009f7a9 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5-RC2-57843'; +$wp_version = '6.5-RC2-57844'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.