Make WordPress Core


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:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.4

  • 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.