Make WordPress Core

Changeset 61987


Ignore:
Timestamp:
03/12/2026 01:21:11 PM (5 months ago)
Author:
ellatrix
Message:

REST API: Support nested _fields in revisions controller.

Use rest_is_field_included() instead of in_array() for content, title, excerpt, and guid fields in WP_REST_Revisions_Controller. This lets clients request specific sub-fields (e.g. _fields=content.raw) and skip expensive the_content rendering.

The [REST API _fields documentation](https://developer.wordpress.org/rest-api/using-the-rest-api/global-parameters/#_fields) states that nested fields are supported using dot notation. However, the revisions controller uses in_array() which doesn't match nested keys like content.raw.

Developed in: https://github.com/WordPress/wordpress-develop/pull/11230.

Props ellatrix, andrewserong, mukeshpanchal27.
Fixes #64844.

File:
1 edited

Legend:

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

    r61773 r61987  
    640640                }
    641641
    642                 if ( in_array( 'guid', $fields, true ) ) {
    643                         $data['guid'] = array(
    644                                 /** This filter is documented in wp-includes/post-template.php */
    645                                 'rendered' => apply_filters( 'get_the_guid', $post->guid, $post->ID ),
    646                                 'raw'      => $post->guid,
    647                         );
    648                 }
    649 
    650                 if ( in_array( 'title', $fields, true ) ) {
    651                         $data['title'] = array(
    652                                 'raw'      => $post->post_title,
    653                                 'rendered' => get_the_title( $post->ID ),
    654                         );
    655                 }
    656 
    657                 if ( in_array( 'content', $fields, true ) ) {
    658 
    659                         $data['content'] = array(
    660                                 'raw'      => $post->post_content,
    661                                 /** This filter is documented in wp-includes/post-template.php */
    662                                 'rendered' => apply_filters( 'the_content', $post->post_content ),
    663                         );
    664                 }
    665 
    666                 if ( in_array( 'excerpt', $fields, true ) ) {
    667                         $data['excerpt'] = array(
    668                                 'raw'      => $post->post_excerpt,
    669                                 'rendered' => $this->prepare_excerpt_response( $post->post_excerpt, $post ),
    670                         );
     642                if ( rest_is_field_included( 'guid', $fields ) ) {
     643                        $data['guid'] = array();
     644                }
     645                if ( rest_is_field_included( 'guid.rendered', $fields ) ) {
     646                        /** This filter is documented in wp-includes/post-template.php */
     647                        $data['guid']['rendered'] = apply_filters( 'get_the_guid', $post->guid, $post->ID );
     648                }
     649                if ( rest_is_field_included( 'guid.raw', $fields ) ) {
     650                        $data['guid']['raw'] = $post->guid;
     651                }
     652
     653                if ( rest_is_field_included( 'title', $fields ) ) {
     654                        $data['title'] = array();
     655                }
     656                if ( rest_is_field_included( 'title.raw', $fields ) ) {
     657                        $data['title']['raw'] = $post->post_title;
     658                }
     659                if ( rest_is_field_included( 'title.rendered', $fields ) ) {
     660                        $data['title']['rendered'] = get_the_title( $post->ID );
     661                }
     662
     663                if ( rest_is_field_included( 'content', $fields ) ) {
     664                        $data['content'] = array();
     665                }
     666                if ( rest_is_field_included( 'content.raw', $fields ) ) {
     667                        $data['content']['raw'] = $post->post_content;
     668                }
     669                if ( rest_is_field_included( 'content.rendered', $fields ) ) {
     670                        /** This filter is documented in wp-includes/post-template.php */
     671                        $data['content']['rendered'] = apply_filters( 'the_content', $post->post_content );
     672                }
     673
     674                if ( rest_is_field_included( 'excerpt', $fields ) ) {
     675                        $data['excerpt'] = array();
     676                }
     677                if ( rest_is_field_included( 'excerpt.raw', $fields ) ) {
     678                        $data['excerpt']['raw'] = $post->post_excerpt;
     679                }
     680                if ( rest_is_field_included( 'excerpt.rendered', $fields ) ) {
     681                        $data['excerpt']['rendered'] = $this->prepare_excerpt_response( $post->post_excerpt, $post );
    671682                }
    672683
Note: See TracChangeset for help on using the changeset viewer.