Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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( 'type' => '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( 'type' => 'widgets' ) );

$preload_paths = array(
array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
Expand Down
41 changes: 35 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,24 +7,47 @@
*/

/**
* 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. For example, which
* type of editor it is or what post is being edited.
*
* @since 5.8.0
*/
final class WP_Block_Editor_Context {
/**
* Post being edited. Optional.
* String that identifies which type of block editor is being rendered. Can
* be one of:
*
* - `'post'` - The post editor, at `/wp-admin/edit.php`.
* - `'widgets'` - The widgets editor, at `/wp-admin/widgets.php` or `/wp-admin/customize.php`.
* - `'site'` - The site editor, at `/wp-admin/site-editor.php`.
*
* Defaults to 'post'.
*
* @since 6.0.0
*
* @var string
*/
public $type = 'post';

/**
* The post being edited by the block editor. Optional.
*
* @since 5.8.0
*
* @var WP_Post|null
*/
public $post = null;

/**
* Whether the block editor is rendered within the Customizer at
* `/wp-admin/customize.php`. Defaults to `false`.
*
* @since 6.0.0
*
* @var bool
*/
public $is_customizer = false;

/**
* Constructor.
*
Expand All @@ -35,8 +58,14 @@ 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['type'] ) ) {
$this->type = $settings['type'];
}
if ( isset( $settings['post'] ) ) {
$this->post = $settings['post'];
}
if ( isset( $settings['is_customizer'] ) ) {
$this->is_customizer = $settings['is_customizer'];
}
}
}
7 changes: 6 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,12 @@ 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(
'type' => 'widgets',
'is_customizer' => true,
)
);

$editor_settings = get_block_editor_settings(
get_legacy_widget_block_editor_settings(),
Expand Down
42 changes: 42 additions & 0 deletions tests/phpunit/tests/blocks/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ 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( 'post', $context->type );
$this->assertNull( $context->post );
$this->assertFalse( $context->is_customizer );
}

/**
Expand All @@ -89,7 +91,47 @@ 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( 'post', $context->type );
$this->assertSame( get_post(), $context->post );
$this->assertFalse( $context->is_customizer );
}

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

$this->assertSame( 'widgets', $context->type );
$this->assertNull( $context->post );
$this->assertFalse( $context->is_customizer );
}

/**
* @ticket 55301
*/
public function test_block_editor_context_widgets_customizer() {
$context = new WP_Block_Editor_Context(
array(
'type' => 'widgets',
'is_customizer' => true,
)
);

$this->assertSame( 'widgets', $context->type );
$this->assertNull( $context->post );
$this->assertTrue( $context->is_customizer );
}

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

$this->assertSame( 'site', $context->type );
$this->assertNull( $context->post );
$this->assertFalse( $context->is_customizer );
}

/**
Expand Down