Skip to content

Commit

Permalink
Blocks: Make the block registration filterable on the server
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Mar 26, 2018
1 parent ffd9d53 commit 5ddf413
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 43 deletions.
14 changes: 2 additions & 12 deletions blocks/library/block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
6 changes: 5 additions & 1 deletion blocks/library/block/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
) );
24 changes: 0 additions & 24 deletions blocks/library/categories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
21 changes: 21 additions & 0 deletions blocks/library/categories/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
) );
}
Expand Down
6 changes: 0 additions & 6 deletions blocks/library/latest-posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
4 changes: 4 additions & 0 deletions blocks/library/latest-posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions lib/class-wp-block-type-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions lib/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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 );
}
}
39 changes: 39 additions & 0 deletions phpunit/class-block-type-registry-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 5ddf413

Please sign in to comment.