Skip to content

Commit 71b6a61

Browse files
committed
Capture modules enqueued on enqueue_block_assets
1 parent 0b50baa commit 71b6a61

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

src/wp-includes/block-editor.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -286,28 +286,33 @@ function get_legacy_widget_block_editor_settings() {
286286
* Collect the block editor assets that need to be loaded into the editor's iframe.
287287
*
288288
* @since 6.0.0
289+
* @since 7.0.0 Allow enqueuing script modules.
289290
* @access private
290291
*
291-
* @global WP_Styles $wp_styles The WP_Styles current instance.
292-
* @global WP_Scripts $wp_scripts The WP_Scripts current instance.
292+
* @global WP_Styles $wp_styles The WP_Styles current instance.
293+
* @global WP_Scripts $wp_scripts The WP_Scripts current instance.
294+
* @global WP_Script_Modules $wp_script_modules The WP_Script_Modules current instance.
293295
*
294296
* @return array {
295297
* The block editor assets.
296298
*
297-
* @type string|false $styles String containing the HTML for styles.
298-
* @type string|false $scripts String containing the HTML for scripts.
299+
* @type string|false $styles String containing the HTML for styles.
300+
* @type string|false $scripts String containing the HTML for scripts.
301+
* @type string|false $script_modules String containing the HTML for scripts.
299302
* }
300303
*/
301304
function _wp_get_iframed_editor_assets() {
302-
global $wp_styles, $wp_scripts;
305+
global $wp_styles, $wp_scripts, $wp_script_modules;
303306

304307
// Keep track of the styles and scripts instance to restore later.
305-
$current_wp_styles = $wp_styles;
306-
$current_wp_scripts = $wp_scripts;
308+
$current_wp_styles = $wp_styles;
309+
$current_wp_scripts = $wp_scripts;
310+
$current_wp_script_modules = $wp_script_modules;
307311

308312
// Create new instances to collect the assets.
309313
$wp_styles = new WP_Styles();
310314
$wp_scripts = new WP_Scripts();
315+
$wp_script_modules = $wp_script_modules->clone_without_enqueued_modules();
311316

312317
/*
313318
* Register all currently registered styles and scripts. The actions that
@@ -379,13 +384,22 @@ function _wp_get_iframed_editor_assets() {
379384
wp_print_footer_scripts();
380385
$scripts = ob_get_clean();
381386

387+
ob_start();
388+
$wp_script_modules->print_import_map();
389+
$wp_script_modules->print_head_enqueued_script_modules();
390+
$wp_script_modules->print_enqueued_script_modules();
391+
$wp_script_modules->print_script_module_preloads();
392+
$script_modules = ob_get_clean();
393+
382394
// Restore the original instances.
383-
$wp_styles = $current_wp_styles;
384-
$wp_scripts = $current_wp_scripts;
395+
$wp_styles = $current_wp_styles;
396+
$wp_scripts = $current_wp_scripts;
397+
$wp_script_modules = $current_wp_script_modules;
385398

386399
return array(
387-
'styles' => $styles,
388-
'scripts' => $scripts,
400+
'styles' => $styles,
401+
'scripts' => $scripts,
402+
'script_modules' => $script_modules,
389403
);
390404
}
391405

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,4 +949,12 @@ public function print_a11y_script_module_html() {
949949
. '<div id="a11y-speak-polite" class="a11y-speak-region" aria-live="polite" aria-relevant="additions text" aria-atomic="true"></div>'
950950
. '</div>';
951951
}
952+
953+
public function clone_without_enqueued_modules(): WP_Script_Modules {
954+
$clone = new WP_Script_Modules();
955+
$clone->registered = $this->registered;
956+
$clone->dependents_map = $this->dependents_map;
957+
$clone->modules_with_missing_dependencies = $this->modules_with_missing_dependencies;
958+
return $clone;
959+
}
952960
}

0 commit comments

Comments
 (0)