-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add WP_Block_Editor_Context::$name #2374
Add WP_Block_Editor_Context::$name #2374
Conversation
Adds new `WP_Block_Editor_Context::$type` and `WP_Block_Editor_Context::$is_customizer` fields. This allows plugin developers to tell which block editor is being loaded when using filters such as `allowed_block_types_all` and `block_editor_rest_api_preload_paths`.
The part I'm most unsure about here is If the widget editor add_filter(
'allowed_block_types_all',
function( $allowed_block_types, $block_editor_context ) {
if (
'customize-widgets' === $block_editor_context->type ||
'edit-widgets' === $block_editor_context->type
) {
return array( 'core/paragraph', 'core/heading' );
}
return $allowed_block_types;
},
10,
2
); However, I'm not sure if edit: another option for consistency is to use the names that we're using in JavaScript ('core/edit-post', 'core/edit-widgets', 'core/customize-widgets', 'core/edit-site'). The interface and preferences APIs are calling this 'scope'. Not sure if that's a good name. |
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'm glad we finally have enough data to improve the implementation of the WP_Block_Editor_Context
class. That was exactly the goal here, to be able to customize settings based on conditions like the editor screen type.
I will leave the details for the new class properties to @noisysocks and @talldan as they know better the requirements here. I just wanted to emphasize that the overall proposal is sound 👍🏻
I agree with @talldan we should only rely on |
I did consider this but I don't like it 😅 API design should nudge developers towards doing the right thing. I want developers to configure the block editor exactly the same way for the widgets editor in the widgets screen and the widgets editor in the customizer. They are the same editor and so should provide the same user experience. By having both
I'd argue that Moreover, Ideally I think abstract class WP_Block_Editor_Context {
}
class WP_Post_Block_Editor_Context extends WP_Block_Editor_Context {
public $post;
}
class WP_Widgets_Block_Editor_Context extends WP_Block_Editor_Context {
public $is_customizer;
}
class WP_Site_Block_Editor_Context extends WP_Block_Editor_Context {
} We can't do this though as it requires renaming So instead I'm leaning into |
My feeling on this isn't particularly strong, I just think this is based on a use case of one, and there are no concrete plans to add more customizer editors. Usually we work on the basis of being terrified by future maintainability, so it seems a little outside the norm to be this bold. Anyway, our editors are a bit weird, and I don't think names or types really mean that much. We have one 'post' editor that is used to also edit pages and any custom post type. We have a site editor that can literally edit anything and allowed blocks won't mean very much there. Then we have two widget editors that can only edit the same individual type of thing. If anything it might be best to work on the basis the data type being edited (which is I suppose what There's no real consistency, so we're probably only able to implement something with trade-offs. I think that's why a prefer to use names that are already in regular usage ( |
I do like that |
Let's make @draganescu decide 😛 |
Waiting for Andrei to decide... 🍿 🦗
Yes, that would work, too. It would be a bit harder to document it though because you would have 5 class names with different class properties. In the future, we might also have a site editor context that allows editing post content that will require |
OK, I updated this to use |
18820e9
to
a312776
Compare
Configures the Gutenberg plugin to set $name on WP_Block_Editor_Context wherever the plugin initializes an editor. This helps plugins identify which editor they are customizing in filters such as 'allowed_block_types_all'. Passing the extra array param is a no-op on versions of WordPress prior to 6.0 which do not have WP_Block_Editor_Context::$name. See WordPress/wordpress-develop#2374.
I opened WordPress/gutenberg#39299 in the Gutenberg repo to compliment this PR. It updates some usages of |
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.
This and the Gutenberg PR (WordPress/gutenberg#39299) look good to me 👍
Thanks all. Committed in https://core.trac.wordpress.org/changeset/52942. |
* Set $name on WP_Block_Editor_Context Configures the Gutenberg plugin to set $name on WP_Block_Editor_Context wherever the plugin initializes an editor. This helps plugins identify which editor they are customizing in filters such as 'allowed_block_types_all'. Passing the extra array param is a no-op on versions of WordPress prior to 6.0 which do not have WP_Block_Editor_Context::$name. See WordPress/wordpress-develop#2374. * Set $context->name depending on context query param
Is this available on the |
@joyously It doesn't look like it as that action doesn't seem to receive the block editor context:
It may be possible to add it—I'd recommend making a core trac ticket. |
Trac ticket: https://core.trac.wordpress.org/ticket/55301
Original Gutenberg issue: WordPress/gutenberg#28517
What this is
Adds new
WP_Block_Editor_Context::$type
andWP_Block_Editor_Context::$is_customizer
fields. This allows plugin developers to tell which block editor is being loaded when using filters such asallowed_block_types_all
andblock_editor_rest_api_preload_paths
.How to test
Here's some code you can put into e.g.
wp-content/mu-plugins/allowed-blocks.php
: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.