Make WordPress Core

Ticket #48266: 48266.1.diff

File 48266.1.diff, 1.6 KB (added by kadamwhite, 6 years ago)
  • src/wp-includes/rest-api.php

    diff --git src/wp-includes/rest-api.php src/wp-includes/rest-api.php
    index c397bdc116..52a6e62dce 100644
    function rest_filter_response_fields( $response, $server, $request ) { 
    744744                $ref   = &$fields_as_keyed;
    745745                while ( count( $parts ) > 1 ) {
    746746                        $next         = array_shift( $parts );
    747                         $ref[ $next ] = array();
     747                        $ref[ $next ] = isset( $ref[ $next ] ) ? $ref[ $next ] : array();
    748748                        $ref          = &$ref[ $next ];
    749749                }
    750750                $last         = array_shift( $parts );
    751                 $ref[ $last ] = true;
     751                $ref[ $last ] = array();
    752752        }
    753753
    754754        if ( wp_is_numeric_array( $data ) ) {
  • tests/phpunit/tests/rest-api.php

    diff --git tests/phpunit/tests/rest-api.php tests/phpunit/tests/rest-api.php
    index 2b8310f168..0a28108d47 100644
    class Tests_REST_API extends WP_UnitTestCase { 
    560560                );
    561561        }
    562562
     563        /**
     564         * Ensure that specifying two sibling properties in _fields causes both to be included.
     565         *
     566         * @ticket 42094
     567         */
     568        public function test_rest_filter_response_fields_include_all_specified_siblings() {
     569                $response = new WP_REST_Response();
     570
     571                $response->set_data(
     572                        array(
     573                                'meta' => array(
     574                                        'key1' => 1,
     575                                        'key2' => 2,
     576                                ),
     577                        )
     578                );
     579                $request = array(
     580                        '_fields' => 'meta.key1,meta.key2',
     581                );
     582
     583                $response = rest_filter_response_fields( $response, null, $request );
     584                $this->assertEquals(
     585                        array(
     586                                'meta' => array(
     587                                        'key1' => 1,
     588                                        'key2' => 2,
     589                                ),
     590                        ),
     591                        $response->get_data()
     592                );
     593        }
     594
    563595        /**
    564596         * @ticket 42094
    565597         */