<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[59115] trunk: REST API/Editor: Support post formats in Query Block & Posts API.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/59115">59115</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/59115","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>peterwilsoncc</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-09-30 01:17:40 +0000 (Mon, 30 Sep 2024)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>REST API/Editor: Support post formats in Query Block & Posts API.
Introduces post format support for both the Query Block with the new parameter `format`. In the `build_query_vars_from_query_block()` function, this is converted to a `post_format` taxonomy query passed to `WP_Query`.
Also introduces the `format` parameter to the REST API's Posts controller to support the feature in the Query block. The parameter type is an enumerated string accepted the post formats supported by each post type.
Props poena, mukesh27, mamaduka, noisysocks, TimothyBlynJacobs.
Fixes <a href="https://core.trac.wordpress.org/ticket/62014">#62014</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocksphp">trunk/src/wp-includes/blocks.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestpostscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php</a></li>
<li><a href="#trunktestsphpunittestsblockswpBlockphp">trunk/tests/phpunit/tests/blocks/wpBlock.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestpostscontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php</a></li>
<li><a href="#trunktestsqunitfixtureswpapigeneratedjs">trunk/tests/qunit/fixtures/wp-api-generated.js</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesblocksphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/blocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/blocks.php 2024-09-29 22:47:03 UTC (rev 59114)
+++ trunk/src/wp-includes/blocks.php 2024-09-30 01:17:40 UTC (rev 59115)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2335,6 +2335,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.8.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.1.0 Added `query_loop_block_query_vars` filter and `parents` support in query.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.7.0 Added support for the `format` property in query.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_Block $block Block instance.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $page Current query's page.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2347,6 +2348,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'order' => 'DESC',
</span><span class="cx" style="display: block; padding: 0 10px"> 'orderby' => 'date',
</span><span class="cx" style="display: block; padding: 0 10px"> 'post__not_in' => array(),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'tax_query' => array(),
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $block->context['query'] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2396,9 +2398,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> // Migrate `categoryIds` and `tagIds` to `tax_query` for backwards compatibility.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $block->context['query']['categoryIds'] ) || ! empty( $block->context['query']['tagIds'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $tax_query = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $tax_query_back_compat = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $block->context['query']['categoryIds'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $tax_query[] = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $tax_query_back_compat[] = array(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'taxonomy' => 'category',
</span><span class="cx" style="display: block; padding: 0 10px"> 'terms' => array_filter( array_map( 'intval', $block->context['query']['categoryIds'] ) ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'include_children' => false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2405,19 +2407,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $block->context['query']['tagIds'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $tax_query[] = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $tax_query_back_compat[] = array(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'taxonomy' => 'post_tag',
</span><span class="cx" style="display: block; padding: 0 10px"> 'terms' => array_filter( array_map( 'intval', $block->context['query']['tagIds'] ) ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'include_children' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $query['tax_query'] = $tax_query;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $query['tax_query'] = array_merge( $query['tax_query'], $tax_query_back_compat );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $block->context['query']['taxQuery'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $query['tax_query'] = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $tax_query = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $query['tax_query'][] = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $tax_query[] = array(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'taxonomy' => $taxonomy,
</span><span class="cx" style="display: block; padding: 0 10px"> 'terms' => array_filter( array_map( 'intval', $terms ) ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'include_children' => false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2424,7 +2426,75 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $query['tax_query'] = array_merge( $query['tax_query'], $tax_query );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 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( array( 'standard' ), get_post_format_slugs() );
+ $formats = array_intersect( $formats, $valid_formats );
+
+ /*
+ * The relation needs to be set to `OR` since the request can contain
+ * two separate conditions. The user may be querying for items that have
+ * either the `standard` format or a specific format.
+ */
+ $formats_query = array( 'relation' => 'OR' );
+
+ /*
+ * 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 ) ) {
+ $formats_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 formats query.
+ if ( ! empty( $formats ) ) {
+ // Add the `post-format-` prefix.
+ $terms = array_map(
+ static function ( $format ) {
+ return "post-format-$format";
+ },
+ $formats
+ );
+ $formats_query[] = array(
+ 'taxonomy' => 'post_format',
+ 'field' => 'slug',
+ 'terms' => $terms,
+ 'operator' => 'IN',
+ );
+ }
+
+ /*
+ * Add `$formats_query` to `$query`, as long as it contains more than one key:
+ * If `$formats_query` only contains the initial `relation` key, there are no valid formats to query,
+ * and the query should not be modified.
+ */
+ if ( count( $formats_query ) > 1 ) {
+ // Enable filtering by both post formats and other taxonomies by combining them with `AND`.
+ if ( empty( $query['tax_query'] ) ) {
+ $query['tax_query'] = $formats_query;
+ } else {
+ $query['tax_query'] = array(
+ 'relation' => 'AND',
+ $query['tax_query'],
+ $formats_query,
+ );
+ }
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if (
</span><span class="cx" style="display: block; padding: 0 10px"> isset( $block->context['query']['order'] ) &&
</span><span class="cx" style="display: block; padding: 0 10px"> in_array( strtoupper( $block->context['query']['order'] ), array( 'ASC', 'DESC' ), true )
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestpostscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 2024-09-29 22:47:03 UTC (rev 59114)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 2024-09-30 01:17:40 UTC (rev 59115)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -346,6 +346,59 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $args = $this->prepare_tax_query( $args, $request );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $request['format'] ) ) {
+ $formats = $request['format'];
+ /*
+ * The relation needs to be set to `OR` since the request can contain
+ * two separate conditions. The user may be querying for items that have
+ * either the `standard` format or a specific format.
+ */
+ $formats_query = array( 'relation' => 'OR' );
+
+ /*
+ * 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 ) ) {
+ $formats_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 formats query.
+ if ( ! empty( $formats ) ) {
+ // Add the `post-format-` prefix.
+ $terms = array_map(
+ static function ( $format ) {
+ return "post-format-$format";
+ },
+ $formats
+ );
+
+ $formats_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',
+ $formats_query,
+ );
+ } else {
+ $args['tax_query'] = $formats_query;
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Force the post_type argument, since it's not a user input variable.
</span><span class="cx" style="display: block; padding: 0 10px"> $args['post_type'] = $this->post_type;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2992,6 +3045,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 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',
+ ),
+ );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters collection parameters for the posts controller.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span></span></pre></div>
<a id="trunktestsphpunittestsblockswpBlockphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/blocks/wpBlock.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/wpBlock.php 2024-09-29 22:47:03 UTC (rev 59114)
+++ trunk/tests/phpunit/tests/blocks/wpBlock.php 2024-09-30 01:17:40 UTC (rev 59115)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -464,6 +464,134 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 62014
+ */
+ public function test_build_query_vars_from_query_block_standard_post_formats() {
+ $this->registry->register(
+ 'core/example',
+ array( 'uses_context' => array( 'query' ) )
+ );
+
+ $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
+ $parsed_block = $parsed_blocks[0];
+ $context = array(
+ 'query' => array(
+ 'postType' => 'post',
+ 'format' => array( 'standard' ),
+ ),
+ );
+ $block = new WP_Block( $parsed_block, $context, $this->registry );
+ $query = build_query_vars_from_query_block( $block, 1 );
+
+ $this->assertSame(
+ array(
+ 'post_type' => 'post',
+ 'order' => 'DESC',
+ 'orderby' => 'date',
+ 'post__not_in' => array(),
+ 'tax_query' => array(
+ 'relation' => 'OR',
+ array(
+ 'taxonomy' => 'post_format',
+ 'field' => 'slug',
+ 'operator' => 'NOT EXISTS',
+ ),
+ ),
+ ),
+ $query
+ );
+ }
+
+ /**
+ * @ticket 62014
+ */
+ public function test_build_query_vars_from_query_block_post_format() {
+ $this->registry->register(
+ 'core/example',
+ array( 'uses_context' => array( 'query' ) )
+ );
+
+ $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
+ $parsed_block = $parsed_blocks[0];
+ $context = array(
+ 'query' => array(
+ 'postType' => 'post',
+ 'format' => array( 'aside' ),
+ ),
+ );
+ $block = new WP_Block( $parsed_block, $context, $this->registry );
+ $query = build_query_vars_from_query_block( $block, 1 );
+
+ $this->assertSame(
+ array(
+ 'post_type' => 'post',
+ 'order' => 'DESC',
+ 'orderby' => 'date',
+ 'post__not_in' => array(),
+ 'tax_query' => array(
+ 'relation' => 'OR',
+ array(
+ 'taxonomy' => 'post_format',
+ 'field' => 'slug',
+ 'terms' => array( 'post-format-aside' ),
+ 'operator' => 'IN',
+ ),
+ ),
+ ),
+ $query
+ );
+ }
+ /**
+ * @ticket 62014
+ */
+ public function test_build_query_vars_from_query_block_post_formats_with_category() {
+ $this->registry->register(
+ 'core/example',
+ array( 'uses_context' => array( 'query' ) )
+ );
+
+ $parsed_blocks = parse_blocks( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
+ $parsed_block = $parsed_blocks[0];
+ $context = array(
+ 'query' => array(
+ 'postType' => 'post',
+ 'format' => array( 'standard' ),
+ 'categoryIds' => array( 56 ),
+ ),
+ );
+ $block = new WP_Block( $parsed_block, $context, $this->registry );
+ $query = build_query_vars_from_query_block( $block, 1 );
+
+ $this->assertSame(
+ array(
+ 'post_type' => 'post',
+ 'order' => 'DESC',
+ 'orderby' => 'date',
+ 'post__not_in' => array(),
+ 'tax_query' => array(
+ 'relation' => 'AND',
+ array(
+ array(
+ 'taxonomy' => 'category',
+ 'terms' => array( 56 ),
+ 'include_children' => false,
+ ),
+ ),
+ array(
+ 'relation' => 'OR',
+ array(
+ 'taxonomy' => 'post_format',
+ 'field' => 'slug',
+ 'operator' => 'NOT EXISTS',
+ ),
+ ),
+ ),
+ ),
+ $query
+ );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 52991
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_build_query_vars_from_query_block_no_context() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -481,6 +609,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'order' => 'DESC',
</span><span class="cx" style="display: block; padding: 0 10px"> 'orderby' => 'date',
</span><span class="cx" style="display: block; padding: 0 10px"> 'post__not_in' => array(),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'tax_query' => array(),
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -512,6 +641,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'order' => 'DESC',
</span><span class="cx" style="display: block; padding: 0 10px"> 'orderby' => 'date',
</span><span class="cx" style="display: block; padding: 0 10px"> 'post__not_in' => array(),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'tax_query' => array(),
</ins><span class="cx" style="display: block; padding: 0 10px"> 'offset' => 0,
</span><span class="cx" style="display: block; padding: 0 10px"> 'posts_per_page' => 2,
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -544,6 +674,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'order' => 'DESC',
</span><span class="cx" style="display: block; padding: 0 10px"> 'orderby' => 'date',
</span><span class="cx" style="display: block; padding: 0 10px"> 'post__not_in' => array(),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'tax_query' => array(),
</ins><span class="cx" style="display: block; padding: 0 10px"> 'offset' => 10,
</span><span class="cx" style="display: block; padding: 0 10px"> 'posts_per_page' => 5,
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -576,6 +707,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'order' => 'DESC',
</span><span class="cx" style="display: block; padding: 0 10px"> 'orderby' => 'date',
</span><span class="cx" style="display: block; padding: 0 10px"> 'post__not_in' => array(),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'tax_query' => array(),
</ins><span class="cx" style="display: block; padding: 0 10px"> 'offset' => 12,
</span><span class="cx" style="display: block; padding: 0 10px"> 'posts_per_page' => 5,
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -619,6 +751,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'order' => 'DESC',
</span><span class="cx" style="display: block; padding: 0 10px"> 'orderby' => 'title',
</span><span class="cx" style="display: block; padding: 0 10px"> 'post__not_in' => array(),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'tax_query' => array(),
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestpostscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php 2024-09-29 22:47:03 UTC (rev 59114)
+++ trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php 2024-09-30 01:17:40 UTC (rev 59115)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -196,6 +196,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'categories_exclude',
</span><span class="cx" style="display: block; padding: 0 10px"> 'context',
</span><span class="cx" style="display: block; padding: 0 10px"> 'exclude',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'format',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'include',
</span><span class="cx" style="display: block; padding: 0 10px"> 'modified_after',
</span><span class="cx" style="display: block; padding: 0 10px"> 'modified_before',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5497,6 +5498,136 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Test the REST API support for the standard post format.
+ *
+ * @ticket 62014
+ *
+ * @covers WP_REST_Posts_Controller::get_items
+ */
+ public function test_standard_post_format_support() {
+ $initial_theme_support = get_theme_support( 'post-formats' );
+ add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );
+
+ $post_id = self::factory()->post->create(
+ array(
+ 'post_type' => 'post',
+ 'post_status' => 'publish',
+ )
+ );
+ set_post_format( $post_id, 'aside' );
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
+ $request->set_param( 'format', array( 'standard' ) );
+ $request->set_param( 'per_page', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
+
+ $response = rest_get_server()->dispatch( $request );
+
+ /*
+ * Restore the initial post formats support.
+ *
+ * This needs to be done prior to the assertions to avoid unexpected
+ * results for other tests should an assertion fail.
+ */
+ if ( $initial_theme_support ) {
+ add_theme_support( 'post-formats', $initial_theme_support[0] );
+ } else {
+ remove_theme_support( 'post-formats' );
+ }
+
+ $this->assertCount( 3, $response->get_data(), 'The response should only include standard post formats' );
+ }
+
+ /**
+ * Test the REST API support for post formats.
+ *
+ * @ticket 62014
+ *
+ * @covers WP_REST_Posts_Controller::get_items
+ */
+ public function test_post_format_support() {
+ $initial_theme_support = get_theme_support( 'post-formats' );
+ add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );
+
+ $post_id = self::factory()->post->create(
+ array(
+ 'post_type' => 'post',
+ 'post_status' => 'publish',
+ )
+ );
+ set_post_format( $post_id, 'aside' );
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
+ $request->set_param( 'format', array( 'aside' ) );
+
+ $response_aside = rest_get_server()->dispatch( $request );
+
+ $request->set_param( 'format', array( 'invalid_format' ) );
+ $response_invalid = rest_get_server()->dispatch( $request );
+
+ /*
+ * Restore the initial post formats support.
+ *
+ * This needs to be done prior to the assertions to avoid unexpected
+ * results for other tests should an assertion fail.
+ */
+ if ( $initial_theme_support ) {
+ add_theme_support( 'post-formats', $initial_theme_support[0] );
+ } else {
+ remove_theme_support( 'post-formats' );
+ }
+
+ $this->assertCount( 1, $response_aside->get_data(), 'Only one post is expected to be returned.' );
+ $this->assertErrorResponse( 'rest_invalid_param', $response_invalid, 400, 'An invalid post format should return an error' );
+ }
+
+ /**
+ * Test the REST API support for multiple post formats.
+ *
+ * @ticket 62014
+ *
+ * @covers WP_REST_Posts_Controller::get_items
+ */
+ public function test_multiple_post_format_support() {
+ $initial_theme_support = get_theme_support( 'post-formats' );
+ add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );
+
+ $post_id = self::factory()->post->create(
+ array(
+ 'post_type' => 'post',
+ 'post_status' => 'publish',
+ )
+ );
+ set_post_format( $post_id, 'aside' );
+
+ $post_id_2 = self::factory()->post->create(
+ array(
+ 'post_type' => 'post',
+ 'post_status' => 'publish',
+ )
+ );
+ set_post_format( $post_id_2, 'gallery' );
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
+ $request->set_param( 'format', array( 'aside', 'gallery' ) );
+
+ $response = rest_get_server()->dispatch( $request );
+
+ /*
+ * Restore the initial post formats support.
+ *
+ * This needs to be done prior to the assertions to avoid unexpected
+ * results for other tests should an assertion fail.
+ */
+ if ( $initial_theme_support ) {
+ add_theme_support( 'post-formats', $initial_theme_support[0] );
+ } else {
+ remove_theme_support( 'post-formats' );
+ }
+
+ $this->assertCount( 2, $response->get_data(), 'Two posts are expected to be returned' );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Internal function used to disable an insert query which
</span><span class="cx" style="display: block; padding: 0 10px"> * will trigger a wpdb error for testing purposes.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span></span></pre></div>
<a id="trunktestsqunitfixtureswpapigeneratedjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/qunit/fixtures/wp-api-generated.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/qunit/fixtures/wp-api-generated.js 2024-09-29 22:47:03 UTC (rev 59114)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js 2024-09-30 01:17:40 UTC (rev 59115)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -626,6 +626,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "description": "Limit result set to items that are sticky.",
</span><span class="cx" style="display: block; padding: 0 10px"> "type": "boolean",
</span><span class="cx" style="display: block; padding: 0 10px"> "required": false
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ },
+ "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
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span></span></pre>
</div>
</div>
</body>
</html>