Repository navigation
I18N: Print translations for the script modules that classic scripts depend on #14260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 `<script>` tags that call `wp.i18n.setLocaleData()` with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * the translated strings for each script module. This must run before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -379,10 +392,23 @@ public function set_translations( string $id, string $domain = 'default', string | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * {@see WP_Script_Modules::set_translations()}. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 7.0.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 7.2.0 Also prints the translations of the script modules that classic scripts depend on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * ('module_dependencies'), and of their dependencies. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public function print_script_module_translations(): void { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Collect all module IDs that will be on the page (enqueued + their dependencies). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $module_ids = $this->get_sorted_dependencies( $this->queue ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Collect the IDs of the script modules that can load on the page: the enqueued ones, the ones that classic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * scripts depend on, and the dependencies of both. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $classic_script_module_dependencies = $this->get_classic_script_module_dependencies(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $module_ids = array_unique( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| array_merge( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->get_sorted_dependencies( $this->queue ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $classic_script_module_dependencies, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| array_keys( $this->get_dependencies( $classic_script_module_dependencies ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+399
to
+411
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
wordpress-develop/src/wp-includes/class-wp-script-modules.php Lines 1063 to 1076 in 4fa8850
The same works here and gives the same set: the import map is the classic script module dependencies plus
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think the biggest issue after we have this PR is that there are two loops over all reachable modules,
Unifying the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $set_locale_data_js_function = <<<'JS' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ( domain, translations ) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -636,17 +662,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<string, array<string, string>> 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. Script modules that are not registered trigger a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * {@see _doing_it_wrong()} notice, once per classic script. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @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( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -684,7 +740,10 @@ private function get_import_map(): array { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( count( $missing_module_dependencies ) > 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| count( $missing_module_dependencies ) > 0 && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ! in_array( $handle, $this->classic_scripts_with_missing_module_dependencies, true ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _doing_it_wrong( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'WP_Scripts::add_data', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sprintf( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -696,6 +755,7 @@ private function get_import_map(): array { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| '7.0.0' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->classic_scripts_with_missing_module_dependencies[] = $handle; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -707,20 +767,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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming nit: let's call these just "scripts", not "classic scripts". Everything else in the codebase calls them "scripts". The
WP_Scriptsclass, functions likewp_register_script, actions likeadmin_print_scripts, etc.