-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Block Editor: Enqueue script modules via enqueue_block_assets action #10597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Block Editor: Enqueue script modules via enqueue_block_assets action #10597
Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
| . '</div>'; | ||
| } | ||
|
|
||
| public function clone_without_enqueued_modules(): WP_Script_Modules { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be better?
| $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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| // 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(); |
There was a problem hiding this comment.
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:
| $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 ); |
There was a problem hiding this comment.
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.
Trac ticket: https://core.trac.wordpress.org/ticket/64360
Requires Gutenberg changes to the block editor. See WordPress/gutenberg#73765.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.