From ce82d94b9ffa541cdb45fcdb51a6a082c574d508 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Thu, 12 Dec 2024 11:34:43 -0500 Subject: [PATCH 1/2] Add multi variant --- packages/create-block/lib/templates.js | 22 +++++++++++++++++++ .../lib/templates/plugin/$slug.php.mustache | 19 ++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/packages/create-block/lib/templates.js b/packages/create-block/lib/templates.js index 4e70ee66fd3a4..242cdd11e323e 100644 --- a/packages/create-block/lib/templates.js +++ b/packages/create-block/lib/templates.js @@ -58,6 +58,22 @@ const predefinedPluginTemplates = { }, viewScript: 'file:./view.js', example: {}, + transformer: ( view ) => { + const { + variantVars: { isMultiVariant }, + slug, + folderName, + plugin, + } = view; + + return { + ...view, + folderName: + isMultiVariant && plugin + ? `${ folderName }/${ slug }` + : view.folderName, + }; + }, }, variants: { static: {}, @@ -66,6 +82,12 @@ const predefinedPluginTemplates = { title: 'Example Dynamic', render: 'file:./render.php', }, + multi: { + customScripts: { + 'build-blocks-manifest': 'wp-scripts build-blocks-manifest', + postbuild: 'npm run build-blocks-manifest', + }, + }, }, }, }; diff --git a/packages/create-block/lib/templates/plugin/$slug.php.mustache b/packages/create-block/lib/templates/plugin/$slug.php.mustache index 75666af3a850b..25defe904a524 100644 --- a/packages/create-block/lib/templates/plugin/$slug.php.mustache +++ b/packages/create-block/lib/templates/plugin/$slug.php.mustache @@ -42,6 +42,25 @@ if ( ! defined( 'ABSPATH' ) ) { * @see https://developer.wordpress.org/reference/functions/register_block_type/ */ function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() { + + {{#isMultiVariant}} + // wp_register_block_metadata_collection was introduced in WP 6.7. + if ( function_exists( 'wp_register_block_metadata_collection' ) ) { + // Register the block metadata. + wp_register_block_metadata_collection( + __DIR__ . '/build', + __DIR__ . '/build/blocks-manifest.php' + ); + } + // Register the blocks. + $blocks = glob( __DIR__ . '/*', GLOB_ONLYDIR ); + foreach ( $blocks as $block ) { + register_block_type( $block ); + } + {{/isMultiVariant}} + {{^isMultiVariant}} + // Register the block. register_block_type( __DIR__ . '/build' ); + {{/isMultiVariant}} } add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' ); From 8f5b35bf1160f7421aeff7305c2961d0a0986d8e Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Thu, 12 Dec 2024 11:52:35 -0500 Subject: [PATCH 2/2] Fix bag glob path. --- packages/create-block/lib/templates/plugin/$slug.php.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/create-block/lib/templates/plugin/$slug.php.mustache b/packages/create-block/lib/templates/plugin/$slug.php.mustache index 25defe904a524..9fd71c227e513 100644 --- a/packages/create-block/lib/templates/plugin/$slug.php.mustache +++ b/packages/create-block/lib/templates/plugin/$slug.php.mustache @@ -53,7 +53,7 @@ function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() { ); } // Register the blocks. - $blocks = glob( __DIR__ . '/*', GLOB_ONLYDIR ); + $blocks = glob( __DIR__ . '/build/*', GLOB_ONLYDIR ); foreach ( $blocks as $block ) { register_block_type( $block ); }