Fix Product Query block hijacking all Query blocks queries (https://github.com/woocommerce/woocommerce-blocks/pull/6952)
* Fix Product Query block hijacking all Query blocks queries The Product Query block was adding a filter to Gutenberg's core Query Loop block. When this filter was added once, it would from then on apply to all other Query Loop blocks (or variations thereof) on a page. This change makes sure that: 1. No filters are added in the first place if our custom `__woocommerceVariationProps` are not set. 2. The filter only gets run once.
This commit is contained in:
parent
58e3b33439
commit
95963a7bab
|
@ -12,6 +12,13 @@ class ProductQuery extends AbstractBlock {
|
||||||
*/
|
*/
|
||||||
protected $block_name = 'product-query';
|
protected $block_name = 'product-query';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Block with its attributes before it gets rendered
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $parsed_block;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize this block type.
|
* Initialize this block type.
|
||||||
*
|
*
|
||||||
|
@ -30,6 +37,28 @@ class ProductQuery extends AbstractBlock {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the query block filter and parse the custom query
|
||||||
|
*
|
||||||
|
* This function is supposed to be called by the `gutenberg_build_query_vars_from_query_block`
|
||||||
|
* filter. It de-registers the filter to make sure it runs only once and doesn't end
|
||||||
|
* up hi-jacking future Query Loop blocks.
|
||||||
|
*
|
||||||
|
* It needs unfortunately to be `public` or otherwise the filter can't call it.
|
||||||
|
*
|
||||||
|
* @param WP_Query $query The WordPress Query.
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function get_query_by_attributes_once( $query ) {
|
||||||
|
remove_filter(
|
||||||
|
'gutenberg_build_query_vars_from_query_block',
|
||||||
|
array( $this, 'get_query_by_attributes_once' ),
|
||||||
|
10
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->get_query_by_attributes( $query, $this->parsed_block );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the query for the product query block.
|
* Update the query for the product query block.
|
||||||
*
|
*
|
||||||
|
@ -37,17 +66,17 @@ class ProductQuery extends AbstractBlock {
|
||||||
* @param array $parsed_block The block being rendered.
|
* @param array $parsed_block The block being rendered.
|
||||||
*/
|
*/
|
||||||
public function update_query( $pre_render, $parsed_block ) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->parsed_block = $parsed_block;
|
||||||
|
|
||||||
add_filter(
|
add_filter(
|
||||||
'gutenberg_build_query_vars_from_query_block',
|
'gutenberg_build_query_vars_from_query_block',
|
||||||
function( $query ) use ( $parsed_block ) {
|
array( $this, 'get_query_by_attributes_once' ),
|
||||||
return $this->get_query_by_attributes( $query, $parsed_block );
|
|
||||||
},
|
|
||||||
10,
|
10,
|
||||||
3
|
1
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue