Skip to content

Commit

Permalink
Update PHP filters for block editor to be a type agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Apr 22, 2021
1 parent bce6109 commit 77a3658
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
14 changes: 7 additions & 7 deletions lib/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function gutenberg_get_block_categories( $editor_name_or_post ) {
*
* @param array[] $default_categories Array of categories for block types.
*/
$block_categories = apply_filters( "block_categories_{$editor_name}", $default_categories );
$block_categories = apply_filters( 'block_categories_all', $default_categories );
if ( 'post-editor' === $editor_name ) {
$post = is_object( $editor_name_or_post ) ? $editor_name_or_post : get_post();

Expand All @@ -99,7 +99,7 @@ function gutenberg_get_block_categories( $editor_name_or_post ) {
* @param array[] $block_categories Array of categories for block types.
* @param WP_Post $post Post being loaded.
*/
$block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', "block_categories_{$editor_name}" );
$block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', 'block_categories_all' );
}

return $block_categories;
Expand Down Expand Up @@ -131,7 +131,7 @@ function gutenberg_get_allowed_block_types( $editor_name ) {
* @param bool|array $allowed_block_types Array of block type slugs, or
* boolean to enable/disable all.
*/
$allowed_block_types = apply_filters( "allowed_block_types_{$editor_name}", $allowed_block_types );
$allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types );
if ( 'post-editor' === $editor_name ) {
$post = get_post();

Expand All @@ -146,7 +146,7 @@ function gutenberg_get_allowed_block_types( $editor_name ) {
* boolean to enable/disable all.
* @param WP_Post $post The post resource data.
*/
$allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', "allowed_block_types_{$editor_name}" );
$allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', 'allowed_block_types_all' );
}

return $allowed_block_types;
Expand Down Expand Up @@ -267,13 +267,13 @@ function gutenberg_get_block_editor_settings( $editor_name, $custom_settings = a
);

/**
* Filters the settings to pass to the block editor for a given editor type.
* Filters the settings to pass to the block editor for all editor types.
*
* @since 5.8.0
*
* @param array $editor_settings Default editor settings.
*/
$editor_settings = apply_filters( "block_editor_settings_{$editor_name}", $editor_settings );
$editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings );
if ( 'post-editor' === $editor_name ) {
$post = get_post();

Expand All @@ -286,7 +286,7 @@ function gutenberg_get_block_editor_settings( $editor_name, $custom_settings = a
* @param array $editor_settings Default editor settings.
* @param WP_Post $post Post being edited.
*/
$editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', "block_editor_settings_{$editor_name}" );
$editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', 'block_editor_settings_all' );
}

return $editor_settings;
Expand Down
24 changes: 7 additions & 17 deletions phpunit/block-editor-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,7 @@ function test_get_default_block_editor_settings() {
/**
* @ticket 52920
*/
function test_get_block_editor_settings_returns_default_settings() {
$this->assertSameSets(
gutenberg_get_block_editor_settings( 'my-editor' ),
gutenberg_get_default_block_editor_settings()
);
}

/**
* @ticket 52920
*/
function test_get_block_editor_settings_overrides_default_settings_my_editor() {
function test_get_block_editor_settings_overrides_default_settings_all_editors() {
function filter_allowed_block_types_my_editor() {
return array( 'test/filtered-my-block' );
}
Expand All @@ -271,15 +261,15 @@ function filter_block_editor_settings_my_editor( $editor_settings ) {
return $editor_settings;
}

add_filter( 'allowed_block_types_my-editor', 'filter_allowed_block_types_my_editor', 10, 1 );
add_filter( 'block_categories_my-editor', 'filter_block_categories_my_editor', 10, 1 );
add_filter( 'block_editor_settings_my-editor', 'filter_block_editor_settings_my_editor', 10, 1 );
add_filter( 'allowed_block_types_all', 'filter_allowed_block_types_my_editor', 10, 1 );
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 = gutenberg_get_block_editor_settings( 'my-editor' );

remove_filter( 'allowed_block_types_my-editor', 'filter_allowed_block_types_my_editor' );
remove_filter( 'block_categories_my-editor', 'filter_block_categories_my_editor' );
remove_filter( 'block_editor_settings_my-editor', 'filter_block_editor_settings_my_editor' );
remove_filter( 'allowed_block_types_all', 'filter_allowed_block_types_my_editor' );
remove_filter( 'block_categories_all', 'filter_block_categories_my_editor' );
remove_filter( 'block_editor_settings_all', 'filter_block_editor_settings_my_editor' );

$this->assertSameSets( array( 'test/filtered-my-block' ), $settings['allowedBlockTypes'] );
$this->assertSameSets(
Expand Down

0 comments on commit 77a3658

Please sign in to comment.