Skip to content

Commit

Permalink
Create Block: Update templates to use APIs introduced in WP 6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Sep 23, 2022
1 parent 4a61eb8 commit 2fea1ad
Show file tree
Hide file tree
Showing 17 changed files with 31 additions and 133 deletions.
4 changes: 4 additions & 0 deletions packages/create-block-tutorial-template/CHANGELOG.md
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.6.0 (2022-09-21)

## 2.5.0 (2022-08-24)
Expand Down
1 change: 1 addition & 0 deletions packages/create-block-tutorial-template/index.js
Expand Up @@ -30,6 +30,7 @@ module.exports = {
type: 'string',
},
},
render: 'file:./render.php',
},
},
pluginTemplatesPath: join( __dirname, 'plugin-templates' ),
Expand Down
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}}
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
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.2.0 (2022-09-21)

## 4.0.0 (2022-08-24)
Expand Down
1 change: 1 addition & 0 deletions packages/create-block/docs/external-template.md
Expand Up @@ -101,3 +101,4 @@ 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.
2 changes: 2 additions & 0 deletions packages/create-block/lib/init-block.js
Expand Up @@ -29,6 +29,7 @@ async function initBlockJSON( {
editorScript,
editorStyle,
style,
render,
} ) {
info( '' );
info( 'Creating a "block.json" file.' );
Expand Down Expand Up @@ -56,6 +57,7 @@ async function initBlockJSON( {
editorScript,
editorStyle,
style,
render,
} ).filter( ( [ , value ] ) => !! value )
),
null,
Expand Down
2 changes: 2 additions & 0 deletions packages/create-block/lib/scaffold.js
Expand Up @@ -43,6 +43,7 @@ module.exports = async (
editorScript,
editorStyle,
style,
render,
variantVars,
}
) => {
Expand Down Expand Up @@ -99,6 +100,7 @@ module.exports = async (
editorScript,
editorStyle,
style,
render,
...variantVars,
};

Expand Down
11 changes: 8 additions & 3 deletions packages/create-block/lib/templates.js
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
@@ -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
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',
{{#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
Expand Up @@ -33,53 +33,6 @@
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
*/
registerBlockType( '{{namespace}}/{{slug}}', {
/**
* @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
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
@@ -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
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}}
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

0 comments on commit 2fea1ad

Please sign in to comment.