diff --git a/docs/rfc/block-registration.md b/docs/rfc/block-registration.md index 2a39d2571b9146..2313fbdfe48d9d 100644 --- a/docs/rfc/block-registration.md +++ b/docs/rfc/block-registration.md @@ -6,7 +6,7 @@ This RFC is intended to serve both as a specification and as documentation for t Behind any block type registration is some abstract concept of a unit of content. This content type can be described without consideration of any particular technology. In much the same way, we should be able to describe the core constructs of a block type in a way which can be interpreted in any runtime. -In more practical terms, an implementation should fulfill requirements that... +In more practical terms, an implementation should fulfill requirements that: * A block type registration should be declarative and context-agnostic. Any runtime (PHP, JS, or other) should be able to interpret the basics of a block type (see "Block API" in the sections below) and should be able to fetch or retrieve the definitions of the context-specific implementation details. The following things should be made possible: * Fetching the available block types through REST APIs. @@ -16,7 +16,7 @@ In more practical terms, an implementation should fulfill requirements that... It can statically analyze the files of any plugin to retrieve blocks and their properties. * It should not require a build tool compilation step (e.g. Babel, Webpack) to author code which would be referenced in a block type definition. -* There should allow the potential to dynamically load ("lazy-load") block types, or parts of block type definitions. It practical terms, it means that the editor should be able to be loaded without enqueuing all the assets (scripts and styles) of all block types. What it needs is the basic metadata (`title`, `description`, `category`, `icon`, etc...) to start with. It should be fine to defer loading all other code (`edit`, `save`, `transforms`, and other JavaScript implementations) until it is explicitly used (inserted into the post content). +* There should allow the potential to dynamically load ("lazy-load") block types, or parts of block type definitions. It practical terms, it means that the editor should be able to be loaded without enqueuing all the assets (scripts and styles) of all block types. What it needs is the basic metadata (`title`, `description`, `category`, `icon`, etc…) to start with. It should be fine to defer loading all other code (`edit`, `save`, `transforms`, and other JavaScript implementations) until it is explicitly used (inserted into the post content). ## References @@ -67,7 +67,7 @@ To register a new block type, start by creating a `block.json` file. This file: "category": "common", "parent": [ "core/group" ], "icon": "star", - "description": "Shows warning, error or success notices ...", + "description": "Shows warning, error or success notices…", "keywords": [ "alert", "message" ], "textDomain": "my-plugin", "attributes": { diff --git a/lib/compat.php b/lib/compat.php index d988fee7ec0e25..600c2ff6f8c45c 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -66,12 +66,15 @@ function register_block_type_from_metadata( $path, $args = array() ) { $block_dir . '/' . substr_replace( $editor_script, '.asset.php', -3 ) ); if ( ! file_exists( $editor_script_asset_path ) ) { - /* translators: %s: Block name. */ - $message = sprintf( __( 'The asset file for the "editorScript" defined in "%s" block definition is missing.' ), $block_name ); + $message = sprintf( + /* translators: %s: Block name. */ + __( 'The asset file for the "editorScript" defined in "%s" block definition is missing.', 'default' ), + $block_name + ); _doing_it_wrong( __METHOD__, $message, '5.5.0' ); return false; } - $editor_script_asset = require( $editor_script_asset_path ); + $editor_script_asset = require( $editor_script_asset_path ); wp_register_script( $editor_script_handle, plugins_url( $editor_script, $metadata_file ), @@ -88,12 +91,15 @@ function register_block_type_from_metadata( $path, $args = array() ) { $block_dir . '/' . substr_replace( $script, '.asset.php', -3 ) ); if ( ! file_exists( $script_asset_path ) ) { - /* translators: %s: Block name. */ - $message = sprintf( __( 'The asset file for the "script" defined in "%s" block definition is missing.' ), $block_name ); + $message = sprintf( + /* translators: %s: Block name. */ + __( 'The asset file for the "script" defined in "%s" block definition is missing.', 'default' ), + $block_name + ); _doing_it_wrong( __METHOD__, $message, '5.5.0' ); return false; } - $script_asset = require( $script_asset_path ); + $script_asset = require( $script_asset_path ); wp_register_script( $script_handle, plugins_url( $script, $metadata_file ), diff --git a/phpunit/class-register-block-type-from-metadata-test.php b/phpunit/class-register-block-type-from-metadata-test.php index e039c078d01640..4d881e7e36f0d7 100644 --- a/phpunit/class-register-block-type-from-metadata-test.php +++ b/phpunit/class-register-block-type-from-metadata-test.php @@ -32,14 +32,47 @@ function test_metadata_not_found_in_the_current_directory() { */ function test_block_registers_with_metadata_fixture() { $result = register_block_type_from_metadata( - __DIR__ . '/fixtures', - array( - 'foo' => 'bar', - ) + __DIR__ . '/fixtures' ); $this->assertInstanceOf( 'WP_Block_Type', $result ); - $this->assertEquals( 'test/block-name', $result->name ); - $this->assertEquals( 'bar', $result->foo ); + $this->assertEquals( 'my-plugin/notice', $result->name ); + $this->assertEquals( 'Notice', $result->title ); + $this->assertEquals( 'common', $result->category ); + $this->assertEquals( array( 'core/group' ), $result->parent ); + $this->assertEquals( 'star', $result->icon ); + $this->assertEquals( 'Shows warning, error or success notices…', $result->description ); + $this->assertEquals( array( 'alert', 'message' ), $result->keywords ); + $this->assertEquals( + array( + 'message' => array( + 'type' => 'string', + 'source' => 'html', + 'selector' => '.message', + ), + ), + $result->attributes + ); + $this->assertEquals( + array( + 'align' => true, + 'lightBlockWrapper' => true, + ), + $result->supports + ); + $this->assertEquals( + array( + array( + 'name' => 'default', + 'label' => 'Default', + 'isDefault' => true, + ), + array( + 'name' => 'other', + 'label' => 'Other', + ), + ), + $result->styles + ); } } diff --git a/phpunit/fixtures/block.json b/phpunit/fixtures/block.json index 3da9b88d722a27..f25264d1672fa2 100644 --- a/phpunit/fixtures/block.json +++ b/phpunit/fixtures/block.json @@ -1,4 +1,37 @@ { - "name": "test/block-name", - "category": "widgets" + "name": "my-plugin/notice", + "title": "Notice", + "category": "common", + "parent": [ + "core/group" + ], + "icon": "star", + "description": "Shows warning, error or success notices…", + "keywords": [ + "alert", + "message" + ], + "textDomain": "my-plugin", + "attributes": { + "message": { + "type": "string", + "source": "html", + "selector": ".message" + } + }, + "supports": { + "align": true, + "lightBlockWrapper": true + }, + "styleVariations": [ + { + "name": "default", + "label": "Default", + "isDefault": true + }, + { + "name": "other", + "label": "Other" + } + ] }