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

CLI Tools: update scaffolding, use block.json to register blocks. #33883

Merged
merged 11 commits into from
Nov 13, 2023
Merged
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/update-cli-blocks-scaffold
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

CLI Tools: update block scaffolding CLI tool to use block.json to register blocks.
57 changes: 27 additions & 30 deletions projects/plugins/jetpack/class.jetpack-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,8 @@ public function block( $args, $assoc_args ) {
? $assoc_args['slug']
: sanitize_title( $title );

$next_version = "\x24\x24next-version$$"; // Escapes to hide the string from tools/replace-next-version-tag.sh

$variation_options = array( 'production', 'experimental', 'beta' );
$variation = ( isset( $assoc_args['variation'] ) && in_array( $assoc_args['variation'], $variation_options, true ) )
? $assoc_args['variation']
Expand Down Expand Up @@ -2004,56 +2006,51 @@ public function block( $args, $assoc_args ) {

$wp_filesystem->mkdir( $path );

$has_keywords = isset( $assoc_args['keywords'] );
$keywords = isset( $assoc_args['keywords'] )
? array_map(
function ( $keyword ) {
return trim( $keyword );
},
array_slice( explode( ',', $assoc_args['keywords'] ), 0, 3 )
)
: array();

$files = array(
"$path/$slug.php" => self::render_block_file(
'block-register-php',
"$path/block.json" => self::render_block_file(
'block-block-json',
array(
'nextVersion' => "\x24\x24next-version$$", // Escapes to hide the string from tools/replace-next-version-tag.sh
'slug' => $slug,
'title' => $title,
'underscoredSlug' => str_replace( '-', '_', $slug ),
'underscoredTitle' => str_replace( ' ', '_', $title ),
'slug' => $slug,
'title' => wp_json_encode( $title, JSON_UNESCAPED_UNICODE ),
'description' => isset( $assoc_args['description'] )
? wp_json_encode( $assoc_args['description'], JSON_UNESCAPED_UNICODE )
: wp_json_encode( $title, JSON_UNESCAPED_UNICODE ),
'nextVersion' => $next_version,
'keywords' => wp_json_encode( $keywords, JSON_UNESCAPED_UNICODE ),
)
),
"$path/index.js" => self::render_block_file(
'block-index-js',
"$path/$slug.php" => self::render_block_file(
'block-register-php',
array(
'slug' => $slug,
'title' => $title,
'description' => isset( $assoc_args['description'] )
? $assoc_args['description']
: $title,
'keywords' => $has_keywords
? array_map(
function ( $keyword ) {
// Construction necessary for Mustache lists.
return array( 'keyword' => trim( $keyword ) );
},
explode( ',', $assoc_args['keywords'], 3 )
)
: '',
'hasKeywords' => $has_keywords,
'nextVersion' => $next_version,
'title' => $title,
'underscoredTitle' => str_replace( ' ', '_', $title ),
)
),
"$path/editor.js" => self::render_block_file( 'block-editor-js' ),
"$path/editor.scss" => self::render_block_file(
"$path/editor.js" => self::render_block_file( 'block-editor-js' ),
"$path/editor.scss" => self::render_block_file(
'block-editor-scss',
array(
'slug' => $slug,
'title' => $title,
)
),
"$path/edit.js" => self::render_block_file(
"$path/edit.js" => self::render_block_file(
'block-edit-js',
array(
'title' => $title,
'className' => str_replace( ' ', '', ucwords( str_replace( '-', ' ', $slug ) ) ),
)
),
"$path/icon.js" => self::render_block_file( 'block-icon-js' ),
"$path/attributes.js" => self::render_block_file( 'block-attributes-js' ),
);

$files_written = array();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should new blocks be defaulting to a later api version?

For version 2, it looks like we'll also have to have the default edit function use useBlockProps as documented at https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/. And maybe something similar for the save function and useBlockProps.save.

Version 3 will probably have to wait until we're not using 'enqueue_block_editor_assets' to enqueue the monolithic editor.js though.

Copy link
Member Author

Choose a reason for hiding this comment

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

@monsieur-z What's your take on this?

Copy link
Contributor

Choose a reason for hiding this comment

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

It makes sense to nudge people to use the latest version when possible. In this case (using 2 or 3), we'd probably need to edit the save and edit functions in the templates to include useBlockProps(); (as in the link Brad copied) and make it visible it's required.

As for which version to use, I can't say I fully understand what using version 3 involves. The iframing might be an issue for some, but it seems it wouldn't occur unless all blocks are registered with 3 anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

What I've managed to gather is that we have to make sure the scripts will be enqueued inside the iframe. Which it seems may involve somehow using the 'enqueue_block_assets' hook rather than 'enqueue_block_editor_assets', or else doing the enqueuing via block.json.

https://developer.wordpress.org/block-editor/how-to-guides/enqueueing-assets-in-the-editor/#editor-content-scripts-and-styles (and the following section) might be relevant.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 Discussion can continue in #34120

"name": "jetpack/{{ slug }}",
"title": {{ title }},
"description": {{ description }},
"keywords": {{ keywords }},
"version": "{{ nextVersion }}",
"textdomain": "jetpack",
"category": "grow",
"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>",
"supports": {
"align": false,
"alignWide": true,
"anchor": false,
"customClassName": true,
"className": true,
"html": false,
"multiple": true,
"reusable": true
},
"attributes": {}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { getBlockIconComponent } from '@automattic/jetpack-shared-extension-utils';
import { BlockIcon } from '@wordpress/block-editor';
import { Placeholder, withNotices } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import metadata from './block.json';
import './editor.scss';
import icon from './icon';

const icon = getBlockIconComponent( metadata );

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

registerJetpackBlock( name, settings );
registerJetpackBlockFromMetadata( metadata, {
edit,
save: () => null, // TODO: Implement save.
} );

This file was deleted.

56 changes: 0 additions & 56 deletions projects/plugins/jetpack/wp-cli-templates/block-index-js.mustache

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ namespace Automattic\Jetpack\Extensions\{{ underscoredTitle }};
use Automattic\Jetpack\Blocks;
use Jetpack_Gutenberg;

const FEATURE_NAME = '{{ slug }}';
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;

/**
* Registers the block for use in Gutenberg
* This is done via an action so that we can disable
* registration if we need to.
*/
function register_block() {
Blocks::jetpack_register_block(
BLOCK_NAME,
__DIR__,
array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
);
}
Expand All @@ -40,11 +37,11 @@ function load_assets( $attr, $content ) {
/*
* Enqueue necessary scripts and styles.
*/
Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
Jetpack_Gutenberg::load_assets_as_required( __DIR__ );

return sprintf(
'<div class="%1$s">%2$s</div>',
esc_attr( Blocks::classes( FEATURE_NAME, $attr ) ),
esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ),
$content
);
}