Skip to content

Commit b89fb3f

Browse files
authored
CLI Tools: update scaffolding, use block.json to register blocks. (#33883)
1 parent a5cf00b commit b89fb3f

9 files changed

+69
-107
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: other
3+
4+
CLI Tools: update block scaffolding CLI tool to use block.json to register blocks.

projects/plugins/jetpack/class.jetpack-cli.php

+27-30
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,8 @@ public function block( $args, $assoc_args ) {
19761976
? $assoc_args['slug']
19771977
: sanitize_title( $title );
19781978

1979+
$next_version = "\x24\x24next-version$$"; // Escapes to hide the string from tools/replace-next-version-tag.sh
1980+
19791981
$variation_options = array( 'production', 'experimental', 'beta' );
19801982
$variation = ( isset( $assoc_args['variation'] ) && in_array( $assoc_args['variation'], $variation_options, true ) )
19811983
? $assoc_args['variation']
@@ -2004,56 +2006,51 @@ public function block( $args, $assoc_args ) {
20042006

20052007
$wp_filesystem->mkdir( $path );
20062008

2007-
$has_keywords = isset( $assoc_args['keywords'] );
2009+
$keywords = isset( $assoc_args['keywords'] )
2010+
? array_map(
2011+
function ( $keyword ) {
2012+
return trim( $keyword );
2013+
},
2014+
array_slice( explode( ',', $assoc_args['keywords'] ), 0, 3 )
2015+
)
2016+
: array();
20082017

20092018
$files = array(
2010-
"$path/$slug.php" => self::render_block_file(
2011-
'block-register-php',
2019+
"$path/block.json" => self::render_block_file(
2020+
'block-block-json',
20122021
array(
2013-
'nextVersion' => "\x24\x24next-version$$", // Escapes to hide the string from tools/replace-next-version-tag.sh
2014-
'slug' => $slug,
2015-
'title' => $title,
2016-
'underscoredSlug' => str_replace( '-', '_', $slug ),
2017-
'underscoredTitle' => str_replace( ' ', '_', $title ),
2022+
'slug' => $slug,
2023+
'title' => wp_json_encode( $title, JSON_UNESCAPED_UNICODE ),
2024+
'description' => isset( $assoc_args['description'] )
2025+
? wp_json_encode( $assoc_args['description'], JSON_UNESCAPED_UNICODE )
2026+
: wp_json_encode( $title, JSON_UNESCAPED_UNICODE ),
2027+
'nextVersion' => $next_version,
2028+
'keywords' => wp_json_encode( $keywords, JSON_UNESCAPED_UNICODE ),
20182029
)
20192030
),
2020-
"$path/index.js" => self::render_block_file(
2021-
'block-index-js',
2031+
"$path/$slug.php" => self::render_block_file(
2032+
'block-register-php',
20222033
array(
2023-
'slug' => $slug,
2024-
'title' => $title,
2025-
'description' => isset( $assoc_args['description'] )
2026-
? $assoc_args['description']
2027-
: $title,
2028-
'keywords' => $has_keywords
2029-
? array_map(
2030-
function ( $keyword ) {
2031-
// Construction necessary for Mustache lists.
2032-
return array( 'keyword' => trim( $keyword ) );
2033-
},
2034-
explode( ',', $assoc_args['keywords'], 3 )
2035-
)
2036-
: '',
2037-
'hasKeywords' => $has_keywords,
2034+
'nextVersion' => $next_version,
2035+
'title' => $title,
2036+
'underscoredTitle' => str_replace( ' ', '_', $title ),
20382037
)
20392038
),
2040-
"$path/editor.js" => self::render_block_file( 'block-editor-js' ),
2041-
"$path/editor.scss" => self::render_block_file(
2039+
"$path/editor.js" => self::render_block_file( 'block-editor-js' ),
2040+
"$path/editor.scss" => self::render_block_file(
20422041
'block-editor-scss',
20432042
array(
20442043
'slug' => $slug,
20452044
'title' => $title,
20462045
)
20472046
),
2048-
"$path/edit.js" => self::render_block_file(
2047+
"$path/edit.js" => self::render_block_file(
20492048
'block-edit-js',
20502049
array(
20512050
'title' => $title,
20522051
'className' => str_replace( ' ', '', ucwords( str_replace( '-', ' ', $slug ) ) ),
20532052
)
20542053
),
2055-
"$path/icon.js" => self::render_block_file( 'block-icon-js' ),
2056-
"$path/attributes.js" => self::render_block_file( 'block-attributes-js' ),
20572054
);
20582055

20592056
$files_written = array();

projects/plugins/jetpack/wp-cli-templates/block-attributes-js.mustache

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://schemas.wp.org/trunk/block.json",
3+
"apiVersion": 1,
4+
"name": "jetpack/{{ slug }}",
5+
"title": {{ title }},
6+
"description": {{ description }},
7+
"keywords": {{ keywords }},
8+
"version": "{{ nextVersion }}",
9+
"textdomain": "jetpack",
10+
"category": "grow",
11+
"icon": "<svg viewBox='0 0 32 32' width='32' height='32' xmlns='http://www.w3.org/2000/svg'><path d='M9 15h2V9H9v6zm1-10c-.5 0-1 .5-1 1s.5 1 1 1 1-.5 1-1-.5-1-1-1zm0-4c-5 0-9 4-9 9s4 9 9 9 9-4 9-9-4-9-9-9zm0 16c-3.9 0-7-3.1-7-7s3.1-7 7-7 7 3.1 7 7-3.1 7-7 7z'/></svg>",
12+
"supports": {
13+
"align": false,
14+
"alignWide": true,
15+
"anchor": false,
16+
"customClassName": true,
17+
"className": true,
18+
"html": false,
19+
"multiple": true,
20+
"reusable": true
21+
},
22+
"attributes": {}
23+
}

projects/plugins/jetpack/wp-cli-templates/block-edit-js.mustache

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { getBlockIconComponent } from '@automattic/jetpack-shared-extension-utils';
12
import { BlockIcon } from '@wordpress/block-editor';
23
import { Placeholder, withNotices } from '@wordpress/components';
34
import { useState } from '@wordpress/element';
45
import { __ } from '@wordpress/i18n';
6+
import metadata from './block.json';
57
import './editor.scss';
6-
import icon from './icon';
8+
9+
const icon = getBlockIconComponent( metadata );
710

811
function {{ className }}Edit( { attributes, className, noticeOperations, noticeUI, setAttributes } ) {
912
/**
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import registerJetpackBlock from '../../shared/register-jetpack-block';
2-
import { name, settings } from '.';
1+
import { registerJetpackBlockFromMetadata } from '../../shared/register-jetpack-block';
2+
import metadata from './block.json';
3+
import edit from './edit';
4+
import './editor.scss';
35

4-
registerJetpackBlock( name, settings );
6+
registerJetpackBlockFromMetadata( metadata, {
7+
edit,
8+
save: () => null, // TODO: Implement save.
9+
} );

projects/plugins/jetpack/wp-cli-templates/block-icon-js.mustache

-8
This file was deleted.

projects/plugins/jetpack/wp-cli-templates/block-index-js.mustache

-56
This file was deleted.

projects/plugins/jetpack/wp-cli-templates/block-register-php.mustache

+3-6
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@ namespace Automattic\Jetpack\Extensions\{{ underscoredTitle }};
1212
use Automattic\Jetpack\Blocks;
1313
use Jetpack_Gutenberg;
1414

15-
const FEATURE_NAME = '{{ slug }}';
16-
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;
17-
1815
/**
1916
* Registers the block for use in Gutenberg
2017
* This is done via an action so that we can disable
2118
* registration if we need to.
2219
*/
2320
function register_block() {
2421
Blocks::jetpack_register_block(
25-
BLOCK_NAME,
22+
__DIR__,
2623
array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
2724
);
2825
}
@@ -40,11 +37,11 @@ function load_assets( $attr, $content ) {
4037
/*
4138
* Enqueue necessary scripts and styles.
4239
*/
43-
Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
40+
Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
4441
4542
return sprintf(
4643
'<div class="%1$s">%2$s</div>',
47-
esc_attr( Blocks::classes( FEATURE_NAME, $attr ) ),
44+
esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ),
4845
$content
4946
);
5047
}

0 commit comments

Comments
 (0)