Update woocommerce/create-product-editor-block to use template API (#39993)
This commit is contained in:
commit
a66a48108f
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: minor
|
||||||
|
Type: update
|
||||||
|
|
||||||
|
Update to use the template API.
|
|
@ -31,6 +31,9 @@
|
||||||
* @package {{namespace}}
|
* @package {{namespace}}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use Automattic\WooCommerce\Admin\BlockTemplates\BlockTemplateInterface;
|
||||||
|
use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates\ProductFormTemplateInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers the block using the metadata loaded from the `block.json` file.
|
* Registers the block using the metadata loaded from the `block.json` file.
|
||||||
* Behind the scenes, it registers also all assets so they can be enqueued
|
* Behind the scenes, it registers also all assets so they can be enqueued
|
||||||
|
@ -45,53 +48,22 @@ function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
|
||||||
}
|
}
|
||||||
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
|
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
|
||||||
|
|
||||||
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_add_block_to_product_editor( $args ) {
|
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_add_block_to_product_editor( BlockTemplateInterface $template ) {
|
||||||
// if the product block editor is not enabled, return the args as-is
|
if ( $template instanceof ProductFormTemplateInterface && 'simple-product' === $template->get_id() ) {
|
||||||
if ( ! class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ||
|
$basic_details = $template->get_section_by_id( 'basic-details' );
|
||||||
! \Automattic\WooCommerce\Utilities\FeaturesUtil::feature_is_enabled( 'product_block_editor' ) ) {
|
|
||||||
return $args;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if the template is not set or is not an array, return the args as-is
|
if ( $basic_details ) {
|
||||||
if ( ! isset( $args['template'] ) || ! is_array( $args['template'] ) ) {
|
$basic_details->add_block(
|
||||||
return $args;
|
[
|
||||||
}
|
'id' => '{{namespace}}-{{slug}}',
|
||||||
|
'order' => 40,
|
||||||
$template = $args['template'];
|
'blockName' => '{{namespace}}/{{slug}}',
|
||||||
|
'attributes' => [
|
||||||
// find the 'Basic details' section and add our block to the end of it
|
'message' => '{{title}}',
|
||||||
foreach ( $template as $tab_index => $tab ) {
|
]
|
||||||
$tab_properties = $tab[1];
|
]
|
||||||
|
);
|
||||||
if ( 'general' === $tab_properties['id'] ) {
|
|
||||||
$tab_sections = $tab[2];
|
|
||||||
|
|
||||||
foreach ( $tab_sections as $section_index => $section ) {
|
|
||||||
$section_properties = $section[1];
|
|
||||||
|
|
||||||
// TODO: this is not the right way to do this, since it is checking a localized string.
|
|
||||||
if ( 'Basic details' === $section_properties['title'] ) {
|
|
||||||
$section_fields = $section[2];
|
|
||||||
|
|
||||||
// add our block to the end of the section
|
|
||||||
$section_fields[] = [
|
|
||||||
'{{namespace}}/{{slug}}',
|
|
||||||
[
|
|
||||||
'message' => '{{title}}',
|
|
||||||
]
|
|
||||||
];
|
|
||||||
|
|
||||||
// update the template with our new block
|
|
||||||
$args[ 'template' ][ $tab_index ][2][ $section_index ][2] = $section_fields;
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $args;
|
|
||||||
}
|
}
|
||||||
add_filter( 'woocommerce_register_post_type_product', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_add_block_to_product_editor', 100 );
|
add_filter( 'woocommerce_block_template_register', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_add_block_to_product_editor', 100 );
|
||||||
|
|
Loading…
Reference in New Issue