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
This commit is contained in:
parent
af01052bbe
commit
ec01be5a3b
|
@ -351,32 +351,8 @@ class BlockTemplatesController {
|
||||||
* @return int[]|\WP_Post[] An array of found templates.
|
* @return int[]|\WP_Post[] An array of found templates.
|
||||||
*/
|
*/
|
||||||
public function get_block_templates_from_db( $slugs = array(), $template_type = 'wp_template' ) {
|
public function get_block_templates_from_db( $slugs = array(), $template_type = 'wp_template' ) {
|
||||||
$check_query_args = array(
|
wc_deprecated_function( 'BlockTemplatesController::get_block_templates_from_db()', '7.8', '\Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils::get_block_templates_from_db()' );
|
||||||
'post_type' => $template_type,
|
return BlockTemplateUtils::get_block_templates_from_db( $slugs, $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
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -470,7 +446,7 @@ class BlockTemplatesController {
|
||||||
* @return array WP_Block_Template[] An array of block template objects.
|
* @return array WP_Block_Template[] An array of block template objects.
|
||||||
*/
|
*/
|
||||||
public function get_block_templates( $slugs = array(), $template_type = 'wp_template' ) {
|
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_from_woo = $this->get_block_templates_from_woocommerce( $slugs, $templates_from_db, $template_type );
|
||||||
$templates = array_merge( $templates_from_db, $templates_from_woo );
|
$templates = array_merge( $templates_from_db, $templates_from_woo );
|
||||||
|
|
||||||
|
|
|
@ -471,10 +471,15 @@ class MiniCart extends AbstractBlock {
|
||||||
|
|
||||||
$template_part_contents = '';
|
$template_part_contents = '';
|
||||||
|
|
||||||
// Determine if we need to load the template part from the theme, or WooCommerce in that order.
|
// Determine if we need to load the template part from the DB, the theme or WooCommerce in that order.
|
||||||
$theme_has_mini_cart = BlockTemplateUtils::theme_has_template_part( 'mini-cart' );
|
$templates_from_db = BlockTemplateUtils::get_block_templates_from_db( array( 'mini-cart' ), 'wp_template_part' );
|
||||||
$template_slug_to_load = $theme_has_mini_cart ? get_stylesheet() : BlockTemplateUtils::PLUGIN_SLUG;
|
if ( count( $templates_from_db ) > 0 ) {
|
||||||
$template_part = BlockTemplateUtils::get_block_template( $template_slug_to_load . '//mini-cart', 'wp_template_part' );
|
$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 ) ) {
|
if ( $template_part && ! empty( $template_part->content ) ) {
|
||||||
$template_part_contents = do_blocks( $template_part->content );
|
$template_part_contents = do_blocks( $template_part->content );
|
||||||
|
|
|
@ -675,4 +675,41 @@ class BlockTemplateUtils {
|
||||||
public static function template_has_legacy_template_block( $template ) {
|
public static function template_has_legacy_template_block( $template ) {
|
||||||
return has_block( 'woocommerce/legacy-template', $template->content );
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue