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

Refactor/prod blocks registration 3 #33020

Closed
wants to merge 57 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
8789c36
[not verified] Enable block registration by specifying block.json path
monsieur-z Aug 25, 2023
72db3ec
[not verified] Add test for registering block by its metadata file
monsieur-z Aug 29, 2023
1ccb7fb
[not verified] Fix failing test
monsieur-z Sep 5, 2023
beea761
[not verified] Refactor: register business hours block with block.jso…
monsieur-z Aug 25, 2023
9c7ca33
[not verified] Make block CSS right-to-left compatible
monsieur-z Aug 28, 2023
e80fb89
[not verified] Remove hardcoded editorScript value in business hours …
monsieur-z Aug 30, 2023
f6e7572
[not verified] Move code to get block metadata path in Blocks
monsieur-z Aug 31, 2023
d7dbfd2
[not verified] Move logic to render a block icon on the client
monsieur-z Aug 31, 2023
38301f3
[not verified] Remove deprecated file
monsieur-z Aug 31, 2023
046eb0f
[not verified] Add changelogs
monsieur-z Aug 31, 2023
032a674
[not verified] Fix failing JS test
monsieur-z Sep 5, 2023
b7d1b65
[not verified] Pass package build dir to get_path_to_block_metadata
monsieur-z Sep 6, 2023
6be9ca9
[not verified] Move block icon helpers to shared-extension-utils package
monsieur-z Sep 6, 2023
5f99e40
[not verified] changelog
monsieur-z Sep 6, 2023
3bfaa9f
[not verified] Add missing import
monsieur-z Sep 6, 2023
fe0408f
[not verified] Update package version
monsieur-z Sep 6, 2023
b592181
[not verified] Add missing color-studio dependency to shared-extensio…
monsieur-z Sep 6, 2023
10e2877
[not verified] Add missing jetpack-constants dep to blocks package
monsieur-z Sep 6, 2023
33b4fad
[not verified] Remove call to realpath from get_block_metadata_from_file
monsieur-z Sep 7, 2023
354b15e
[not verified] Fix monorepo package version refs
monsieur-z Sep 7, 2023
873b12c
[not verified] Pass path to source block.json to jetpack_register_block
monsieur-z Sep 7, 2023
8a05697
[not verified] Fix wrong getIconColor import
monsieur-z Sep 7, 2023
9a2f37d
Falls back to reading block.json in source directory
monsieur-z Sep 7, 2023
756c086
Remove color studio dep in shared-extensions-util
monsieur-z Sep 8, 2023
4565eb7
Bump yoast/phpunit-polyfills version in blocks package
monsieur-z Sep 8, 2023
539cb97
Update composer.lock in Jetpack plugin
monsieur-z Sep 8, 2023
47d7aa2
Remove unhelpful changelogs
monsieur-z Sep 9, 2023
7b0f5c3
Add fallback logic to get_path_to_block_metadata
monsieur-z Sep 9, 2023
1ff4dec
[not verified] Fix package versions
monsieur-z Aug 28, 2023
cf23389
[not verified] Add tests for blocks helpers functions
monsieur-z Aug 29, 2023
b7e70b9
[not verified] Add test for registering block by its metadata file
monsieur-z Aug 29, 2023
2887e4c
[not verified] Refactor Amazon block registration
monsieur-z Aug 31, 2023
c9d3099
[not verified] Refactor AI Chat block registration
monsieur-z Aug 31, 2023
dcbba90
[not verified] Refactor Blogroll block registration
monsieur-z Aug 31, 2023
37a9483
[not verified] Refactor Recipe block registration
monsieur-z Aug 31, 2023
bf32446
[not verified] Refactor Create with Voice block registration
monsieur-z Sep 1, 2023
ac6d04d
[not verified] Fix missing imports in Recipe child blocks
monsieur-z Sep 1, 2023
b5fcc8d
[not verified] Refactor Docs blocks registration
monsieur-z Sep 1, 2023
9d6db4f
[not verified] Fix Amazon icon
monsieur-z Sep 1, 2023
766b54e
[not verified] Use CSS logical properties for i18n
monsieur-z Sep 1, 2023
ab6be7d
[not verified] Fix Recipe block not correctly displayed in inserter
monsieur-z Sep 1, 2023
93a6d3b
[not verified] Fix Amazon icon alignment
monsieur-z Sep 1, 2023
5cfcb99
[not verified] Fix rebasing error
monsieur-z Sep 7, 2023
dcaa0a5
[not verified] Update path passed to jetpack_register_block
monsieur-z Sep 7, 2023
0608a7c
[not verified] Import icon helpers from shared-extension-utils package
monsieur-z Sep 7, 2023
dabcf1f
[not verified] Update path passed to jetpack_register_block
monsieur-z Sep 7, 2023
9ae5025
[not verified] Add changelog
monsieur-z Sep 7, 2023
51c3763
[not verified] Fix failing JS test
monsieur-z Sep 7, 2023
d19fe0d
Revert composer.lock after rebasing
monsieur-z Sep 10, 2023
cf27a27
Add helper to get block's feature name
monsieur-z Sep 10, 2023
c8d10f1
Add changelog
monsieur-z Sep 10, 2023
ee8e5fe
Refactor Mailchimp block registration
monsieur-z Sep 12, 2023
101b934
Refactor Map block registration
monsieur-z Sep 12, 2023
50c948d
Refactor Markdown block registration
monsieur-z Sep 12, 2023
59fbb80
Refactor OpenTable block registration
monsieur-z Sep 12, 2023
2e90629
Refactor Payment Button block registration
monsieur-z Sep 12, 2023
8a137e7
Add changelog
monsieur-z Sep 12, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Helpers functions for block icons
1 change: 1 addition & 0 deletions projects/js-packages/shared-extension-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export { default as isCurrentUserConnected } from './src/is-current-user-connect
export { default as useAnalytics } from './src/hooks/use-analytics';
export { default as useModuleStatus } from './src/hooks/use-module-status';
export { default as JetpackEditorPanelLogo } from './src/components/jetpack-editor-panel-logo';
export { getBlockIconComponent, getBlockIconProp } from './src/get-block-icon-from-metadata';
18 changes: 9 additions & 9 deletions projects/js-packages/shared-extension-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@automattic/jetpack-shared-extension-utils",
"version": "0.11.4",
"version": "0.12.0-alpha",
"description": "Utility functions used by the block editor extensions",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/shared-extension-utils/#readme",
"bugs": {
Expand Down Expand Up @@ -29,20 +29,20 @@
"lodash": "4.17.21"
},
"devDependencies": {
"@automattic/jetpack-webpack-config": "workspace:*",
"@babel/core": "7.22.11",
"@babel/plugin-transform-react-jsx": "7.22.5",
"@babel/preset-react": "7.22.5",
"@testing-library/dom": "8.19.1",
"@testing-library/react": "13.4.0",
"@testing-library/user-event": "14.4.3",
"@wordpress/babel-plugin-import-jsx-pragma": "4.24.0",
"babel-jest": "29.3.1",
"jest": "29.3.1",
"jest-environment-jsdom": "29.3.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"jetpack-js-tools": "workspace:*",
"@automattic/jetpack-webpack-config": "workspace:*",
"@wordpress/babel-plugin-import-jsx-pragma": "4.24.0",
"@testing-library/dom": "8.19.1",
"@testing-library/react": "13.4.0",
"@testing-library/user-event": "14.4.3",
"@babel/plugin-transform-react-jsx": "7.22.5"
"react": "18.2.0",
"react-dom": "18.2.0"
},
"engines": {
"node": "^18.13.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable jsdoc/no-undefined-types */
import getIconColor from './get-icon-color';

/**
* Generate an icon as a React component from the SVG markup defined in a block.json metadata file.
* This prevents us from duplicating the markup in various places.
*
* @param {object} metadata - Block.json content
* @returns {React.Component} Icon component
*/
export function getBlockIconComponent( metadata ) {
// Note: using an `img` tag and passing the SVG markup as a data URI doesn't allow
// us to dynamically set the icon color later on.
/* eslint-disable-next-line react/no-danger */
return <span dangerouslySetInnerHTML={ { __html: metadata.icon } } />;
}

/**
* A block icon needs to be redefined on the front end as a React component, since a string - even
* SVG markup - is interpreted as a dashicon. This function returns the object that must be passed
* to the `icon` attribute when registering the block in the front end. It also sets the color
* of the icon.
*
* @param {object} metadata - Block.json content
* @returns {object} Icon property for client registration
*/
export function getBlockIconProp( metadata ) {
return {
src: getBlockIconComponent( metadata ),
foreground: getIconColor(),
};
}
23 changes: 23 additions & 0 deletions projects/js-packages/shared-extension-utils/src/get-icon-color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isAtomicSite, isSimpleSite } from './site-type-utils';

/**
* Constants
*/
const JETPACK_GREEN_40 = '#069e08';

/**
* Returns the icon color for Jetpack blocks.
*
* Green in the Jetpack context, otherwise black for Simple sites or Atomic sites.
*
* @returns {string} HEX color for block editor icons
*/
export default function getIconColor() {
if ( isAtomicSite() || isSimpleSite() ) {
// Return null to match core block styling
return null;
}

// Jetpack Green
return JETPACK_GREEN_40;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Helper to get a block's feature name
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add function to get path to block metadata file
4 changes: 3 additions & 1 deletion projects/packages/blocks/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"description": "Register and manage blocks within a plugin. Used to manage block registration, enqueues, and more.",
"type": "jetpack-library",
"license": "GPL-2.0-or-later",
"require": {},
"require": {
"automattic/jetpack-constants": "@dev"
},
"require-dev": {
"automattic/wordbless": "dev-master",
"brain/monkey": "2.6.1",
Expand Down
58 changes: 53 additions & 5 deletions projects/packages/blocks/src/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Automattic\Jetpack;

use Automattic\Jetpack\Constants as Jetpack_Constants;
use Jetpack_Gutenberg;

/**
Expand All @@ -27,7 +28,7 @@ class Blocks {
*
* @since 1.1.0
*
* @param string $slug Slug of the block or absolute path to the directory containing the block.json file.
* @param string $slug Slug of the block or absolute path to the block source code directory.
* @param array $args {
* Arguments that are passed into register_block_type.
* See register_block_type for full list of arguments.
Expand All @@ -49,12 +50,12 @@ public static function jetpack_register_block( $slug, $args = array() ) {

$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.
// If a path 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 );
$metadata = self::get_block_metadata_from_file( self::get_path_to_block_metadata( $slug ) );
$name = self::get_block_name_from_metadata( $metadata );

if ( ! empty( $name ) ) {
Expand Down Expand Up @@ -130,7 +131,7 @@ function () use ( $feature_name, $method_name ) {
public static function get_block_metadata_from_file( $filename ) {
$metadata = array();
$needle = '/block.json';
$filename = $needle === substr( $filename, -strlen( $needle ) ) ? $filename : realpath( $filename . $needle );
$filename = $needle === substr( $filename, -strlen( $needle ) ) ? $filename : $filename . $needle;

if ( file_exists( $filename ) ) {
try {
Expand All @@ -154,6 +155,31 @@ 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).
*
* @param string|array $arg Path to block.json or its parent folder, or its content as an array.
*
* @return string The block feature name.
*/
public static function get_block_feature( $arg ) {
$metadata = is_array( $arg ) ? $arg : null;

if ( ! isset( $metadata ) ) {
$path = is_string( $arg ) ? $arg : null;

if ( isset( $path ) && ! empty( $path ) ) {
$metadata = self::get_block_metadata_from_file( self::get_path_to_block_metadata( $path ) );
}
}

if ( ! empty( $metadata ) ) {
return self::get_block_feature_from_metadata( $metadata );
}

return '';
}

/**
* Get the block feature name (i.e. the name without the `jetpack` prefix) from its metadata.
*
Expand Down Expand Up @@ -352,4 +378,26 @@ public static function is_standalone_block() {
*/
return apply_filters( 'jetpack_is_standalone_block', $is_standalone_block );
}

/**
* Returns the path to the directory containing the block.json metadata file of a block, given its
* source code directory and, optionally, the directory that holds the blocks built files of
* the package. If the directory doesn't exist, falls back to the source directory.
*
* @since $$next-version$$
*
* @param string $block_src_dir The path to the folder containing the block source code.
* Typically this is done by passing __DIR__ as the argument.
* @param string $package_dist_dir Optional. A full path to the directory containing the blocks
* built files of the package. Default empty.
*
* @return string The path to the directory.
*/
public static function get_path_to_block_metadata( $block_src_dir, $package_dist_dir = '' ) {
$dir = basename( $block_src_dir );
$dist_path = empty( $package_dist_dir ) ? dirname( Jetpack_Constants::get_constant( 'JETPACK__PLUGIN_FILE' ) ) . '/_inc/blocks' : $package_dist_dir;
$result = realpath( "$dist_path/$dir" );

return false === $result ? $block_src_dir : $result;
}
}
14 changes: 14 additions & 0 deletions projects/packages/blocks/tests/php/fixtures/test-block/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": ""
}
69 changes: 66 additions & 3 deletions projects/packages/blocks/tests/php/test-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Automattic\Jetpack;

use Automattic\Jetpack\Constants as Jetpack_Constants;
use Brain\Monkey;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -339,7 +340,7 @@ public function test_jetpack_register_block_with_existing_editor_style() {
* @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' );
$result = Blocks::jetpack_register_block( __DIR__ . '/fixtures/test-block/block.json' );

$this->assertInstanceOf( 'WP_Block_Type', $result );
}
Expand All @@ -352,7 +353,7 @@ public function test_jetpack_register_block_from_metadata_file() {
* @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' );
$result = Blocks::get_block_metadata_from_file( __DIR__ . '/fixtures/test-block/block.json' );

// phpcs:ignore MediaWiki.PHPUnit.SpecificAssertions.assertIsArray -- assertIsArray not supported by all PHP versions we support.
$this->assertTrue( is_array( $result ) );
Expand All @@ -367,7 +368,7 @@ public function test_get_block_metadata_from_file() {
* @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/' );
$result = Blocks::get_block_metadata_from_file( __DIR__ . '/fixtures/test-block' );

// phpcs:ignore MediaWiki.PHPUnit.SpecificAssertions.assertIsArray -- assertIsArray not supported by all PHP versions we support.
$this->assertTrue( is_array( $result ) );
Expand Down Expand Up @@ -425,4 +426,66 @@ public function test_get_block_feature_from_metadata() {

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

/**
* Test getting the path to a block's metadata file.
*
* @since $$next-version$$
*
* @covers Automattic\Jetpack\Blocks::get_path_to_block_metadata
*/
public function test_get_path_to_block_metadata() {
$base_dir = __DIR__ . '/fixtures';
$block_dir = $base_dir . '/test-block';

// Existing build folder

Jetpack_Constants::set_constant( 'JETPACK__PLUGIN_FILE', $base_dir . '/jetpack.php' );

$result = Blocks::get_path_to_block_metadata( $block_dir );
$this->assertEquals( $base_dir . '/_inc/blocks/test-block', $result );

$result = Blocks::get_path_to_block_metadata( $block_dir, $base_dir . '/_inc/blocks/' );
$this->assertEquals( $base_dir . '/_inc/blocks/test-block', $result );

$result = Blocks::get_path_to_block_metadata( $block_dir, $base_dir . '/_inc/blocks' );
$this->assertEquals( $base_dir . '/_inc/blocks/test-block', $result );

// Invalid build folder

Jetpack_Constants::set_constant( 'JETPACK__PLUGIN_FILE', '/a/b/c/jetpack.php' );

$result = Blocks::get_path_to_block_metadata( $block_dir );
$this->assertEquals( $block_dir, $result );

$result = Blocks::get_path_to_block_metadata( $block_dir, '/dist' );
$this->assertEquals( $block_dir, $result );

Jetpack_Constants::clear_constants();
}

/**
* Test getting the block feature name.
*
* @since $$next-version$$
*
* @covers Automattic\Jetpack\Blocks::get_block_feature
*/
public function test_get_block_feature() {
// Pass metadata

$result = Blocks::get_block_feature( array() );
$this->assertSame( '', $result );

$result = Blocks::get_block_feature( array( 'name' => 'jetpack/test-block' ) );
$this->assertEquals( 'test-block', $result );

// Pass path

$result = Blocks::get_block_feature( '' );
$this->assertSame( '', $result );

$result = Blocks::get_block_feature( __DIR__ . '/fixtures/test-block/block.json' );
$this->assertEquals( 'test-block', $result );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

Register beta blocks by specifying path to block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

Refactor Business Hours block registration to avoid duplication
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Refactor production blocks registration
7 changes: 4 additions & 3 deletions 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 or absolute path to the directory containing the block.json file.
* @param string $type Slug of the block or absolute path to the block source code directory.
* @param array $script_dependencies Script dependencies. Will be merged with automatically
* detected script dependencies from the webpack build.
*
Expand All @@ -478,9 +478,10 @@ public static function load_assets_as_required( $type, $script_dependencies = ar
return;
}

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

if ( ! empty( $feature ) ) {
$type = $feature;
Expand Down
5 changes: 4 additions & 1 deletion projects/plugins/jetpack/composer.lock

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

Loading