diff --git a/lib/blocks.php b/lib/blocks.php index e30917096dcc5..8fecc848dd0e7 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -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 ); } diff --git a/lib/class-wp-block-type-registry.php b/lib/class-wp-block-type-registry.php index d1c63d182b537..c4244f09b749b 100644 --- a/lib/class-wp-block-type-registry.php +++ b/lib/class-wp-block-type-registry.php @@ -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' ); @@ -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; diff --git a/lib/class-wp-block-type.php b/lib/class-wp-block-type.php index 0193652352c05..a95a935f14186 100644 --- a/lib/class-wp-block-type.php +++ b/lib/class-wp-block-type.php @@ -13,7 +13,7 @@ * * @see register_block_type() */ -final class WP_Block_Type { +class WP_Block_Type { /** * Block type key. * diff --git a/phpunit/bootstrap.php b/phpunit/bootstrap.php index cb97777e2b12f..ec5f74bad3115 100644 --- a/phpunit/bootstrap.php +++ b/phpunit/bootstrap.php @@ -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' ); diff --git a/phpunit/class-block-type-registry-test.php b/phpunit/class-block-type-registry-test.php index 4fca2c54eb6f6..50f1550d8bbbb 100644 --- a/phpunit/class-block-type-registry-test.php +++ b/phpunit/class-block-type-registry-test.php @@ -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 * diff --git a/phpunit/class-wp-dummy-block-type.php b/phpunit/class-wp-dummy-block-type.php new file mode 100644 index 0000000000000..cdd697872044a --- /dev/null +++ b/phpunit/class-wp-dummy-block-type.php @@ -0,0 +1,16 @@ +' . $content . ''; + } +}