Skip to content

Commit 548d248

Browse files
gzioloryanwelcher
andauthored
Create Block: Update templates to use APIs introduced in WP 6.1 (#44185)
* Create Block: Update templates to use APIs introduced in WP 6.1 * Update the section about frontend enqueueing Co-authored-by: Ryan Welcher <me@ryanwelcher.com>
1 parent 00e632a commit 548d248

18 files changed

+32
-134
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ return array(
646646
Starting in the WordPress 5.8 release, it is possible to instruct WordPress to enqueue scripts and styles for a block type only when rendered on the frontend. It applies to the following asset fields in the `block.json` file:
647647

648648
- `script`
649-
- `viewScript` (when the block defines `render_callback` during registration in PHP or a `render` field in its `block.json`, then the script is registered but the block author is responsible for enqueuing it)
649+
- `viewScript`
650650
- `style`
651651

652652
## Internationalization

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

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Enhancement
6+
7+
- Update templates to use the `render` field in `block.json` introduced in WordPress 6.1 ([#44185](https://github.com/WordPress/gutenberg/pull/44185)).
8+
59
## 2.8.0 (2022-10-19)
610

711
## 2.7.0 (2022-10-05)

packages/create-block-tutorial-template/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = {
3030
type: 'string',
3131
},
3232
},
33+
render: 'file:./render.php',
3334
},
3435
},
3536
pluginTemplatesPath: join( __dirname, 'plugin-templates' ),

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

+1-28
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Description: {{description}}
99
{{/description}}
1010
* Version: {{version}}
11-
* Requires at least: 5.9
11+
* Requires at least: 6.1
1212
* Requires PHP: 7.0
1313
{{#author}}
1414
* Author: {{author}}
@@ -38,33 +38,6 @@
3838
* @see https://developer.wordpress.org/reference/functions/register_block_type/
3939
*/
4040
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
41-
{{#isStaticVariant}}
4241
register_block_type( __DIR__ . '/build' );
43-
{{/isStaticVariant}}
44-
{{#isDynamicVariant}}
45-
register_block_type(
46-
__DIR__ . '/build',
47-
array(
48-
'render_callback' => '{{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback',
49-
)
50-
);
51-
{{/isDynamicVariant}}
5242
}
5343
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
54-
{{#isDynamicVariant}}
55-
56-
/**
57-
* Render callback function.
58-
*
59-
* @param array $attributes The block attributes.
60-
* @param string $content The block content.
61-
* @param WP_Block $block Block instance.
62-
*
63-
* @return string The rendered output.
64-
*/
65-
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback( $atts, $content, $block ) {
66-
ob_start();
67-
require plugin_dir_path( __FILE__ ) . 'build/template.php';
68-
return ob_get_clean();
69-
}
70-
{{/isDynamicVariant}}

packages/create-block-tutorial-template/plugin-templates/readme.txt.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Contributors: {{author}}
44
{{/author}}
55
Tags: block
6-
Tested up to: 6.0
6+
Tested up to: 6.1
77
Stable tag: {{version}}
88
{{#license}}
99
License: {{license}}

packages/create-block/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Enhancement
6+
7+
- Update templates to use the `render` field in `block.json` introduced in WordPress 6.1 ([#44185](https://github.com/WordPress/gutenberg/pull/44185)).
8+
59
## 4.4.0 (2022-10-19)
610

711
### New Feature

packages/create-block/docs/external-template.md

+1
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,5 @@ The following configurable variables are used with the template files. Template
102102
- `editorScript` (default: `'file:./index.js'`) – an editor script definition.
103103
- `editorStyle` (default: `'file:./index.css'`) – an editor style definition.
104104
- `style` (default: `'file:./style-index.css'`) – a frontend and editor style definition.
105+
- `render` (no default) – a path to the PHP file used when rendering the block type on the server before presenting on the front end.
105106
- `customBlockJSON` (no default) - allows definition of additional properties for the generated block.json file.

packages/create-block/lib/init-block.js

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ async function initBlockJSON( {
2929
editorScript,
3030
editorStyle,
3131
style,
32+
render,
3233
customBlockJSON,
3334
} ) {
3435
info( '' );
@@ -57,6 +58,7 @@ async function initBlockJSON( {
5758
editorScript,
5859
editorStyle,
5960
style,
61+
render,
6062
...customBlockJSON,
6163
} ).filter( ( [ , value ] ) => !! value )
6264
),

packages/create-block/lib/scaffold.js

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module.exports = async (
4343
editorScript,
4444
editorStyle,
4545
style,
46+
render,
4647
variantVars,
4748
customPackageJSON,
4849
customBlockJSON,
@@ -101,6 +102,7 @@ module.exports = async (
101102
editorScript,
102103
editorStyle,
103104
style,
105+
render,
104106
customPackageJSON,
105107
customBlockJSON,
106108
...variantVars,

packages/create-block/lib/templates.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,21 @@ const predefinedPluginTemplates = {
2626
description:
2727
'Example block scaffolded with Create Block tool – no build step required.',
2828
dashicon: 'smiley',
29+
supports: {
30+
html: false,
31+
},
2932
wpScripts: false,
30-
editorScript: 'file:./index.js',
31-
editorStyle: 'file:./editor.css',
32-
style: 'file:./style.css',
33+
editorScript: null,
34+
editorStyle: null,
35+
style: null,
3336
},
3437
templatesPath: join( __dirname, 'templates', 'es5' ),
3538
variants: {
3639
static: {},
3740
dynamic: {
3841
slug: 'example-dynamic-es5',
3942
title: 'Example Dynamic (ES5)',
43+
render: 'file:./render.php',
4044
},
4145
},
4246
},
@@ -55,6 +59,7 @@ const predefinedPluginTemplates = {
5559
dynamic: {
5660
slug: 'example-dynamic',
5761
title: 'Example Dynamic',
62+
render: 'file:./render.php',
5863
},
5964
},
6065
},
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{#isDynamicVariant}}
22
<p <?php echo get_block_wrapper_attributes(); ?>>
3-
<?php esc_html_e( '{{title}} – hello from a dynamic block!', '{{textdomain}}' ); ?>
3+
<?php esc_html_e( '{{title}} – hello from a dynamic block!', '{{textdomain}}' ); ?>
44
</p>
55
{{/isDynamicVariant}}

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

+2-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{{#description}}
88
* Description: {{description}}
99
{{/description}}
10-
* Requires at least: 5.7
10+
* Requires at least: 6.1
1111
* Requires PHP: 7.0
1212
* Version: {{version}}
1313
{{#author}}
@@ -70,32 +70,12 @@ function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
7070
);
7171
7272
register_block_type(
73-
'{{namespace}}/{{slug}}',
73+
$dir,
7474
array(
7575
'editor_script' => '{{namespace}}-{{slug}}-block-editor',
7676
'editor_style' => '{{namespace}}-{{slug}}-block-editor',
7777
'style' => '{{namespace}}-{{slug}}-block',
78-
{{#isDynamicVariant}}
79-
'render_callback' => '{{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback',
80-
{{/isDynamicVariant}}
8178
)
8279
);
8380
}
8481
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
85-
{{#isDynamicVariant}}
86-
87-
/**
88-
* Render callback function.
89-
*
90-
* @param array $attributes The block attributes.
91-
* @param string $content The block content.
92-
* @param WP_Block $block Block instance.
93-
*
94-
* @return string The rendered output.
95-
*/
96-
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback( $attributes, $content, $block ) {
97-
ob_start();
98-
require plugin_dir_path( __FILE__ ) . '/template.php';
99-
return ob_get_clean();
100-
}
101-
{{/isDynamicVariant}}

packages/create-block/lib/templates/es5/index.js.mustache

-47
Original file line numberDiff line numberDiff line change
@@ -33,53 +33,6 @@
3333
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
3434
*/
3535
registerBlockType( '{{namespace}}/{{slug}}', {
36-
/**
37-
* @see https://make.wordpress.org/core/2020/11/18/block-api-version-2/
38-
*/
39-
apiVersion: {{apiVersion}},
40-
41-
/**
42-
* This is the display title for your block, which can be translated with `i18n` functions.
43-
* The block inserter will show this name.
44-
*/
45-
title: __(
46-
'{{title}}',
47-
'{{textdomain}}'
48-
),
49-
50-
{{#description}}
51-
/**
52-
* This is a short description for your block, can be translated with `i18n` functions.
53-
* It will be shown in the Block Tab in the Settings Sidebar.
54-
*/
55-
description: __(
56-
'{{description}}',
57-
'{{textdomain}}'
58-
),
59-
60-
{{/description}}
61-
/**
62-
* Blocks are grouped into categories to help users browse and discover them.
63-
* The categories provided by core are `text`, `media`, `design`, `widgets`, and `embed`.
64-
*/
65-
category: '{{category}}',
66-
67-
{{#dashicon}}
68-
/**
69-
* An icon property should be specified to make it easier to identify a block.
70-
* These can be any of WordPress’ Dashicons, or a custom svg element.
71-
*/
72-
icon: '{{dashicon}}',
73-
74-
{{/dashicon}}
75-
/**
76-
* Optional block extended support features.
77-
*/
78-
supports: {
79-
// Removes support for an HTML mode.
80-
html: false,
81-
},
82-
8336
/**
8437
* The edit function describes the structure of your block in the context of the editor.
8538
* This represents what the editor will render when the block is used.

packages/create-block/lib/templates/es5/readme.txt.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Contributors: {{author}}
44
{{/author}}
55
Tags: block
6-
Tested up to: 5.9
6+
Tested up to: 6.1
77
Stable tag: {{version}}
88
{{#license}}
99
License: {{license}}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{#isDynamicVariant}}
22
<p <?php echo get_block_wrapper_attributes(); ?>>
3-
<?php esc_html_e( '{{title}} – hello from a dynamic block!', '{{textdomain}}' ); ?>
3+
<?php esc_html_e( '{{title}} – hello from a dynamic block!', '{{textdomain}}' ); ?>
44
</p>
55
{{/isDynamicVariant}}

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

+1-28
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{{#description}}
88
* Description: {{description}}
99
{{/description}}
10-
* Requires at least: 5.9
10+
* Requires at least: 6.1
1111
* Requires PHP: 7.0
1212
* Version: {{version}}
1313
{{#author}}
@@ -38,33 +38,6 @@
3838
* @see https://developer.wordpress.org/reference/functions/register_block_type/
3939
*/
4040
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
41-
{{#isStaticVariant}}
4241
register_block_type( __DIR__ . '/build' );
43-
{{/isStaticVariant}}
44-
{{#isDynamicVariant}}
45-
register_block_type(
46-
__DIR__ . '/build',
47-
array(
48-
'render_callback' => '{{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback',
49-
)
50-
);
51-
{{/isDynamicVariant}}
5242
}
5343
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
54-
{{#isDynamicVariant}}
55-
56-
/**
57-
* Render callback function.
58-
*
59-
* @param array $attributes The block attributes.
60-
* @param string $content The block content.
61-
* @param WP_Block $block Block instance.
62-
*
63-
* @return string The rendered output.
64-
*/
65-
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback( $attributes, $content, $block ) {
66-
ob_start();
67-
require plugin_dir_path( __FILE__ ) . 'build/template.php';
68-
return ob_get_clean();
69-
}
70-
{{/isDynamicVariant}}

packages/create-block/lib/templates/plugin/readme.txt.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Contributors: {{author}}
44
{{/author}}
55
Tags: block
6-
Tested up to: 6.0
6+
Tested up to: 6.1
77
Stable tag: {{version}}
88
{{#license}}
99
License: {{license}}

0 commit comments

Comments
 (0)