Skip to content

Conversation

@noisysocks
Copy link
Member

What?

Compliments WordPress/wordpress-develop#2374 by configuring the Gutenberg plugin to set $name on WP_Block_Editor_Context wherever the plugin initialises an editor.

Why?

It's anticipated that WP_Block_Editor_Context::$name will land in WP 6.0. See WordPress/wordpress-develop#2374. It was added so that plugins can identify which editor they are customising when using filters such as allowed_block_types_all. See #28517.

How?

Passes 'name' to the constructor of WP_Block_Editor_Context. This is a safe no-op in versions of WordPress prior to 6.0
which won't have WP_Block_Editor_Context::$name.

Testing Instructions

Here's some code you can put into e.g. wp-content/mu-plugins/allowed-blocks.php:

<?php
add_filter(
	'allowed_block_types_all',
	function( $allowed_block_types, $block_editor_context ) {
		if ( 'core/edit-site' === $block_editor_context->name ) {
			return array( 'core/paragraph' );
		}
		if ( 'core/edit-widgets' === $block_editor_context->name ) {
			return array( 'core/heading' );
		}
		if ( 'core/customize-widgets' === $block_editor_context->name ) {
			return array( 'core/image' );
		}
		if ( 'core/edit-post' === $block_editor_context->name ) {
			return array( 'core/separator' );
		}
		return $allowed_block_types;
	},
	10,
	2
);

With the above plugin running, activate the plugin and navigate to Appearance → Editor. You should only be allowed to add a Paragraph block.

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.
@noisysocks noisysocks added [Type] Enhancement A suggestion for improvement. [Feature] Extensibility The ability to extend blocks or the editing experience labels Mar 9, 2022
$settings = gutenberg_get_block_editor_settings( $settings );

$context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-navigation' ) );
$settings = get_block_editor_settings( $custom_settings, $context );
Copy link
Member Author

Choose a reason for hiding this comment

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

Updated the Navigation screen to use get_block_editor_settings which is the more canonical way to do this. Difficult to test though since the Navigation screen is mostly borked 😅

*/
public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis
$editor_context = new WP_Block_Editor_Context();
$editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );
Copy link
Member Author

Choose a reason for hiding this comment

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

This one is tricky. Technically there's no screen at all since it's just a REST API endpoint. I did some digging and this endpoint is used by the mobile apps for Global Styles (#29063), so I think using core/edit-site makes sense.

Copy link
Contributor

@talldan talldan Mar 9, 2022

Choose a reason for hiding this comment

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

Is this the same thing as the 'site' entity?

{
label: __( 'Site' ),
name: 'site',
kind: 'root',
baseURL: '/wp/v2/settings',
getTitle: ( record ) => {
return get( record, [ 'title' ], __( 'Site Title' ) );
},
},

edit: Oh, no, different namespace.

Copy link
Contributor

Choose a reason for hiding this comment

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

Feels like it probably shouldn't have a name, or the name should be passed as a param to the REST request.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is this the same thing as the 'site' entity?

It's not, no. I can't see anywhere where this endpoint is being used but I assume from the issue (#29063) that it's used by the mobile app somehow.

Copy link
Member Author

Choose a reason for hiding this comment

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

Feels like it probably shouldn't have a name

Yeah maybe name needs to be optional after all 😕

Copy link
Member

Choose a reason for hiding this comment

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

@geriux worked on the mobile integration so he might be the best person to share recommendations here.

Copy link
Member

Choose a reason for hiding this comment

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

I can't see anywhere where this endpoint is being used but I assume from the issue (#29063) that it's used by the mobile app somehow.

That's correct, this is currently used on the WordPress mobile app to load the global styles data.

Copy link
Member

Choose a reason for hiding this comment

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

In regards to the name, the one that's currently set core/edit-site doesn't affect the response data we need so that works.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for confirming @geriux.

I looked at #29969 more closely and realised that this endpoint already has a 'context' param which we can use to set $context->name correctly! Done in e739e41.

Copy link
Contributor

@talldan talldan left a comment

Choose a reason for hiding this comment

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

This and the wordpress-develop PR (WordPress/wordpress-develop#2374) look good to me 👍

@gziolo gziolo marked this pull request as ready for review March 15, 2022 16:20
@noisysocks
Copy link
Member Author

Thanks everyone. https://core.trac.wordpress.org/changeset/52942 committed the Core component of this. We may as well get these changes in Gutenberg, though they'll be a no-op if Gutenberg is not running against latest Core trunk.

@noisysocks noisysocks merged commit 6487569 into trunk Mar 17, 2022
@noisysocks noisysocks deleted the add/name-to-block-editor-context branch March 17, 2022 03:37
@github-actions github-actions bot added this to the Gutenberg 12.9 milestone Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Extensibility The ability to extend blocks or the editing experience [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants