From 6f79976ce3bc3ed5c21a7ee8fd0f621d6f175bec Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 13 Oct 2026 21:20:57 +0100 Subject: [PATCH 1/2] Script Loader: Move the collection of classic script module dependencies into a helper. Moves the code that collects the script module dependencies of classic scripts out of `WP_Script_Modules::get_import_map()` into a private `get_classic_script_module_dependencies()` method, so other methods can use the same list. There is no change in behavior. --- src/wp-includes/class-wp-script-modules.php | 53 ++++++++++++++------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php index 9c9f8a762af5e..509efaf48545a 100644 --- a/src/wp-includes/class-wp-script-modules.php +++ b/src/wp-includes/class-wp-script-modules.php @@ -636,17 +636,47 @@ public function print_import_map() { * @since 6.5.0 * @since 7.0.0 Script module dependencies ('module_dependencies') of classic scripts are now included. * - * @global WP_Scripts $wp_scripts - * * @return array> Array with an `imports` key mapping to an array of script module * identifiers and their respective URLs, including the version query. */ private function get_import_map(): array { - global $wp_scripts; - $imports = array(); // Identify script modules that are dependencies of classic scripts. + $classic_script_module_dependencies = $this->get_classic_script_module_dependencies(); + + // Note: the script modules in $this->queue are not included in the importmap because they get printed as scripts. + $ids = array_unique( + array_merge( + $classic_script_module_dependencies, + array_keys( $this->get_dependencies( array_merge( $this->queue, $classic_script_module_dependencies ) ) ) + ) + ); + foreach ( $ids as $id ) { + $src = $this->get_src( $id ); + if ( '' !== $src ) { + $imports[ $id ] = $src; + } + } + return array( 'imports' => $imports ); + } + + /** + * Gets the IDs of the script modules that classic scripts depend on. + * + * Walks the enqueued, to-do and done classic scripts and their dependencies, and collects the registered + * script modules in their 'module_dependencies' data. A script module that is not registered triggers a + * {@see _doing_it_wrong()} notice. + * + * @since 7.2.0 + * + * @global WP_Scripts $wp_scripts + * + * @return string[] Script module IDs. + */ + private function get_classic_script_module_dependencies(): array { + global $wp_scripts; + $classic_script_module_dependencies = array(); if ( $wp_scripts instanceof WP_Scripts ) { $handles = array_merge( @@ -707,20 +737,7 @@ private function get_import_map(): array { } } - // Note: the script modules in $this->queue are not included in the importmap because they get printed as scripts. - $ids = array_unique( - array_merge( - $classic_script_module_dependencies, - array_keys( $this->get_dependencies( array_merge( $this->queue, $classic_script_module_dependencies ) ) ) - ) - ); - foreach ( $ids as $id ) { - $src = $this->get_src( $id ); - if ( '' !== $src ) { - $imports[ $id ] = $src; - } - } - return array( 'imports' => $imports ); + return $classic_script_module_dependencies; } /** From 4fa885059f2088366da1b392cbf17af8064c5811 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 13 Oct 2026 21:26:39 +0100 Subject: [PATCH 2/2] I18N: Print translations for the script modules that classic scripts depend on. `WP_Script_Modules::print_script_module_translations()` only covered the enqueued script modules and their dependencies. A script module that only a classic script imports, declared in the classic script's `module_dependencies`, is added to the import map and loads, but its translations were never printed, so its strings showed untranslated. The method now also covers the script module dependencies of classic scripts and their dependencies: the same script modules the import map adds for classic scripts, collected by the same helper. A script module that is both enqueued and imported by a classic script gets its translations printed once, and a missing script module dependency still triggers a single `_doing_it_wrong()` notice. --- src/wp-includes/class-wp-script-modules.php | 42 ++- .../tests/script-modules/wpScriptModules.php | 240 ++++++++++++++++++ 2 files changed, 276 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php index 509efaf48545a..60db7c44b60f1 100644 --- a/src/wp-includes/class-wp-script-modules.php +++ b/src/wp-includes/class-wp-script-modules.php @@ -92,6 +92,18 @@ class WP_Script_Modules { */ private $modules_with_missing_dependencies = array(); + /** + * List of handles for classic scripts encountered which have missing script module dependencies. + * + * A handle is added to this list when it is discovered to have script module dependencies that are not + * registered. At this time, a warning is emitted with {@see _doing_it_wrong()}. The handle is then added to this + * list, so that duplicate warnings don't occur. + * + * @since 7.2.0 + * @var string[] + */ + private $classic_scripts_with_missing_module_dependencies = array(); + /** * Registers the script module if no script module with that script module * identifier has already been registered. @@ -367,7 +379,8 @@ public function set_translations( string $id, string $domain = 'default', string } /** - * Prints translations for all enqueued script modules. + * Prints translations for all enqueued script modules and for the script + * modules that classic scripts depend on. * * Outputs inline `