Make WordPress Core


Ignore:
Timestamp:
09/30/2024 01:17:40 AM (19 months ago)
Author:
peterwilsoncc
Message:

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 #62014.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r59064 r59115  
    346346
    347347        $args = $this->prepare_tax_query( $args, $request );
     348
     349        if ( ! empty( $request['format'] ) ) {
     350            $formats = $request['format'];
     351            /*
     352             * The relation needs to be set to `OR` since the request can contain
     353             * two separate conditions. The user may be querying for items that have
     354             * either the `standard` format or a specific format.
     355             */
     356            $formats_query = array( 'relation' => 'OR' );
     357
     358            /*
     359             * The default post format, `standard`, is not stored in the database.
     360             * If `standard` is part of the request, the query needs to exclude all post items that
     361             * have a format assigned.
     362             */
     363            if ( in_array( 'standard', $formats, true ) ) {
     364                $formats_query[] = array(
     365                    'taxonomy' => 'post_format',
     366                    'field'    => 'slug',
     367                    'operator' => 'NOT EXISTS',
     368                );
     369                // Remove the `standard` format, since it cannot be queried.
     370                unset( $formats[ array_search( 'standard', $formats, true ) ] );
     371            }
     372
     373            // Add any remaining formats to the formats query.
     374            if ( ! empty( $formats ) ) {
     375                // Add the `post-format-` prefix.
     376                $terms = array_map(
     377                    static function ( $format ) {
     378                        return "post-format-$format";
     379                    },
     380                    $formats
     381                );
     382
     383                $formats_query[] = array(
     384                    'taxonomy' => 'post_format',
     385                    'field'    => 'slug',
     386                    'terms'    => $terms,
     387                    'operator' => 'IN',
     388                );
     389            }
     390
     391            // Enable filtering by both post formats and other taxonomies by combining them with `AND`.
     392            if ( isset( $args['tax_query'] ) ) {
     393                $args['tax_query'][] = array(
     394                    'relation' => 'AND',
     395                    $formats_query,
     396                );
     397            } else {
     398                $args['tax_query'] = $formats_query;
     399            }
     400        }
    348401
    349402        // Force the post_type argument, since it's not a user input variable.
     
    29933046        }
    29943047
     3048        if ( post_type_supports( $this->post_type, 'post-formats' ) ) {
     3049            $query_params['format'] = array(
     3050                'description' => __( 'Limit result set to items assigned one or more given formats.' ),
     3051                'type'        => 'array',
     3052                'uniqueItems' => true,
     3053                'items'       => array(
     3054                    'enum' => array_values( get_post_format_slugs() ),
     3055                    'type' => 'string',
     3056                ),
     3057            );
     3058        }
     3059
    29953060        /**
    29963061         * Filters collection parameters for the posts controller.
Note: See TracChangeset for help on using the changeset viewer.