diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php index 058e5f0eef0e6..48a97ff4acf16 100644 --- a/src/wp-includes/class-wp-script-modules.php +++ b/src/wp-includes/class-wp-script-modules.php @@ -12,6 +12,15 @@ * Core class used to register script modules. * * @since 6.5.0 + * + * @phpstan-type ScriptModule array{ + * src: string, + * version: string|false|null, + * dependencies: array, + * in_footer: bool, + * fetchpriority: 'auto'|'low'|'high', + * translations?: array{ textdomain: string, path: string }, + * } */ class WP_Script_Modules { /** @@ -19,6 +28,7 @@ class WP_Script_Modules { * * @since 6.5.0 * @var array> + * @phpstan-var array */ private $registered = array(); @@ -328,6 +338,89 @@ public function deregister( string $id ) { unset( $this->registered[ $id ] ); } + /** + * Overrides the text domain and path used to load translations for a script module. + * + * This is only needed for modules whose text domain differs from 'default' + * or whose translation files live outside the standard locations, for + * example plugin modules that register their own text domain. Translations + * for modules that use the default domain are loaded automatically by + * {@see WP_Script_Modules::print_script_module_translations()}. + * + * @since 7.0.0 + * + * @param string $id The identifier of the script module. + * @param string $domain Optional. Text domain. Default 'default'. + * @param string $path Optional. The full file path to the directory containing translation files. + * @return bool True if the text domain was registered, false if the module is not registered. + */ + public function set_translations( string $id, string $domain = 'default', string $path = '' ): bool { + if ( ! isset( $this->registered[ $id ] ) ) { + return false; + } + + $this->registered[ $id ]['translations'] = array( + 'textdomain' => $domain, + 'path' => $path, + ); + + return true; + } + + /** + * Prints translations for all enqueued script modules. + * + * Outputs inline `