Skip to content

Commit 3af54e5

Browse files
I18N: Print translations for the script modules that 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 script imports, declared in the 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 covers the enqueued script modules and the script modules in the import map, which includes the script module dependencies of scripts and their dependencies. `WP_Script_Modules::print_script_module_data()` already covered these script modules, and both methods now get them from the same private `get_loadable_script_module_ids()` method, which sorts them with `get_sorted_dependencies()`. This way both methods always cover the same script modules, in the same order, with dependencies before the script modules that depend on them. A script module that is both enqueued and imported by a script gets its translations printed once. Since the import map is now built three times per page, a missing script module dependency of a script triggers the `_doing_it_wrong()` notice once per script. Fixes #66591.
1 parent 31a6fb4 commit 3af54e5

2 files changed

Lines changed: 360 additions & 17 deletions

File tree

‎src/wp-includes/class-wp-script-modules.php‎

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ class WP_Script_Modules {
9292
*/
9393
private $modules_with_missing_dependencies = array();
9494

95+
/**
96+
* List of handles for scripts encountered which have missing script module dependencies.
97+
*
98+
* A handle is added to this list when it is discovered to have script module dependencies that are not
99+
* registered. At this time, a warning is emitted with {@see _doing_it_wrong()}. The handle is then added to this
100+
* list, so that duplicate warnings don't occur.
101+
*
102+
* @since 7.2.0
103+
* @var string[]
104+
*/
105+
private $scripts_with_missing_module_dependencies = array();
106+
95107
/**
96108
* Registers the script module if no script module with that script module
97109
* identifier has already been registered.
@@ -367,7 +379,8 @@ public function set_translations( string $id, string $domain = 'default', string
367379
}
368380

369381
/**
370-
* Prints translations for all enqueued script modules.
382+
* Prints translations for the enqueued script modules and for the script
383+
* modules in the import map.
371384
*
372385
* Outputs inline `<script>` tags that call `wp.i18n.setLocaleData()` with
373386
* the translated strings for each script module. This must run before
@@ -379,10 +392,11 @@ public function set_translations( string $id, string $domain = 'default', string
379392
* {@see WP_Script_Modules::set_translations()}.
380393
*
381394
* @since 7.0.0
395+
* @since 7.2.0 Translations of the script module dependencies ('module_dependencies') of scripts, and of their
396+
* dependencies, are now printed.
382397
*/
383398
public function print_script_module_translations(): void {
384-
// Collect all module IDs that will be on the page (enqueued + their dependencies).
385-
$module_ids = $this->get_sorted_dependencies( $this->queue );
399+
$module_ids = $this->get_loadable_script_module_ids();
386400

387401
$set_locale_data_js_function = <<<'JS'
388402
( domain, translations ) => {
@@ -684,7 +698,10 @@ private function get_import_map(): array {
684698
}
685699
}
686700

687-
if ( count( $missing_module_dependencies ) > 0 ) {
701+
if (
702+
count( $missing_module_dependencies ) > 0 &&
703+
! in_array( $handle, $this->scripts_with_missing_module_dependencies, true )
704+
) {
688705
_doing_it_wrong(
689706
'WP_Scripts::add_data',
690707
sprintf(
@@ -696,6 +713,7 @@ private function get_import_map(): array {
696713
),
697714
'7.0.0'
698715
);
716+
$this->scripts_with_missing_module_dependencies[] = $handle;
699717
}
700718
}
701719

@@ -723,6 +741,27 @@ private function get_import_map(): array {
723741
return array( 'imports' => $imports );
724742
}
725743

744+
/**
745+
* Gets the IDs of the script modules that can load on the page, sorted based on their dependencies.
746+
*
747+
* These are the enqueued script modules and the script modules in the import map: the dependencies of the
748+
* enqueued script modules, the script module dependencies ('module_dependencies') of scripts, and the
749+
* dependencies of those. Dependencies come before the script modules that depend on them, and a script module
750+
* with a dependency that is not registered is left out.
751+
*
752+
* @since 7.2.0
753+
*
754+
* @return string[] Sorted list of script module IDs.
755+
*/
756+
private function get_loadable_script_module_ids(): array {
757+
return $this->get_sorted_dependencies(
758+
array_merge(
759+
$this->queue,
760+
array_keys( $this->get_import_map()['imports'] )
761+
)
762+
);
763+
}
764+
726765
/**
727766
* Retrieves the list of script modules marked for enqueue.
728767
*
@@ -1014,21 +1053,13 @@ private function get_src( string $id ): string {
10141053
* form `wp-script-module-data-{$module_id}`.
10151054
*/
10161055
public function print_script_module_data(): void {
1017-
$modules = array();
1018-
foreach ( array_unique( $this->queue ) as $id ) {
1019-
if ( '@wordpress/a11y' === $id ) {
1020-
$this->a11y_available = true;
1021-
}
1022-
$modules[ $id ] = true;
1023-
}
1024-
foreach ( array_keys( $this->get_import_map()['imports'] ) as $id ) {
1025-
if ( '@wordpress/a11y' === $id ) {
1026-
$this->a11y_available = true;
1027-
}
1028-
$modules[ $id ] = true;
1056+
$module_ids = $this->get_loadable_script_module_ids();
1057+
1058+
if ( in_array( '@wordpress/a11y', $module_ids, true ) ) {
1059+
$this->a11y_available = true;
10291060
}
10301061

1031-
foreach ( array_keys( $modules ) as $module_id ) {
1062+
foreach ( $module_ids as $module_id ) {
10321063
/**
10331064
* Filters data associated with a given Script Module.
10341065
*

0 commit comments

Comments
 (0)