Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block API: Add new utility to register block types from metadata in PHP #20794

Merged
merged 2 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,42 @@
* @package gutenberg
*/

if ( ! function_exists( 'register_block_type_from_metadata' ) ) {
gziolo marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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
)
);
}
}

/**
* Adds a polyfill for the WHATWG URL in environments which do not support it.
* The intention in how this action is handled is under the assumption that this
Expand Down
14 changes: 4 additions & 10 deletions packages/block-library/src/post-author/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
}
Expand Down
22 changes: 8 additions & 14 deletions packages/block-library/src/post-comments-count/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
}
Expand Down
14 changes: 4 additions & 10 deletions packages/block-library/src/post-comments-form/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
}
Expand Down
14 changes: 4 additions & 10 deletions packages/block-library/src/post-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
}
Expand Down
14 changes: 4 additions & 10 deletions packages/block-library/src/post-date/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
}
Expand Down
14 changes: 4 additions & 10 deletions packages/block-library/src/post-excerpt/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
}
Expand Down
14 changes: 4 additions & 10 deletions packages/block-library/src/post-featured-image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
}
Expand Down
14 changes: 4 additions & 10 deletions packages/block-library/src/post-tags/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
}
Expand Down
14 changes: 4 additions & 10 deletions packages/block-library/src/post-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
}
Expand Down
13 changes: 4 additions & 9 deletions packages/block-library/src/shortcode/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
}
Expand Down
14 changes: 4 additions & 10 deletions packages/block-library/src/site-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
}
Expand Down
14 changes: 4 additions & 10 deletions packages/block-library/src/social-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,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',
)
);
}
Expand Down
45 changes: 45 additions & 0 deletions phpunit/class-register-block-type-from-metadata-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Test `register_block_type_from_metadata`.
*
* @package Gutenberg
*/

class Register_Block_Type_From_Metadata_Test extends WP_UnitTestCase {
/**
* Tests that the function returns false when the `block.json` is not found
* in the WordPress core.
*/
function test_metadata_not_found_in_wordpress_core() {
$result = register_block_type_from_metadata( 'unknown' );

$this->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 );
}
gziolo marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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 );
}
}
4 changes: 4 additions & 0 deletions phpunit/fixtures/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "test/block-name",
"category": "widgets"
}