From 936415934dfef7e6de2021439e474825a38e3e00 Mon Sep 17 00:00:00 2001 From: "M. L. Giannotta" Date: Tue, 16 Aug 2022 23:41:10 +0200 Subject: [PATCH 1/6] Add a filter to `build_query_vars_from_query_block` The filter allows Query Loop block variation to hook into the query and possibly inject custom query parameters. --- lib/compat/wordpress-6.0/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.0/blocks.php b/lib/compat/wordpress-6.0/blocks.php index be2d739329d026..5defd67d5a758a 100644 --- a/lib/compat/wordpress-6.0/blocks.php +++ b/lib/compat/wordpress-6.0/blocks.php @@ -123,7 +123,7 @@ function gutenberg_build_query_vars_from_query_block( $block, $page ) { $query['post_parent__in'] = array_filter( array_map( 'intval', $block->context['query']['parents'] ) ); } } - return $query; + return apply_filters( 'gutenberg_build_query_vars_from_query_block', $query, $block, $page ); } if ( ! function_exists( 'build_comment_query_vars_from_block' ) ) { From 241b84da8ccc28e5c84eebc3b5093cbbed27f7b8 Mon Sep 17 00:00:00 2001 From: "M. L. Giannotta" Date: Fri, 26 Aug 2022 02:30:28 +0200 Subject: [PATCH 2/6] Move `build_query_vars_from_query_block` to 6.1 compat and add docs --- lib/compat/wordpress-6.0/blocks.php | 121 ------------------------- lib/compat/wordpress-6.1/blocks.php | 136 ++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 121 deletions(-) diff --git a/lib/compat/wordpress-6.0/blocks.php b/lib/compat/wordpress-6.0/blocks.php index 5defd67d5a758a..ed83f62f76d420 100644 --- a/lib/compat/wordpress-6.0/blocks.php +++ b/lib/compat/wordpress-6.0/blocks.php @@ -5,127 +5,6 @@ * @package gutenberg */ -/** - * Helper function that constructs a WP_Query args array from - * a `Query` block properties. - * - * It's used in QueryLoop, QueryPaginationNumbers and QueryPaginationNext blocks. - * - * `build_query_vars_from_query_block` was introduced in 5.8, for 6.0 we just need - * to update that function and not create a new one. - * - * @param WP_Block $block Block instance. - * @param int $page Current query's page. - * - * @return array Returns the constructed WP_Query arguments. - */ -function gutenberg_build_query_vars_from_query_block( $block, $page ) { - $query = array( - 'post_type' => 'post', - 'order' => 'DESC', - 'orderby' => 'date', - 'post__not_in' => array(), - ); - - if ( isset( $block->context['query'] ) ) { - if ( ! empty( $block->context['query']['postType'] ) ) { - $post_type_param = $block->context['query']['postType']; - if ( is_post_type_viewable( $post_type_param ) ) { - $query['post_type'] = $post_type_param; - } - } - if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) { - $sticky = get_option( 'sticky_posts' ); - if ( 'only' === $block->context['query']['sticky'] ) { - /** - * Passing an empty array to post__in will return have_posts() as true (and all posts will be returned). - * Logic should be used before hand to determine if WP_Query should be used in the event that the array - * being passed to post__in is empty. - * - * @see https://core.trac.wordpress.org/ticket/28099 - */ - $query['post__in'] = ! empty( $sticky ) ? $sticky : array( 0 ); - $query['ignore_sticky_posts'] = 1; - } else { - $query['post__not_in'] = array_merge( $query['post__not_in'], $sticky ); - } - } - if ( ! empty( $block->context['query']['exclude'] ) ) { - $excluded_post_ids = array_map( 'intval', $block->context['query']['exclude'] ); - $excluded_post_ids = array_filter( $excluded_post_ids ); - $query['post__not_in'] = array_merge( $query['post__not_in'], $excluded_post_ids ); - } - if ( - isset( $block->context['query']['perPage'] ) && - is_numeric( $block->context['query']['perPage'] ) - ) { - $per_page = absint( $block->context['query']['perPage'] ); - $offset = 0; - - if ( - isset( $block->context['query']['offset'] ) && - is_numeric( $block->context['query']['offset'] ) - ) { - $offset = absint( $block->context['query']['offset'] ); - } - - $query['offset'] = ( $per_page * ( $page - 1 ) ) + $offset; - $query['posts_per_page'] = $per_page; - } - - // We need to migrate `categoryIds` and `tagIds` to `tax_query` for backwards compatibility. - if ( ! empty( $block->context['query']['categoryIds'] ) || ! empty( $block->context['query']['tagIds'] ) ) { - $tax_query = array(); - if ( ! empty( $block->context['query']['categoryIds'] ) ) { - $tax_query[] = array( - 'taxonomy' => 'category', - 'terms' => array_filter( array_map( 'intval', $block->context['query']['categoryIds'] ) ), - 'include_children' => false, - ); - } - if ( ! empty( $block->context['query']['tagIds'] ) ) { - $tax_query[] = array( - 'taxonomy' => 'post_tag', - 'terms' => array_filter( array_map( 'intval', $block->context['query']['tagIds'] ) ), - 'include_children' => false, - ); - } - $query['tax_query'] = $tax_query; - } - if ( ! empty( $block->context['query']['taxQuery'] ) ) { - $query['tax_query'] = array(); - foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) { - if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) { - $query['tax_query'][] = array( - 'taxonomy' => $taxonomy, - 'terms' => array_filter( array_map( 'intval', $terms ) ), - 'include_children' => false, - ); - } - } - } - if ( - isset( $block->context['query']['order'] ) && - in_array( strtoupper( $block->context['query']['order'] ), array( 'ASC', 'DESC' ), true ) - ) { - $query['order'] = strtoupper( $block->context['query']['order'] ); - } - if ( isset( $block->context['query']['orderBy'] ) ) { - $query['orderby'] = $block->context['query']['orderBy']; - } - if ( ! empty( $block->context['query']['author'] ) ) { - $query['author'] = $block->context['query']['author']; - } - if ( ! empty( $block->context['query']['search'] ) ) { - $query['s'] = $block->context['query']['search']; - } - if ( ! empty( $block->context['query']['parents'] ) && is_post_type_hierarchical( $query['post_type'] ) ) { - $query['post_parent__in'] = array_filter( array_map( 'intval', $block->context['query']['parents'] ) ); - } - } - return apply_filters( 'gutenberg_build_query_vars_from_query_block', $query, $block, $page ); -} - if ( ! function_exists( 'build_comment_query_vars_from_block' ) ) { /** * Helper function that constructs a comment query vars array from the passed block properties. diff --git a/lib/compat/wordpress-6.1/blocks.php b/lib/compat/wordpress-6.1/blocks.php index be5fb5a15c786b..38f8fbbd8628ab 100644 --- a/lib/compat/wordpress-6.1/blocks.php +++ b/lib/compat/wordpress-6.1/blocks.php @@ -205,3 +205,139 @@ function gutenberg_block_type_metadata_multiple_view_scripts( $metadata ) { return $metadata; } add_filter( 'block_type_metadata', 'gutenberg_block_type_metadata_multiple_view_scripts' ); + +/** + * Helper function that constructs a WP_Query args array from + * a `Query` block properties. + * + * It's used in QueryLoop, QueryPaginationNumbers and QueryPaginationNext blocks. + * + * `build_query_vars_from_query_block` was introduced in 5.8, for 6.0 we just need + * to update that function and not create a new one. + * + * @param WP_Block $block Block instance. + * @param int $page Current query's page. + * + * @return array Returns the constructed WP_Query arguments. + */ +function gutenberg_build_query_vars_from_query_block( $block, $page ) { + $query = array( + 'post_type' => 'post', + 'order' => 'DESC', + 'orderby' => 'date', + 'post__not_in' => array(), + ); + + if ( isset( $block->context['query'] ) ) { + if ( ! empty( $block->context['query']['postType'] ) ) { + $post_type_param = $block->context['query']['postType']; + if ( is_post_type_viewable( $post_type_param ) ) { + $query['post_type'] = $post_type_param; + } + } + if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) { + $sticky = get_option( 'sticky_posts' ); + if ( 'only' === $block->context['query']['sticky'] ) { + /** + * Passing an empty array to post__in will return have_posts() as true (and all posts will be returned). + * Logic should be used before hand to determine if WP_Query should be used in the event that the array + * being passed to post__in is empty. + * + * @see https://core.trac.wordpress.org/ticket/28099 + */ + $query['post__in'] = ! empty( $sticky ) ? $sticky : array( 0 ); + $query['ignore_sticky_posts'] = 1; + } else { + $query['post__not_in'] = array_merge( $query['post__not_in'], $sticky ); + } + } + if ( ! empty( $block->context['query']['exclude'] ) ) { + $excluded_post_ids = array_map( 'intval', $block->context['query']['exclude'] ); + $excluded_post_ids = array_filter( $excluded_post_ids ); + $query['post__not_in'] = array_merge( $query['post__not_in'], $excluded_post_ids ); + } + if ( + isset( $block->context['query']['perPage'] ) && + is_numeric( $block->context['query']['perPage'] ) + ) { + $per_page = absint( $block->context['query']['perPage'] ); + $offset = 0; + + if ( + isset( $block->context['query']['offset'] ) && + is_numeric( $block->context['query']['offset'] ) + ) { + $offset = absint( $block->context['query']['offset'] ); + } + + $query['offset'] = ( $per_page * ( $page - 1 ) ) + $offset; + $query['posts_per_page'] = $per_page; + } + + // We need to migrate `categoryIds` and `tagIds` to `tax_query` for backwards compatibility. + if ( ! empty( $block->context['query']['categoryIds'] ) || ! empty( $block->context['query']['tagIds'] ) ) { + $tax_query = array(); + if ( ! empty( $block->context['query']['categoryIds'] ) ) { + $tax_query[] = array( + 'taxonomy' => 'category', + 'terms' => array_filter( array_map( 'intval', $block->context['query']['categoryIds'] ) ), + 'include_children' => false, + ); + } + if ( ! empty( $block->context['query']['tagIds'] ) ) { + $tax_query[] = array( + 'taxonomy' => 'post_tag', + 'terms' => array_filter( array_map( 'intval', $block->context['query']['tagIds'] ) ), + 'include_children' => false, + ); + } + $query['tax_query'] = $tax_query; + } + if ( ! empty( $block->context['query']['taxQuery'] ) ) { + $query['tax_query'] = array(); + foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) { + if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) { + $query['tax_query'][] = array( + 'taxonomy' => $taxonomy, + 'terms' => array_filter( array_map( 'intval', $terms ) ), + 'include_children' => false, + ); + } + } + } + if ( + isset( $block->context['query']['order'] ) && + in_array( strtoupper( $block->context['query']['order'] ), array( 'ASC', 'DESC' ), true ) + ) { + $query['order'] = strtoupper( $block->context['query']['order'] ); + } + if ( isset( $block->context['query']['orderBy'] ) ) { + $query['orderby'] = $block->context['query']['orderBy']; + } + if ( ! empty( $block->context['query']['author'] ) ) { + $query['author'] = $block->context['query']['author']; + } + if ( ! empty( $block->context['query']['search'] ) ) { + $query['s'] = $block->context['query']['search']; + } + if ( ! empty( $block->context['query']['parents'] ) && is_post_type_hierarchical( $query['post_type'] ) ) { + $query['post_parent__in'] = array_filter( array_map( 'intval', $block->context['query']['parents'] ) ); + } + } + + /** + * Filters the arguments which will be passed to `WP_Query` for the Query Loop Block + * + * Anything to this filter should be compatible with the `WP_Query` API to form + * the query context which will be passed down to the Query Loop Block's children. + * This can help, for example, to include additional custom post types or meta queries not + * directly supported by the core Query Loop Block, and extend its capabilities. + * + * @since 6.1.0 + * + * @param array $query Array containing parameters for `WP_Query` as parsed by the block context. + * @param WP_Block $block Block instance. + * @param int $page Current query page. + */ + return apply_filters( 'query_block_query_vars', $query, $block, $page ); +} From 288b06e0638dd7c15031111ffd2422dad0ca2198 Mon Sep 17 00:00:00 2001 From: Lucio Giannotta Date: Tue, 30 Aug 2022 13:31:59 +0200 Subject: [PATCH 3/6] Apply suggestions from code review * Change filter name to `query_loop_block_query_vars` * Fix docs typo * Fix docs version Co-authored-by: Nik Tsekouras --- lib/compat/wordpress-6.1/blocks.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/compat/wordpress-6.1/blocks.php b/lib/compat/wordpress-6.1/blocks.php index 38f8fbbd8628ab..bd163dd41ef90c 100644 --- a/lib/compat/wordpress-6.1/blocks.php +++ b/lib/compat/wordpress-6.1/blocks.php @@ -212,7 +212,7 @@ function gutenberg_block_type_metadata_multiple_view_scripts( $metadata ) { * * It's used in QueryLoop, QueryPaginationNumbers and QueryPaginationNext blocks. * - * `build_query_vars_from_query_block` was introduced in 5.8, for 6.0 we just need + * `build_query_vars_from_query_block` was introduced in 5.8, for 6.1 we just need * to update that function and not create a new one. * * @param WP_Block $block Block instance. @@ -337,7 +337,7 @@ function gutenberg_build_query_vars_from_query_block( $block, $page ) { * * @param array $query Array containing parameters for `WP_Query` as parsed by the block context. * @param WP_Block $block Block instance. - * @param int $page Current query page. + * @param int $page Current query's page. */ - return apply_filters( 'query_block_query_vars', $query, $block, $page ); + return apply_filters( 'query_loop_block_query_vars', $query, $block, $page ); } From b8a6f9bb62db1238aba316c7137e20869274b028 Mon Sep 17 00:00:00 2001 From: "M. L. Giannotta" Date: Tue, 30 Aug 2022 13:37:30 +0200 Subject: [PATCH 4/6] Elaborate filter docs --- lib/compat/wordpress-6.1/blocks.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.1/blocks.php b/lib/compat/wordpress-6.1/blocks.php index bd163dd41ef90c..52a8e5e762d821 100644 --- a/lib/compat/wordpress-6.1/blocks.php +++ b/lib/compat/wordpress-6.1/blocks.php @@ -330,9 +330,15 @@ function gutenberg_build_query_vars_from_query_block( $block, $page ) { * * Anything to this filter should be compatible with the `WP_Query` API to form * the query context which will be passed down to the Query Loop Block's children. - * This can help, for example, to include additional custom post types or meta queries not + * This can help, for example, to include additional settings or meta queries not * directly supported by the core Query Loop Block, and extend its capabilities. * + * Please note that this will only influence the query that will be rendered on the + * front-end. The editor preview is not affected by this filter. Also, worth noting + * that the editor preview uses the REST API, so, ideally, one should aim to provide + * attributes which are also compatible with the REST API, in order to be able to + * implement identical queries on both sides. + * * @since 6.1.0 * * @param array $query Array containing parameters for `WP_Query` as parsed by the block context. From c1d5e3f6fbe7e469e714f3322535655ffa7b4665 Mon Sep 17 00:00:00 2001 From: Nik Tsekouras Date: Tue, 30 Aug 2022 18:30:16 +0300 Subject: [PATCH 5/6] Update lib/compat/wordpress-6.1/blocks.php --- lib/compat/wordpress-6.1/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.1/blocks.php b/lib/compat/wordpress-6.1/blocks.php index 52a8e5e762d821..0e3734311bad65 100644 --- a/lib/compat/wordpress-6.1/blocks.php +++ b/lib/compat/wordpress-6.1/blocks.php @@ -326,7 +326,7 @@ function gutenberg_build_query_vars_from_query_block( $block, $page ) { } /** - * Filters the arguments which will be passed to `WP_Query` for the Query Loop Block + * Filters the arguments which will be passed to `WP_Query` for the Query Loop Block. * * Anything to this filter should be compatible with the `WP_Query` API to form * the query context which will be passed down to the Query Loop Block's children. From 3cd0f8aa8dce34e846f68bb73a0db38ac0c70452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=B3=C5=82kowski?= Date: Wed, 31 Aug 2022 12:17:44 +0200 Subject: [PATCH 6/6] Update lib/compat/wordpress-6.1/blocks.php --- lib/compat/wordpress-6.1/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.1/blocks.php b/lib/compat/wordpress-6.1/blocks.php index 94aae9ee92c70c..82956d1bf8dd49 100644 --- a/lib/compat/wordpress-6.1/blocks.php +++ b/lib/compat/wordpress-6.1/blocks.php @@ -348,7 +348,7 @@ function gutenberg_build_query_vars_from_query_block( $block, $page ) { return apply_filters( 'query_loop_block_query_vars', $query, $block, $page ); } -/* +/** * Register render template for core blocks if handling is missing in WordPress core. * * @since 6.1.0