Make WordPress Core


Ignore:
Timestamp:
09/19/2019 02:04:51 PM (7 years ago)
Author:
kadamwhite
Message:

REST API: Support dot.nested hierarchical properties in _fields query parameter.

Enable clients to opt-in to receipt of one or more specific sub-properties within a response, and not other sub-properties.
Skip potentially expensive filtering and processing for post resources which were explicitly not requested.

Props kadamwhite, TimothyBlynJacobs, dlh.
Fixes #42094.

File:
1 edited

Legend:

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

    r46068 r46184  
    14401440                $data = array();
    14411441
    1442                 if ( in_array( 'id', $fields, true ) ) {
     1442                if ( rest_is_field_included( 'id', $fields ) ) {
    14431443                        $data['id'] = $post->ID;
    14441444                }
    14451445
    1446                 if ( in_array( 'date', $fields, true ) ) {
     1446                if ( rest_is_field_included( 'date', $fields ) ) {
    14471447                        $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
    14481448                }
    14491449
    1450                 if ( in_array( 'date_gmt', $fields, true ) ) {
     1450                if ( rest_is_field_included( 'date_gmt', $fields ) ) {
    14511451                        // For drafts, `post_date_gmt` may not be set, indicating that the
    14521452                        // date of the draft should be updated each time it is saved (see
     
    14611461                }
    14621462
    1463                 if ( in_array( 'guid', $fields, true ) ) {
     1463                if ( rest_is_field_included( 'guid', $fields ) ) {
    14641464                        $data['guid'] = array(
    14651465                                /** This filter is documented in wp-includes/post-template.php */
     
    14691469                }
    14701470
    1471                 if ( in_array( 'modified', $fields, true ) ) {
     1471                if ( rest_is_field_included( 'modified', $fields ) ) {
    14721472                        $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
    14731473                }
    14741474
    1475                 if ( in_array( 'modified_gmt', $fields, true ) ) {
     1475                if ( rest_is_field_included( 'modified_gmt', $fields ) ) {
    14761476                        // For drafts, `post_modified_gmt` may not be set (see
    14771477                        // `post_date_gmt` comments above).  In this case, shim the value
     
    14861486                }
    14871487
    1488                 if ( in_array( 'password', $fields, true ) ) {
     1488                if ( rest_is_field_included( 'password', $fields ) ) {
    14891489                        $data['password'] = $post->post_password;
    14901490                }
    14911491
    1492                 if ( in_array( 'slug', $fields, true ) ) {
     1492                if ( rest_is_field_included( 'slug', $fields ) ) {
    14931493                        $data['slug'] = $post->post_name;
    14941494                }
    14951495
    1496                 if ( in_array( 'status', $fields, true ) ) {
     1496                if ( rest_is_field_included( 'status', $fields ) ) {
    14971497                        $data['status'] = $post->post_status;
    14981498                }
    14991499
    1500                 if ( in_array( 'type', $fields, true ) ) {
     1500                if ( rest_is_field_included( 'type', $fields ) ) {
    15011501                        $data['type'] = $post->post_type;
    15021502                }
    15031503
    1504                 if ( in_array( 'link', $fields, true ) ) {
     1504                if ( rest_is_field_included( 'link', $fields ) ) {
    15051505                        $data['link'] = get_permalink( $post->ID );
    15061506                }
    15071507
    1508                 if ( in_array( 'title', $fields, true ) ) {
     1508                if ( rest_is_field_included( 'title', $fields ) ) {
     1509                        $data['title'] = array();
     1510                }
     1511                if ( rest_is_field_included( 'title.raw', $fields ) ) {
     1512                        $data['title']['raw'] = $post->post_title;
     1513                }
     1514                if ( rest_is_field_included( 'title.rendered', $fields ) ) {
    15091515                        add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
    15101516
    1511                         $data['title'] = array(
    1512                                 'raw'      => $post->post_title,
    1513                                 'rendered' => get_the_title( $post->ID ),
    1514                         );
     1517                        $data['title']['rendered'] = get_the_title( $post->ID );
    15151518
    15161519                        remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
     
    15261529                }
    15271530
    1528                 if ( in_array( 'content', $fields, true ) ) {
    1529                         $data['content'] = array(
    1530                                 'raw'           => $post->post_content,
    1531                                 /** This filter is documented in wp-includes/post-template.php */
    1532                                 'rendered'      => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ),
    1533                                 'protected'     => (bool) $post->post_password,
    1534                                 'block_version' => block_version( $post->post_content ),
    1535                         );
    1536                 }
    1537 
    1538                 if ( in_array( 'excerpt', $fields, true ) ) {
     1531                if ( rest_is_field_included( 'content', $fields ) ) {
     1532                        $data['content'] = array();
     1533                }
     1534                if ( rest_is_field_included( 'content.raw', $fields ) ) {
     1535                        $data['content']['raw'] = $post->post_content;
     1536                }
     1537                if ( rest_is_field_included( 'content.rendered', $fields ) ) {
     1538                        /** This filter is documented in wp-includes/post-template.php */
     1539                        $data['content']['rendered'] = post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content );
     1540                }
     1541                if ( rest_is_field_included( 'content.protected', $fields ) ) {
     1542                        $data['content']['protected'] = (bool) $post->post_password;
     1543                }
     1544                if ( rest_is_field_included( 'content.block_version', $fields ) ) {
     1545                        $data['content']['block_version'] = block_version( $post->post_content );
     1546                }
     1547
     1548                if ( rest_is_field_included( 'excerpt', $fields ) ) {
    15391549                        /** This filter is documented in wp-includes/post-template.php */
    15401550                        $excerpt         = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );
     
    15511561                }
    15521562
    1553                 if ( in_array( 'author', $fields, true ) ) {
     1563                if ( rest_is_field_included( 'author', $fields ) ) {
    15541564                        $data['author'] = (int) $post->post_author;
    15551565                }
    15561566
    1557                 if ( in_array( 'featured_media', $fields, true ) ) {
     1567                if ( rest_is_field_included( 'featured_media', $fields ) ) {
    15581568                        $data['featured_media'] = (int) get_post_thumbnail_id( $post->ID );
    15591569                }
    15601570
    1561                 if ( in_array( 'parent', $fields, true ) ) {
     1571                if ( rest_is_field_included( 'parent', $fields ) ) {
    15621572                        $data['parent'] = (int) $post->post_parent;
    15631573                }
    15641574
    1565                 if ( in_array( 'menu_order', $fields, true ) ) {
     1575                if ( rest_is_field_included( 'menu_order', $fields ) ) {
    15661576                        $data['menu_order'] = (int) $post->menu_order;
    15671577                }
    15681578
    1569                 if ( in_array( 'comment_status', $fields, true ) ) {
     1579                if ( rest_is_field_included( 'comment_status', $fields ) ) {
    15701580                        $data['comment_status'] = $post->comment_status;
    15711581                }
    15721582
    1573                 if ( in_array( 'ping_status', $fields, true ) ) {
     1583                if ( rest_is_field_included( 'ping_status', $fields ) ) {
    15741584                        $data['ping_status'] = $post->ping_status;
    15751585                }
    15761586
    1577                 if ( in_array( 'sticky', $fields, true ) ) {
     1587                if ( rest_is_field_included( 'sticky', $fields ) ) {
    15781588                        $data['sticky'] = is_sticky( $post->ID );
    15791589                }
    15801590
    1581                 if ( in_array( 'template', $fields, true ) ) {
     1591                if ( rest_is_field_included( 'template', $fields ) ) {
    15821592                        $template = get_page_template_slug( $post->ID );
    15831593                        if ( $template ) {
     
    15881598                }
    15891599
    1590                 if ( in_array( 'format', $fields, true ) ) {
     1600                if ( rest_is_field_included( 'format', $fields ) ) {
    15911601                        $data['format'] = get_post_format( $post->ID );
    15921602
     
    15971607                }
    15981608
    1599                 if ( in_array( 'meta', $fields, true ) ) {
     1609                if ( rest_is_field_included( 'meta', $fields ) ) {
    16001610                        $data['meta'] = $this->meta->get_value( $post->ID, $request );
    16011611                }
     
    16061616                        $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
    16071617
    1608                         if ( in_array( $base, $fields, true ) ) {
     1618                        if ( rest_is_field_included( $base, $fields ) ) {
    16091619                                $terms         = get_the_terms( $post, $taxonomy->name );
    16101620                                $data[ $base ] = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array();
     
    16141624                $post_type_obj = get_post_type_object( $post->post_type );
    16151625                if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
    1616                         $permalink_template_requested = in_array( 'permalink_template', $fields, true );
    1617                         $generated_slug_requested     = in_array( 'generated_slug', $fields, true );
     1626                        $permalink_template_requested = rest_is_field_included( 'permalink_template', $fields );
     1627                        $generated_slug_requested     = rest_is_field_included( 'generated_slug', $fields );
    16181628
    16191629                        if ( $permalink_template_requested || $generated_slug_requested ) {
Note: See TracChangeset for help on using the changeset viewer.