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

Fix Product Query block hijacking all Query blocks queries #6952

Merged
merged 8 commits into from
Aug 25, 2022
16 changes: 15 additions & 1 deletion src/BlockTypes/ProductQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,27 @@ protected function initialize() {
* @param array $parsed_block The block being rendered.
*/
public function update_query( $pre_render, $parsed_block ) {
if ( 'core/query' !== $parsed_block['blockName'] ) {
if ( 'core/query' !== $parsed_block['blockName'] || ! isset( $parsed_block['attrs']['__woocommerceVariationProps'] ) ) {
return;
}

add_filter(
sunyatasattva marked this conversation as resolved.
Show resolved Hide resolved
'gutenberg_build_query_vars_from_query_block',
function( $query ) use ( $parsed_block ) {
// We need to make sure the filter doesn't run multiple times. However,
// this is the only place we can register the filter, and we cannot unregister
// it because we need to use an anonymous callback as we need to pass the
// `$parsed_block` to this closure.
// A brief discussion of the reasoning was had in the following PR:
// https://github.com/woocommerce/woocommerce-blocks/pull/6952 .
static $has_ran = false;

if ( $has_ran ) {
return $query;
}

$has_ran = true;

return $this->get_query_by_attributes( $query, $parsed_block );
},
10,
Expand Down