Make WordPress Core

Ticket #42094: 42094.2.diff

File 42094.2.diff, 16.0 KB (added by kadamwhite, 6 years ago)

Propose an implementation for nested _fields handling

  • src/wp-includes/rest-api.php

    diff --git src/wp-includes/rest-api.php src/wp-includes/rest-api.php
    index 1c73b97824..8e8e3718f9 100644
    function rest_send_allow_header( $response, $server, $request ) { 
    691691        return $response;
    692692}
    693693
     694/**
     695 * Recursively computes the intersection of arrays using keys for comparison.
     696 *
     697 * @param  array $array1 The array with master keys to check.
     698 * @param  array $array2 An array to compare keys against.
     699 *
     700 * @return array An associative array containing all the entries of array1 which have keys that are present in all arguments.
     701 */
     702function _rest_array_intersect_key_recursive( $array1, $array2 ) {
     703        $array1 = array_intersect_key( $array1, $array2 );
     704        foreach ( $array1 as $key => $value ) {
     705                if ( is_array( $value ) && is_array( $array2[ $key ] ) ) {
     706                        $array1[ $key ] = _rest_array_intersect_key_recursive( $value, $array2[ $key ] );
     707                }
     708        }
     709        return $array1;
     710}
     711
    694712/**
    695713 * Filter the API response to include only a white-listed set of response object fields.
    696714 *
    function rest_filter_response_fields( $response, $server, $request ) { 
    718736        // Trim off outside whitespace from the comma delimited list.
    719737        $fields = array_map( 'trim', $fields );
    720738
    721         $fields_as_keyed = array_combine( $fields, array_fill( 0, count( $fields ), true ) );
     739        // Create nested array of accepted field hierarchy.
     740        $fields_as_keyed = array();
     741        foreach ( $fields as $field ) {
     742                $parts = explode( '.', $field );
     743                $ref   = &$fields_as_keyed;
     744                while ( count( $parts ) > 1 ) {
     745                        $next         = array_shift( $parts );
     746                        $ref[ $next ] = array();
     747                        $ref          = &$ref[ $next ];
     748                }
     749                $last         = array_shift( $parts );
     750                $ref[ $last ] = true;
     751        }
    722752
    723753        if ( wp_is_numeric_array( $data ) ) {
    724754                $new_data = array();
    725755                foreach ( $data as $item ) {
    726                         $new_data[] = array_intersect_key( $item, $fields_as_keyed );
     756                        $new_data[] = _rest_array_intersect_key_recursive( $item, $fields_as_keyed );
    727757                }
    728758        } else {
    729                 $new_data = array_intersect_key( $data, $fields_as_keyed );
     759                $new_data = _rest_array_intersect_key_recursive( $data, $fields_as_keyed );
    730760        }
    731761
    732762        $response->set_data( $new_data );
  • src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
    index d610504692..46b5b85a48 100644
    abstract class WP_REST_Controller { 
    542542                if ( in_array( 'id', $fields, true ) ) {
    543543                        $requested_fields[] = 'id';
    544544                }
    545                 return array_intersect( $fields, $requested_fields );
     545                // Return the list of all requested fields which appear in the schema.
     546                return array_reduce(
     547                        $requested_fields,
     548                        function( $response_fields, $field ) use ( $fields ) {
     549                                if ( in_array( $field, $fields, true ) ) {
     550                                        $response_fields[] = $field;
     551                                        return $response_fields;
     552                                }
     553                                // Check for nested fields if $field is not a direct match.
     554                                $nested_fields = explode( '.', $field );
     555                                // A nested field is included so long as its top-level property is
     556                                // present in the schema.
     557                                if ( in_array( $nested_fields[0], $fields, true ) ) {
     558                                        $response_fields[] = $field;
     559                                }
     560                                return $response_fields;
     561                        },
     562                        array()
     563                );
     564        }
     565
     566        /**
     567         * Given an array of fields to include in the response, some of which may be
     568         * `nested.fields`, determine whether a provided field should be included in
     569         * the response body.
     570         *
     571         * If a parent field is passed in, the presence of any nested field within
     572         * that parent will cause the method to return `true`. For example "title"
     573         * will return true if any of `title`, `title.raw` or `title.rendered` is
     574         * provided.
     575         *
     576         * @since 5.3.0
     577         *
     578         * @param string $field  A field to test for inclusion in the response body.
     579         * @param array  $fields An array of string fields supported by the endpoint.
     580         * @return bool Whether to include the field or not.
     581         */
     582        public function is_field_included( $field, $fields ) {
     583                if ( in_array( $field, $fields, true ) ) {
     584                        return true;
     585                }
     586                foreach ( $fields as $accepted_field ) {
     587                        // Check to see if $field is the parent of any item in $fields.
     588                        // A field "parent" should be accepted if "parent.child" is accepted.
     589                        if ( strpos( $accepted_field, "$field." ) === 0 ) {
     590                                return true;
     591                        }
     592                        // Conversely, if "parent" is accepted, all "parent.child" fields should
     593                        // also be accepted.
     594                        if ( strpos( $field, "$accepted_field." ) === 0 ) {
     595                                return true;
     596                        }
     597                }
     598                return false;
    546599        }
    547600
    548601        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index f24bc068c8..c8eeae4e32 100644
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    14391439                // Base fields for every post.
    14401440                $data = array();
    14411441
    1442                 if ( in_array( 'id', $fields, true ) ) {
     1442                if ( $this->is_field_included( 'id', $fields ) ) {
    14431443                        $data['id'] = $post->ID;
    14441444                }
    14451445
    1446                 if ( in_array( 'date', $fields, true ) ) {
     1446                if ( $this->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 ( $this->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
    14531453                        // #38883).  In this case, shim the value based on the `post_date`
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    14601460                        $data['date_gmt'] = $this->prepare_date_response( $post_date_gmt );
    14611461                }
    14621462
    1463                 if ( in_array( 'guid', $fields, true ) ) {
     1463                if ( $this->is_field_included( 'guid', $fields ) ) {
    14641464                        $data['guid'] = array(
    14651465                                /** This filter is documented in wp-includes/post-template.php */
    14661466                                'rendered' => apply_filters( 'get_the_guid', $post->guid, $post->ID ),
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    14681468                        );
    14691469                }
    14701470
    1471                 if ( in_array( 'modified', $fields, true ) ) {
     1471                if ( $this->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 ( $this->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
    14781478                        // based on the `post_modified` field with the site's timezone
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    14851485                        $data['modified_gmt'] = $this->prepare_date_response( $post_modified_gmt );
    14861486                }
    14871487
    1488                 if ( in_array( 'password', $fields, true ) ) {
     1488                if ( $this->is_field_included( 'password', $fields ) ) {
    14891489                        $data['password'] = $post->post_password;
    14901490                }
    14911491
    1492                 if ( in_array( 'slug', $fields, true ) ) {
     1492                if ( $this->is_field_included( 'slug', $fields ) ) {
    14931493                        $data['slug'] = $post->post_name;
    14941494                }
    14951495
    1496                 if ( in_array( 'status', $fields, true ) ) {
     1496                if ( $this->is_field_included( 'status', $fields ) ) {
    14971497                        $data['status'] = $post->post_status;
    14981498                }
    14991499
    1500                 if ( in_array( 'type', $fields, true ) ) {
     1500                if ( $this->is_field_included( 'type', $fields ) ) {
    15011501                        $data['type'] = $post->post_type;
    15021502                }
    15031503
    1504                 if ( in_array( 'link', $fields, true ) ) {
     1504                if ( $this->is_field_included( 'link', $fields ) ) {
    15051505                        $data['link'] = get_permalink( $post->ID );
    15061506                }
    15071507
    1508                 if ( in_array( 'title', $fields, true ) ) {
     1508                if ( $this->is_field_included( 'title', $fields ) ) {
     1509                        $data['title'] = array();
     1510                }
     1511                if ( $this->is_field_included( 'title.raw', $fields ) ) {
     1512                        $data['title']['raw'] = $post->post_title;
     1513                }
     1514                if ( $this->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' ) );
    15171520                }
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    15251528                        $has_password_filter = true;
    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                         );
     1531                if ( $this->is_field_included( 'content', $fields ) ) {
     1532                        $data['content'] = array();
     1533                }
     1534                if ( $this->is_field_included( 'content.raw', $fields ) ) {
     1535                        $data['content']['raw'] = $post->post_content;
     1536                }
     1537                if ( $this->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 ( $this->is_field_included( 'content.protected', $fields ) ) {
     1542                        $data['content']['protected'] = (bool) $post->post_password;
     1543                }
     1544                if ( $this->is_field_included( 'content.block_version', $fields ) ) {
     1545                        $data['content']['block_version'] = block_version( $post->post_content );
    15361546                }
    15371547
    1538                 if ( in_array( 'excerpt', $fields, true ) ) {
     1548                if ( $this->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 ) );
    15411551                        $data['excerpt'] = array(
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    15501560                        remove_filter( 'post_password_required', '__return_false' );
    15511561                }
    15521562
    1553                 if ( in_array( 'author', $fields, true ) ) {
     1563                if ( $this->is_field_included( 'author', $fields ) ) {
    15541564                        $data['author'] = (int) $post->post_author;
    15551565                }
    15561566
    1557                 if ( in_array( 'featured_media', $fields, true ) ) {
     1567                if ( $this->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 ( $this->is_field_included( 'parent', $fields ) ) {
    15621572                        $data['parent'] = (int) $post->post_parent;
    15631573                }
    15641574
    1565                 if ( in_array( 'menu_order', $fields, true ) ) {
     1575                if ( $this->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 ( $this->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 ( $this->is_field_included( 'ping_status', $fields ) ) {
    15741584                        $data['ping_status'] = $post->ping_status;
    15751585                }
    15761586
    1577                 if ( in_array( 'sticky', $fields, true ) ) {
     1587                if ( $this->is_field_included( 'sticky', $fields ) ) {
    15781588                        $data['sticky'] = is_sticky( $post->ID );
    15791589                }
    15801590
    1581                 if ( in_array( 'template', $fields, true ) ) {
     1591                if ( $this->is_field_included( 'template', $fields ) ) {
    15821592                        if ( $template = get_page_template_slug( $post->ID ) ) {
    15831593                                $data['template'] = $template;
    15841594                        } else {
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    15861596                        }
    15871597                }
    15881598
    1589                 if ( in_array( 'format', $fields, true ) ) {
     1599                if ( $this->is_field_included( 'format', $fields ) ) {
    15901600                        $data['format'] = get_post_format( $post->ID );
    15911601
    15921602                        // Fill in blank post format.
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    15951605                        }
    15961606                }
    15971607
    1598                 if ( in_array( 'meta', $fields, true ) ) {
     1608                if ( $this->is_field_included( 'meta', $fields ) ) {
    15991609                        $data['meta'] = $this->meta->get_value( $post->ID, $request );
    16001610                }
    16011611
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    16041614                foreach ( $taxonomies as $taxonomy ) {
    16051615                        $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
    16061616
    1607                         if ( in_array( $base, $fields, true ) ) {
     1617                        if ( $this->is_field_included( $base, $fields ) ) {
    16081618                                $terms         = get_the_terms( $post, $taxonomy->name );
    16091619                                $data[ $base ] = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array();
    16101620                        }
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    16191629
    16201630                        $sample_permalink = get_sample_permalink( $post->ID, $post->post_title, '' );
    16211631
    1622                         if ( in_array( 'permalink_template', $fields, true ) ) {
     1632                        if ( $this->is_field_included( 'permalink_template', $fields ) ) {
    16231633                                $data['permalink_template'] = $sample_permalink[0];
    16241634                        }
    1625                         if ( in_array( 'generated_slug', $fields, true ) ) {
     1635                        if ( $this->is_field_included( 'generated_slug', $fields ) ) {
    16261636                                $data['generated_slug'] = $sample_permalink[1];
    16271637                        }
    16281638                }
  • tests/phpunit/tests/rest-api.php

    diff --git tests/phpunit/tests/rest-api.php tests/phpunit/tests/rest-api.php
    index 7e72708d43..e3bf21974f 100644
    class Tests_REST_API extends WP_UnitTestCase { 
    519519                );
    520520        }
    521521
     522        /**
     523         * Ensure that nested fields may be whitelisted with request['_fields'].
     524         */
     525        public function test_rest_filter_response_fields_nested_field_filter() {
     526                $response = new WP_REST_Response();
     527
     528                $response->set_data(
     529                        array(
     530                                'a' => 0,
     531                                'b' => array(
     532                                        '1' => 1,
     533                                        '2' => 2,
     534                                ),
     535                                'c' => 3,
     536                                'd' => array(
     537                                        '4' => 4,
     538                                        '5' => 5,
     539                                ),
     540                        )
     541                );
     542                $request = array(
     543                        '_fields' => 'b.1,c,d.5',
     544                );
     545
     546                $response = rest_filter_response_fields( $response, null, $request );
     547                $this->assertEquals(
     548                        array(
     549                                'b' => array(
     550                                        '1' => 1,
     551                                ),
     552                                'c' => 3,
     553                                'd' => array(
     554                                        '5' => 5,
     555                                ),
     556                        ),
     557                        $response->get_data()
     558                );
     559        }
     560
    522561        /**
    523562         * The get_rest_url function should return a URL consistently terminated with a "/",
    524563         * whether the blog is configured with pretty permalink support or not.
  • tests/phpunit/tests/rest-api/rest-controller.php

    diff --git tests/phpunit/tests/rest-api/rest-controller.php tests/phpunit/tests/rest-api/rest-controller.php
    index 6301e64d9d..a9dc094447 100644
    class WP_Test_REST_Controller extends WP_Test_REST_TestCase { 
    232232        public function data_get_fields_for_response() {
    233233                return array(
    234234                        array(
    235                                 'somestring,someinteger',
     235                                'somestring,someinteger,someinvalidkey',
    236236                                array(
    237237                                        'somestring',
    238238                                        'someinteger',
    class WP_Test_REST_Controller extends WP_Test_REST_TestCase { 
    255255                );
    256256        }
    257257
     258        public function test_is_field_included() {
     259                $controller = new WP_REST_Test_Controller();
     260
     261                $fields = array(
     262                        'id',
     263                        'title',
     264                        'content.raw',
     265                        'custom.property',
     266                );
     267
     268                $this->assertTrue( $controller->is_field_included( 'id', $fields ) );
     269                $this->assertTrue( $controller->is_field_included( 'title', $fields ) );
     270                $this->assertTrue( $controller->is_field_included( 'title.raw', $fields ) );
     271                $this->assertTrue( $controller->is_field_included( 'title.rendered', $fields ) );
     272                $this->assertTrue( $controller->is_field_included( 'content', $fields ) );
     273                $this->assertTrue( $controller->is_field_included( 'content.raw', $fields ) );
     274                $this->assertTrue( $controller->is_field_included( 'custom.property', $fields ) );
     275                $this->assertFalse( $controller->is_field_included( 'content.rendered', $fields ) );
     276                $this->assertFalse( $controller->is_field_included( 'type', $fields ) );
     277                $this->assertFalse( $controller->is_field_included( 'meta', $fields ) );
     278                $this->assertFalse( $controller->is_field_included( 'meta.value', $fields ) );
     279        }
     280
    258281        public function test_add_additional_fields_to_object_respects_fields_param() {
    259282                $controller = new WP_REST_Test_Controller();
    260283                $request    = new WP_REST_Request( 'GET', '/wp/v2/testroute' );