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 beta blocks registration #32815

Merged
merged 42 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
7b7cd52
[not verified] Enable block registration by specifying block.json path
monsieur-z Aug 25, 2023
43ca42a
[not verified] Add test for registering block by its metadata file
monsieur-z Aug 29, 2023
7db9bd7
[not verified] Fix failing test
monsieur-z Sep 5, 2023
0146d3a
[not verified] Refactor: register business hours block with block.jso…
monsieur-z Aug 25, 2023
bf32a53
[not verified] Move logic to render a block icon on the client
monsieur-z Aug 31, 2023
da6fa23
[not verified] Remove deprecated file
monsieur-z Aug 31, 2023
1afc1ae
[not verified] Pass package build dir to get_path_to_block_metadata
monsieur-z Sep 6, 2023
fc89c2f
[not verified] Move block icon helpers to shared-extension-utils package
monsieur-z Sep 6, 2023
0484c40
[not verified] Add missing color-studio dependency to shared-extensio…
monsieur-z Sep 6, 2023
6d00b0c
[not verified] Add missing jetpack-constants dep to blocks package
monsieur-z Sep 6, 2023
3a2148d
[not verified] Fix monorepo package version refs
monsieur-z Sep 7, 2023
f114eb3
[not verified] Remove color studio dep in shared-extensions-util
monsieur-z Sep 8, 2023
720ccad
[not verified] Bump yoast/phpunit-polyfills version in blocks package
monsieur-z Sep 8, 2023
af025da
[not verified] Remove unhelpful changelogs
monsieur-z Sep 9, 2023
92b176a
[not verified] Add missing color-studio dependency to shared-extensio…
monsieur-z Sep 6, 2023
b58e969
[not verified] Add tests for blocks helpers functions
monsieur-z Aug 29, 2023
730fc9a
[not verified] Refactor Amazon block registration
monsieur-z Aug 31, 2023
9a3efe1
[not verified] Refactor AI Chat block registration
monsieur-z Aug 31, 2023
acceb10
[not verified] Refactor Blogroll block registration
monsieur-z Aug 31, 2023
2155137
[not verified] Refactor Recipe block registration
monsieur-z Aug 31, 2023
4cfe6b4
[not verified] Refactor Create with Voice block registration
monsieur-z Sep 1, 2023
487d6eb
[not verified] Fix missing imports in Recipe child blocks
monsieur-z Sep 1, 2023
ed76afc
[not verified] Refactor Docs blocks registration
monsieur-z Sep 1, 2023
7982995
[not verified] Fix Amazon icon
monsieur-z Sep 1, 2023
3a9745d
[not verified] Use CSS logical properties for i18n
monsieur-z Sep 1, 2023
acf1e78
[not verified] Fix Recipe block not correctly displayed in inserter
monsieur-z Sep 1, 2023
cd49ce7
[not verified] Fix Amazon icon alignment
monsieur-z Sep 1, 2023
505c19b
[not verified] Fix rebasing error
monsieur-z Sep 7, 2023
7a00151
[not verified] Update path passed to jetpack_register_block
monsieur-z Sep 7, 2023
db15070
[not verified] Import icon helpers from shared-extension-utils package
monsieur-z Sep 7, 2023
9c50cbe
[not verified] Update path passed to jetpack_register_block
monsieur-z Sep 7, 2023
5b2a9b0
[not verified] Add changelog
monsieur-z Sep 7, 2023
c9b2a5f
[not verified] Fix failing JS test
monsieur-z Sep 7, 2023
42a87bf
[not verified] Add helper to get block's feature name
monsieur-z Sep 10, 2023
3e2bcc9
[not verified] Add changelog
monsieur-z Sep 10, 2023
dbd0413
[not verified] Remove color studio dep
monsieur-z Sep 13, 2023
594fa95
[not verified] Add helper to get a block's name
monsieur-z Sep 14, 2023
a83cea1
[not verified] Remove deprecated CSS
monsieur-z Sep 14, 2023
dc45f9e
[not verified] Add missing Blocks::get_block_feature call
monsieur-z Sep 14, 2023
95e303a
[not verified] Remove unnecessary export
monsieur-z Sep 14, 2023
133828b
[not verified] Fix Google embeds previews
monsieur-z Sep 14, 2023
5cd9120
Revert composer.lock
monsieur-z Sep 14, 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

Helper to get a block's feature name
50 changes: 50 additions & 0 deletions projects/packages/blocks/src/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,30 @@ function () use ( $feature_name, $method_name ) {
return register_block_type( $block_type, $args );
}

/**
* Get the block metadata. Accepts a block.json file's path (or its folder's) or its content, in
* which case it becomes an identity function.
*
* It's used by other helpers in this class so that they can accept various types as argument.
*
* @param string|array $arg Path to block.json or its parent folder, or its content as an array.
*
* @return array The block metadata.
*/
private static function get_block_metadata( $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 ) );
}
}

return isset( $metadata ) ? $metadata : array();
}

/**
* Read block metadata from a block.json file.
*
Expand All @@ -144,6 +168,19 @@ public static function get_block_metadata_from_file( $filename ) {
return $metadata;
}

/**
* Get the block name (includes 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 name.
*/
public static function get_block_name( $arg ) {
$metadata = self::get_block_metadata( $arg );

return self::get_block_name_from_metadata( $metadata );
}

/**
* Get the block name from the its metadata.
*
Expand All @@ -155,6 +192,19 @@ 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 = self::get_block_metadata( $arg );

return self::get_block_feature_from_metadata( $metadata );
}

/**
* Get the block feature name (i.e. the name without the `jetpack` prefix) from its metadata.
*
Expand Down
50 changes: 50 additions & 0 deletions projects/packages/blocks/tests/php/test-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,54 @@ public function test_get_path_to_block_metadata() {

Jetpack_Constants::clear_constants();
}

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

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

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

// Pass path

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

$result = Blocks::get_block_name( __DIR__ . '/fixtures/test-block/block.json' );
$this->assertEquals( 'jetpack/test-block', $result );
}

/**
* 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
Expand Up @@ -12,17 +12,14 @@
use Automattic\Jetpack\Blocks;
use Jetpack_Gutenberg;

const FEATURE_NAME = 'ai-chat';
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME;

/**
* Registers our 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 @@ -39,7 +36,7 @@ function load_assets( $attr ) {
/*
* Enqueue necessary scripts and styles.
*/
Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
Jetpack_Gutenberg::load_assets_as_required( __DIR__ );

$ask_button_label = isset( $attr['askButtonLabel'] ) ? $attr['askButtonLabel'] : __( 'Ask', 'jetpack' );

Expand Down

This file was deleted.

11 changes: 8 additions & 3 deletions projects/plugins/jetpack/extensions/blocks/ai-chat/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"name": "jetpack/ai-chat",
"title": "AI Chat (Beta)",
"description": "Provides summarized chat across a site’s content, powered by AI magic.",
"keywords": [ "AI", "GPT", "Chat", "Search" ],
"keywords": ["AI", "GPT", "Chat", "Search"],
"version": "12.5.0",
"textdomain": "jetpack",
"category": "text",
"icon": "superhero",
"icon": "<svg viewBox='0 0 20 20' width='20' height='20' xmlns='http://www.w3.org/2000/svg'><path d='M11.1 10L18 2 7 10h2l-7 8 11-8h-1.9zm-4.3 1H3.9l2.5-1.8 7.5-5.5L10 2 3 5v3c0 2 .5 3.9 1.5 5.6L6.8 11zm6.4-2H16l-2.4 1.8L6.5 16c1 .9 2.2 1.6 3.5 2 4.2-1.5 7.1-5.5 7-10V5l-.2-.1L13.2 9z'/></svg>",
"supports": {
"align": true,
"alignWide": true,
Expand All @@ -18,5 +18,10 @@
"multiple": false,
"reusable": false
},
"editorScript": "file:../editor-beta.js"
"attributes": {
"askButtonLabel": {
"type": "string",
"default": "Ask"
}
}
}
14 changes: 11 additions & 3 deletions projects/plugins/jetpack/extensions/blocks/ai-chat/editor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
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 save from './save';

registerJetpackBlock( name, settings );
import './editor.scss';
import './components/feedback/style.scss';

registerJetpackBlockFromMetadata( metadata, {
edit,
save,
} );
57 changes: 0 additions & 57 deletions projects/plugins/jetpack/extensions/blocks/ai-chat/index.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}

button {
margin-left: 1rem;
margin-inline-start: 1rem;
border: none;
transition: 0.3s;
cursor: pointer;
Expand Down
7 changes: 2 additions & 5 deletions projects/plugins/jetpack/extensions/blocks/amazon/amazon.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@
use Automattic\Jetpack\Blocks;
use Jetpack_Gutenberg;

const FEATURE_NAME = 'amazon';
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 @@ -37,6 +34,6 @@ function register_block() {
* @return string
*/
function load_assets( $attr, $content ) {
Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
return $content;
}
44 changes: 0 additions & 44 deletions projects/plugins/jetpack/extensions/blocks/amazon/attributes.js

This file was deleted.

Loading
Loading