Skip to content

Commit

Permalink
Fix missing block translations (#33326)
Browse files Browse the repository at this point in the history
* Fix missing block translations

* changelog

* Remove duplicate block registration
  • Loading branch information
monsieur-z committed Sep 28, 2023
1 parent 507e8e3 commit b8d9424
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 69 deletions.
4 changes: 4 additions & 0 deletions projects/packages/blocks/changelog/fix-block-translations
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fix missing block translations
15 changes: 4 additions & 11 deletions projects/packages/blocks/src/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,11 @@ public static function jetpack_register_block( $slug, $args = array() ) {

$block_type = $slug;

// 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 a path is passed, make sure to get the block.json file from the build directory and get
// the block name from that file.
if ( path_is_absolute( $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 ) ) {
$slug = $name;
$block_type = new \WP_Block_Type( $slug, array_merge( $metadata, $args ) );
}
$block_type = self::get_path_to_block_metadata( $slug );
$slug = self::get_block_name( $block_type );
}

if (
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/fix-block-translations
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Fix missing block translations
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type BlockSettingsProps = {
};

export const isAiAssistantSupportExtensionEnabled =
window?.Jetpack_Editor_Initial_State.available_blocks?.[ AI_ASSISTANT_SUPPORT_NAME ];
window?.Jetpack_Editor_Initial_State?.available_blocks?.[ AI_ASSISTANT_SUPPORT_NAME ];

/**
* Check if it is possible to extend the block.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const JETPACK_FORM_AI_COMPOSITION_EXTENSION = 'ai-assistant-form-support' as const;

export const isJetpackFromBlockAiCompositionAvailable =
window?.Jetpack_Editor_Initial_State.available_blocks?.[ JETPACK_FORM_AI_COMPOSITION_EXTENSION ]
window?.Jetpack_Editor_Initial_State?.available_blocks?.[ JETPACK_FORM_AI_COMPOSITION_EXTENSION ]
?.available;

// All blocks to extend
Expand Down
2 changes: 0 additions & 2 deletions projects/plugins/jetpack/extensions/blocks/ai-chat/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import './editor.scss';
import './components/feedback/style.scss';

registerJetpackBlockFromMetadata( metadata, {
// The API version needs to be explicitly specified in this instance for styles to be loaded.
apiVersion: metadata.apiVersion,
edit,
save,
} );
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"apiVersion": 1,
"name": "jetpack/amazon",
"title": "Amazon (Beta)",
"description": "Promote Amazon products and earn a commission from sales.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"apiVersion": 1,
"name": "jetpack/business-hours",
"title": "Business Hours",
"description": "Display opening hours for your business.",
Expand Down
91 changes: 48 additions & 43 deletions projects/plugins/jetpack/extensions/blocks/recipe/recipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,60 @@

use Automattic\Jetpack\Blocks;

Blocks::jetpack_register_block(
__DIR__,
array(
'render_callback' => array( 'Automattic\\Jetpack\\Extensions\\Recipe\\Jetpack_Recipe_Block', 'render' ),
)
);
add_action(
'init',
function () {
Blocks::jetpack_register_block(
__DIR__,
array(
'render_callback' => array( 'Automattic\\Jetpack\\Extensions\\Recipe\\Jetpack_Recipe_Block', 'render' ),
)
);

Blocks::jetpack_register_block(
'jetpack/recipe-details',
array(
'parent' => array( 'jetpack/recipe' ),
)
);
Blocks::jetpack_register_block(
'jetpack/recipe-details',
array(
'parent' => array( 'jetpack/recipe' ),
)
);

Blocks::jetpack_register_block(
'jetpack/recipe-hero',
array(
'parent' => array( 'jetpack/recipe' ),
'render_callback' => array( 'Automattic\\Jetpack\\Extensions\\Recipe\\Jetpack_Recipe_Block', 'render_hero' ),
)
);
Blocks::jetpack_register_block(
'jetpack/recipe-hero',
array(
'parent' => array( 'jetpack/recipe' ),
'render_callback' => array( 'Automattic\\Jetpack\\Extensions\\Recipe\\Jetpack_Recipe_Block', 'render_hero' ),
)
);

Blocks::jetpack_register_block(
'jetpack/recipe-ingredients-list',
array(
'parent' => array( 'jetpack/recipe' ),
)
);
Blocks::jetpack_register_block(
'jetpack/recipe-ingredients-list',
array(
'parent' => array( 'jetpack/recipe' ),
)
);

Blocks::jetpack_register_block(
'jetpack/recipe-ingredient-item',
array(
'parent' => array( 'jetpack/recipe' ),
)
);
Blocks::jetpack_register_block(
'jetpack/recipe-ingredient-item',
array(
'parent' => array( 'jetpack/recipe' ),
)
);

Blocks::jetpack_register_block(
'jetpack/recipe-steps',
array(
'parent' => array( 'jetpack/recipe' ),
)
);
Blocks::jetpack_register_block(
'jetpack/recipe-steps',
array(
'parent' => array( 'jetpack/recipe' ),
)
);

Blocks::jetpack_register_block(
'jetpack/recipe-step',
array(
'parent' => array( 'jetpack/recipe' ),
'render_callback' => array( 'Automattic\\Jetpack\\Extensions\\Recipe\\Jetpack_Recipe_Block', 'render_step' ),
)
Blocks::jetpack_register_block(
'jetpack/recipe-step',
array(
'parent' => array( 'jetpack/recipe' ),
'render_callback' => array( 'Automattic\\Jetpack\\Extensions\\Recipe\\Jetpack_Recipe_Block', 'render_step' ),
)
);
}
);

require_once __DIR__ . '/class-jetpack-recipe-block.php';
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { aiExcerptPluginName, aiExcerptPluginSettings } from '.';
export const AI_CONTENT_LENS = 'ai-content-lens';

const isAiAssistantSupportExtensionEnabled =
window?.Jetpack_Editor_Initial_State.available_blocks[ 'ai-content-lens' ];
window?.Jetpack_Editor_Initial_State?.available_blocks[ 'ai-content-lens' ];

/**
* Extend the editor with AI Content Lens features,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ const JETPACK_PREFIX = 'jetpack/';
/**
* Registers a gutenberg block if the availability requirements are met.
*
* @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 {string} nameOrMetadata - The block's name or metadata object. 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 ) {
export default function registerJetpackBlock(
nameOrMetadata,
settings,
childBlocks = [],
prefix = true
) {
const name = typeof nameOrMetadata === 'string' ? nameOrMetadata : nameOrMetadata.name;
const isNamePrefixed = name.startsWith( JETPACK_PREFIX );
const rawName = isNamePrefixed ? name.slice( JETPACK_PREFIX.length ) : name;

Expand All @@ -40,7 +46,10 @@ export default function registerJetpackBlock( name, settings, childBlocks = [],
}

const prefixedName = jpPrefix + rawName;
const result = registerBlockType( prefixedName, settings );
const result = registerBlockType(
nameOrMetadata === 'object' ? nameOrMetadata : prefixedName,
settings
);

if ( requiredPlan ) {
addFilter(
Expand Down Expand Up @@ -69,20 +78,20 @@ export default function registerJetpackBlock( name, settings, childBlocks = [],
* @returns {object|boolean} Either false if the block is not available, or the results of `registerBlockType`
*/
export function registerJetpackBlockFromMetadata( metadata, settings, childBlocks, prefix ) {
const clientSettings = {
const mergedSettings = {
...settings,
icon: getBlockIconProp( metadata ),
};
const { variations } = metadata;

if ( Array.isArray( variations ) && variations.length > 0 ) {
clientSettings.variations = variations.map( variation => {
mergedSettings.variations = variations.map( variation => {
return {
...variation,
icon: getBlockIconProp( variation ),
};
} );
}

return registerJetpackBlock( metadata.name, clientSettings, childBlocks, prefix );
return registerJetpackBlock( metadata, mergedSettings, childBlocks, prefix );
}

0 comments on commit b8d9424

Please sign in to comment.