From 2116931cac0327d7f2013be6fe11b86891e5c906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=B3=C5=82kowski?= Date: Tue, 2 Jun 2020 10:12:31 +0200 Subject: [PATCH] REST API: Update default values for fields in the block type schema (#22695) * REST API: Update default value for parent in the block type schema * Align changes with the WordPress core patch * Fix missed spelling change for textdomain * Fix copy&paste in block types REST API endpoint * Update lib/class-wp-rest-block-types-controller.php Co-authored-by: Jonny Harris * Fix issues raised by PHP linter Co-authored-by: Jonny Harris --- docs/rfc/block-registration.md | 41 ++++++++++++---- lib/class-wp-rest-block-types-controller.php | 48 ++++++++++++------- ...ss-wp-rest-block-types-controller-test.php | 12 +++-- 3 files changed, 70 insertions(+), 31 deletions(-) diff --git a/docs/rfc/block-registration.md b/docs/rfc/block-registration.md index 7455ed6b2f0398..63b42319598c20 100644 --- a/docs/rfc/block-registration.md +++ b/docs/rfc/block-registration.md @@ -69,7 +69,7 @@ To register a new block type, start by creating a `block.json` file. This file: "icon": "star", "description": "Shows warning, error or success notices ...", "keywords": [ "alert", "message" ], - "textDomain": "my-plugin", + "textdomain": "my-plugin", "attributes": { "message": { "type": "string", @@ -81,10 +81,15 @@ To register a new block type, start by creating a `block.json` file. This file: "align": true, "lightBlockWrapper": true }, - "styleVariations": [ + "styles": [ { "name": "default", "label": "Default", "isDefault": true }, { "name": "other", "label": "Other" } ], + "example": { + "attributes": { + "message": "This is a notice!" + }, + }, "editorScript": "build/editor.js", "script": "build/main.js", "editorStyle": "build/editor.css", @@ -210,10 +215,10 @@ Sometimes a block could have aliases that help users discover it while searching * Type: `string` * Optional * Localized: No -* Property: `textDomain` +* Property: `textdomain` ```json -{ "textDomain": "my-plugin" } +{ "textdomain": "my-plugin" } ``` The [gettext](https://www.gnu.org/software/gettext/) text domain of the plugin/block. More information can be found in the [Text Domain](https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#text-domains) section of the [How to Internationalize your Plugin](https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/) page. @@ -264,11 +269,10 @@ See the [the attributes documentation](/docs/designers-developers/developers/blo * Optional * Localized: Yes (`label` only) * Property: `styles` -* Alias: `styleVariations` ```json { - "styleVariations": [ + "styles": [ { "name": "default", "label": "Default", "isDefault": true }, { "name": "other", "label": "Other" } ] @@ -279,6 +283,27 @@ Block styles can be used to provide alternative styles to block. It works by add Plugins and Themes can also register [custom block style](/docs/designers-developers/developers/filters/block-filters.md#block-style-variations) for existing blocks. +### Example + + * Type: `object` + * Optional + * Localized: No + * Property: `example` + + ```json +{ + "example": { + "attributes": { + "message": "This is a notice!" + }, + } +} + ``` + + It provides structured example data for the block. This data is used to construct a preview for the block to be shown in the Inspector Help Panel when the user mouses over the block. + + See the [the example documentation](/docs/designers-developers/developers/block-api/block-registration.md#example-optional) for more details. + ### Editor Script * Type: `string` ([WPDefinedAsset](#WPDefinedAsset)) @@ -414,7 +439,7 @@ return array( Localized properties are automatically wrapped in `_x` function calls on the backend and the frontend of WordPress. These translations are added as an inline script to the `wp-block-library` script handle in WordPress core or to the plugin's script handle when it defines metadata definition. -WordPress string discovery automatically translates these strings using the `textDomain` property specified in the `block.json` file. +WordPress string discovery automatically translates these strings using the `textdomain` property specified in the `block.json` file. **Example:** @@ -423,7 +448,7 @@ WordPress string discovery automatically translates these strings using the `tex "title": "My block", "description": "My block is fantastic", "keywords": [ "fantastic" ], - "textDomain": "my-plugin" + "textdomain": "my-plugin" } ``` diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index 9246c2104b5713..cc1c831ba2ca71 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -240,25 +240,26 @@ public function prepare_item_for_response( $block_type, $request ) { $schema = $this->get_item_schema(); $extra_fields = array( 'name' => 'name', + 'title' => 'title', + 'description' => 'description', + 'icon' => 'icon', 'category' => 'category', + 'keywords' => 'keywords', + 'parent' => 'parent', + 'supports' => 'supports', + 'styles' => 'styles', + 'textdomain' => 'textdomain', + 'example' => 'example', 'editor_script' => 'editor_script', 'script' => 'script', 'editor_style' => 'editor_style', 'style' => 'style', - 'supports' => 'supports', - 'title' => 'title', - 'icon' => 'icon', - 'description' => 'description', - 'keywords' => 'keywords', - 'parent' => 'parent', - 'styles' => 'styleVariations', - 'text_domain' => 'textDomain', ); foreach ( $extra_fields as $key => $extra_field ) { if ( rest_is_field_included( $key, $fields ) ) { if ( isset( $block_type->$extra_field ) ) { $field = $block_type->$extra_field; - } elseif ( isset( $schema['properties'][ $key ]['default'] ) ) { + } elseif ( array_key_exists( 'default', $schema['properties'][ $key ] ) ) { $field = $schema['properties'][ $key ]['default']; } else { $field = ''; @@ -366,9 +367,9 @@ public function get_item_schema() { ), 'attributes' => array( 'description' => __( 'Block attributes.', 'gutenberg' ), - 'type' => 'object', + 'type' => array( 'object', 'null' ), 'properties' => array(), - 'default' => array(), + 'default' => null, 'additionalProperties' => array( 'type' => 'object', ), @@ -427,7 +428,7 @@ public function get_item_schema() { ), 'styles' => array( 'description' => __( 'Block style variations.', 'gutenberg' ), - 'type' => 'object', + 'type' => 'array', 'properties' => array(), 'additionalProperties' => array( 'type' => 'object', @@ -436,20 +437,20 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'text_domain' => array( + 'textdomain' => array( 'description' => __( 'Public text domain.', 'gutenberg' ), - 'type' => 'string', - 'default' => '', + 'type' => array( 'string', 'null' ), + 'default' => null, 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), 'parent' => array( - 'description' => __( 'Parent blocks, defaults to empty it no parents', 'gutenberg' ), - 'type' => 'array', + 'description' => __( 'Parent blocks.', 'gutenberg' ), + 'type' => array( 'array', 'null' ), 'items' => array( 'type' => 'string', ), - 'default' => array(), + 'default' => null, 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), @@ -463,6 +464,17 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), + 'example' => array( + 'description' => __( 'Block example.', 'gutenberg' ), + 'type' => array( 'object', 'null' ), + 'default' => null, + 'properties' => array(), + 'additionalProperties' => array( + 'type' => 'object', + ), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), ), ); diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index 19bab634427407..00b981c532ee0b 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -145,7 +145,7 @@ public function test_get_item_with_styles_merge() { 'style_handle' => 'myguten-style', ); $settings = array( - 'styleVariations' => array( + 'styles' => array( array( 'name' => 'blue-quote', 'label' => 'Blue Quote', @@ -218,13 +218,13 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertCount( 16, $properties ); + $this->assertCount( 17, $properties ); $this->assertArrayHasKey( 'title', $properties ); $this->assertArrayHasKey( 'icon', $properties ); $this->assertArrayHasKey( 'description', $properties ); $this->assertArrayHasKey( 'keywords', $properties ); $this->assertArrayHasKey( 'styles', $properties ); - $this->assertArrayHasKey( 'text_domain', $properties ); + $this->assertArrayHasKey( 'textdomain', $properties ); $this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'attributes', $properties ); $this->assertArrayHasKey( 'supports', $properties ); @@ -235,6 +235,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'editor_style', $properties ); $this->assertArrayHasKey( 'style', $properties ); $this->assertArrayHasKey( 'parent', $properties ); + $this->assertArrayHasKey( 'example', $properties ); } /** @@ -344,8 +345,9 @@ public function check_block_type_object( $block_type, $data, $links ) { 'description' => 'description', 'keywords' => 'keywords', 'parent' => 'parent', - 'styles' => 'styleVariations', - 'text_domain' => 'textDomain', + 'styles' => 'styles', + 'textdomain' => 'textdomain', + 'example' => 'example', ); foreach ( $extra_fields as $key => $extra_field ) {