Fix overriding `archive-product` when saving a fallback template (https://github.com/woocommerce/woocommerce-blocks/pull/7975)

* Fix overriding archive-product when saving a fallback template

* Add comment
This commit is contained in:
Alba Rincón 2022-12-19 15:31:00 +01:00 committed by GitHub
parent 7dfced254a
commit 5b095eb1a2
1 changed files with 12 additions and 6 deletions

View File

@ -93,12 +93,11 @@ class BlockTemplatesController {
} }
$wp_query_args = array( $wp_query_args = array(
'post_name__in' => array( 'archive-product' ), 'post_name__in' => array( 'archive-product', $slug ),
'post_type' => $template_type, 'post_type' => $template_type,
'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ), 'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ),
'posts_per_page' => 1, 'no_found_rows' => true,
'no_found_rows' => true, 'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
array( array(
'taxonomy' => 'wp_theme', 'taxonomy' => 'wp_theme',
'field' => 'name', 'field' => 'name',
@ -109,6 +108,12 @@ class BlockTemplatesController {
$template_query = new \WP_Query( $wp_query_args ); $template_query = new \WP_Query( $wp_query_args );
$posts = $template_query->posts; $posts = $template_query->posts;
// If we have more than one result from the query, it means that the current template is present in the db (has
// been customized by the user) and we should not return the `archive-product` template.
if ( count( $posts ) > 1 ) {
return null;
}
if ( count( $posts ) > 0 ) { if ( count( $posts ) > 0 ) {
$template = _build_block_template_result_from_post( $posts[0] ); $template = _build_block_template_result_from_post( $posts[0] );
@ -117,6 +122,7 @@ class BlockTemplatesController {
$template->slug = $slug; $template->slug = $slug;
$template->title = BlockTemplateUtils::get_block_template_title( $slug ); $template->title = BlockTemplateUtils::get_block_template_title( $slug );
$template->description = BlockTemplateUtils::get_block_template_description( $slug ); $template->description = BlockTemplateUtils::get_block_template_description( $slug );
unset( $template->source );
return $template; return $template;
} }