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

Enable block registration by specifying the path to block.json #32697

Merged
merged 16 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Enable block registration by specifying block.json path
2 changes: 1 addition & 1 deletion projects/packages/blocks/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"link-template": "https://github.com/Automattic/jetpack-blocks/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "1.4.x-dev"
"dev-trunk": "1.5.x-dev"
}
},
"config": {
Expand Down
69 changes: 66 additions & 3 deletions projects/packages/blocks/src/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Blocks {
*
* @since 1.1.0
*
* @param string $slug Slug of the block.
* @param string $slug Slug of the block or absolute path to the directory containing the block.json file.
* @param array $args {
* Arguments that are passed into register_block_type.
* See register_block_type for full list of arguments.
Expand All @@ -40,11 +40,29 @@ class Blocks {
* @return WP_Block_Type|false The registered block type on success, or false on failure.
*/
public static function jetpack_register_block( $slug, $args = array() ) {
if ( 0 !== strpos( $slug, 'jetpack/' ) && ! strpos( $slug, '/' ) ) {
// Slug doesn't start with `jetpack/`, isn't an absolute path, or doesn't contain a slash
// (synonym of a namespace) at all.
if ( 0 !== strpos( $slug, 'jetpack/' ) && 0 !== strpos( $slug, '/' ) && ! strpos( $slug, '/' ) ) {
jeherve marked this conversation as resolved.
Show resolved Hide resolved
_doing_it_wrong( 'jetpack_register_block', 'Prefix the block with jetpack/ ', 'Jetpack 9.0.0' );
$slug = 'jetpack/' . $slug;
}

$block_type = $slug;

// If the path to block.json is passed, find the slug in the file then create a block type
// object to register the block.
// Note: passing the path directly to register_block_type seems to loose the interactivity of
// the block once in the editor once it's out of focus.
if ( '/' === substr( $slug, 0, 1 ) ) {
$metadata = self::get_block_metadata_from_file( $slug );
kraftbj marked this conversation as resolved.
Show resolved Hide resolved
$name = self::get_block_name_from_metadata( $metadata );

if ( ! empty( $name ) ) {
$slug = $name;
$block_type = new \WP_Block_Type( $slug, array_merge( $metadata, $args ) );
}
}

if (
isset( $args['version_requirements'] )
&& ! self::is_gutenberg_version_available( $args['version_requirements'], $slug )
Expand Down Expand Up @@ -99,7 +117,52 @@ function () use ( $feature_name, $method_name ) {
}
}

return register_block_type( $slug, $args );
return register_block_type( $block_type, $args );
}

/**
* Read block metadata from a block.json file.
*
* @param string $filename The path to the block.json file or its directory.
*
* @return array The block metadata.
*/
public static function get_block_metadata_from_file( $filename ) {
jeherve marked this conversation as resolved.
Show resolved Hide resolved
$metadata = array();
$needle = '/block.json';
$filename = $needle === substr( $filename, -strlen( $needle ) ) ? $filename : realpath( $filename . $needle );

if ( file_exists( $filename ) ) {
try {
$metadata = wp_json_file_decode( $filename, array( 'associative' => true ) );
} catch ( \Exception $e ) {
$metadata = array();
}
}

return $metadata;
}

/**
* Get the block name from the its metadata.
*
* @param array $metadata The block metadata.
*
* @return string The block name.
*/
public static function get_block_name_from_metadata( $metadata ) {
return ! isset( $metadata['name'] ) || empty( $metadata['name'] ) ? '' : $metadata['name'];
}

/**
* Get the block feature name (i.e. the name without the `jetpack` prefix) from its metadata.
*
* @param array $metadata The block metadata.
*
* @return string The block feature name.
*/
public static function get_block_feature_from_metadata( $metadata ) {
return str_replace( 'jetpack/', '', self::get_block_name_from_metadata( $metadata ) );
}

/**
Expand Down
14 changes: 14 additions & 0 deletions projects/packages/blocks/tests/php/fixtures/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "jetpack/test-block",
"title": "Test Block",
"description": "",
"keywords": [],
"version": "",
"textdomain": "jetpack",
"category": "",
"icon": "",
"supports": {},
"editorScript": ""
}
95 changes: 95 additions & 0 deletions projects/packages/blocks/tests/php/test-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,99 @@ public function test_jetpack_register_block_with_existing_editor_style() {
remove_filter( 'jetpack_is_standalone_block', '__return_false' );
}
}

/**
* Test registering a block by specifying the path to its metadata file.
*
* @since $$next-version$$
*
* @covers Automattic\Jetpack\Blocks::get_block_metadata_from_file
*/
public function test_jetpack_register_block_from_metadata_file() {
$result = Blocks::jetpack_register_block( __DIR__ . '/fixtures/block.json' );

$this->assertInstanceOf( 'WP_Block_Type', $result );
}

/**
* Test reading metadata from a block.json file by specifying its path.
*
* @since $$next-version$$
*
* @covers Automattic\Jetpack\Blocks::get_block_metadata_from_file
*/
public function test_get_block_metadata_from_file() {
$result = Blocks::get_block_metadata_from_file( __DIR__ . '/fixtures/block.json' );

// phpcs:ignore MediaWiki.PHPUnit.SpecificAssertions.assertIsArray -- assertIsArray not supported by all PHP versions we support.
$this->assertTrue( is_array( $result ) );
$this->assertNotEmpty( $result );
}

/**
* Test reading metadata from a block.json file by specifying its folder.
*
* @since $$next-version$$
*
* @covers Automattic\Jetpack\Blocks::get_block_metadata_from_file
*/
public function test_get_block_metadata_from_folder() {
$result = Blocks::get_block_metadata_from_file( __DIR__ . '/fixtures/' );

// phpcs:ignore MediaWiki.PHPUnit.SpecificAssertions.assertIsArray -- assertIsArray not supported by all PHP versions we support.
$this->assertTrue( is_array( $result ) );
$this->assertNotEmpty( $result );
}

/**
* Test reading metadata from a file that doesn't exist.
*
* @since $$next-version$$
*
* @covers Automattic\Jetpack\Blocks::get_block_metadata_from_file
*/
public function test_get_block_metadata_from_wrong_file() {
$result = Blocks::get_block_metadata_from_file( __DIR__ . '/fixtures/ghost-folder/block.json' );

// phpcs:ignore MediaWiki.PHPUnit.SpecificAssertions.assertIsArray -- assertIsArray not supported by all PHP versions we support.
$this->assertTrue( is_array( $result ) );
$this->assertEmpty( $result );
}

/**
* Test reading the name of a block from its metadata.
*
* @since $$next-version$$
*
* @covers Automattic\Jetpack\Blocks::get_block_name_from_metadata
*/
public function test_get_block_name_from_metadata() {
$name = 'jetpack/test-block';
$result = Blocks::get_block_name_from_metadata( array() );

$this->assertSame( '', $result );

$result = Blocks::get_block_name_from_metadata( array( 'name' => $name ) );

$this->assertEquals( $result, $name );
}

/**
* Test reading the feature name of a block from its metadata.
*
* @since $$next-version$$
*
* @covers Automattic\Jetpack\Blocks::get_block_name_from_metadata
*/
public function test_get_block_feature_from_metadata() {
$feature = 'test-block';
$name = 'jetpack/' . $feature;
$result = Blocks::get_block_feature_from_metadata( array() );

$this->assertSame( '', $result );

$result = Blocks::get_block_feature_from_metadata( array( 'name' => $name ) );

$this->assertEquals( $result, $feature );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Enable block registration by specifying block.json path
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Updated composer.lock.


11 changes: 10 additions & 1 deletion projects/plugins/jetpack/class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public static function should_load() {
/**
* Only enqueue block assets when needed.
*
* @param string $type Slug of the block.
* @param string $type Slug of the block or absolute path to the directory containing the block.json file.
* @param array $script_dependencies Script dependencies. Will be merged with automatically
* detected script dependencies from the webpack build.
*
Expand All @@ -478,6 +478,15 @@ public static function load_assets_as_required( $type, $script_dependencies = ar
return;
}

// Retrieve the feature from block.json if its path is passed.
if ( '/' === substr( $type, 0, 1 ) ) {
$feature = Blocks::get_block_feature_from_metadata( Blocks::get_block_metadata_from_file( $type ) );

if ( ! empty( $feature ) ) {
$type = $feature;
}
}

$type = sanitize_title_with_dashes( $type );
self::load_styles_as_required( $type );
self::load_scripts_as_required( $type, $script_dependencies );
Expand Down
4 changes: 2 additions & 2 deletions projects/plugins/jetpack/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions projects/plugins/jetpack/extensions/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ function setBetaBlockTitle( settings, name ) {
return settings;
}

const { title, keywords } = settings;
const titleSuffix = '(beta)';
const betaKeyword = 'beta';

return {
...settings,
title: `${ settings.title } (beta)`,
kewords: [ ...settings.keywords, 'beta' ],
title: title.toLowerCase().endsWith( titleSuffix )
jeherve marked this conversation as resolved.
Show resolved Hide resolved
? title
: `${ settings.title } ${ titleSuffix }`,
kewords: keywords.includes( betaKeyword ) ? keywords : [ ...keywords, betaKeyword ],
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,46 @@ import {
import { registerBlockType } from '@wordpress/blocks';
import { addFilter } from '@wordpress/hooks';

const JETPACK_PREFIX = 'jetpack/';

/**
* Registers a gutenberg block if the availability requirements are met.
*
* @param {string} name - The block's name.
* @param {string} name - The block's name. Jetpack blocks must be registered with a name prefixed
* with `jetpack/`. This function accepts an unprefixed name too, though (it'd handle both
* `business-hours` and `jetpack/business-hours` similarly, for instance).
* @param {object} settings - The block's settings.
* @param {object} childBlocks - The block's child blocks.
* @param {boolean} prefix - Should this block be prefixed with `jetpack/`?
* @returns {object|boolean} Either false if the block is not available, or the results of `registerBlockType`
*/
export default function registerJetpackBlock( name, settings, childBlocks = [], prefix = true ) {
jeherve marked this conversation as resolved.
Show resolved Hide resolved
const { available, details, unavailableReason } = getJetpackExtensionAvailability( name );
const isNamePrefixed = name.startsWith( JETPACK_PREFIX );
const rawName = isNamePrefixed ? name.slice( JETPACK_PREFIX.length ) : name;

const { available, details, unavailableReason } = getJetpackExtensionAvailability( rawName );

const requiredPlan = requiresPaidPlan( unavailableReason, details );
const jpPrefix = prefix ? 'jetpack/' : '';
const jpPrefix = prefix || isNamePrefixed ? JETPACK_PREFIX : '';

if ( ! available && ! requiredPlan ) {
if ( 'production' !== process.env.NODE_ENV ) {
// eslint-disable-next-line no-console
console.warn(
`Block ${ name } couldn't be registered because it is unavailable (${ unavailableReason }).`
`Block ${ rawName } couldn't be registered because it is unavailable (${ unavailableReason }).`
);
}
return false;
}

const result = registerBlockType( jpPrefix + name, settings );
const prefixedName = jpPrefix + rawName;
const result = registerBlockType( prefixedName, settings );

if ( requiredPlan ) {
addFilter(
'editor.BlockListBlock',
`${ jpPrefix + name }-with-has-warning-is-interactive-class-names`,
withHasWarningIsInteractiveClassNames( jpPrefix + name )
`${ prefixedName }-with-has-warning-is-interactive-class-names`,
withHasWarningIsInteractiveClassNames( prefixedName )
);
}

Expand Down