Skip to content

Commit

Permalink
Merge pull request #1560 from tomusborne/bug/query-current
Browse files Browse the repository at this point in the history
Query - fix issue with current handling
  • Loading branch information
tomusborne authored Dec 4, 2024
2 parents 8552507 + 010c3ef commit 219567b
Show file tree
Hide file tree
Showing 3 changed files with 2,201 additions and 4,162 deletions.
71 changes: 25 additions & 46 deletions includes/class-query-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,52 +95,20 @@ public function get_wp_query( $request ) {
$args = $request->get_param( 'args' );
$page = $args['paged'] ?? $request->get_param( 'page' ) ?? 1;
$attributes = $request->get_param( 'attributes' ) ?? [];
$current_post = $request->get_param( 'currentPost' ) ?? null;
$current_author = $current_post['author'] ?? 0;

// Handle current entity values.
if ( $current_post ) {
$included_posts = $args['post__in'] ?? [];
$excluded_posts = $args['post__not_in'] ?? [];

foreach ( $included_posts as &$the_post ) {
if ( 'current' === $the_post ) {
$the_post = $current_post;
}
}

foreach ( $excluded_posts as &$the_post ) {
if ( 'current' === $the_post ) {
$the_post = $current_post;
}
}

$args['post__in'] = $included_posts;
$args['post__not_in'] = $excluded_posts;
}

if ( $current_author ) {
$included_authors = $args['author__in'] ?? [];
$excluded_authors = $args['author__not_in'] ?? [];

foreach ( $included_authors as &$the_post ) {
if ( 'current' === $the_post ) {
$the_post = $current_author;
}
}

foreach ( $excluded_authors as &$the_post ) {
if ( 'current' === $the_post ) {
$the_post = $current_author;
}
}

$args['author__in'] = $included_authors;
$args['author__not_in'] = $excluded_authors;
}
$current_post = $request->get_param( 'postId' ) ?? null;
$current_author = $request->get_param( 'authorId' ) ?? null;

$query = new WP_Query(
self::get_wp_query_args( $args, $page, $attributes )
self::get_wp_query_args(
$args,
$page,
$attributes,
null,
[
'post_id' => $current_post,
'author_id' => $current_author,
]
)
);

return rest_ensure_response( $query );
Expand All @@ -153,10 +121,14 @@ public function get_wp_query( $request ) {
* @param int $page Current query's page.
* @param array $attributes The query block's attributes. Used for reference in the filters.
* @param WP_Block|array $block The current block.
* @param array $current Array of current entities (post, author, etc.).
*
* @return array $query_args The optimized WP_Query args array.
*/
public static function get_wp_query_args( $args = [], $page = 1, $attributes = [], $block = null ) {
public static function get_wp_query_args( $args = [], $page = 1, $attributes = [], $block = null, $current = [] ) {
$current_post_id = $current['post_id'] ?? get_the_ID();
$current_author_id = $current['author_id'] ?? get_the_author_meta( 'ID' );

// Set up our pagination.
if ( ! isset( $args['paged'] ) && -1 < (int) $page ) {
$args['paged'] = $page;
Expand Down Expand Up @@ -261,12 +233,19 @@ function( $value ) {
* @param array @query_args The array of args for the WP_Query.
* @param array $attributes The block attributes.
* @param WP_Block|array $block The current block.
* @param array $current The current entities (post, author, etc.).
*
* @return $args The modified query arguments.
*/
return apply_filters(
'generateblocks_query_wp_query_args',
$args,
$attributes,
null === $block ? new stdClass() : $block
null === $block ? new stdClass() : $block,
[
'post_id' => $current_post_id,
'author_id' => $current_author_id,
]
);
}

Expand Down
Loading

0 comments on commit 219567b

Please sign in to comment.