[WIP] Handle All Asset Registration and Enqueueing For Blocks #5084
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related: #2751 #4514 #2756 #4841
Enhancement Overview
After my conversation with @youknowriad and @davidbhayes I really think that block registration should take the handle and url for assets. The block registration system (not
WP_Block_Type_Registry
, which as several concerns already) should be responsible for registering and enqueuing CSS and JavaScript for blocks.Description
This PR, if completed, will allow core or plugins or hosts to take alternative approaches in the future to dealing with problems such as:
I've prepared a quick PR of psuedo-code for what I'd like to propose. This isn't tested, I did not test it at all, or even include the files (sorry so used to autoloaders,) this is just a concept for discussion. I'll happily make it work, and provide unit tests and run PHP_CS on it if there is interest in this PR.
Proposed Change to register_block_type()
In the handbook's example:
Would change to:
Goals:
WP_Dependency
automatic by default, and totally extensible.What I've Done
WP_Block_Assets_Enqueue
handles registering the block's assets.WP_Registers_Block_Assets
this interface is important as the factory is written so you can provide ownWP_Registers_Block_Assets
.wp_register_script/style
at the right time.WP_Registers_Block_Assets
on thepre_register_block_assets
filter if they wish to optimize the process in some way.WP_Block_Assets_Enqueue
handles registering the block's assets.WP_Enqueues_Block_Assets
this interface is important as the factory is written so you can provide your ownWP_Enqueues_Block_Assets
.wp_enqueue_script/style
at the right time.WP_Enqueues_Block_Assets
on thepre_enqueue_block_assets
filter if they wish to optimize the process in some way.WP_Block_Type_Registry
I've implemented this new system, so that all hooks are added, by default.Everything?
This is the current best practice:
With this Gutenberg has everything it needs to call
wp_enqueue_script
at the right time, with the right handle. So, you could argue this is all we really need. So why did I make this new system do everything? I thinkWP_Dependency
is deficient for this task. Yes, I'm using it here, but I'd like to have a layer above it, so plugin developers can use a system that includes the following concepts:It's probably best not to use
wp_register_script
but instead, create something totally new. By wrapping the current dependency Gutenberg is creating - that it has no way to modify later - inWP_Dependency
we're not tied long-term to a system that was designed way back when we wrote JavaScript by opening up a file and writing JavaScript and then saving it. And people still do that, and that's fine,WP_Dependency
is fine for that.Discussion Questions
WP_Block_Type_Registry
track the factory and objects the factory creates? This is needed for making hooks removable,How Has This Been Tested?
Not at all. This code breaks things as-is, just wanted to discuss.
Checklist: