Make WordPress Core

Changeset 47563 for branches/5.4


Ignore:
Timestamp:
04/09/2020 10:52:45 PM (6 years ago)
Author:
whyisjake
Message:

REST API: Fix _fields filtering of registered rest fields.

Use rest_is_field_included when determining which additional fields to include to permit filtering by nested field properties.

Backportting r47511 to the 5.4 branch.

Props Dudo, kadamwhite, TimothyBlynJacobs.
Fixes #49648.

Location:
branches/5.4
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/5.4

  • branches/5.4/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

    r47391 r47563  
    452452                        }
    453453
    454                         if ( ! in_array( $field_name, $requested_fields, true ) ) {
     454                        if ( ! rest_is_field_included( $field_name, $requested_fields ) ) {
    455455                                continue;
    456456                        }
  • branches/5.4/tests/phpunit/tests/rest-api.php

    r47224 r47563  
    555555                                'd' => array(
    556556                                        '5' => 5,
     557                                ),
     558                        ),
     559                        $response->get_data()
     560                );
     561        }
     562
     563        /**
     564         * Ensure inclusion of deeply nested fields may be controlled with request['_fields'].
     565         *
     566         * @ticket 49648
     567         */
     568        public function test_rest_filter_response_fields_deeply_nested_field_filter() {
     569                $response = new WP_REST_Response();
     570
     571                $response->set_data(
     572                        array(
     573                                'field' => array(
     574                                        'a' => array(
     575                                                'i'  => 'value i',
     576                                                'ii' => 'value ii',
     577                                        ),
     578                                        'b' => array(
     579                                                'iii' => 'value iii',
     580                                                'iv'  => 'value iv',
     581                                        ),
     582                                ),
     583                        )
     584                );
     585                $request = array(
     586                        '_fields' => 'field.a.i,field.b.iv',
     587                );
     588
     589                $response = rest_filter_response_fields( $response, null, $request );
     590                $this->assertEquals(
     591                        array(
     592                                'field' => array(
     593                                        'a' => array(
     594                                                'i' => 'value i',
     595                                        ),
     596                                        'b' => array(
     597                                                'iv' => 'value iv',
     598                                        ),
    557599                                ),
    558600                        ),
  • branches/5.4/tests/phpunit/tests/rest-api/rest-controller.php

    r47328 r47563  
    4343                        )
    4444                );
     45        }
     46
     47        public function tearDown() {
     48                parent::tearDown();
     49
     50                global $wp_rest_additional_fields;
     51                $wp_rest_additional_fields = array();
    4552        }
    4653
     
    417424                $this->assertTrue( $listener->get_call_count( $method ) > $first_call_count );
    418425        }
     426
     427        /**
     428         * @dataProvider data_filter_nested_registered_rest_fields
     429         * @ticket 49648
     430         */
     431        public function test_filter_nested_registered_rest_fields( $filter, $expected ) {
     432                $controller = new WP_REST_Test_Controller();
     433
     434                register_rest_field(
     435                        'type',
     436                        'field',
     437                        array(
     438                                'schema'       => array(
     439                                        'type'        => 'object',
     440                                        'description' => 'A complex object',
     441                                        'context'     => array( 'view', 'edit' ),
     442                                        'properties'  => array(
     443                                                'a' => array(
     444                                                        'i'  => 'string',
     445                                                        'ii' => 'string',
     446                                                ),
     447                                                'b' => array(
     448                                                        'iii' => 'string',
     449                                                        'iv'  => 'string',
     450                                                ),
     451                                        ),
     452                                ),
     453                                'get_callback' => array( $this, 'register_nested_rest_field_get_callback' ),
     454                        )
     455                );
     456
     457                $request = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
     458                $request->set_param( '_fields', $filter );
     459
     460                $response = $controller->prepare_item_for_response( array(), $request );
     461                $response = rest_filter_response_fields( $response, rest_get_server(), $request );
     462
     463                $this->assertEquals( $expected, $response->get_data() );
     464        }
     465
     466        public function register_nested_rest_field_get_callback() {
     467                return array(
     468                        'a' => array(
     469                                'i'  => 'value i',
     470                                'ii' => 'value ii',
     471                        ),
     472                        'b' => array(
     473                                'iii' => 'value iii',
     474                                'iv'  => 'value iv',
     475                        ),
     476                );
     477        }
     478
     479        public function data_filter_nested_registered_rest_fields() {
     480                return array(
     481                        array(
     482                                'field',
     483                                array(
     484                                        'field' => array(
     485                                                'a' => array(
     486                                                        'i'  => 'value i',
     487                                                        'ii' => 'value ii',
     488                                                ),
     489                                                'b' => array(
     490                                                        'iii' => 'value iii',
     491                                                        'iv'  => 'value iv',
     492                                                ),
     493                                        ),
     494                                ),
     495                        ),
     496                        array(
     497                                'field.a',
     498                                array(
     499                                        'field' => array(
     500                                                'a' => array(
     501                                                        'i'  => 'value i',
     502                                                        'ii' => 'value ii',
     503                                                ),
     504                                        ),
     505                                ),
     506                        ),
     507                        array(
     508                                'field.b',
     509                                array(
     510                                        'field' => array(
     511                                                'b' => array(
     512                                                        'iii' => 'value iii',
     513                                                        'iv'  => 'value iv',
     514                                                ),
     515                                        ),
     516                                ),
     517                        ),
     518                        array(
     519                                'field.a.i,field.b.iv',
     520                                array(
     521                                        'field' => array(
     522                                                'a' => array(
     523                                                        'i' => 'value i',
     524                                                ),
     525                                                'b' => array(
     526                                                        'iv' => 'value iv',
     527                                                ),
     528                                        ),
     529                                ),
     530                        ),
     531                        array(
     532                                'field.a,field.b.iii',
     533                                array(
     534                                        'field' => array(
     535                                                'a' => array(
     536                                                        'i'  => 'value i',
     537                                                        'ii' => 'value ii',
     538                                                ),
     539                                                'b' => array(
     540                                                        'iii' => 'value iii',
     541                                                ),
     542                                        ),
     543                                ),
     544                        ),
     545                );
     546        }
    419547}
Note: See TracChangeset for help on using the changeset viewer.