Skip to content

Commit bcf3aad

Browse files
gziolosarayourfriend
authored andcommitted
Block API: Apply enhancements included in WordPress 5.8 (#33252)
* Block API: Apply enhancements included in WordPress 5.8 See https://make.wordpress.org/core/2021/06/23/block-api-enhancements-in-wordpress-5-8/. * Document two new server-side filters to use when registering a block
1 parent cdd06b4 commit bcf3aad

File tree

10 files changed

+141
-44
lines changed

10 files changed

+141
-44
lines changed

docs/getting-started/tutorials/create-block/block-code.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ In the `gutenpride.php` file, the enqueue process is already setup from the gene
1212

1313
```php
1414
function create_block_gutenpride_block_init() {
15-
register_block_type_from_metadata( __DIR__ );
15+
register_block_type( __DIR__ );
1616
}
1717
add_action( 'init', 'create_block_gutenpride_block_init' );
1818
```

docs/getting-started/tutorials/create-block/wp-plugin.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ To load the built script, so it is run within the editor, you need to tell WordP
9292

9393
```php
9494
function create_block_gutenpride_block_init() {
95-
register_block_type_from_metadata( __DIR__ );
95+
register_block_type( __DIR__ );
9696
}
9797
add_action( 'init', 'create_block_gutenpride_block_init' );
9898
```
9999

100-
The `register_block_type_from_metadata` function registers the block we are going to create and specifies the `editor_script` file handle registered from the metadata provided in `block.json` file. So now when the editor loads it will load this script.
100+
The `register_block_type` function registers the block we are going to create and specifies the editor script handle registered from the metadata provided in `block.json` file with the `editorScript` field. So now when the editor loads it will load this script.
101101

102102
```json
103103
{
@@ -137,6 +137,6 @@ For more info, see the build section of the [Getting Started with JavaScript tut
137137

138138
## Summary
139139

140-
Hopefully, at this point, you have your plugin created and activated. We have the package.json with the `@wordpress/scripts` dependency, that defines the build and start scripts. The basic block is in place and can be added to the editor.
140+
Hopefully, at this point, you have your plugin created and activated. We have the `package.json` with the `@wordpress/scripts` dependency, that defines the build and start scripts. The basic block is in place and can be added to the editor.
141141

142142
Next Section: [Anatomy of a Block](/docs/getting-started/tutorials/create-block/block-anatomy.md)

docs/reference-guides/block-api/block-metadata.md

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
# Metadata
22

3-
To register a new block type using metadata that can be shared between codebase that uses JavaScript and PHP, start by creating a `block.json` file. This file:
4-
5-
- Gives a name to the block type.
6-
- Defines some important metadata about the registered block type (title, category, icon, description, keywords).
7-
- Defines the attributes of the block type.
8-
- Registers all the scripts and styles for your block type.
3+
Starting in WordPress 5.8 release, we encourage using the `block.json` metadata file as the canonical way to register block types. Here is an example `block.json` file that would define the metadata for a plugin create a notice block.
94

105
**Example:**
116

@@ -50,9 +45,19 @@ To register a new block type using metadata that can be shared between codebase
5045
}
5146
```
5247

53-
The same file is also used when [submitting block to Block Directory](/docs/getting-started/tutorials/create-block/submitting-to-block-directory.md).
48+
## Benefits using the metadata file
49+
50+
The block definition allows code sharing between JavaScript, PHP, and other languages when processing block types stored as JSON, and registering blocks with the `block.json` metadata file provides multiple benefits on top of it.
51+
52+
From a performance perspective, when themes support lazy loading assets, blocks registered with `block.json` will have their asset enqueuing optimized out of the box. The frontend CSS and JavaScript assets listed in the `style` or `script` properties will only be enqueued when the block is present on the page, resulting in reduced page sizes.
53+
54+
Furthermore, because the [Block Type REST API Endpoint](https://developer.wordpress.org/rest-api/reference/block-types/) can only list blocks registered on the server, registering blocks server-side is recommended; using the `block.json` file simplifies this registration.
55+
56+
Last, but not least, the [WordPress Plugins Directory](https://wordpress.org/plugins/) can detect `block.json` files, highlight blocks included in plugins, and extract their metadata. If you wish to [submit your block(s) to the Block Directory](/docs/getting-started/tutorials/create-block/submitting-to-block-directory.md), all blocks contained in your plugin must have a `block.json` file for the Block Directory to recognize them.
5457

55-
## Server-side registration
58+
## Block registration
59+
60+
### PHP (server-side)
5661

5762
The [`register_block_type`](https://developer.wordpress.org/reference/functions/register_block_type/) function that aims to simplify the block type registration on the server, can read metadata stored in the `block.json` file.
5863

@@ -75,6 +80,41 @@ register_block_type(
7580
);
7681
```
7782

83+
### JavaScript (client-side)
84+
85+
When the block is registered on the server, you only need to register the client-side settings on the client using the same block’s name.
86+
87+
**Example:**
88+
89+
```js
90+
registerBlockType( 'my-plugin/notice', {
91+
edit: Edit,
92+
// ...other client-side settings
93+
} );
94+
```
95+
96+
Although registering the block also on the server with PHP is still recommended for the reasons above, if you want to register it only client-side you can now use `registerBlockType` method from `@wordpress/blocks` package to register a block type using the metadata loaded from `block.json` file.
97+
98+
The function takes two params:
99+
100+
- `$blockNameOrMetadata` (`string`|`Object`) – block type name (supported previously) or the metadata object loaded from the `block.json` file with a bundler (e.g., webpack) or a custom Babel plugin.
101+
- `$settings` (`Object`) – client-side block settings.
102+
103+
It returns the registered block type (`WPBlock`) on success or `undefined` on failure.
104+
105+
**Example:**
106+
107+
```js
108+
import { registerBlockType } from '@wordpress/blocks';
109+
import Edit from './edit';
110+
import metadata from './block.json';
111+
112+
registerBlockType( metadata, {
113+
edit: Edit,
114+
// ...other client-side settings
115+
} );
116+
```
117+
78118
## Block API
79119

80120
This section describes all the properties that can be added to the `block.json` file to define the behavior and metadata of block types.

docs/reference-guides/block-api/block-registration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Block registration API reference.
44

5-
**Note:** You can use the functions documented on this page, but a flexible method to register new block types is to use the block.json metadata file. See [metadata documentation for complete information](/docs/reference-guides/block-api/block-metadata.md).
5+
**Note:** You can use the functions documented on this page to register a block on the client-side only, but a flexible method to register new block types is to use the `block.json` metadata file. See [metadata documentation for complete information](/docs/reference-guides/block-api/block-metadata.md).
66

77
## `registerBlockType`
88

docs/reference-guides/filters/block-filters.md

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,56 @@
22

33
To modify the behavior of existing blocks, WordPress exposes several APIs:
44

5-
## Filters
5+
## Registration
66

7-
The following filters are available to extend the settings for existing blocks.
7+
The following filters are available to extend the settings for blocks during their registration.
8+
9+
### `block_type_metadata`
10+
11+
Filters the raw metadata loaded from the `block.json` file when registering a block type on the server with PHP. It allows applying modifications before the metadata gets processed.
12+
13+
The filter takes one param:
14+
15+
- `$metadata` (`array`) – metadata loaded from `block.json` for registering a block type.
16+
17+
_Example_:
18+
19+
```php
20+
<?php
21+
22+
function filter_metadata_registration( $metadata ) {
23+
$metadata['apiVersion'] = 1;
24+
return $metadata;
25+
};
26+
add_filter( 'block_type_metadata', 'filter_metadata_registration', 10, 2 );
27+
28+
register_block_type( __DIR__ );
29+
```
30+
31+
### `block_type_metadata_settings`
32+
33+
Filters the settings determined from the processed block type metadata. It makes it possible to apply custom modifications using the block metadata that isn’t handled by default.
34+
35+
The filter takes two params:
36+
37+
- `$settings` (`array`) – Array of determined settings for registering a block type.
38+
- `$metadata` (`array`) – Metadata loaded from the `block.json` file.
39+
40+
_Example:_
41+
42+
```php
43+
function filter_metadata_registration( $settings, $metadata ) {
44+
$settings['api_version'] = $metadata['apiVersion'] + 1;
45+
return $settings;
46+
};
47+
add_filter( 'block_type_metadata_settings', 'filter_metadata_registration', 10, 2 );
48+
49+
register_block_type( __DIR__ );
50+
```
851

952
### `blocks.registerBlockType`
1053

11-
Used to filter the block settings. It receives the block settings and the name of the registered block as arguments. Since v6.1.0 this filter is also applied to each of a block's deprecated settings.
54+
Used to filter the block settings when registering the block on the client with JavaScript. It receives the block settings and the name of the registered block as arguments. This filter is also applied to each of a block's deprecated settings.
1255

1356
_Example:_
1457

@@ -34,6 +77,10 @@ wp.hooks.addFilter(
3477
);
3578
```
3679

80+
## Block Editor
81+
82+
The following filters are available to change the behavior of blocks while editing in the block editor.
83+
3784
### `blocks.getSaveElement`
3885

3986
A filter that applies to the result of a block's `save` function. This filter is used to replace or extend the element, for example using `wp.element.cloneElement` to modify the element's props or replace its children, or returning an entirely new element.
@@ -221,30 +268,6 @@ wp.hooks.addFilter(
221268

222269
{% end %}
223270

224-
#### `media.crossOrigin`
225-
226-
Used to set or modify the `crossOrigin` attribute for foreign-origin media elements (i.e `<img>`, `<audio>` , `<img>` , `<link>` , `<script>`, `<video>`). See this [article](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin) for more information the `crossOrigin` attribute, its values and how it applies to each element.
227-
228-
One example of it in action is in the Image block's transform feature to allow cross-origin images to be used in a `<canvas>`.
229-
230-
_Example:_
231-
232-
```js
233-
addFilter(
234-
'media.crossOrigin',
235-
'my-plugin/with-cors-media',
236-
// The callback accepts a second `mediaSrc` argument which references
237-
// the url to actual foreign media, useful if you want to decide
238-
// the value of crossOrigin based upon it.
239-
( crossOrigin, mediaSrc ) => {
240-
if ( mediaSrc.startsWith( 'https://example.com' ) ) {
241-
return 'use-credentials';
242-
}
243-
return crossOrigin;
244-
}
245-
);
246-
```
247-
248271
## Removing Blocks
249272

250273
### Using a deny list

docs/reference-guides/filters/editor-filters.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ wp.hooks.addFilter(
3838
);
3939
```
4040

41+
### `media.crossOrigin`
42+
43+
Used to set or modify the `crossOrigin` attribute for foreign-origin media elements (i.e `<img>`, `<audio>` , `<img>` , `<link>` , `<script>`, `<video>`). See this [article](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin) for more information the `crossOrigin` attribute, its values and how it applies to each element.
44+
45+
One example of it in action is in the Image block's transform feature to allow cross-origin images to be used in a `<canvas>`.
46+
47+
_Example:_
48+
49+
```js
50+
addFilter(
51+
'media.crossOrigin',
52+
'my-plugin/with-cors-media',
53+
// The callback accepts a second `mediaSrc` argument which references
54+
// the url to actual foreign media, useful if you want to decide
55+
// the value of crossOrigin based upon it.
56+
( crossOrigin, mediaSrc ) => {
57+
if ( mediaSrc.startsWith( 'https://example.com' ) ) {
58+
return 'use-credentials';
59+
}
60+
return crossOrigin;
61+
}
62+
);
63+
```
64+
4165
## Editor settings
4266

4367
### `block_editor_settings`

packages/create-block-tutorial-template/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
### Enhancement
6+
7+
- Plugin scaffolded requires WordPress 5.8 now ([#33252](https://github.com/WordPress/gutenberg/pull/33252).
8+
- Scaffolded block is now registered from `block.json` with the `register_block_type` helper ([#33252](https://github.com/WordPress/gutenberg/pull/33252)).
9+
510
## 1.2.0 (2021-04-06)
611

712
### Enhancement

packages/create-block-tutorial-template/templates/$slug.php.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{{#description}}
55
* Description: {{description}}
66
{{/description}}
7-
* Requires at least: 5.7
7+
* Requires at least: 5.8
88
* Requires PHP: 7.0
99
* Version: {{version}}
1010
{{#author}}
@@ -29,6 +29,6 @@
2929
* @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/writing-your-first-block-type/
3030
*/
3131
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
32-
register_block_type_from_metadata( __DIR__ );
32+
register_block_type( __DIR__ );
3333
}
3434
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );

packages/create-block/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
### Enhancement
6+
7+
- Plugin scaffolded with the `esnext` template requires WordPress 5.8 now ([#33252](https://github.com/WordPress/gutenberg/pull/33252).
8+
- Block scaffolded with the `esnext` template is now registered from `block.json` with the `register_block_type` helper ([#33252](https://github.com/WordPress/gutenberg/pull/33252)).
9+
510
## 2.3.0 (2021-04-29)
611

712
### Enhancement

packages/create-block/lib/templates/esnext/$slug.php.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{{#description}}
55
* Description: {{description}}
66
{{/description}}
7-
* Requires at least: 5.7
7+
* Requires at least: 5.8
88
* Requires PHP: 7.0
99
* Version: {{version}}
1010
{{#author}}
@@ -29,6 +29,6 @@
2929
* @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/writing-your-first-block-type/
3030
*/
3131
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
32-
register_block_type_from_metadata( __DIR__ );
32+
register_block_type( __DIR__ );
3333
}
3434
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );

0 commit comments

Comments
 (0)