From 5ddf41383a3ee2cdbbfe24fa771ac32b8b37ffa2 Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Mon, 26 Mar 2018 16:15:53 +0200 Subject: [PATCH] Blocks: Make the block registration filterable on the server --- blocks/library/block/index.js | 14 ++------ blocks/library/block/index.php | 6 +++- blocks/library/categories/index.js | 24 ------------- blocks/library/categories/index.php | 21 ++++++++++++ blocks/library/latest-posts/index.js | 6 ---- blocks/library/latest-posts/index.php | 4 +++ lib/class-wp-block-type-registry.php | 2 ++ lib/class-wp-block-type.php | 34 +++++++++++++++++++ phpunit/class-block-type-registry-test.php | 39 ++++++++++++++++++++++ 9 files changed, 107 insertions(+), 43 deletions(-) diff --git a/blocks/library/block/index.js b/blocks/library/block/index.js index fc6c8ef1c9ae42..5f2bf19366cdaf 100644 --- a/blocks/library/block/index.js +++ b/blocks/library/block/index.js @@ -172,20 +172,10 @@ export const name = 'core/block'; export const settings = { title: __( 'Shared Block' ), - category: 'shared', - isPrivate: true, - - attributes: { - ref: { - type: 'number', - }, - }, - supports: { - customClassName: false, - html: false, - }, + isPrivate: true, edit: EnhancedReusableBlockEdit, + save: () => null, }; diff --git a/blocks/library/block/index.php b/blocks/library/block/index.php index 98807d0bd04284..d9e079d24d2bfa 100644 --- a/blocks/library/block/index.php +++ b/blocks/library/block/index.php @@ -33,11 +33,15 @@ function gutenberg_render_block_core_reusable_block( $attributes ) { } register_block_type( 'core/block', array( + 'category' => 'shared', + 'supports' => array( + 'customClassName' => false, + 'html' => false, + ), 'attributes' => array( 'ref' => array( 'type' => 'number', ), ), - 'render_callback' => 'gutenberg_render_block_core_reusable_block', ) ); diff --git a/blocks/library/categories/index.js b/blocks/library/categories/index.js index d180b7df245df7..83f9dad2ef769b 100644 --- a/blocks/library/categories/index.js +++ b/blocks/library/categories/index.js @@ -19,30 +19,6 @@ export const settings = { icon: 'list-view', - category: 'widgets', - - attributes: { - showPostCounts: { - type: 'boolean', - default: false, - }, - displayAsDropdown: { - type: 'boolean', - default: false, - }, - showHierarchy: { - type: 'boolean', - default: false, - }, - align: { - type: 'string', - }, - }, - - supports: { - html: false, - }, - getEditWrapperProps( attributes ) { const { align } = attributes; if ( 'left' === align || 'right' === align || 'full' === align ) { diff --git a/blocks/library/categories/index.php b/blocks/library/categories/index.php index 61dc720fbe4590..4b16003135c9fa 100644 --- a/blocks/library/categories/index.php +++ b/blocks/library/categories/index.php @@ -89,6 +89,27 @@ function onCatChange() { */ function register_block_core_categories() { register_block_type( 'core/categories', array( + 'category' => 'widgets', + 'supports' => array( + 'html' => false, + ), + 'attributes' => array( + 'showPostCounts' => array( + 'type' => 'boolean', + 'default' => false, + ), + 'displayAsDropdown' => array( + 'type' => 'boolean', + 'default' => false, + ), + 'showHierarchy' => array( + 'type' => 'boolean', + 'default' => false, + ), + 'align' => array( + 'type' => 'string', + ), + ), 'render_callback' => 'render_block_core_categories', ) ); } diff --git a/blocks/library/latest-posts/index.js b/blocks/library/latest-posts/index.js index 3762b61e2a68b9..b78c6581247cba 100644 --- a/blocks/library/latest-posts/index.js +++ b/blocks/library/latest-posts/index.js @@ -19,14 +19,8 @@ export const settings = { icon: 'list-view', - category: 'widgets', - keywords: [ __( 'recent posts' ) ], - supports: { - html: false, - }, - getEditWrapperProps( attributes ) { const { align } = attributes; if ( 'left' === align || 'right' === align || 'wide' === align || 'full' === align ) { diff --git a/blocks/library/latest-posts/index.php b/blocks/library/latest-posts/index.php index bb06d4b4fe5a5e..cd103e632783d8 100644 --- a/blocks/library/latest-posts/index.php +++ b/blocks/library/latest-posts/index.php @@ -70,6 +70,10 @@ function render_block_core_latest_posts( $attributes ) { */ function register_block_core_latest_posts() { register_block_type( 'core/latest-posts', array( + 'category' => 'widgets', + 'supports' => array( + 'html' => false, + ), 'attributes' => array( 'categories' => array( 'type' => 'string', diff --git a/lib/class-wp-block-type-registry.php b/lib/class-wp-block-type-registry.php index 2e91b53933a72b..f263256d080913 100644 --- a/lib/class-wp-block-type-registry.php +++ b/lib/class-wp-block-type-registry.php @@ -86,6 +86,8 @@ public function register( $name, $args = array() ) { $block_type = new WP_Block_Type( $name, $args ); } + $block_type->filter_settings(); + $this->registered_block_types[ $name ] = $block_type; return $block_type; diff --git a/lib/class-wp-block-type.php b/lib/class-wp-block-type.php index 39f08e54fddc28..0d948419ce3706 100644 --- a/lib/class-wp-block-type.php +++ b/lib/class-wp-block-type.php @@ -22,6 +22,14 @@ class WP_Block_Type { */ public $name; + /** + * Block type category. + * + * @since 2.5.0 + * @var string + */ + public $category; + /** * Block type render callback. * @@ -30,6 +38,14 @@ class WP_Block_Type { */ public $render_callback; + /** + * Block type supports property schemas. + * + * @since 2.5.0 + * @var array + */ + public $supports; + /** * Block type attributes property schemas. * @@ -169,4 +185,22 @@ public function set_props( $args ) { $this->$property_name = $property_value; } } + + /** + * Filters settings for the block just before it gets registered. + * + * @since 2.5.0 + */ + public function filter_settings() { + if ( ! has_filter( 'register_block_type' ) ) { + return; + } + + $this->set_props( apply_filters( 'register_block_type', array( + 'category' => $this->category, + 'supports' => $this->supports, + 'attributes' => $this->attributes, + 'render_callback' => $this->render_callback, + ) ), $this->name ); + } } diff --git a/phpunit/class-block-type-registry-test.php b/phpunit/class-block-type-registry-test.php index 0ba75cd8d07e70..dbcf0e915e68a2 100644 --- a/phpunit/class-block-type-registry-test.php +++ b/phpunit/class-block-type-registry-test.php @@ -26,6 +26,7 @@ function setUp() { function tearDown() { parent::tearDown(); + remove_filter( 'register_block_type', array( $this, 'register_block_type_my_filter' ) ); $this->registry = null; } @@ -84,6 +85,44 @@ function test_register_block_type() { $this->assertEquals( $block_type, $this->registry->get_registered( $name ) ); } + function register_block_type_my_filter( $settings ) { + $settings['category'] = 'my-category'; + $settings['supports']['html'] = true; + $settings['attributes']['something']['default'] = 'my-value'; + $settings['render_callback'] = 'render_block_core_my_paragraph'; + + return $settings; + } + + /** + * Should update the block settings with the filter. + */ + function test_register_block_type_with_settings_filter() { + $name = 'core/paragraph'; + $settings = array( + 'category' => 'common', + 'supports' => array( + 'html' => false, + ), + 'attributes' => array( + 'something' => array( + 'type' => 'string', + ), + ), + 'render_callback' => 'render_block_core_paragraph', + ); + + add_filter( 'register_block_type', array( $this, 'register_block_type_my_filter' ) ); + + $block_type = $this->registry->register( $name, $settings ); + $this->assertEquals( $name, $block_type->name ); + $this->assertEquals( 'my-category', $block_type->category ); + $this->assertTrue( $block_type->supports['html'] ); + $this->assertEquals( 'my-value', $block_type->attributes['something']['default'] ); + $this->assertEquals( 'render_block_core_my_paragraph', $block_type->render_callback ); + $this->assertEquals( $block_type, $this->registry->get_registered( $name ) ); + } + /** * Should fail to re-register the same block *