From 62953f6e1b047a75c21eba748d01e39521302899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=28Greg=29=20Zi=C3=B3=C5=82kowski?= Date: Thu, 9 Apr 2020 06:09:11 +0200 Subject: [PATCH] Block API: Add new utility to register block types from metadata in PHP (#20794) * Blocks: Add new utility to register block types from metadata in PHP * Add basic PHPUnit tests for block metadata registration --- lib/compat.php | 36 +++++++++++++++ .../block-library/src/post-author/index.php | 14 ++---- .../src/post-comments-count/index.php | 22 ++++----- .../src/post-comments-form/index.php | 14 ++---- .../block-library/src/post-content/index.php | 14 ++---- .../block-library/src/post-date/index.php | 14 ++---- .../block-library/src/post-excerpt/index.php | 14 ++---- .../src/post-featured-image/index.php | 14 ++---- .../block-library/src/post-tags/index.php | 14 ++---- .../block-library/src/post-title/index.php | 14 ++---- .../block-library/src/shortcode/index.php | 13 ++---- .../block-library/src/site-title/index.php | 14 ++---- .../block-library/src/social-link/index.php | 14 ++---- ...register-block-type-from-metadata-test.php | 45 +++++++++++++++++++ phpunit/fixtures/block.json | 4 ++ 15 files changed, 137 insertions(+), 123 deletions(-) create mode 100644 phpunit/class-register-block-type-from-metadata-test.php create mode 100644 phpunit/fixtures/block.json diff --git a/lib/compat.php b/lib/compat.php index 6e68f2134b2cff..80c3e460bf8bb2 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -8,6 +8,42 @@ * @package gutenberg */ +if ( ! function_exists( 'register_block_type_from_metadata' ) ) { + /** + * Registers a block type from metadata stored in the `block.json` file. + * + * @since 7.8.0 + * + * @param string $path Path to the folder where the `block.json` file is located. + * @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 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_from_metadata( $path, $args = array() ) { + $file = trailingslashit( $path ) . 'block.json'; + if ( ! file_exists( $file ) ) { + return false; + } + + $metadata = json_decode( file_get_contents( $file ), true ); + if ( ! is_array( $metadata ) ) { + return false; + } + + return register_block_type( + $metadata['name'], + array_merge( + $metadata, + $args + ) + ); + } +} + /** * Extends block editor settings to include a list of image dimensions per size. * diff --git a/packages/block-library/src/post-author/index.php b/packages/block-library/src/post-author/index.php index 6dc30ea5b4ba18..57f1895f6cb0c6 100644 --- a/packages/block-library/src/post-author/index.php +++ b/packages/block-library/src/post-author/index.php @@ -23,16 +23,10 @@ function render_block_core_post_author() { * Registers the `core/post-author` block on the server. */ function register_block_core_post_author() { - $path = __DIR__ . '/post-author/block.json'; - $metadata = json_decode( file_get_contents( $path ), true ); - - register_block_type( - $metadata['name'], - array_merge( - $metadata, - array( - 'render_callback' => 'render_block_core_post_author', - ) + register_block_type_from_metadata( + __DIR__ . '/post-author', + array( + 'render_callback' => 'render_block_core_post_author', ) ); } diff --git a/packages/block-library/src/post-comments-count/index.php b/packages/block-library/src/post-comments-count/index.php index 0c22f35068f175..a7e811b8157324 100644 --- a/packages/block-library/src/post-comments-count/index.php +++ b/packages/block-library/src/post-comments-count/index.php @@ -32,21 +32,15 @@ function render_block_core_post_comments_count( $attributes ) { * Registers the `core/post-comments-count` block on the server. */ function register_block_core_post_comments_count() { - $path = __DIR__ . '/post-comments-count/block.json'; - $metadata = json_decode( file_get_contents( $path ), true ); - - register_block_type( - $metadata['name'], - array_merge( - $metadata, - array( - 'attributes' => array( - 'className' => array( - 'type' => 'string', - ), + register_block_type_from_metadata( + __DIR__ . '/post-comments-count', + array( + 'attributes' => array( + 'className' => array( + 'type' => 'string', ), - 'render_callback' => 'render_block_core_post_comments_count', - ) + ), + 'render_callback' => 'render_block_core_post_comments_count', ) ); } diff --git a/packages/block-library/src/post-comments-form/index.php b/packages/block-library/src/post-comments-form/index.php index cb1aff17fc5af1..9d5e78cffa5cfa 100644 --- a/packages/block-library/src/post-comments-form/index.php +++ b/packages/block-library/src/post-comments-form/index.php @@ -26,16 +26,10 @@ function render_block_core_post_comments_form() { * Registers the `core/post-comments-form` block on the server. */ function register_block_core_post_comments_form() { - $path = __DIR__ . '/post-comments-form/block.json'; - $metadata = json_decode( file_get_contents( $path ), true ); - - register_block_type( - $metadata['name'], - array_merge( - $metadata, - array( - 'render_callback' => 'render_block_core_post_comments_form', - ) + register_block_type_from_metadata( + __DIR__ . '/post-comments-form', + array( + 'render_callback' => 'render_block_core_post_comments_form', ) ); } diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index 1e6870fd0b3134..dc7eebdf00ffa2 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -26,16 +26,10 @@ function render_block_core_post_content() { * Registers the `core/post-content` block on the server. */ function register_block_core_post_content() { - $path = __DIR__ . '/post-content/block.json'; - $metadata = json_decode( file_get_contents( $path ), true ); - - register_block_type( - $metadata['name'], - array_merge( - $metadata, - array( - 'render_callback' => 'render_block_core_post_content', - ) + register_block_type_from_metadata( + __DIR__ . '/post-content', + array( + 'render_callback' => 'render_block_core_post_content', ) ); } diff --git a/packages/block-library/src/post-date/index.php b/packages/block-library/src/post-date/index.php index 22e4c53e311eaf..dd86ff7e8c92ca 100644 --- a/packages/block-library/src/post-date/index.php +++ b/packages/block-library/src/post-date/index.php @@ -27,16 +27,10 @@ function render_block_core_post_date( $attributes ) { * Registers the `core/post-date` block on the server. */ function register_block_core_post_date() { - $path = __DIR__ . '/post-date/block.json'; - $metadata = json_decode( file_get_contents( $path ), true ); - - register_block_type( - $metadata['name'], - array_merge( - $metadata, - array( - 'render_callback' => 'render_block_core_post_date', - ) + register_block_type_from_metadata( + __DIR__ . '/post-date', + array( + 'render_callback' => 'render_block_core_post_date', ) ); } diff --git a/packages/block-library/src/post-excerpt/index.php b/packages/block-library/src/post-excerpt/index.php index 5d2ee7755beee4..7f5ab586670fa8 100644 --- a/packages/block-library/src/post-excerpt/index.php +++ b/packages/block-library/src/post-excerpt/index.php @@ -47,16 +47,10 @@ function render_block_core_post_excerpt( $attributes ) { * Registers the `core/post-excerpt` block on the server. */ function register_block_core_post_excerpt() { - $path = __DIR__ . '/post-excerpt/block.json'; - $metadata = json_decode( file_get_contents( $path ), true ); - - register_block_type( - $metadata['name'], - array_merge( - $metadata, - array( - 'render_callback' => 'render_block_core_post_excerpt', - ) + register_block_type_from_metadata( + __DIR__ . '/post-excerpt', + array( + 'render_callback' => 'render_block_core_post_excerpt', ) ); } diff --git a/packages/block-library/src/post-featured-image/index.php b/packages/block-library/src/post-featured-image/index.php index 4a03b2e4d30284..9a80c2525fa3ee 100644 --- a/packages/block-library/src/post-featured-image/index.php +++ b/packages/block-library/src/post-featured-image/index.php @@ -22,16 +22,10 @@ function render_block_core_post_featured_image() { * Registers the `core/post-featured-image` block on the server. */ function register_block_core_post_featured_image() { - $path = __DIR__ . '/post-featured-image/block.json'; - $metadata = json_decode( file_get_contents( $path ), true ); - - register_block_type( - $metadata['name'], - array_merge( - $metadata, - array( - 'render_callback' => 'render_block_core_post_featured_image', - ) + register_block_type_from_metadata( + __DIR__ . '/post-featured-image', + array( + 'render_callback' => 'render_block_core_post_featured_image', ) ); } diff --git a/packages/block-library/src/post-tags/index.php b/packages/block-library/src/post-tags/index.php index 4fff8d0014c840..ed03b6055f7cfa 100644 --- a/packages/block-library/src/post-tags/index.php +++ b/packages/block-library/src/post-tags/index.php @@ -29,16 +29,10 @@ function render_block_core_post_tags() { * Registers the `core/post-tags` block on the server. */ function register_block_core_post_tags() { - $path = __DIR__ . '/post-tags/block.json'; - $metadata = json_decode( file_get_contents( $path ), true ); - - register_block_type( - $metadata['name'], - array_merge( - $metadata, - array( - 'render_callback' => 'render_block_core_post_tags', - ) + register_block_type_from_metadata( + __DIR__ . '/post-tags', + array( + 'render_callback' => 'render_block_core_post_tags', ) ); } diff --git a/packages/block-library/src/post-title/index.php b/packages/block-library/src/post-title/index.php index c635ad2a0f0eb9..d1b6f245b2e627 100644 --- a/packages/block-library/src/post-title/index.php +++ b/packages/block-library/src/post-title/index.php @@ -22,16 +22,10 @@ function render_block_core_post_title() { * Registers the `core/post-title` block on the server. */ function register_block_core_post_title() { - $path = __DIR__ . '/post-title/block.json'; - $metadata = json_decode( file_get_contents( $path ), true ); - - register_block_type( - $metadata['name'], - array_merge( - $metadata, - array( - 'render_callback' => 'render_block_core_post_title', - ) + register_block_type_from_metadata( + __DIR__ . '/post-title', + array( + 'render_callback' => 'render_block_core_post_title', ) ); } diff --git a/packages/block-library/src/shortcode/index.php b/packages/block-library/src/shortcode/index.php index cfa8ab97dfa2bc..97a40b386d9c92 100644 --- a/packages/block-library/src/shortcode/index.php +++ b/packages/block-library/src/shortcode/index.php @@ -21,15 +21,10 @@ function render_block_core_shortcode( $attributes, $content ) { * Registers the `core/shortcode` block on server. */ function register_block_core_shortcode() { - $path = __DIR__ . '/shortcode/block.json'; - $metadata = json_decode( file_get_contents( $path ), true ); - register_block_type( - $metadata['name'], - array_merge( - $metadata, - array( - 'render_callback' => 'render_block_core_shortcode', - ) + register_block_type_from_metadata( + __DIR__ . '/shortcode', + array( + 'render_callback' => 'render_block_core_shortcode', ) ); } diff --git a/packages/block-library/src/site-title/index.php b/packages/block-library/src/site-title/index.php index a63760983436e2..7241bc50dffa56 100644 --- a/packages/block-library/src/site-title/index.php +++ b/packages/block-library/src/site-title/index.php @@ -24,16 +24,10 @@ function render_block_core_site_title( $attributes ) { * Registers the `core/site-title` block on the server. */ function register_block_core_site_title() { - $path = __DIR__ . '/site-title/block.json'; - $metadata = json_decode( file_get_contents( $path ), true ); - - register_block_type( - $metadata['name'], - array_merge( - $metadata, - array( - 'render_callback' => 'render_block_core_site_title', - ) + register_block_type_from_metadata( + __DIR__ . '/site-title', + array( + 'render_callback' => 'render_block_core_site_title', ) ); } diff --git a/packages/block-library/src/social-link/index.php b/packages/block-library/src/social-link/index.php index f10864a20ab0da..dc732a7a1f90ed 100644 --- a/packages/block-library/src/social-link/index.php +++ b/packages/block-library/src/social-link/index.php @@ -31,16 +31,10 @@ function render_block_core_social_link( $attributes ) { * Registers the `core/social-link` blocks. */ function register_block_core_social_link() { - $path = __DIR__ . '/social-link/block.json'; - $metadata = json_decode( file_get_contents( $path ), true ); - - register_block_type( - $metadata['name'], - array_merge( - $metadata, - array( - 'render_callback' => 'render_block_core_social_link', - ) + register_block_type_from_metadata( + __DIR__ . '/social-link', + array( + 'render_callback' => 'render_block_core_social_link', ) ); } diff --git a/phpunit/class-register-block-type-from-metadata-test.php b/phpunit/class-register-block-type-from-metadata-test.php new file mode 100644 index 00000000000000..e039c078d01640 --- /dev/null +++ b/phpunit/class-register-block-type-from-metadata-test.php @@ -0,0 +1,45 @@ +assertFalse( $result ); + } + + /** + * Tests that the function returns false when the `block.json` is not found + * in the current directory. + */ + function test_metadata_not_found_in_the_current_directory() { + $result = register_block_type_from_metadata( __DIR__ ); + + $this->assertFalse( $result ); + } + + /** + * Tests that the function returns the registered block when the `block.json` + * is found in the fixtures directory. + */ + function test_block_registers_with_metadata_fixture() { + $result = register_block_type_from_metadata( + __DIR__ . '/fixtures', + array( + 'foo' => 'bar', + ) + ); + + $this->assertInstanceOf( 'WP_Block_Type', $result ); + $this->assertEquals( 'test/block-name', $result->name ); + $this->assertEquals( 'bar', $result->foo ); + } +} diff --git a/phpunit/fixtures/block.json b/phpunit/fixtures/block.json new file mode 100644 index 00000000000000..3da9b88d722a27 --- /dev/null +++ b/phpunit/fixtures/block.json @@ -0,0 +1,4 @@ +{ + "name": "test/block-name", + "category": "widgets" +}