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

Editor: Abstract block editor configuration #1118

Closed
wants to merge 1 commit into from
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
3 changes: 1 addition & 2 deletions src/wp-admin/edit-form-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes;

$editor_name = 'post-editor';
$block_editor_context = new WP_Block_Editor_Context( array( 'post' => $post ) );

// Flag that we're loading the block editor.
Expand Down Expand Up @@ -280,7 +279,7 @@
unset( $editor_settings['enableCustomFields'] );
}

$editor_settings = get_block_editor_settings( $editor_name, $editor_settings );
$editor_settings = get_block_editor_settings( $editor_settings, $block_editor_context );

$init_script = <<<JS
( function() {
Expand Down
65 changes: 35 additions & 30 deletions src/wp-includes/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,33 @@ function get_default_block_categories() {
* Returns all the categories for block types that will be shown in the block editor.
*
* @since 5.0.0
* @since 5.8.0 It is possible to pass the block editor context as param.
*
* @param string|WP_Post $editor_name_or_post The name of the editor (e.g. 'post-editor')
* or the post object.
* @param WP_Post|WP_Block_Editor_Context $post_or_block_editor_context The current post object or
* the block editor context.
*
* @return array[] Array of categories for block types.
*/
function get_block_categories( $editor_name_or_post ) {
// Assume the post editor when the WP_Post object passed.
$editor_name = is_object( $editor_name_or_post ) ? 'post-editor' : $editor_name_or_post;
$block_categories = get_default_block_categories();
function get_block_categories( $post_or_block_editor_context ) {
$block_categories = get_default_block_categories();
$block_editor_context = $post_or_block_editor_context instanceof WP_Post ?
new WP_Block_Editor_Context(
array(
'post' => $post_or_block_editor_context,
)
) : $post_or_block_editor_context;

/**
* Filters the default array of categories for block types.
*
* @since 5.8.0
*
* @param array[] $block_categories Array of categories for block types.
* @param string $editor_name The name of the editor, e.g. 'post-editor'.
* @param array[] $block_categories Array of categories for block types.
* @param WP_Block_Editor_Context $block_editor_context The current block editor context.
*/
$block_categories = apply_filters( 'block_categories_all', $block_categories, $editor_name );
if ( 'post-editor' === $editor_name ) {
$post = is_object( $editor_name_or_post ) ? $editor_name_or_post : get_post();
$block_categories = apply_filters( 'block_categories_all', $block_categories, $block_editor_context );
if ( ! empty( $block_editor_context->post ) ) {
$post = $block_editor_context->post;

/**
* Filters the default array of categories for block types.
Expand All @@ -101,11 +106,11 @@ function get_block_categories( $editor_name_or_post ) {
*
* @since 5.8.0
*
* @param string $editor_name The name of the editor (e.g. 'post-editor').
* @param WP_Block_Editor_Context $block_editor_context The current block editor context.
*
* @return bool|array Array of block type slugs, or boolean to enable/disable all.
*/
function get_allowed_block_types( $editor_name ) {
function get_allowed_block_types( $block_editor_context ) {
$allowed_block_types = true;

/**
Expand All @@ -115,13 +120,13 @@ function get_allowed_block_types( $editor_name ) {
*
* @since 5.8.0
*
* @param bool|array $allowed_block_types Array of block type slugs, or
* boolean to enable/disable all.
* @param string $editor_name The name of the editor, e.g. 'post-editor'.
* @param bool|array $allowed_block_types Array of block type slugs, or
* boolean to enable/disable all.
* @param WP_Block_Editor_Context $block_editor_context The current block editor context.
*/
$allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $editor_name );
if ( 'post-editor' === $editor_name ) {
$post = get_post();
$allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $block_editor_context );
if ( ! empty( $block_editor_context->post ) ) {
$post = $block_editor_context->post;

/**
* Filters the allowed block types for the editor, defaulting to true (all
Expand Down Expand Up @@ -224,21 +229,21 @@ function get_default_block_editor_settings() {
}

/**
* Returns the contextualized block editor settings settings for a selected editor type.
* Returns the contextualized block editor settings settings for a selected editor context.
*
* @since 5.8.0
*
* @param string $editor_name The name of the editor (e.g. 'post-editor').
* @param array $custom_settings Optional custom settings to use with the editor type.
* @param array $custom_settings Optional custom settings to use with the editor type.
* @param WP_Block_Editor_Context $block_editor_context The current block editor context.
*
* @return array The contextualized block editor settings.
*/
function get_block_editor_settings( $editor_name, $custom_settings = array() ) {
function get_block_editor_settings( array $custom_settings, $block_editor_context ) {
$editor_settings = array_merge(
get_default_block_editor_settings(),
array(
'allowedBlockTypes' => get_allowed_block_types( $editor_name ),
'blockCategories' => get_block_categories( $editor_name ),
'allowedBlockTypes' => get_allowed_block_types( $block_editor_context ),
'blockCategories' => get_block_categories( $block_editor_context ),
),
$custom_settings
);
Expand Down Expand Up @@ -292,12 +297,12 @@ function get_block_editor_settings( $editor_name, $custom_settings = array() ) {
*
* @since 5.8.0
*
* @param array $editor_settings Default editor settings.
* @param string $editor_name The name of the editor, e.g. 'post-editor'.
* @param array $editor_settings Default editor settings.
* @param WP_Block_Editor_Context $block_editor_context The current block editor context.
*/
$editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $editor_name );
if ( 'post-editor' === $editor_name ) {
$post = get_post();
$editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context );
if ( ! empty( $block_editor_context->post ) ) {
$post = $block_editor_context->post;

/**
* Filters the settings to pass to the block editor.
Expand Down
31 changes: 18 additions & 13 deletions tests/phpunit/tests/blocks/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ function test_block_editor_context_no_settings() {
* @ticket 52920
*/
function test_block_editor_context_post() {
$context = new WP_Block_Editor_Context( array( 'post' => $this->post ) );
$context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );

$this->assertSame( $this->post, $context->post );
$this->assertSame( get_post(), $context->post );
}

/**
Expand Down Expand Up @@ -111,7 +111,8 @@ function test_get_block_categories_deprecated_filter_post_object() {
function test_get_block_categories_deprecated_filter_post_editor() {
add_filter( 'block_categories', array( $this, 'filter_set_block_categories_post' ), 10, 2 );

$block_categories = get_block_categories( 'post-editor' );
$post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
$block_categories = get_block_categories( $post_editor_context );

remove_filter( 'block_categories', array( $this, 'filter_set_block_categories_post' ) );

Expand All @@ -131,7 +132,8 @@ function test_get_block_categories_deprecated_filter_post_editor() {
* @ticket 52920
*/
function test_get_allowed_block_types_default() {
$allowed_block_types = get_allowed_block_types( 'post-editor' );
$post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
$allowed_block_types = get_allowed_block_types( $post_editor_context );

$this->assertTrue( $allowed_block_types );
}
Expand All @@ -143,7 +145,8 @@ function test_get_allowed_block_types_default() {
function test_get_allowed_block_types_deprecated_filter_post_editor() {
add_filter( 'allowed_block_types', array( $this, 'filter_set_allowed_block_types_post' ), 10, 2 );

$allowed_block_types = get_allowed_block_types( 'post-editor' );
$post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
$allowed_block_types = get_allowed_block_types( $post_editor_context );

remove_filter( 'allowed_block_types', array( $this, 'filter_set_allowed_block_types_post' ) );

Expand Down Expand Up @@ -279,7 +282,8 @@ function filter_block_editor_settings_my_editor( $editor_settings ) {
add_filter( 'block_categories_all', 'filter_block_categories_my_editor', 10, 1 );
add_filter( 'block_editor_settings_all', 'filter_block_editor_settings_my_editor', 10, 1 );

$settings = get_block_editor_settings( 'my-editor' );
$my_editor_context = new WP_Block_Editor_Context();
$settings = get_block_editor_settings( array(), $my_editor_context );

remove_filter( 'allowed_block_types_all', 'filter_allowed_block_types_my_editor' );
remove_filter( 'block_categories_all', 'filter_block_categories_my_editor' );
Expand All @@ -306,7 +310,8 @@ function filter_block_editor_settings_my_editor( $editor_settings ) {
function test_get_block_editor_settings_deprecated_filter_post_editor() {
add_filter( 'block_editor_settings', array( $this, 'filter_set_block_editor_settings_post' ), 10, 2 );

$settings = get_block_editor_settings( 'post-editor' );
$post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
$settings = get_block_editor_settings( array(), $post_editor_context );

remove_filter( 'block_editor_settings', array( $this, 'filter_set_block_editor_settings_post' ) );

Expand All @@ -322,8 +327,8 @@ function test_get_block_editor_settings_deprecated_filter_post_editor() {
* @ticket 52920
*/
function test_block_editor_rest_api_preload_no_paths() {
$context = new WP_Block_Editor_Context();
block_editor_rest_api_preload( array(), $context );
$editor_context = new WP_Block_Editor_Context();
block_editor_rest_api_preload( array(), $editor_context );

$after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] );
$this->assertNotContains( 'wp.apiFetch.createPreloadingMiddleware', $after );
Expand All @@ -342,12 +347,12 @@ function filter_remove_preload_paths( $preload_paths, $post ) {
}
add_filter( 'block_editor_preload_paths', 'filter_remove_preload_paths', 10, 2 );

$context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
$post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) );
block_editor_rest_api_preload(
array(
array( '/wp/v2/blocks', 'OPTIONS' ),
),
$context
$post_editor_context
);

remove_filter( 'block_editor_preload_paths', 'filter_remove_preload_paths' );
Expand All @@ -369,12 +374,12 @@ function filter_add_preload_paths( $preload_paths, WP_Block_Editor_Context $cont
}
add_filter( 'block_editor_rest_api_preload_paths', 'filter_add_preload_paths', 10, 2 );

$context = new WP_Block_Editor_Context();
$editor_context = new WP_Block_Editor_Context();
block_editor_rest_api_preload(
array(
array( '/wp/v2/blocks', 'OPTIONS' ),
),
$context
$editor_context
);

remove_filter( 'block_editor_rest_api_preload_paths', 'filter_add_preload_paths' );
Expand Down