Make WordPress Core


Ignore:
Timestamp:
09/30/2024 01:17:40 AM (8 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/blocks.php

    r59101 r59115  
    23362336 * @since 5.8.0
    23372337 * @since 6.1.0 Added `query_loop_block_query_vars` filter and `parents` support in query.
     2338 * @since 6.7.0 Added support for the `format` property in query.
    23382339 *
    23392340 * @param WP_Block $block Block instance.
     
    23482349        'orderby'      => 'date',
    23492350        'post__not_in' => array(),
     2351        'tax_query'    => array(),
    23502352    );
    23512353
     
    23972399        // Migrate `categoryIds` and `tagIds` to `tax_query` for backwards compatibility.
    23982400        if ( ! empty( $block->context['query']['categoryIds'] ) || ! empty( $block->context['query']['tagIds'] ) ) {
    2399             $tax_query = array();
     2401            $tax_query_back_compat = array();
    24002402            if ( ! empty( $block->context['query']['categoryIds'] ) ) {
    2401                 $tax_query[] = array(
     2403                $tax_query_back_compat[] = array(
    24022404                    'taxonomy'         => 'category',
    24032405                    'terms'            => array_filter( array_map( 'intval', $block->context['query']['categoryIds'] ) ),
     
    24062408            }
    24072409            if ( ! empty( $block->context['query']['tagIds'] ) ) {
    2408                 $tax_query[] = array(
     2410                $tax_query_back_compat[] = array(
    24092411                    'taxonomy'         => 'post_tag',
    24102412                    'terms'            => array_filter( array_map( 'intval', $block->context['query']['tagIds'] ) ),
     
    24122414                );
    24132415            }
    2414             $query['tax_query'] = $tax_query;
     2416            $query['tax_query'] = array_merge( $query['tax_query'], $tax_query_back_compat );
    24152417        }
    24162418        if ( ! empty( $block->context['query']['taxQuery'] ) ) {
    2417             $query['tax_query'] = array();
     2419            $tax_query = array();
    24182420            foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) {
    24192421                if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) {
    2420                     $query['tax_query'][] = array(
     2422                    $tax_query[] = array(
    24212423                        'taxonomy'         => $taxonomy,
    24222424                        'terms'            => array_filter( array_map( 'intval', $terms ) ),
     
    24252427                }
    24262428            }
    2427         }
     2429            $query['tax_query'] = array_merge( $query['tax_query'], $tax_query );
     2430        }
     2431        if ( ! empty( $block->context['query']['format'] ) && is_array( $block->context['query']['format'] ) ) {
     2432            $formats = $block->context['query']['format'];
     2433            /*
     2434             * Validate that the format is either `standard` or a supported post format.
     2435             * - First, add `standard` to the array of valid formats.
     2436             * - Then, remove any invalid formats.
     2437             */
     2438            $valid_formats = array_merge( array( 'standard' ), get_post_format_slugs() );
     2439            $formats       = array_intersect( $formats, $valid_formats );
     2440
     2441            /*
     2442             * The relation needs to be set to `OR` since the request can contain
     2443             * two separate conditions. The user may be querying for items that have
     2444             * either the `standard` format or a specific format.
     2445             */
     2446            $formats_query = array( 'relation' => 'OR' );
     2447
     2448            /*
     2449             * The default post format, `standard`, is not stored in the database.
     2450             * If `standard` is part of the request, the query needs to exclude all post items that
     2451             * have a format assigned.
     2452             */
     2453            if ( in_array( 'standard', $formats, true ) ) {
     2454                $formats_query[] = array(
     2455                    'taxonomy' => 'post_format',
     2456                    'field'    => 'slug',
     2457                    'operator' => 'NOT EXISTS',
     2458                );
     2459                // Remove the `standard` format, since it cannot be queried.
     2460                unset( $formats[ array_search( 'standard', $formats, true ) ] );
     2461            }
     2462            // Add any remaining formats to the formats query.
     2463            if ( ! empty( $formats ) ) {
     2464                // Add the `post-format-` prefix.
     2465                $terms = array_map(
     2466                    static function ( $format ) {
     2467                        return "post-format-$format";
     2468                    },
     2469                    $formats
     2470                );
     2471                $formats_query[] = array(
     2472                    'taxonomy' => 'post_format',
     2473                    'field'    => 'slug',
     2474                    'terms'    => $terms,
     2475                    'operator' => 'IN',
     2476                );
     2477            }
     2478
     2479            /*
     2480             * Add `$formats_query` to `$query`, as long as it contains more than one key:
     2481             * If `$formats_query` only contains the initial `relation` key, there are no valid formats to query,
     2482             * and the query should not be modified.
     2483             */
     2484            if ( count( $formats_query ) > 1 ) {
     2485                // Enable filtering by both post formats and other taxonomies by combining them with `AND`.
     2486                if ( empty( $query['tax_query'] ) ) {
     2487                    $query['tax_query'] = $formats_query;
     2488                } else {
     2489                    $query['tax_query'] = array(
     2490                        'relation' => 'AND',
     2491                        $query['tax_query'],
     2492                        $formats_query,
     2493                    );
     2494                }
     2495            }
     2496        }
     2497
    24282498        if (
    24292499            isset( $block->context['query']['order'] ) &&
Note: See TracChangeset for help on using the changeset viewer.