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

Extensions: Build individual blocks #31294

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

4 changes: 4 additions & 0 deletions projects/packages/blocks/changelog/update-extensions-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Build individual blocks
6 changes: 3 additions & 3 deletions projects/packages/blocks/src/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ 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() ) {
public static function jetpack_register_block( $slug, $args = array(), $metadata_dir = '' ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Directory holding the block.json metadata file.

if ( 0 !== strpos( $slug, 'jetpack/' ) && ! strpos( $slug, '/' ) ) {
_doing_it_wrong( 'jetpack_register_block', 'Prefix the block with jetpack/ ', 'Jetpack 9.0.0' );
$slug = 'jetpack/' . $slug;
Expand Down Expand Up @@ -94,12 +94,12 @@ function () use ( $feature_name, $method_name ) {

// Ensure editor styles are registered so that the site editor knows about the
// editor style dependency when copying styles to the editor iframe.
if ( ! isset( $args['editor_style'] ) ) {
if ( ! isset( $args['editor_style'] ) && ! $metadata_dir ) {
$args['editor_style'] = 'jetpack-blocks-editor';
}
}

return register_block_type( $slug, $args );
return register_block_type( $metadata_dir ? $metadata_dir : $slug, $args );
}

/**
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/update-extensions-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Build individual blocks
127 changes: 99 additions & 28 deletions projects/plugins/jetpack/class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,11 @@ public static function preset_exists( $preset ) {
*
* @return mixed Returns an object if the file is present, or false if a valid .json file is not present.
*/
public static function get_preset( $preset ) {
public static function get_preset( $preset, $associative = null ) {
return json_decode(
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' )
file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' ),
$associative
);
}

Expand All @@ -287,6 +288,37 @@ public static function get_jetpack_gutenberg_extensions_allowed_list() {
return self::get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation );
}

public static function get_jetpack_gutenberg_single_extensions() {
$preset_extensions_manifest = self::preset_exists( 'index' )
? self::get_preset( 'index' )
: (object) array();

return isset( $preset_extensions_manifest->single )
? (array) $preset_extensions_manifest->single
: array();
}

public static function has_bundled_extension() {
$preset_extensions_manifest = self::preset_exists( 'index' )
? self::get_preset( 'index', true )
: array();

$production = isset( $preset_extensions_manifest['production'] )
? $preset_extensions_manifest['production']
: array();
$beta = isset( $preset_extensions_manifest['beta'] )
? $preset_extensions_manifest['beta']
: array();
$experimental = isset( $preset_extensions_manifest['experimental'] )
? $preset_extensions_manifest['experimental']
: array();
$no_post = isset( $preset_extensions_manifest['no-post-editor'] )
? $preset_extensions_manifest['no-post-editor']
: array();

return count( $production ) > 0 || count( $beta ) > 0 || count( $experimental ) > 0 || count( $no_post ) > 0;
}

/**
* Returns a diff from a combined list of allowed extensions and extensions determined to be excluded
*
Expand Down Expand Up @@ -611,35 +643,59 @@ public static function enqueue_block_editor_assets() {

$blocks_dir = self::get_blocks_directory();
$blocks_variation = self::blocks_variation();
$has_bundle = self::has_bundled_extension();
$single_blocks = self::get_jetpack_gutenberg_single_extensions();
$has_single = count( $single_blocks ) > 0;

if ( 'production' !== $blocks_variation ) {
$blocks_env = '-' . esc_attr( $blocks_variation );
} else {
$blocks_env = '';
}

Assets::register_script(
'jetpack-blocks-editor',
"{$blocks_dir}editor{$blocks_env}.js",
JETPACK__PLUGIN_FILE,
array( 'textdomain' => 'jetpack' )
);
if ( $has_single ) {
Assets::register_script(
'editor-core',
"{$blocks_dir}editor-core.js",
JETPACK__PLUGIN_FILE,
array( 'textdomain' => 'jetpack' )
);

// Hack around #20357 (specifically, that the editor bundle depends on
// wp-edit-post but wp-edit-post's styles break the Widget Editor and
// Site Editor) until a real fix gets unblocked.
// @todo Remove this once #20357 is properly fixed.
wp_styles()->query( 'jetpack-blocks-editor', 'registered' )->deps = array();
wp_enqueue_style( 'editor-core' );

Assets::enqueue_script( 'jetpack-blocks-editor' );
wp_localize_script(
'editor-core',
'Jetpack_Block_Assets_Base_Url',
array(
'url' => plugins_url( $blocks_dir . '/', JETPACK__PLUGIN_FILE ),
)
);
}

wp_localize_script(
'jetpack-blocks-editor',
'Jetpack_Block_Assets_Base_Url',
array(
'url' => plugins_url( $blocks_dir . '/', JETPACK__PLUGIN_FILE ),
)
);
if ( $has_bundle ) {
Assets::register_script(
'jetpack-blocks-editor',
"{$blocks_dir}editor{$blocks_env}.js",
JETPACK__PLUGIN_FILE,
array( 'textdomain' => 'jetpack' )
);

// Hack around #20357 (specifically, that the editor bundle depends on
// wp-edit-post but wp-edit-post's styles break the Widget Editor and
// Site Editor) until a real fix gets unblocked.
// @todo Remove this once #20357 is properly fixed.
wp_styles()->query( 'jetpack-blocks-editor', 'registered' )->deps = array();

Assets::enqueue_script( 'jetpack-blocks-editor' );

wp_localize_script(
'jetpack-blocks-editor',
'Jetpack_Block_Assets_Base_Url',
array(
'url' => plugins_url( $blocks_dir . '/', JETPACK__PLUGIN_FILE ),
)
);
}

if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
$user = wp_get_current_user();
Expand Down Expand Up @@ -734,14 +790,27 @@ public static function enqueue_block_editor_assets() {
);
}

wp_localize_script(
'jetpack-blocks-editor',
'Jetpack_Editor_Initial_State',
$initial_state
);
if ( $has_bundle ) {
wp_localize_script(
'jetpack-blocks-editor',
'Jetpack_Editor_Initial_State',
$initial_state
);

// Adds Connection package initial state.
wp_add_inline_script( 'jetpack-blocks-editor', Connection_Initial_State::render(), 'before' );
}

// Adds Connection package initial state.
wp_add_inline_script( 'jetpack-blocks-editor', Connection_Initial_State::render(), 'before' );
if ( $has_single && ! $has_bundle ) {
wp_localize_script(
'editor-core',
'Jetpack_Editor_Initial_State',
$initial_state
);

// Adds Connection package initial state.
wp_add_inline_script( 'editor-core', Connection_Initial_State::render(), 'before' );
}
}

/**
Expand Down Expand Up @@ -1017,6 +1086,8 @@ public static function get_extensions_preset_for_variation( $preset_extensions_m
$preset_extensions = array_unique( array_merge( $preset_extensions, $production_extensions ) );
}

$preset_extensions = array_unique( array_merge( $preset_extensions, self::get_jetpack_gutenberg_single_extensions() ) );

return $preset_extensions;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "jetpack/business-hours-single",
"title": "Business Hours Single",
"description": "Display opening hours for your business.",
"keywords": [ "opening hours", "closing time", "schedule", "working day" ],
"version": "1.0.0",
"textdomain": "jetpack",
"category": "grow",
"supports": {
"html": true,
"color": {
"gradients": true
},
"spacing": {
"margin": true,
"padding": true
},
"typography": {
"fontSize": true,
"lineHeight": true
},
"align": [ "wide", "full" ]
},
"editorScript": "file:./editor.js",
"editorStyle": "file:./editor.css",
"style": "file:./view.css"
}
Loading