Ticket #39696: 39696.4.diff
File 39696.4.diff, 7.0 KB (added by , 5 years ago) |
---|
-
src/wp-includes/rest-api.php
diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index c5093cc33b..070cefe8d8 100644
a b function rest_preload_api_request( $memo, $path ) { 1561 1561 1562 1562 return $memo; 1563 1563 } 1564 1565 /** 1566 * Parses the "_embed" parameter into the list of resources to embed. 1567 * 1568 * @since 5.4.0 1569 * 1570 * @param string|array $embed Raw "_embed" parameter value. 1571 * @return true|string[] Either true to embed all embeds, or a list of relations to embed. 1572 */ 1573 function rest_parse_embed_param( $embed ) { 1574 if ( ! $embed || 'true' === $embed || '1' === $embed ) { 1575 return true; 1576 } 1577 1578 $rels = wp_parse_list( $embed ); 1579 1580 if ( ! $rels ) { 1581 return true; 1582 } 1583 1584 return $rels; 1585 } -
src/wp-includes/rest-api/class-wp-rest-server.php
diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index e07e214d97..a7a8dfa613 100644
a b class WP_REST_Server { 386 386 } 387 387 388 388 // Embed links inside the request. 389 $result = $this->response_to_data( $result, isset( $_GET['_embed'] ) ); 389 $embed = isset( $_GET['_embed'] ) ? rest_parse_embed_param( $_GET['_embed'] ) : false; 390 $result = $this->response_to_data( $result, $embed ); 390 391 391 392 /** 392 393 * Filters the API response. … … class WP_REST_Server { 431 432 * Converts a response to data to send. 432 433 * 433 434 * @since 4.4.0 435 * @since 5.4.0 The $embed parameter can now contain a list of link relations to include. 434 436 * 435 437 * @param WP_REST_Response $response Response object. 436 * @param bool $embed Whether links should be embedded.438 * @param bool|string[] $embed Whether to embed all links, a filtered list of link relations, or no links. 437 439 * @return array { 438 440 * Data with sub-requests embedded. 439 441 * … … class WP_REST_Server { 452 454 if ( $embed ) { 453 455 // Determine if this is a numeric array. 454 456 if ( wp_is_numeric_array( $data ) ) { 455 $data = array_map( array( $this, 'embed_links' ), $data ); 457 foreach ( $data as $key => $link ) { 458 $data[ $key ] = $this->embed_links( $data, $embed ); 459 } 456 460 } else { 457 $data = $this->embed_links( $data );461 $data = $this->embed_links( $data, $embed ); 458 462 } 459 463 } 460 464 … … class WP_REST_Server { 548 552 * Embeds the links from the data into the request. 549 553 * 550 554 * @since 4.4.0 555 * @since 5.4.0 The $embed parameter can now contain a list of link relations to include. 551 556 * 552 * @param array $data Data from the request. 557 * @param array $data Data from the request. 558 * @param bool|string[] $embed Whether to embed all links or a filtered list of link relations. 553 559 * @return array { 554 560 * Data with sub-requests embedded. 555 561 * … … class WP_REST_Server { 557 563 * @type array [$_embedded] Embeddeds. 558 564 * } 559 565 */ 560 protected function embed_links( $data ) {566 protected function embed_links( $data, $embed = true ) { 561 567 if ( empty( $data['_links'] ) ) { 562 568 return $data; 563 569 } … … class WP_REST_Server { 565 571 $embedded = array(); 566 572 567 573 foreach ( $data['_links'] as $rel => $links ) { 574 // If a list of relations was specified, and the link relation is not in the whitelist, don't process the link. 575 if ( is_array( $embed ) && ! in_array( $rel, $embed, true ) ) { 576 continue; 577 } 578 568 579 $embeds = array(); 569 580 570 581 foreach ( $links as $item ) { -
tests/phpunit/tests/rest-api.php
diff --git a/tests/phpunit/tests/rest-api.php b/tests/phpunit/tests/rest-api.php index 1efb81a348..15d0852627 100644
a b class Tests_REST_API extends WP_UnitTestCase { 906 906 $this->assertEquals( '/wp/v2/posts', $request->get_route() ); 907 907 $this->assertEquals( 'GET', $request->get_method() ); 908 908 } 909 910 /** 911 * @dataProvider _dp_rest_parse_embed_param 912 */ 913 public function test_rest_parse_embed_param( $expected, $embed ) { 914 $this->assertEquals( $expected, rest_parse_embed_param( $embed ) ); 915 } 916 917 public function _dp_rest_parse_embed_param() { 918 return array( 919 array( true, '' ), 920 array( true, null ), 921 array( true, '1' ), 922 array( true, 'true' ), 923 array( true, array() ), 924 array( array( 'author' ), 'author' ), 925 array( array( 'author', 'replies' ), 'author,replies' ), 926 array( array( 'author', 'replies' ), 'author,replies ' ), 927 array( array( 'wp:term' ), 'wp:term' ), 928 array( array( 'wp:term', 'wp:attachment' ), 'wp:term,wp:attachment' ), 929 array( array( 'author' ), array( 'author' ) ), 930 array( array( 'author', 'replies' ), array( 'author', 'replies' ) ), 931 array( array( 'https://api.w.org/term' ), 'https://api.w.org/term' ), 932 array( array( 'https://api.w.org/term', 'https://api.w.org/attachment' ), 'https://api.w.org/term,https://api.w.org/attachment' ), 933 array( array( 'https://api.w.org/term', 'https://api.w.org/attachment' ), array( 'https://api.w.org/term', 'https://api.w.org/attachment' ) ), 934 ); 935 } 909 936 } -
tests/phpunit/tests/rest-api/rest-server.php
diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php index 74d957cbe5..ae29c43c5a 100644
a b class Tests_REST_Server extends WP_Test_REST_TestCase { 743 743 $this->assertEquals( $self_not_filtered, $data['_links']['self'][0] ); 744 744 } 745 745 746 /** 747 * @dataProvider _dp_response_to_data_embedding 748 */ 749 public function test_response_to_data_embedding( $expected, $embed ) { 750 $response = new WP_REST_Response(); 751 $response->add_link( 'author', rest_url( '404' ), array( 'embeddable' => true ) ); 752 $response->add_link( 'https://api.w.org/term', rest_url( '404' ), array( 'embeddable' => true ) ); 753 $response->add_link( 'https://wordpress.org', rest_url( '404' ), array( 'embeddable' => true ) ); 754 $response->add_link( 'no-embed', rest_url( '404' ) ); 755 756 $data = rest_get_server()->response_to_data( $response, $embed ); 757 758 if ( false === $expected ) { 759 $this->assertArrayNotHasKey( '_embedded', $data ); 760 } else { 761 $this->assertEqualSets( $expected, array_keys( $data['_embedded'] ) ); 762 } 763 } 764 765 public function _dp_response_to_data_embedding() { 766 return array( 767 array( 768 array( 'author', 'wp:term', 'https://wordpress.org' ), 769 true, 770 ), 771 array( 772 array( 'author', 'wp:term', 'https://wordpress.org' ), 773 array( 'author', 'wp:term', 'https://wordpress.org' ), 774 ), 775 array( 776 array( 'author' ), 777 array( 'author' ), 778 ), 779 array( 780 array( 'wp:term' ), 781 array( 'wp:term' ), 782 ), 783 array( 784 array( 'https://wordpress.org' ), 785 array( 'https://wordpress.org' ), 786 ), 787 array( 788 array( 'author', 'wp:term' ), 789 array( 'author', 'wp:term' ), 790 ), 791 array( 792 false, 793 false, 794 ), 795 array( 796 false, 797 array( 'no-embed' ), 798 ), 799 array( 800 array( 'author' ), 801 array( 'author', 'no-embed' ), 802 ), 803 ); 804 } 805 746 806 public function test_get_index() { 747 807 $server = new WP_REST_Server(); 748 808 $server->register_route(