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/tests/phpunit/tests/rest-api.php

    r46099 r46184  
    518518                        $response->get_data()
    519519                );
     520        }
     521
     522        /**
     523         * Ensure that nested fields may be whitelisted with request['_fields'].
     524         *
     525         * @ticket 42094
     526         */
     527        public function test_rest_filter_response_fields_nested_field_filter() {
     528                $response = new WP_REST_Response();
     529
     530                $response->set_data(
     531                        array(
     532                                'a' => 0,
     533                                'b' => array(
     534                                        '1' => 1,
     535                                        '2' => 2,
     536                                ),
     537                                'c' => 3,
     538                                'd' => array(
     539                                        '4' => 4,
     540                                        '5' => 5,
     541                                ),
     542                        )
     543                );
     544                $request = array(
     545                        '_fields' => 'b.1,c,d.5',
     546                );
     547
     548                $response = rest_filter_response_fields( $response, null, $request );
     549                $this->assertEquals(
     550                        array(
     551                                'b' => array(
     552                                        '1' => 1,
     553                                ),
     554                                'c' => 3,
     555                                'd' => array(
     556                                        '5' => 5,
     557                                ),
     558                        ),
     559                        $response->get_data()
     560                );
     561        }
     562
     563        /**
     564         * @ticket 42094
     565         */
     566        public function test_rest_is_field_included() {
     567                $fields = array(
     568                        'id',
     569                        'title',
     570                        'content.raw',
     571                        'custom.property',
     572                );
     573
     574                $this->assertTrue( rest_is_field_included( 'id', $fields ) );
     575                $this->assertTrue( rest_is_field_included( 'title', $fields ) );
     576                $this->assertTrue( rest_is_field_included( 'title.raw', $fields ) );
     577                $this->assertTrue( rest_is_field_included( 'title.rendered', $fields ) );
     578                $this->assertTrue( rest_is_field_included( 'content', $fields ) );
     579                $this->assertTrue( rest_is_field_included( 'content.raw', $fields ) );
     580                $this->assertTrue( rest_is_field_included( 'custom.property', $fields ) );
     581                $this->assertFalse( rest_is_field_included( 'content.rendered', $fields ) );
     582                $this->assertFalse( rest_is_field_included( 'type', $fields ) );
     583                $this->assertFalse( rest_is_field_included( 'meta', $fields ) );
     584                $this->assertFalse( rest_is_field_included( 'meta.value', $fields ) );
    520585        }
    521586
Note: See TracChangeset for help on using the changeset viewer.