Skip to content

Commit

Permalink
Merge pull request #13 from s3rgiosan/fix/multiple-query-loop-blocks
Browse files Browse the repository at this point in the history
Fix multiple query loops
  • Loading branch information
psorensen committed Mar 13, 2024
2 parents 2ff6209 + 3588bff commit 506faa8
Showing 1 changed file with 61 additions and 26 deletions.
87 changes: 61 additions & 26 deletions includes/classes/QueryModifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class QueryModifications extends Module {

/**
* The block with its attributes before it gets rendered.
*
* @var array
*/
public $parsed_block;

/**
* Checks whether the Module should run within the current context.
*
Expand All @@ -33,39 +40,67 @@ public function register() {
/**
* Modifies the query loop to include the post picker posts.
*
* @param $block_content string The block content.
* @param $block array The block object.
*
* @return void
* @param string $block_content The block content.
* @param array $block The block object.
* @return string
*/
public function modify_query_loop_query( $block_content, $block ) {
if ( 'core/query' === $block['blockName'] ) {

$ids = isset( $block['attrs']['selectedPosts'] )
? $this->get_ids_from_content_picker( $block['attrs']['selectedPosts'] )
: [];

if ( $ids !== [] ) {
add_filter(
'query_loop_block_query_vars',
function ( $query_args ) use ( $ids ) {
$query_args['post__in'] = $ids;
$query_args['orderby'] = 'post__in';

return $query_args;
}
);
}

if ( ! isset( $block['attrs']['namespace'] ) ) {
return $block_content;
}

if ( 'curated-query-loop' !== $block['attrs']['namespace'] ) {
return $block_content;
}

$this->parsed_block = $block;

add_filter( 'query_loop_block_query_vars', [ $this, 'get_query_by_attributes_once' ] );

return $block_content;
}

private function get_ids_from_content_picker( $posts ) {
$ids = [];
foreach ( $posts as $post ) {
$ids[] = $post['id'];
/**
* Remove the query block filter and parse the custom query.
*
* @param array $query_args Array containing parameters for `WP_Query`.
* @return array
*/
public function get_query_by_attributes_once( $query_args ) {
remove_filter( 'query_loop_block_query_vars', [ $this, 'get_query_by_attributes_once' ] );
return $this->get_query_by_attributes( $query_args, $this->parsed_block );
}

/**
* Returns a custom query based on block attributes.
*
* @param array $query_args Array containing parameters for `WP_Query`.
* @param array $block The block being rendered.
* @return array
*/
public function get_query_by_attributes( $query_args, $block ) {

if ( ! isset( $block['attrs']['namespace'] ) ) {
return $query_args;
}

if ( 'curated-query-loop' !== $block['attrs']['namespace'] ) {
return $query_args;
}

$post_ids = [];
if ( isset( $block['attrs']['selectedPosts'] ) ) {
$post_ids = wp_list_pluck( $block['attrs']['selectedPosts'], 'id' );
}
return $ids;

if ( empty( $post_ids ) ) {
return $query_args;
}

$query_args['post__in'] = $post_ids;
$query_args['orderby'] = 'post__in';

return $query_args;
}
}

0 comments on commit 506faa8

Please sign in to comment.