Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wp-admin/site-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static function( $classes ) {
$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(
'siteUrl' => site_url(),
'postsPerPage' => get_option( 'posts_per_page' ),
Expand Down
2 changes: 1 addition & 1 deletion src/wp-admin/widgets-form-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
$current_screen = get_current_screen();
$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(
array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
Expand Down
27 changes: 21 additions & 6 deletions src/wp-includes/class-wp-block-editor-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,29 @@
*/

/**
* Class representing a current block editor context.
*
* 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.
* Contains information about a block editor being rendered.
*
* @since 5.8.0
*/
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
*
Expand All @@ -35,6 +47,9 @@ final class WP_Block_Editor_Context {
* @param array $settings The list of optional settings to expose in a given context.
*/
public function __construct( array $settings = array() ) {
if ( isset( $settings['name'] ) ) {
$this->name = $settings['name'];
}
if ( isset( $settings['post'] ) ) {
$this->post = $settings['post'];
}
Expand Down
6 changes: 5 additions & 1 deletion src/wp-includes/class-wp-customize-widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,11 @@ public function enqueue_scripts() {
*/

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(
get_legacy_widget_block_editor_settings(),
Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/tests/blocks/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function filter_set_block_editor_settings_post( $editor_settings, $post )
public function test_block_editor_context_no_settings() {
$context = new WP_Block_Editor_Context();

$this->assertSame( 'core/edit-post', $context->name );
$this->assertNull( $context->post );
}

Expand All @@ -89,9 +90,40 @@ public function test_block_editor_context_no_settings() {
public function test_block_editor_context_post() {
$context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );

$this->assertSame( 'core/edit-post', $context->name );
$this->assertSame( get_post(), $context->post );
}

/**
* @ticket 55301
*/
public function test_block_editor_context_widgets() {
$context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-widgets' ) );

$this->assertSame( 'core/edit-widgets', $context->name );
$this->assertNull( $context->post );
}

/**
* @ticket 55301
*/
public function test_block_editor_context_widgets_customizer() {
$context = new WP_Block_Editor_Context( array( 'name' => 'core/customize-widgets' ) );

$this->assertSame( 'core/customize-widgets', $context->name );
$this->assertNull( $context->post );
}

/**
* @ticket 55301
*/
public function test_block_editor_context_site() {
$context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );

$this->assertSame( 'core/edit-site', $context->name );
$this->assertNull( $context->post );
}

/**
* @ticket 52920
* @expectedDeprecated block_categories
Expand Down