Refactor server-side block registration

This commit is contained in:
Matt Sherman 2023-10-17 10:06:12 -04:00
parent 23b2953934
commit afa2b768b5
1 changed files with 45 additions and 23 deletions

View File

@ -126,6 +126,49 @@ class BlockRegistry {
return $block_name;
}
/**
* Augment the attributes of a block by adding attributes that are used by the product editor.
*
* @param array $attributes Block attributes.
*/
private function augment_attributes( $attributes ) {
// Note: If you modify this function, also update the client-side
// registerWooBlockType function in @woocommerce/block-templates.
return array_merge(
$attributes,
[
'_templateBlockId' => [
'type' => 'string',
'__experimentalRole' => 'content',
],
'_templateBlockOrder' => [
'type' => 'integer',
'__experimentalRole' => 'content',
],
'_templateBlockHideConditions' => [
'type' => 'array',
'__experimentalRole' => 'content',
],
]
);
}
/**
* Augment the uses_context of a block by adding attributes that are used by the product editor.
*
* @param array $uses_context Block uses_context.
*/
private function augment_uses_context( $uses_context ) {
// Note: If you modify this function, also update the client-side
// registerProductEditorBlockType function in @woocommerce/product-editor.
return array_merge(
isset( $uses_context ) ? $uses_context : [],
[
'productType',
]
);
}
/**
* Register a single block.
*
@ -157,29 +200,8 @@ class BlockRegistry {
return register_block_type_from_metadata(
$block_json_file,
[
'attributes' => array_merge(
$metadata['attributes'],
[
'_templateBlockId' => [
'type' => 'string',
'__experimentalRole' => 'content',
],
'_templateBlockOrder' => [
'type' => 'integer',
'__experimentalRole' => 'content',
],
'_templateBlockHideConditions' => [
'type' => 'array',
'__experimentalRole' => 'content',
],
],
),
'uses_context' => array_merge(
isset( $metadata['usesContext'] ) ? $metadata['usesContext'] : [],
[
'productType',
]
),
'attributes' => $this->augment_attributes( $metadata['attributes'] ),
'uses_context' => $this->augment_uses_context( $metadata['usesContext'] ),
]
);
}