Make WordPress Core

Changeset 47511


Ignore:
Timestamp:
03/26/2020 05:50:39 PM (3 years ago)
Author:
kadamwhite
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.

Props Dudo, kadamwhite, TimothyBlynJacobs.
Fixes #49648.

Location:
trunk
Files:
3 edited

Legend:

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

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

    r47224 r47511  
    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            ),
  • trunk/tests/phpunit/tests/rest-api/rest-controller.php

    r47450 r47511  
    4747            )
    4848        );
     49    }
     50
     51    public function tearDown() {
     52        parent::tearDown();
     53
     54        global $wp_rest_additional_fields;
     55        $wp_rest_additional_fields = array();
    4956    }
    5057
     
    438445        $this->assertTrue( $listener->get_call_count( $method ) > $first_call_count );
    439446    }
     447
     448    /**
     449     * @dataProvider data_filter_nested_registered_rest_fields
     450     * @ticket 49648
     451     */
     452    public function test_filter_nested_registered_rest_fields( $filter, $expected ) {
     453        $controller = new WP_REST_Test_Controller();
     454
     455        register_rest_field(
     456            'type',
     457            'field',
     458            array(
     459                'schema'       => array(
     460                    'type'        => 'object',
     461                    'description' => 'A complex object',
     462                    'context'     => array( 'view', 'edit' ),
     463                    'properties'  => array(
     464                        'a' => array(
     465                            'i'  => 'string',
     466                            'ii' => 'string',
     467                        ),
     468                        'b' => array(
     469                            'iii' => 'string',
     470                            'iv'  => 'string',
     471                        ),
     472                    ),
     473                ),
     474                'get_callback' => array( $this, 'register_nested_rest_field_get_callback' ),
     475            )
     476        );
     477
     478        $request = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
     479        $request->set_param( '_fields', $filter );
     480
     481        $response = $controller->prepare_item_for_response( array(), $request );
     482        $response = rest_filter_response_fields( $response, rest_get_server(), $request );
     483
     484        $this->assertEquals( $expected, $response->get_data() );
     485    }
     486
     487    public function register_nested_rest_field_get_callback() {
     488        return array(
     489            'a' => array(
     490                'i'  => 'value i',
     491                'ii' => 'value ii',
     492            ),
     493            'b' => array(
     494                'iii' => 'value iii',
     495                'iv'  => 'value iv',
     496            ),
     497        );
     498    }
     499
     500    public function data_filter_nested_registered_rest_fields() {
     501        return array(
     502            array(
     503                'field',
     504                array(
     505                    'field' => array(
     506                        'a' => array(
     507                            'i'  => 'value i',
     508                            'ii' => 'value ii',
     509                        ),
     510                        'b' => array(
     511                            'iii' => 'value iii',
     512                            'iv'  => 'value iv',
     513                        ),
     514                    ),
     515                ),
     516            ),
     517            array(
     518                'field.a',
     519                array(
     520                    'field' => array(
     521                        'a' => array(
     522                            'i'  => 'value i',
     523                            'ii' => 'value ii',
     524                        ),
     525                    ),
     526                ),
     527            ),
     528            array(
     529                'field.b',
     530                array(
     531                    'field' => array(
     532                        'b' => array(
     533                            'iii' => 'value iii',
     534                            'iv'  => 'value iv',
     535                        ),
     536                    ),
     537                ),
     538            ),
     539            array(
     540                'field.a.i,field.b.iv',
     541                array(
     542                    'field' => array(
     543                        'a' => array(
     544                            'i' => 'value i',
     545                        ),
     546                        'b' => array(
     547                            'iv' => 'value iv',
     548                        ),
     549                    ),
     550                ),
     551            ),
     552            array(
     553                'field.a,field.b.iii',
     554                array(
     555                    'field' => array(
     556                        'a' => array(
     557                            'i'  => 'value i',
     558                            'ii' => 'value ii',
     559                        ),
     560                        'b' => array(
     561                            'iii' => 'value iii',
     562                        ),
     563                    ),
     564                ),
     565            ),
     566        );
     567    }
    440568}
Note: See TracChangeset for help on using the changeset viewer.