This commit is contained in:
Luigi Teschio 2022-09-15 13:53:56 +02:00 committed by GitHub
parent 87e1748811
commit 84432552a8
1 changed files with 12 additions and 31 deletions

View File

@ -37,27 +37,6 @@ class ProductQuery extends AbstractBlock {
} }
/**
* Remove the query block filter and parse the custom query
*
* This function is supposed to be called by the `query_loop_block_query_vars`
* 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(
'query_loop_block_query_vars',
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.
@ -66,28 +45,30 @@ 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'] || ! isset( $parsed_block['attrs']['__woocommerceVariationProps'] ) ) { if ( 'core/query' !== $parsed_block['blockName'] ) {
return; return;
} }
$this->parsed_block = $parsed_block; $this->parsed_block = $parsed_block;
add_filter( if ( isset( $parsed_block['attrs']['__woocommerceVariationProps'] ) ) {
'query_loop_block_query_vars', add_filter(
array( $this, 'get_query_by_attributes_once' ), 'query_loop_block_query_vars',
10, array( $this, 'get_query_by_attributes' ),
1 10,
); 1
);
}
} }
/** /**
* Return a custom query based on the attributes. * Return a custom query based on the attributes.
* *
* @param WP_Query $query The WordPress Query. * @param WP_Query $query The WordPress Query.
* @param WP_Block $parsed_block The block being rendered.
* @return array * @return array
*/ */
public function get_query_by_attributes( $query, $parsed_block ) { public function get_query_by_attributes( $query ) {
$parsed_block = $this->parsed_block;
if ( ! isset( $parsed_block['attrs']['__woocommerceVariationProps'] ) ) { if ( ! isset( $parsed_block['attrs']['__woocommerceVariationProps'] ) ) {
return $query; return $query;
} }