Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query - fix issue with current handling #1560

Merged
merged 6 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading