From ec01be5a3b0d3ab1ad6becd2932a5d37c1d6b95d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Fri, 14 Apr 2023 11:40:30 +0200 Subject: [PATCH] Mini Cart template part: give user customized template priority over theme template (https://github.com/woocommerce/woocommerce-blocks/pull/9005) * Mini Cart template part: give user customized template priority over theme template * Use get_block_templates_from_db() from BlockTemplateUtils * Add deprecation notice --- .../src/BlockTemplatesController.php | 30 ++------------- .../src/BlockTypes/MiniCart.php | 13 +++++-- .../src/Utils/BlockTemplateUtils.php | 37 +++++++++++++++++++ 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/plugins/woocommerce-blocks/src/BlockTemplatesController.php b/plugins/woocommerce-blocks/src/BlockTemplatesController.php index faf6d49a8ed..68c9f73d738 100644 --- a/plugins/woocommerce-blocks/src/BlockTemplatesController.php +++ b/plugins/woocommerce-blocks/src/BlockTemplatesController.php @@ -351,32 +351,8 @@ class BlockTemplatesController { * @return int[]|\WP_Post[] An array of found templates. */ public function get_block_templates_from_db( $slugs = array(), $template_type = 'wp_template' ) { - $check_query_args = array( - 'post_type' => $template_type, - 'posts_per_page' => -1, - 'no_found_rows' => true, - 'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query - array( - 'taxonomy' => 'wp_theme', - 'field' => 'name', - 'terms' => array( BlockTemplateUtils::DEPRECATED_PLUGIN_SLUG, BlockTemplateUtils::PLUGIN_SLUG, get_stylesheet() ), - ), - ), - ); - - if ( is_array( $slugs ) && count( $slugs ) > 0 ) { - $check_query_args['post_name__in'] = $slugs; - } - - $check_query = new \WP_Query( $check_query_args ); - $saved_woo_templates = $check_query->posts; - - return array_map( - function( $saved_woo_template ) { - return BlockTemplateUtils::build_template_result_from_post( $saved_woo_template ); - }, - $saved_woo_templates - ); + wc_deprecated_function( 'BlockTemplatesController::get_block_templates_from_db()', '7.8', '\Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils::get_block_templates_from_db()' ); + return BlockTemplateUtils::get_block_templates_from_db( $slugs, $template_type ); } /** @@ -470,7 +446,7 @@ class BlockTemplatesController { * @return array WP_Block_Template[] An array of block template objects. */ public function get_block_templates( $slugs = array(), $template_type = 'wp_template' ) { - $templates_from_db = $this->get_block_templates_from_db( $slugs, $template_type ); + $templates_from_db = BlockTemplateUtils::get_block_templates_from_db( $slugs, $template_type ); $templates_from_woo = $this->get_block_templates_from_woocommerce( $slugs, $templates_from_db, $template_type ); $templates = array_merge( $templates_from_db, $templates_from_woo ); diff --git a/plugins/woocommerce-blocks/src/BlockTypes/MiniCart.php b/plugins/woocommerce-blocks/src/BlockTypes/MiniCart.php index 52b10037977..54a65135c7c 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/MiniCart.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/MiniCart.php @@ -471,10 +471,15 @@ class MiniCart extends AbstractBlock { $template_part_contents = ''; - // Determine if we need to load the template part from the theme, or WooCommerce in that order. - $theme_has_mini_cart = BlockTemplateUtils::theme_has_template_part( 'mini-cart' ); - $template_slug_to_load = $theme_has_mini_cart ? get_stylesheet() : BlockTemplateUtils::PLUGIN_SLUG; - $template_part = BlockTemplateUtils::get_block_template( $template_slug_to_load . '//mini-cart', 'wp_template_part' ); + // Determine if we need to load the template part from the DB, the theme or WooCommerce in that order. + $templates_from_db = BlockTemplateUtils::get_block_templates_from_db( array( 'mini-cart' ), 'wp_template_part' ); + if ( count( $templates_from_db ) > 0 ) { + $template_slug_to_load = $templates_from_db[0]->theme; + } else { + $theme_has_mini_cart = BlockTemplateUtils::theme_has_template_part( 'mini-cart' ); + $template_slug_to_load = $theme_has_mini_cart ? get_stylesheet() : BlockTemplateUtils::PLUGIN_SLUG; + } + $template_part = BlockTemplateUtils::get_block_template( $template_slug_to_load . '//mini-cart', 'wp_template_part' ); if ( $template_part && ! empty( $template_part->content ) ) { $template_part_contents = do_blocks( $template_part->content ); diff --git a/plugins/woocommerce-blocks/src/Utils/BlockTemplateUtils.php b/plugins/woocommerce-blocks/src/Utils/BlockTemplateUtils.php index a0f1c636c19..34e485a6387 100644 --- a/plugins/woocommerce-blocks/src/Utils/BlockTemplateUtils.php +++ b/plugins/woocommerce-blocks/src/Utils/BlockTemplateUtils.php @@ -675,4 +675,41 @@ class BlockTemplateUtils { public static function template_has_legacy_template_block( $template ) { return has_block( 'woocommerce/legacy-template', $template->content ); } + + /** + * Gets the templates saved in the database. + * + * @param array $slugs An array of slugs to retrieve templates for. + * @param string $template_type wp_template or wp_template_part. + * + * @return int[]|\WP_Post[] An array of found templates. + */ + public static function get_block_templates_from_db( $slugs = array(), $template_type = 'wp_template' ) { + $check_query_args = array( + 'post_type' => $template_type, + 'posts_per_page' => -1, + 'no_found_rows' => true, + 'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query + array( + 'taxonomy' => 'wp_theme', + 'field' => 'name', + 'terms' => array( self::DEPRECATED_PLUGIN_SLUG, self::PLUGIN_SLUG, get_stylesheet() ), + ), + ), + ); + + if ( is_array( $slugs ) && count( $slugs ) > 0 ) { + $check_query_args['post_name__in'] = $slugs; + } + + $check_query = new \WP_Query( $check_query_args ); + $saved_woo_templates = $check_query->posts; + + return array_map( + function( $saved_woo_template ) { + return self::build_template_result_from_post( $saved_woo_template ); + }, + $saved_woo_templates + ); + } }