Add WP_Block_Editor_Context::$name

Adds a new `WP_Block_Editor_Context::$name` and field. 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`.

Fixes #55301.
Props talldanwp, gziolo, andraganescu.

Built from https://develop.svn.wordpress.org/trunk@52942


git-svn-id: http://core.svn.wordpress.org/trunk@52531 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
noisysocks 2022-03-17 03:37:05 +00:00
parent 7e6d939911
commit d2c2dc1c10
5 changed files with 29 additions and 10 deletions

View File

@ -49,7 +49,7 @@ foreach ( get_default_block_template_types() as $slug => $template_type ) {
$indexed_template_types[] = $template_type; $indexed_template_types[] = $template_type;
} }
$block_editor_context = new WP_Block_Editor_Context(); $block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );
$custom_settings = array( $custom_settings = array(
'siteUrl' => site_url(), 'siteUrl' => site_url(),
'postsPerPage' => get_option( 'posts_per_page' ), 'postsPerPage' => get_option( 'posts_per_page' ),

View File

@ -15,7 +15,7 @@ if ( ! defined( 'ABSPATH' ) ) {
$current_screen = get_current_screen(); $current_screen = get_current_screen();
$current_screen->is_block_editor( true ); $current_screen->is_block_editor( true );
$block_editor_context = new WP_Block_Editor_Context(); $block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-widgets' ) );
$preload_paths = array( $preload_paths = array(
array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ), array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),

View File

@ -7,17 +7,29 @@
*/ */
/** /**
* Class representing a current block editor context. * Contains information about a block editor being rendered.
*
* The expectation is that block editor can have a different set
* of requirements on every screen where it is used. This class
* allows to define supporting settings that can be used with filters.
* *
* @since 5.8.0 * @since 5.8.0
*/ */
final class WP_Block_Editor_Context { final class WP_Block_Editor_Context {
/** /**
* Post being edited. Optional. * String that identifies the block editor being rendered. Can be one of:
*
* - `'core/edit-post'` - The post editor at `/wp-admin/edit.php`.
* - `'core/edit-widgets'` - The widgets editor at `/wp-admin/widgets.php`.
* - `'core/customize-widgets'` - The widgets editor at `/wp-admin/customize.php`.
* - `'core/edit-site'` - The site editor at `/wp-admin/site-editor.php`.
*
* Defaults to 'core/edit-post'.
*
* @since 6.0.0
*
* @var string
*/
public $name = 'core/edit-post';
/**
* The post being edited by the block editor. Optional.
* *
* @since 5.8.0 * @since 5.8.0
* *
@ -35,6 +47,9 @@ final class WP_Block_Editor_Context {
* @param array $settings The list of optional settings to expose in a given context. * @param array $settings The list of optional settings to expose in a given context.
*/ */
public function __construct( array $settings = array() ) { public function __construct( array $settings = array() ) {
if ( isset( $settings['name'] ) ) {
$this->name = $settings['name'];
}
if ( isset( $settings['post'] ) ) { if ( isset( $settings['post'] ) ) {
$this->post = $settings['post']; $this->post = $settings['post'];
} }

View File

@ -838,7 +838,11 @@ final class WP_Customize_Widgets {
*/ */
if ( wp_use_widgets_block_editor() ) { if ( wp_use_widgets_block_editor() ) {
$block_editor_context = new WP_Block_Editor_Context(); $block_editor_context = new WP_Block_Editor_Context(
array(
'name' => 'core/customize-widgets',
)
);
$editor_settings = get_block_editor_settings( $editor_settings = get_block_editor_settings(
get_legacy_widget_block_editor_settings(), get_legacy_widget_block_editor_settings(),

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.0-alpha-52941'; $wp_version = '6.0-alpha-52942';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.