diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index 6f5720ec21c9d..b971a41304160 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -286,28 +286,31 @@ function get_legacy_widget_block_editor_settings() { * Collect the block editor assets that need to be loaded into the editor's iframe. * * @since 6.0.0 + * @since 7.0.0 Allow enqueuing script modules. * @access private * - * @global WP_Styles $wp_styles The WP_Styles current instance. - * @global WP_Scripts $wp_scripts The WP_Scripts current instance. + * @global WP_Styles $wp_styles The WP_Styles current instance. + * @global WP_Scripts $wp_scripts The WP_Scripts current instance. + * @global WP_Script_Modules $wp_script_modules The WP_Script_Modules current instance. * * @return array { * The block editor assets. * - * @type string|false $styles String containing the HTML for styles. - * @type string|false $scripts String containing the HTML for scripts. + * @type string|false $html String containing the necessary HTML for the editor iframe. * } */ function _wp_get_iframed_editor_assets() { - global $wp_styles, $wp_scripts; + global $wp_styles, $wp_scripts, $wp_script_modules; // Keep track of the styles and scripts instance to restore later. - $current_wp_styles = $wp_styles; - $current_wp_scripts = $wp_scripts; + $current_wp_styles = $wp_styles; + $current_wp_scripts = $wp_scripts; + $current_wp_script_modules = $wp_script_modules; // Create new instances to collect the assets. - $wp_styles = new WP_Styles(); - $wp_scripts = new WP_Scripts(); + $wp_styles = new WP_Styles(); + $wp_scripts = new WP_Scripts(); + $wp_script_modules = $wp_script_modules->clone_without_enqueued_modules(); /* * Register all currently registered styles and scripts. The actions that @@ -364,28 +367,27 @@ function _wp_get_iframed_editor_assets() { remove_action( 'wp_print_styles', 'print_emoji_styles' ); } + add_action( 'wp_print_iframe_html', array( $wp_script_modules, 'print_import_map' ) ); + add_action( 'wp_print_iframe_html', array( $wp_script_modules, 'print_head_enqueued_script_modules' ) ); + add_action( 'wp_print_iframe_html', array( $wp_script_modules, 'print_enqueued_script_modules' ) ); + add_action( 'wp_print_iframe_html', array( $wp_script_modules, 'print_script_module_preloads' ) ); + ob_start(); - wp_print_styles(); - wp_print_font_faces(); - wp_print_font_faces_from_style_variations(); - $styles = ob_get_clean(); + do_action( 'wp_print_iframe_html' ); + $html = ob_get_clean(); if ( $has_emoji_styles ) { add_action( 'wp_print_styles', 'print_emoji_styles' ); } - ob_start(); - wp_print_head_scripts(); - wp_print_footer_scripts(); - $scripts = ob_get_clean(); - // Restore the original instances. - $wp_styles = $current_wp_styles; - $wp_scripts = $current_wp_scripts; + $wp_styles = $current_wp_styles; + $wp_scripts = $current_wp_scripts; + $wp_script_modules = $current_wp_script_modules; return array( - 'styles' => $styles, - 'scripts' => $scripts, + 'styles' => $html, + 'html' => $html, ); } diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php index ff3973999581b..e4ed47c425fb9 100644 --- a/src/wp-includes/class-wp-script-modules.php +++ b/src/wp-includes/class-wp-script-modules.php @@ -949,4 +949,22 @@ public function print_a11y_script_module_html() { . '
' . ''; } + + /** + * Creates a clone of the instance without enqueued modules. + * + * This method creates a copy of the current instance without any + * state related to enqueued or printed modules. + * + * @since 7.0.0 + * + * @return WP_Script_Modules A new instance. + */ + public function clone_without_enqueued_modules(): self { + $clone = clone $this; + $clone->queue = array(); + $clone->done = array(); + $clone->a11y_available = false; + return $clone; + } } diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 68dccd979f2fe..9251f5bd1b55d 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -373,6 +373,12 @@ add_action( 'wp_enqueue_scripts', 'wp_enqueue_emoji_styles' ); add_action( 'wp_print_styles', 'print_emoji_styles' ); // Retained for backwards-compatibility. Unhooked by wp_enqueue_emoji_styles(). +add_action( 'wp_print_iframe_html', 'wp_print_styles' ); +add_action( 'wp_print_iframe_html', 'wp_print_font_faces' ); +add_action( 'wp_print_iframe_html', 'wp_print_font_faces_from_style_variations' ); +add_action( 'wp_print_iframe_html', 'wp_print_head_scripts' ); +add_action( 'wp_print_iframe_html', 'wp_print_footer_scripts' ); + if ( // Comment reply link. isset( $_GET['replytocom'] )