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

REST API: Update default values for fields in the block type schema #22695

Merged
merged 6 commits into from
Jun 2, 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
41 changes: 33 additions & 8 deletions docs/rfc/block-registration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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" }
]
Expand All @@ -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))
Expand Down Expand Up @@ -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:**

Expand All @@ -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"
}
```

Expand Down
48 changes: 30 additions & 18 deletions lib/class-wp-rest-block-types-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
'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 = '';
Expand Down Expand Up @@ -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',
),
Expand Down Expand Up @@ -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',
Expand All @@ -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,
),
Expand All @@ -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,
),
),
);

Expand Down
12 changes: 7 additions & 5 deletions phpunit/class-wp-rest-block-types-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 );
Expand All @@ -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 );
}

/**
Expand Down Expand Up @@ -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 ) {
Expand Down