diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
index d9d60d2ee9..b9c05eb894 100644
|
|
abstract class WP_REST_Controller { |
451 | 451 | continue; |
452 | 452 | } |
453 | 453 | |
454 | | if ( ! in_array( $field_name, $requested_fields, true ) ) { |
| 454 | if ( ! rest_is_field_included( $field_name, $requested_fields ) ) { |
455 | 455 | continue; |
456 | 456 | } |
457 | 457 | |
diff --git tests/phpunit/tests/rest-api.php tests/phpunit/tests/rest-api.php
index c0eea9804f..97b79c8c88 100644
|
|
class Tests_REST_API extends WP_UnitTestCase { |
560 | 560 | ); |
561 | 561 | } |
562 | 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 | ), |
| 599 | ), |
| 600 | ), |
| 601 | $response->get_data() |
| 602 | ); |
| 603 | } |
| 604 | |
563 | 605 | /** |
564 | 606 | * Ensure that specifying a single top-level key in _fields includes that field and all children. |
565 | 607 | * |
diff --git tests/phpunit/tests/rest-api/rest-controller.php tests/phpunit/tests/rest-api/rest-controller.php
index 0fa98078b5..499bc40929 100644
|
|
class WP_Test_REST_Controller extends WP_Test_REST_TestCase { |
48 | 48 | ); |
49 | 49 | } |
50 | 50 | |
| 51 | public function tearDown() { |
| 52 | parent::tearDown(); |
| 53 | |
| 54 | global $wp_rest_additional_fields; |
| 55 | $wp_rest_additional_fields = array(); |
| 56 | } |
| 57 | |
51 | 58 | public function test_validate_schema_type_integer() { |
52 | 59 | |
53 | 60 | $this->assertTrue( |
… |
… |
class WP_Test_REST_Controller extends WP_Test_REST_TestCase { |
437 | 444 | |
438 | 445 | $this->assertTrue( $listener->get_call_count( $method ) > $first_call_count ); |
439 | 446 | } |
| 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 | } |
440 | 568 | } |