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

Fix missing block translations #33546

Merged
merged 4 commits into from
Oct 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions projects/packages/blocks/changelog/fix-block-translations-2
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-2
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 ];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Required to avoid exception being thrown.


/**
* 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,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not required anymore.

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(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wrap blocks registration in an init action callback.

'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 );
}
10 changes: 1 addition & 9 deletions projects/plugins/jetpack/tools/webpack.config.extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,13 @@ module.exports = [
return metadata;
}

let scriptName = 'editor';

if ( presetIndex.beta.includes( name ) ) {
scriptName += '-beta';
} else if ( presetIndex.experimental.includes( name ) ) {
scriptName += '-experimental';
}

// `editorScript` is required for block.json to be valid and WordPress.org to be able
// to parse it before building the page at https://wordpress.org/plugins/jetpack/.
// Don't add other scripts or styles while block assets are still enqueued manually
// in the backend.
const result = {
...metadata,
editorScript: `file:../${ scriptName }.js`,
editorScript: `jetpack-blocks-editor`,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prevent multiple asset downloads for now since the script is loaded manually on the backend. An editorScript key is required for the block.json file to be valid and for WordPress.org to be able to parse it.

};

return JSON.stringify( result, null, 4 );
Expand Down
Loading