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

Create Block: Update templates to use APIs introduced in WP 6.1 #44185

Merged
merged 3 commits into from
Oct 26, 2022
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
12 changes: 6 additions & 6 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
version: 2

updates:
# Check for updates to GitHub Actions.
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
open-pull-requests-limit: 10
# Check for updates to GitHub Actions.
- package-ecosystem: 'github-actions'
directory: '/'
schedule:
interval: 'daily'
open-pull-requests-limit: 10
2 changes: 1 addition & 1 deletion docs/reference-guides/block-api/block-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ return array(
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:

- `script`
- `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)
- `viewScript`
- `style`

## Internationalization
Expand Down
4 changes: 4 additions & 0 deletions packages/create-block-tutorial-template/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancement

- Update templates to use the `render` field in `block.json` introduced in WordPress 6.1 ([#44185](https://github.com/WordPress/gutenberg/pull/44185)).

## 2.8.0 (2022-10-19)

## 2.7.0 (2022-10-05)
Expand Down
1 change: 1 addition & 0 deletions packages/create-block-tutorial-template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
type: 'string',
},
},
render: 'file:./render.php',
},
},
pluginTemplatesPath: join( __dirname, 'plugin-templates' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Description: {{description}}
{{/description}}
* Version: {{version}}
* Requires at least: 5.9
* Requires at least: 6.1
* Requires PHP: 7.0
{{#author}}
* Author: {{author}}
Expand Down Expand Up @@ -38,33 +38,6 @@
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
{{#isStaticVariant}}
register_block_type( __DIR__ . '/build' );
{{/isStaticVariant}}
{{#isDynamicVariant}}
register_block_type(
__DIR__ . '/build',
array(
'render_callback' => '{{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback',
)
);
{{/isDynamicVariant}}
}
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
{{#isDynamicVariant}}

/**
* Render callback function.
*
* @param array $attributes The block attributes.
* @param string $content The block content.
* @param WP_Block $block Block instance.
*
* @return string The rendered output.
*/
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback( $atts, $content, $block ) {
ob_start();
require plugin_dir_path( __FILE__ ) . 'build/template.php';
return ob_get_clean();
}
{{/isDynamicVariant}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Contributors: {{author}}
{{/author}}
Tags: block
Tested up to: 6.0
Tested up to: 6.1
Stable tag: {{version}}
{{#license}}
License: {{license}}
Expand Down
4 changes: 4 additions & 0 deletions packages/create-block/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancement

- Update templates to use the `render` field in `block.json` introduced in WordPress 6.1 ([#44185](https://github.com/WordPress/gutenberg/pull/44185)).

## 4.4.0 (2022-10-19)

### New Feature
Expand Down
1 change: 1 addition & 0 deletions packages/create-block/docs/external-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,5 @@ The following configurable variables are used with the template files. Template
- `editorScript` (default: `'file:./index.js'`) – an editor script definition.
- `editorStyle` (default: `'file:./index.css'`) – an editor style definition.
- `style` (default: `'file:./style-index.css'`) – a frontend and editor style definition.
- `render` (no default) – a path to the PHP file used when rendering the block type on the server before presenting on the front end.
- `customBlockJSON` (no default) - allows definition of additional properties for the generated block.json file.
2 changes: 2 additions & 0 deletions packages/create-block/lib/init-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async function initBlockJSON( {
editorScript,
editorStyle,
style,
render,
customBlockJSON,
} ) {
info( '' );
Expand Down Expand Up @@ -57,6 +58,7 @@ async function initBlockJSON( {
editorScript,
editorStyle,
style,
render,
...customBlockJSON,
} ).filter( ( [ , value ] ) => !! value )
),
Expand Down
2 changes: 2 additions & 0 deletions packages/create-block/lib/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = async (
editorScript,
editorStyle,
style,
render,
variantVars,
customPackageJSON,
customBlockJSON,
Expand Down Expand Up @@ -101,6 +102,7 @@ module.exports = async (
editorScript,
editorStyle,
style,
render,
customPackageJSON,
customBlockJSON,
...variantVars,
Expand Down
11 changes: 8 additions & 3 deletions packages/create-block/lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ const predefinedPluginTemplates = {
description:
'Example block scaffolded with Create Block tool – no build step required.',
dashicon: 'smiley',
supports: {
html: false,
},
wpScripts: false,
editorScript: 'file:./index.js',
editorStyle: 'file:./editor.css',
style: 'file:./style.css',
editorScript: null,
editorStyle: null,
style: null,
},
templatesPath: join( __dirname, 'templates', 'es5' ),
variants: {
static: {},
dynamic: {
slug: 'example-dynamic-es5',
title: 'Example Dynamic (ES5)',
render: 'file:./render.php',
},
},
},
Expand All @@ -55,6 +59,7 @@ const predefinedPluginTemplates = {
dynamic: {
slug: 'example-dynamic',
title: 'Example Dynamic',
render: 'file:./render.php',
},
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{#isDynamicVariant}}
<p <?php echo get_block_wrapper_attributes(); ?>>
<?php esc_html_e( '{{title}} – hello from a dynamic block!', '{{textdomain}}' ); ?>
<?php esc_html_e( '{{title}} – hello from a dynamic block!', '{{textdomain}}' ); ?>
</p>
{{/isDynamicVariant}}
24 changes: 2 additions & 22 deletions packages/create-block/lib/templates/es5/$slug.php.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{{#description}}
* Description: {{description}}
{{/description}}
* Requires at least: 5.7
* Requires at least: 6.1
* Requires PHP: 7.0
* Version: {{version}}
{{#author}}
Expand Down Expand Up @@ -70,32 +70,12 @@ function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
);

register_block_type(
'{{namespace}}/{{slug}}',
$dir,
array(
'editor_script' => '{{namespace}}-{{slug}}-block-editor',
'editor_style' => '{{namespace}}-{{slug}}-block-editor',
'style' => '{{namespace}}-{{slug}}-block',
Comment on lines 75 to 77
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, I had to leave it as is, but I hope we can figure out a way to move all JS and CSS registration to block.json when someone opts out from using build tools. Tracked in #40447.

{{#isDynamicVariant}}
'render_callback' => '{{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback',
{{/isDynamicVariant}}
)
);
}
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
{{#isDynamicVariant}}

/**
* Render callback function.
*
* @param array $attributes The block attributes.
* @param string $content The block content.
* @param WP_Block $block Block instance.
*
* @return string The rendered output.
*/
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback( $attributes, $content, $block ) {
ob_start();
require plugin_dir_path( __FILE__ ) . '/template.php';
return ob_get_clean();
}
{{/isDynamicVariant}}
47 changes: 0 additions & 47 deletions packages/create-block/lib/templates/es5/index.js.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -33,53 +33,6 @@
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
*/
registerBlockType( '{{namespace}}/{{slug}}', {
/**
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removed code is loading from block.json now.

* @see https://make.wordpress.org/core/2020/11/18/block-api-version-2/
*/
apiVersion: {{apiVersion}},

/**
* This is the display title for your block, which can be translated with `i18n` functions.
* The block inserter will show this name.
*/
title: __(
'{{title}}',
'{{textdomain}}'
),

{{#description}}
/**
* This is a short description for your block, can be translated with `i18n` functions.
* It will be shown in the Block Tab in the Settings Sidebar.
*/
description: __(
'{{description}}',
'{{textdomain}}'
),

{{/description}}
/**
* Blocks are grouped into categories to help users browse and discover them.
* The categories provided by core are `text`, `media`, `design`, `widgets`, and `embed`.
*/
category: '{{category}}',

{{#dashicon}}
/**
* An icon property should be specified to make it easier to identify a block.
* These can be any of WordPress’ Dashicons, or a custom svg element.
*/
icon: '{{dashicon}}',

{{/dashicon}}
/**
* Optional block extended support features.
*/
supports: {
// Removes support for an HTML mode.
html: false,
},

/**
* The edit function describes the structure of your block in the context of the editor.
* This represents what the editor will render when the block is used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Contributors: {{author}}
{{/author}}
Tags: block
Tested up to: 5.9
Tested up to: 6.1
Stable tag: {{version}}
{{#license}}
License: {{license}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{#isDynamicVariant}}
<p <?php echo get_block_wrapper_attributes(); ?>>
<?php esc_html_e( '{{title}} – hello from a dynamic block!', '{{textdomain}}' ); ?>
<?php esc_html_e( '{{title}} – hello from a dynamic block!', '{{textdomain}}' ); ?>
</p>
{{/isDynamicVariant}}
29 changes: 1 addition & 28 deletions packages/create-block/lib/templates/plugin/$slug.php.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{{#description}}
* Description: {{description}}
{{/description}}
* Requires at least: 5.9
* Requires at least: 6.1
* Requires PHP: 7.0
* Version: {{version}}
{{#author}}
Expand Down Expand Up @@ -38,33 +38,6 @@
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
{{#isStaticVariant}}
register_block_type( __DIR__ . '/build' );
{{/isStaticVariant}}
{{#isDynamicVariant}}
register_block_type(
__DIR__ . '/build',
array(
'render_callback' => '{{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback',
)
);
{{/isDynamicVariant}}
}
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
{{#isDynamicVariant}}

/**
* Render callback function.
*
* @param array $attributes The block attributes.
* @param string $content The block content.
* @param WP_Block $block Block instance.
*
* @return string The rendered output.
*/
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_render_callback( $attributes, $content, $block ) {
ob_start();
require plugin_dir_path( __FILE__ ) . 'build/template.php';
return ob_get_clean();
}
{{/isDynamicVariant}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Contributors: {{author}}
{{/author}}
Tags: block
Tested up to: 6.0
Tested up to: 6.1
Stable tag: {{version}}
{{#license}}
License: {{license}}
Expand Down