wordpress-develop icon indicating copy to clipboard operation
wordpress-develop copied to clipboard

Add support for the `format` property in query

Open carolinan opened this issue 1 year ago • 5 comments

Adds handling the format property ($request['format'] ) in the class WP_REST_Posts_Controller. Adds handling the format property ($block->context['query']['format']) in the function build_query_vars_from_query_block.

Trac ticket: https://core.trac.wordpress.org/ticket/62014


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

carolinan avatar Sep 09 '24 11:09 carolinan

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props poena, peterwilsoncc, mukesh27, mamaduka, noisysocks.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

github-actions[bot] avatar Sep 09 '24 11:09 github-actions[bot]

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance, it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

github-actions[bot] avatar Sep 09 '24 11:09 github-actions[bot]

Hey @carolinan. JavaScript packages were updated in r59072 so you might wish to rebase this for easier testing. The deadline to commit this backport is 6.7 Beta 1 which is scheduled for 1 October.

noisysocks avatar Sep 20 '24 02:09 noisysocks

I tested this with the updated packages and was getting a block error when I tried to add the format. Is there something that needs to be merged to the packages in the GB repo before it should work?

query-loop

It's a little confusing with the same variable name in both the legacy query, the tax query so I'm wondering if we can do a little more tidying up there:

diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php
index f3d138475f..25cb1c0d24 100644
--- a/src/wp-includes/blocks.php
+++ b/src/wp-includes/blocks.php
@@ -2346,6 +2346,7 @@ function build_query_vars_from_query_block( $block, $page ) {
 		'order'        => 'DESC',
 		'orderby'      => 'date',
 		'post__not_in' => array(),
+		'tax_query'    => array(),
 	);
 
 	if ( isset( $block->context['query'] ) ) {
@@ -2395,34 +2396,35 @@ function build_query_vars_from_query_block( $block, $page ) {
 		}
 		// Migrate `categoryIds` and `tagIds` to `tax_query` for backwards compatibility.
 		if ( ! empty( $block->context['query']['categoryIds'] ) || ! empty( $block->context['query']['tagIds'] ) ) {
-			$tax_query = array();
+			$tax_query_back_compat = array();
 			if ( ! empty( $block->context['query']['categoryIds'] ) ) {
-				$tax_query[] = array(
+				$tax_query_back_compat[] = 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(
+				$tax_query_back_compat[] = array(
 					'taxonomy'         => 'post_tag',
 					'terms'            => array_filter( array_map( 'intval', $block->context['query']['tagIds'] ) ),
 					'include_children' => false,
 				);
 			}
-			$query['tax_query'] = $tax_query;
+			$query['tax_query'] = array_merge( $query['tax_query'], $tax_query_back_compat );
 		}
 		if ( ! empty( $block->context['query']['taxQuery'] ) ) {
-			$query['tax_query'] = array();
+			$tax_query = array();
 			foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) {
 				if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) {
-					$query['tax_query'][] = array(
+					$tax_query[] = array(
 						'taxonomy'         => $taxonomy,
 						'terms'            => array_filter( array_map( 'intval', $terms ) ),
 						'include_children' => false,
 					);
 				}
 			}
+			$query['tax_query'] = array_merge( $query['tax_query'], $tax_query );
 		}
 		if ( ! empty( $block->context['query']['format'] ) && is_array( $block->context['query']['format'] ) ) {
 			$formats = $block->context['query']['format'];
@@ -2479,13 +2481,14 @@ function build_query_vars_from_query_block( $block, $page ) {
 			 */
 			if ( count( $formats_query ) > 1 ) {
 				// Enable filtering by both post formats and other taxonomies by combining them with `AND`.
-				if ( isset( $query['tax_query'] ) ) {
-					$query['tax_query'][] = array(
+				if ( empty( $query['tax_query'] ) ) {
+					$query['tax_query'] = $formats_query;
+				} else {
+					$query['tax_query'] = array(
 						'relation' => 'AND',
+						$query['tax_query'],
 						$formats_query,
 					);
-				} else {
-					$query['tax_query'] = $formats_query;
 				}
 			}
 		}

However, the code looks good to me in terms of PHP, it's just the blcok crashing that needs to be figured out.

peterwilsoncc avatar Sep 24 '24 01:09 peterwilsoncc

I have submitted a pull request that fixes this bug and it is waiting for review.

carolinan avatar Sep 24 '24 05:09 carolinan

I'm wondering if we can do a little more tidying up there:

I don't mind :)

carolinan avatar Sep 25 '24 03:09 carolinan

@peterwilsoncc My only question is about the first array_merge. The previous code replaced $query['tax_query'], the suggested update merges them, I would like to understand why?

-			$query['tax_query'] = $tax_query;
+			$query['tax_query'] = array_merge( $query['tax_query'], $tax_query_back_compat );

carolinan avatar Sep 25 '24 04:09 carolinan

@peterwilsoncc My only question is about the first array_merge. The previous code replaced $query['tax_query'], the suggested update merges them, I would like to understand why?

The suggested change always defined $query['tax_query'] and renamed the variables so they differed for the legacy queries, current query and formats. As a result it needed to merge rather than append the item.

However, I'm glad you asked because I want to double check I didn't just introduce a bug. I'll double check that prior to merge.

peterwilsoncc avatar Sep 29 '24 23:09 peterwilsoncc

Merged r59115 / https://github.com/WordPress/wordpress-develop/commit/b1db74a765cd6dab13b7d2a09ef3890abcc59e25

peterwilsoncc avatar Sep 30 '24 01:09 peterwilsoncc