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

Add support for the format property in query #7314

Open
wants to merge 18 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
7b34ec9
Add support for the `format` property in query
carolinan Sep 9, 2024
24efb21
Tests: Add format as a valid parameter in WP_Test_REST_Posts_Controll…
carolinan Sep 9, 2024
8e44597
build_query_vars_from_query_block: Fix incorrect variable name and br…
carolinan Sep 11, 2024
d7ab338
Tests: Update the qunit fixture in `wp-api-generated.js`
carolinan Sep 11, 2024
a4fb1b4
`WP_REST_Posts_Controller`: remove redundant array conversion for the…
carolinan Sep 11, 2024
7eec23a
`WP_REST_Posts_Controller`: Tidy up the inline comments.
carolinan Sep 11, 2024
83eefcb
`WP_REST_Posts_Controller` `get_collection_params`: add `uniqueItems`…
carolinan Sep 11, 2024
5fb2c49
Tests: Update the qunit fixtures in wp-api-generated.js
carolinan Sep 11, 2024
5058604
Update the format of inline comments
carolinan Sep 12, 2024
1b974aa
Remove unnecessary `terms` from the query for the standard format
carolinan Sep 17, 2024
3244361
Change the `relation` from `OR` to `AND` when querying for the `format`
carolinan Sep 17, 2024
5ee2750
In build_query_vars_from_query_block: validate the formats
carolinan Sep 17, 2024
c71f92f
WP_REST_Posts_Controller: Change the `relation` from `AND` to `OR` wh…
carolinan Sep 19, 2024
19ba793
build_query_vars_from_query_block: Change the `relation` when queryin…
carolinan Sep 19, 2024
ef44367
Formats: address feedback from code review and PHPCS issues
carolinan Sep 19, 2024
47e1581
build_query_vars_from_query_block: Try to resolve the nested relations
carolinan Sep 19, 2024
ad5ea9f
Merge branch 'WordPress:trunk' into try/post-formats-62014
carolinan Sep 20, 2024
0d3d4dc
Merge branch 'WordPress:trunk' into try/post-formats-62014
carolinan Sep 23, 2024
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
58 changes: 58 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2330,6 +2330,7 @@
*
* @since 5.8.0
* @since 6.1.0 Added `query_loop_block_query_vars` filter and `parents` support in query.
* @since 6.7.0 Added support for the `format` property in query.
*
* @param WP_Block $block Block instance.
* @param int $page Current query's page.
Expand Down Expand Up @@ -2420,6 +2421,63 @@
}
}
}
if ( ! empty( $block->context['query']['format'] ) && is_array( $block->context['query']['format'] ) ) {
$formats = $block->context['query']['format'];
/*
* Validate that the format is either `standard` or a supported post format.
* - First, add `standard` to the array of valid formats.
* - Then, remove any invalid formats.
*/
$valid_formats = array_merge( ['standard'], get_post_format_slugs() );

Check failure on line 2431 in src/wp-includes/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Expected 1 space after the array opener in a single line array. Found: no spaces

Check failure on line 2431 in src/wp-includes/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Short array syntax is not allowed

Check failure on line 2431 in src/wp-includes/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Expected 1 space before the array closer in a single line array. Found: no spaces
carolinan marked this conversation as resolved.
Show resolved Hide resolved
$formats = array_intersect( $formats, $valid_formats );

/*
* Ensure that the format can be combinied with other taxonomies.
* For example, a post that has both a specific category and a specific format.
*/
$tax_query = array( 'relation' => 'AND' );

/*
* The default post format, `standard`, is not stored in the database.
* If `standard` is part of the request, the query needs to exclude all post items that
* have a format assigned.
*/
if ( in_array( 'standard', $formats, true ) ) {
$tax_query[] = array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'NOT EXISTS',
);
// Remove the `standard` format, since it cannot be queried.
unset( $formats[ array_search( 'standard', $formats, true ) ] );
}
// Add any remaining formats to the tax query.
if ( ! empty( $formats ) ) {
carolinan marked this conversation as resolved.
Show resolved Hide resolved
// Add the `post-format-` prefix.
$terms = array_map(
static function ( $format ) {
return 'post-format-' . $format;
carolinan marked this conversation as resolved.
Show resolved Hide resolved
},
$formats
);
$tax_query[] = array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $terms,
'operator' => 'IN',
);
}
if ( ! isset( $query['tax_query'] ) ) {
$query['tax_query'] = array();
}
/*
* This condition is intended to prevent `$tax_query` from being added to `$query`
* if it only contains the relation.
*/
if ( count( $tax_query ) > 1 ) {
$query['tax_query'][] = $tax_query;
}
}
if (
isset( $block->context['query']['order'] ) &&
in_array( strtoupper( $block->context['query']['order'] ), array( 'ASC', 'DESC' ), true )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,58 @@ public function get_items( $request ) {

$args = $this->prepare_tax_query( $args, $request );

if ( ! empty( $request['format'] ) ) {
$formats = $request['format'];
/*
* Ensure that the format can be combinied with other taxonomies.
* For example, a post that has both a specific category and a specific format.
*/
$tax_query = array( 'relation' => 'AND' );

/*
* The default post format, `standard`, is not stored in the database.
* If `standard` is part of the request, the query needs to exclude all post items that
* have a format assigned.
*/
if ( in_array( 'standard', $formats, true ) ) {
$tax_query[] = array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'NOT EXISTS',
);
// Remove the `standard` format, since it cannot be queried.
unset( $formats[ array_search( 'standard', $formats, true ) ] );
}

// Add any remaining formats to the tax query.
if ( ! empty( $formats ) ) {
carolinan marked this conversation as resolved.
Show resolved Hide resolved
// Add the `post-format-` prefix.
$terms = array_map(
static function ( $format ) {
return 'post-format-' . $format;
},
$formats
);

$tax_query[] = array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $terms,
'operator' => 'IN',
);
}

// Enable filtering by both post formats and other taxonomies by combining them with `AND`.
if ( isset( $args['tax_query'] ) ) {
$args['tax_query'][] = array(
'relation' => 'AND',
$tax_query,
);
} else {
$args['tax_query'] = $tax_query;
}
}

// Force the post_type argument, since it's not a user input variable.
$args['post_type'] = $this->post_type;

Expand Down Expand Up @@ -2979,6 +3031,18 @@ public function get_collection_params() {
);
}

if ( post_type_supports( $this->post_type, 'post-formats' ) ) {
$query_params['format'] = array(
'description' => __( 'Limit result set to items assigned one or more given formats.' ),
'type' => 'array',
'uniqueItems' => true,
'items' => array(
'enum' => array_values( get_post_format_slugs() ),
'type' => 'string',
),
);
}

/**
* Filters collection parameters for the posts controller.
*
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public function test_registered_query_params() {
'categories_exclude',
'context',
'exclude',
'format',
'include',
'modified_after',
'modified_before',
Expand Down
21 changes: 21 additions & 0 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,27 @@ mockedApiResponse.Schema = {
"description": "Limit result set to items that are sticky.",
"type": "boolean",
"required": false
},
"format": {
"description": "Limit result set to items assigned one or more given formats.",
"type": "array",
"uniqueItems": true,
"items": {
"enum": [
"standard",
"aside",
"chat",
"gallery",
"link",
"image",
"quote",
"status",
"video",
"audio"
],
"type": "string"
},
"required": false
}
}
},
Expand Down
Loading