-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Block API: Add support for block aliases (PoC) #6523
base: trunk
Are you sure you want to change the base?
Block API: Add support for block aliases (PoC) #6523
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. |
* @param array $aliases Array of registered aliases for a block type. | ||
* @param WP_Block_Type $block_type The full block type object. | ||
*/ | ||
return apply_filters( 'get_block_type_aliases', $this->aliases, $this ); |
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 know this already matches the existing pattern of using a filter similar to get_block_type_variations
, but it's always felt odd to me that there are not more declarative APIs to register variations or in this case, aliases.
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.
There is a method that is a bit more declarative, you can use the variation_callback
when registering the block itself which you can see an example of here.
It still feels different to the client-side registration where you can register them separately using an imported function.
I'd been largely unaware that we already have some mechanics for block variations in place on the server side. (I'd thought they were almost entirely confined to the client.) This comment by @ntsekouras helped me quite a bit. I'd love to keep the API surface as minimal as possible; if we already have I'm thus inclined to start small -- YAGNI and all. To avoid confusion of the alias and the existing name field ("Which do I use for what?"), how about we generate the alias from the canonical block type, with the variation name appended? E.g. the ➖ This has the drawback that 3rd-party plugins cannot use their namespace for variations they add to existing blocks; e.g. WordPress/gutenberg#43743 had the example of a ➕ The approach has the benefit that we wouldn't need to store an explicit reference to the underlying ("canonical") block's name (previously discussed here), as the canonical name is part of the alias -- WordPress will always know that 0️⃣ Another aspect of this approach is that all existing variations automatically get an alias assigned. I'm not sure that's a net negative or positive; it seems somewhat convenient at first glance, as extenders can simply start referring to variation aliases instead of being limited to "actual" block types; e.g. in the Block Hooks/generic blocks example (#6228), it was considered a drawback in terms of Developer Experience that block variation authors would need to manually create an alias for their variation. |
Some more notes on the The parser doesn't currently allow more than one slash, so we'd need to change that (see e.g.).
But that means that if we allow appending the slash-separated variation, things can get ambiguous. Consider
This can now mean two things:
I'm inclined to think that this ambiguity is not a deal-breaker, but I think that we'll need to have the first possibility always take precedence over the second (so that Core blocks can't be spoofed by "malicious" plugins). This will have the side effect that if there are any plugins that include blocks with a namespace that happens to collide with an existing core block name, they'll stop working. Not sure how much of a problem that could be. |
➕ On the plus side, we already seem to be creating class names for block variations that closely match the pattern we're considering for the aliases. For example, the |
Finally, using these "auto-generated" aliases is motivated by YAGNI -- it doesn't preclude allowing manual setting of aliases later. To keep the API simple, I'd add an (It could allow |
Gutenberg counterpart: WordPress/gutenberg#61481
This PR is a little more exploratory/hacky, I'm not sure quite yet how the end result will look but my thinking is that when we come across aliased blocks on the server I want them to also be treated like
WP_Block_Type
's and be aware of its canonical block, but also for the canonical block to know about all of its aliases.Trac ticket:
Related to: WordPress/gutenberg#43743
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.