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

Fix overriding archive-product when saving a fallback template #7975

Merged
merged 3 commits into from
Dec 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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