Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/wp-includes/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,28 +286,33 @@ 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 $styles String containing the HTML for styles.
* @type string|false $scripts String containing the HTML for scripts.
* @type string|false $script_modules String containing the HTML for scripts.
* }
*/
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_script_modules = $wp_script_modules->clone_without_enqueued_modules();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I learned of a hack/feature in PHP to allow setting private members in PostStatus Slack. This should should have the same effect, including my suggestion below:

Suggested change
$wp_script_modules = $wp_script_modules->clone_without_enqueued_modules();
$wp_script_modules = clone $wp_script_modules;
( function () {
$this->done = array();
$this->queue = array();
} )->call( $wp_script_modules );

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's wild. I can't believe it works and private properties are accessible in the closure.


/*
* Register all currently registered styles and scripts. The actions that
Expand Down Expand Up @@ -379,13 +384,22 @@ function _wp_get_iframed_editor_assets() {
wp_print_footer_scripts();
$scripts = ob_get_clean();

ob_start();
$wp_script_modules->print_import_map();
$wp_script_modules->print_head_enqueued_script_modules();
$wp_script_modules->print_enqueued_script_modules();
$wp_script_modules->print_script_module_preloads();
$script_modules = 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' => $styles,
'scripts' => $scripts,
'script_modules' => $script_modules,
);
}

Expand Down
8 changes: 8 additions & 0 deletions src/wp-includes/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -949,4 +949,12 @@ public function print_a11y_script_module_html() {
. '<div id="a11y-speak-polite" class="a11y-speak-region" aria-live="polite" aria-relevant="additions text" aria-atomic="true"></div>'
. '</div>';
}

public function clone_without_enqueued_modules(): WP_Script_Modules {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relatedly, I just learned that PHP 8.5 has clone with support for setting properties on a cloned object: https://www.php.net/releases/8.5/en.php#clone-with

$clone = new WP_Script_Modules();
$clone->registered = $this->registered;
$clone->dependents_map = $this->dependents_map;
$clone->modules_with_missing_dependencies = $this->modules_with_missing_dependencies;
Comment on lines +954 to +957
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be better?

Suggested change
$clone = new WP_Script_Modules();
$clone->registered = $this->registered;
$clone->dependents_map = $this->dependents_map;
$clone->modules_with_missing_dependencies = $this->modules_with_missing_dependencies;
$clone = clone $this;
$clone->queue = array();
$clone->done = array();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result is the same, right? I don't mind either way, if you have reasons to prefer one or the other I'd be happy to know.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mutating the queue member variable is done elsewhere in core for WP_Styles.

And this is just changing two members instead of three. And these two members feel less internal than the others, meaning we won't need to worry about ensuring other members get copied.

And if I understand correctly, resetting the queue and done are conceptually closer to what is desired: reset the state of what has been queued/printed to capture what is newly to be queued.

return $clone;
}
}
Loading