diff --git a/lib/compat/wordpress-6.5/class-wp-script-modules.php b/lib/compat/wordpress-6.5/class-wp-script-modules.php index 714e7ae77d098..d7eaff177823c 100644 --- a/lib/compat/wordpress-6.5/class-wp-script-modules.php +++ b/lib/compat/wordpress-6.5/class-wp-script-modules.php @@ -167,8 +167,6 @@ public function add_hooks() { add_action( $position, array( $this, 'print_import_map' ) ); add_action( $position, array( $this, 'print_enqueued_script_modules' ) ); add_action( $position, array( $this, 'print_script_module_preloads' ) ); - // Prints the script that loads the import map polyfill in the footer. - add_action( 'wp_footer', array( $this, 'print_import_map_polyfill' ), 11 ); } /** @@ -214,10 +212,37 @@ public function print_script_module_preloads() { * Prints the import map using a script tag with a type="importmap" attribute. * * @since 6.5.0 + * + * @global WP_Scripts $wp_scripts The WP_Scripts object for printing the polyfill. */ public function print_import_map() { $import_map = $this->get_import_map(); if ( ! empty( $import_map['imports'] ) ) { + global $wp_scripts; + if ( isset( $wp_scripts ) ) { + /* + * In Core, the polyfill is registered with a different approach. + * See: https://github.com/WordPress/wordpress-develop/blob/4b23ba81ddb067110e41d05550de7f2a4f09dad3/src/wp-includes/script-loader.php#L99 + */ + wp_register_script( + 'wp-polyfill-importmap', + gutenberg_url( '/build/modules/importmap-polyfill.min.js' ), + array(), + '1.8.2', + true + ); + wp_print_inline_script_tag( + wp_get_script_polyfill( + $wp_scripts, + array( + 'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")' => 'wp-polyfill-importmap', + ) + ), + array( + 'id' => 'wp-load-polyfill-importmap', + ) + ); + } wp_print_inline_script_tag( wp_json_encode( $import_map, JSON_HEX_TAG | JSON_HEX_AMP ), array( @@ -228,40 +253,6 @@ public function print_import_map() { } } - /** - * Prints the necessary script to load import map polyfill for browsers that - * do not support import maps. - * - * @since 6.5.0 - */ - public function print_import_map_polyfill() { - $import_map = $this->get_import_map(); - if ( ! empty( $import_map['imports'] ) ) { - global $wp_scripts; - /* - * In Core, the polyfill is registered with a different approach. - * See: https://github.com/WordPress/wordpress-develop/blob/4b23ba81ddb067110e41d05550de7f2a4f09dad3/src/wp-includes/script-loader.php#L99 - */ - wp_register_script( - 'wp-polyfill-importmap', - gutenberg_url( '/build/modules/importmap-polyfill.min.js' ), - array(), - '1.8.2', - true - ); - wp_print_inline_script_tag( - wp_get_script_polyfill( - $wp_scripts, - array( - 'HTMLScriptElement.supports && HTMLScriptElement.supports("importmap")' => 'wp-polyfill-importmap', - ) - ), - array( - 'id' => 'wp-load-polyfill-importmap', - ) - ); - } - } /** * Returns the import map array. * diff --git a/lib/compat/wordpress-6.5/scripts-modules.php b/lib/compat/wordpress-6.5/scripts-modules.php index ba329b255b196..740a16581ce7f 100644 --- a/lib/compat/wordpress-6.5/scripts-modules.php +++ b/lib/compat/wordpress-6.5/scripts-modules.php @@ -20,13 +20,14 @@ * @return WP_Script_Modules The main WP_Script_Modules instance. */ function wp_script_modules(): WP_Script_Modules { - static $instance = null; - if ( is_null( $instance ) ) { - $instance = new WP_Script_Modules(); - $instance->add_hooks(); + global $wp_script_modules; + + if ( ! ( $wp_script_modules instanceof WP_Script_Modules ) ) { + $wp_script_modules = new WP_Script_Modules(); } - return $instance; + return $wp_script_modules; } + wp_script_modules()->add_hooks(); } if ( ! function_exists( 'wp_register_script_module' ) ) {