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: Add multi variant #67886

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions packages/create-block/lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand All @@ -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',
},
},
},
},
};
Expand Down
19 changes: 19 additions & 0 deletions packages/create-block/lib/templates/plugin/$slug.php.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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__ . '/build/*', 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' );
Loading