diff --git src/wp-includes/rest-api.php src/wp-includes/rest-api.php
index c397bdc116..ce6e2d7a23 100644
|
|
function rest_filter_response_fields( $response, $server, $request ) { |
744 | 744 | $ref = &$fields_as_keyed; |
745 | 745 | while ( count( $parts ) > 1 ) { |
746 | 746 | $next = array_shift( $parts ); |
747 | | $ref[ $next ] = array(); |
| 747 | if ( isset( $ref[ $next ] ) && true === $ref[ $next ] ) { |
| 748 | // Skip any sub-properties if their parent prop is already marked for inclusion. |
| 749 | break 2; |
| 750 | } |
| 751 | $ref[ $next ] = isset( $ref[ $next ] ) ? $ref[ $next ] : array(); |
748 | 752 | $ref = &$ref[ $next ]; |
749 | 753 | } |
750 | 754 | $last = array_shift( $parts ); |
diff --git tests/phpunit/tests/rest-api.php tests/phpunit/tests/rest-api.php
index 2b8310f168..4943280cc0 100644
|
|
class Tests_REST_API extends WP_UnitTestCase { |
560 | 560 | ); |
561 | 561 | } |
562 | 562 | |
| 563 | /** |
| 564 | * Ensure that specifying a single top-level key in _fields includes that field and all children. |
| 565 | * |
| 566 | * @ticket 48266 |
| 567 | */ |
| 568 | public function test_rest_filter_response_fields_top_level_key() { |
| 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', |
| 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 | |
| 595 | /** |
| 596 | * Ensure that specifying a single top-level key in _fields includes that field and all children. |
| 597 | * |
| 598 | * @ticket 48266 |
| 599 | */ |
| 600 | public function test_rest_filter_response_fields_child_after_parent() { |
| 601 | $response = new WP_REST_Response(); |
| 602 | |
| 603 | $response->set_data( |
| 604 | array( |
| 605 | 'meta' => array( |
| 606 | 'key1' => 1, |
| 607 | 'key2' => 2, |
| 608 | ), |
| 609 | ) |
| 610 | ); |
| 611 | $request = array( |
| 612 | '_fields' => 'meta,meta.key1', |
| 613 | ); |
| 614 | |
| 615 | $response = rest_filter_response_fields( $response, null, $request ); |
| 616 | $this->assertEquals( |
| 617 | array( |
| 618 | 'meta' => array( |
| 619 | 'key1' => 1, |
| 620 | 'key2' => 2, |
| 621 | ), |
| 622 | ), |
| 623 | $response->get_data() |
| 624 | ); |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Ensure that specifying two sibling properties in _fields causes both to be included. |
| 629 | * |
| 630 | * @ticket 48266 |
| 631 | */ |
| 632 | public function test_rest_filter_response_fields_include_all_specified_siblings() { |
| 633 | $response = new WP_REST_Response(); |
| 634 | |
| 635 | $response->set_data( |
| 636 | array( |
| 637 | 'meta' => array( |
| 638 | 'key1' => 1, |
| 639 | 'key2' => 2, |
| 640 | ), |
| 641 | ) |
| 642 | ); |
| 643 | $request = array( |
| 644 | '_fields' => 'meta.key1,meta.key2', |
| 645 | ); |
| 646 | |
| 647 | $response = rest_filter_response_fields( $response, null, $request ); |
| 648 | $this->assertEquals( |
| 649 | array( |
| 650 | 'meta' => array( |
| 651 | 'key1' => 1, |
| 652 | 'key2' => 2, |
| 653 | ), |
| 654 | ), |
| 655 | $response->get_data() |
| 656 | ); |
| 657 | } |
| 658 | |
563 | 659 | /** |
564 | 660 | * @ticket 42094 |
565 | 661 | */ |