Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Fix overriding archive-product when saving a fallback template (#7975)
Browse files Browse the repository at this point in the history
* Fix overriding archive-product when saving a fallback template

* Add comment
  • Loading branch information
albarin authored Dec 19, 2022
1 parent 7d989a5 commit 77d72eb
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,11 @@ public function get_block_template_fallback( $template, $id, $template_type ) {
}

$wp_query_args = array(
'post_name__in' => array( 'archive-product' ),
'post_type' => $template_type,
'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ),
'posts_per_page' => 1,
'no_found_rows' => true,
'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
'post_name__in' => array( 'archive-product', $slug ),
'post_type' => $template_type,
'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ),
'no_found_rows' => true,
'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
Expand All @@ -109,6 +108,12 @@ public function get_block_template_fallback( $template, $id, $template_type ) {
$template_query = new \WP_Query( $wp_query_args );
$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 ) {
$template = _build_block_template_result_from_post( $posts[0] );

Expand All @@ -117,6 +122,7 @@ public function get_block_template_fallback( $template, $id, $template_type ) {
$template->slug = $slug;
$template->title = BlockTemplateUtils::get_block_template_title( $slug );
$template->description = BlockTemplateUtils::get_block_template_description( $slug );
unset( $template->source );

return $template;
}
Expand Down

0 comments on commit 77d72eb

Please sign in to comment.