From c4f796e03427aff2af4632cc9a2c9e2aee3539bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Tue, 18 Jul 2023 09:12:43 +0200 Subject: [PATCH] Create Mini-Cart template part area (#10203) * Create Mini-Cart template part area * Update Mini-Cart template part name to exclude it * Update src/BlockTemplatesController.php Co-authored-by: Daniel Dudzic * Update src/BlockTemplatesController.php Co-authored-by: Daniel Dudzic * Keep both variations of the template part name when excluding them * Revert "Keep both variations of the template part name when excluding them" This reverts commit c3f293511536d1ebbac0f50caaf74cded28d0025. --------- Co-authored-by: Daniel Dudzic --- assets/js/blocks/mini-cart/index.tsx | 2 +- src/BlockTemplatesController.php | 18 ++++++++++++++++++ src/Utils/BlockTemplateUtils.php | 5 +++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/assets/js/blocks/mini-cart/index.tsx b/assets/js/blocks/mini-cart/index.tsx index 4098ac26b1c..40cd601d329 100644 --- a/assets/js/blocks/mini-cart/index.tsx +++ b/assets/js/blocks/mini-cart/index.tsx @@ -61,7 +61,7 @@ addFilter( ...blockSettings, variations: blockSettings.variations.map( ( variation: { name: string } ) => { - if ( variation.name === 'instance_mini-cart' ) { + if ( variation.name === 'mini-cart' ) { return { ...variation, scope: [], diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index 49f6878d13f..48aa936552c 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -68,6 +68,7 @@ public function __construct( Package $package ) { * Initialization method. */ protected function init() { + add_filter( 'default_wp_template_part_areas', array( $this, 'register_mini_cart_template_part_area' ), 10, 1 ); add_action( 'template_redirect', array( $this, 'render_block_template' ) ); add_filter( 'pre_get_block_template', array( $this, 'get_block_template_fallback' ), 10, 3 ); add_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 ); @@ -103,6 +104,23 @@ function( $settings, $metadata ) { } } + /** + * Add Mini-Cart to the default template part areas. + * + * @param array $default_area_definitions An array of supported area objects. + * @return array The supported template part areas including the Mini-Cart one. + */ + public function register_mini_cart_template_part_area( $default_area_definitions ) { + $mini_cart_template_part_area = [ + 'area' => 'mini-cart', + 'label' => __( 'Mini-Cart', 'woo-gutenberg-products-block' ), + 'description' => __( 'The Mini-Cart template allows shoppers to see their cart items and provides access to the Cart and Checkout pages.', 'woo-gutenberg-products-block' ), + 'icon' => 'mini-cart', + 'area_tag' => 'mini-cart', + ]; + return array_merge( $default_area_definitions, [ $mini_cart_template_part_area ] ); + } + /** * Renders the `core/template-part` block on the server. * diff --git a/src/Utils/BlockTemplateUtils.php b/src/Utils/BlockTemplateUtils.php index d6e63110c3b..1894c64195d 100644 --- a/src/Utils/BlockTemplateUtils.php +++ b/src/Utils/BlockTemplateUtils.php @@ -219,6 +219,11 @@ public static function build_template_result_from_file( $template_file, $templat $template->is_custom = false; // Templates loaded from the filesystem aren't custom, ones that have been edited and loaded from the DB are. $template->post_types = array(); // Don't appear in any Edit Post template selector dropdown. $template->area = 'uncategorized'; + + // Force the Mini-Cart template part to be in the Mini-Cart template part area. + if ( 'wp_template_part' === $template_type && 'mini-cart' === $template_file->slug ) { + $template->area = 'mini-cart'; + } return $template; }