Skip to content

Commit

Permalink
Add support for passing a WP_Block_Type instance to register_block_ty…
Browse files Browse the repository at this point in the history
…pe().
  • Loading branch information
Felix Arntz committed Jul 2, 2017
1 parent 75a065a commit ca02a61
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
12 changes: 7 additions & 5 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
*
* @since 0.1.0
*
* @param string $name Block type name including namespace.
* @param array $args {
* Array of block type arguments. Any arguments may be defined, however the following
* ones are supported by default.
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
* complete WP_Block_Type instance. In case a WP_Block_Type
* is provided, the $args parameter will be ignored.
* @param array $args {
* Optional. Array of block type arguments. Any arguments may be defined, however the
* ones described below are supported by default. Default empty array.
*
* @type callable $render Callback used to render blocks of this block type.
* }
* @return WP_Block_Type|false The registered block type on success, or false on failure.
*/
function register_block_type( $name, $args ) {
function register_block_type( $name, $args = array() ) {
return WP_Block_Type_Registry::get_instance()->register( $name, $args );
}

Expand Down
22 changes: 16 additions & 6 deletions lib/class-wp-block-type-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,24 @@ final class WP_Block_Type_Registry {
* @since 0.2.0
* @access public
*
* @param string $name Block type name including namespace.
* @param array $args {
* Array of block type arguments. Any arguments may be defined, however the following
* ones are supported by default.
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
* complete WP_Block_Type instance. In case a WP_Block_Type
* is provided, the $args parameter will be ignored.
* @param array $args {
* Optional. Array of block type arguments. Any arguments may be defined, however the
* ones described below are supported by default. Default empty array.
*
* @type callable $render Callback used to render blocks of this block type.
* }
* @return WP_Block_Type|false The registered block type on success, or false on failure.
*/
public function register( $name, $args ) {
public function register( $name, $args = array() ) {
$block_type = null;
if ( is_a( $name, 'WP_Block_Type' ) ) {
$block_type = $name;
$name = $block_type->name;
}

if ( ! is_string( $name ) ) {
$message = __( 'Block type names must be strings.' );
_doing_it_wrong( __METHOD__, $message, '0.1.0' );
Expand All @@ -67,7 +75,9 @@ public function register( $name, $args ) {
return false;
}

$block_type = new WP_Block_Type( $name, $args );
if ( ! $block_type ) {
$block_type = new WP_Block_Type( $name, $args );
}

$this->registered_block_types[ $name ] = $block_type;

Expand Down
2 changes: 1 addition & 1 deletion lib/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see register_block_type()
*/
final class WP_Block_Type {
class WP_Block_Type {
/**
* Block type key.
*
Expand Down
3 changes: 3 additions & 0 deletions phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
*/
function _manually_load_plugin() {
require dirname( dirname( __FILE__ ) ) . '/gutenberg.php';

// Require dummy block type class for testing.
require_once dirname( __FILE__ ) . '/class-wp-dummy-block-type.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

Expand Down
10 changes: 10 additions & 0 deletions phpunit/class-block-type-registry-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ function test_register_block_type_twice() {
$this->assertFalse( $result );
}

/**
* Should accept a WP_Block_Type instance
*/
function test_register_block_type_instance() {
$block_type = new WP_Dummy_Block_Type( 'core/dummy' );

$result = $this->registry->register( $block_type );
$this->assertSame( $block_type, $result );
}

/**
* Unregistering should fail if a block is not registered
*
Expand Down
16 changes: 16 additions & 0 deletions phpunit/class-wp-dummy-block-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* WP_Dummy_Block_Type for testing
*
* @package Gutenberg
*/

/**
* Test class extending WP_Block_Type
*/
class WP_Dummy_Block_Type extends WP_Block_Type {

public function render( $attributes = array(), $content = '' ) {
return '<div>' . $content . '</div>';
}
}

0 comments on commit ca02a61

Please sign in to comment.