Ticket #39696: 39696.3.diff
File 39696.3.diff, 6.6 KB (added by , 8 years ago) |
---|
-
src/wp-includes/rest-api/class-wp-rest-server.php
diff --git src/wp-includes/rest-api/class-wp-rest-server.php src/wp-includes/rest-api/class-wp-rest-server.php index 4f5ae76..e312b50 100644
class WP_REST_Server { 381 381 } 382 382 383 383 // Embed links inside the request. 384 $result = $this->response_to_data( $result, isset( $_GET['_embed'] ) );384 $result = $this->response_to_data( $result, isset( $_GET['_embed'] ), $request ); 385 385 386 386 $result = wp_json_encode( $result ); 387 387 … … class WP_REST_Server { 409 409 * @since 4.4.0 410 410 * @access public 411 411 * 412 * @param WP_REST_Response $response Response object. 413 * @param bool $embed Whether links should be embedded. 412 * @param WP_REST_Response $response Response object. 413 * @param bool $embed Whether links should be embedded. 414 * @param bool | WP_REST_Request $request The request to respond to. Not always passed. 414 415 * @return array { 415 416 * Data with sub-requests embedded. 416 417 * … … class WP_REST_Server { 418 419 * @type array [$_embedded] Embeddeds. 419 420 * } 420 421 */ 421 public function response_to_data( $response, $embed ) {422 public function response_to_data( $response, $embed, $request = false ) { 422 423 $data = $response->get_data(); 423 424 $links = $this->get_compact_response_links( $response ); 424 425 … … class WP_REST_Server { 429 430 if ( $embed ) { 430 431 // Determine if this is a numeric array. 431 432 if ( wp_is_numeric_array( $data ) ) { 432 $data = array_map( array( $this, 'embed_links' ), $data ); 433 foreach ( $data as $key => $link ) { 434 $data[ $key ] = $this->embed_links( $data, $request ); 435 } 433 436 } else { 434 $data = $this->embed_links( $data );437 $data = $this->embed_links( $data, $request ); 435 438 } 436 439 } 437 440 … … class WP_REST_Server { 531 534 * @since 4.4.0 532 535 * @access protected 533 536 * 534 * @param array $data Data from the request. 537 * @param array $data Data from the request. 538 * @param bool | WP_REST_Request $request The request that contains passed parameters. Not always passed. 535 539 * @return array { 536 540 * Data with sub-requests embedded. 537 541 * … … class WP_REST_Server { 539 543 * @type array [$_embedded] Embeddeds. 540 544 * } 541 545 */ 542 protected function embed_links( $data ) {546 protected function embed_links( $data, $request = false ) { 543 547 if ( empty( $data['_links'] ) ) { 544 548 return $data; 545 549 } 546 550 547 551 $embedded = array(); 548 552 553 // Get link relationships to filter on, but only if not _embed=1 or _embed=true or empty _embed= 554 $filters = ( empty( $request ) ? false : $request->get_param( '_embed' ) ); 555 $filtered_rels = false; 556 if ( ! empty( $filters ) && '1' !== $filters && 'true' !== $filters ) { 557 $filtered_rels = array_filter( is_array( $filters ) ? $filters : explode( ',', $filters ) ); 558 } 559 549 560 foreach ( $data['_links'] as $rel => $links ) { 550 561 // Ignore links to self, for obvious reasons. 551 562 if ( 'self' === $rel ) { 552 563 continue; 553 564 } 554 565 566 // If filtered rels are specified and current $rel is not in the list, then continue. 567 if ( ! empty( $filtered_rels ) && ! in_array( $rel, $filtered_rels, true ) ) { 568 continue; 569 } 570 555 571 $embeds = array(); 556 572 557 573 foreach ( $links as $item ) { -
tests/phpunit/tests/rest-api/rest-posts-controller.php
diff --git tests/phpunit/tests/rest-api/rest-posts-controller.php tests/phpunit/tests/rest-api/rest-posts-controller.php index ca16356..663b4fa 100644
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 994 994 $this->assertEquals( rest_url( '/wp/v2/users/' . self::$author_id ), $links['author'][0]['href'] ); 995 995 } 996 996 997 public function test_get_item__embed() { 998 999 // Prepare a new post. This will be first item when querying /wp/v2/posts 1000 wp_set_current_user( self::$editor_id ); 1001 $post_id = $this->factory->post->create(); 1002 $category1 = wp_insert_term( 'Embed Test Category', 'category' ); 1003 $category2 = wp_insert_term( 'Second Embed Test Category', 'category' ); 1004 wp_set_post_categories( $post_id, array( $category1['term_id'], $category2['term_id'] ) ); 1005 $this->factory->comment->create_post_comments( $post_id, 1 ); 1006 1007 // ?_embed=1 should return all expected embeds. In this case 'author', 'replies' and 'wp:term'. 1008 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1009 $request->set_param( '_embed', '1' ); 1010 $response = $this->server->dispatch( $request ); 1011 $data = $response->get_data(); 1012 $data = array_shift( $data ); 1013 $embeds = $this->server->embed_links( $data, $request ); 1014 1015 $this->assertArrayHasKey( 'author', $embeds['_embedded'] ); 1016 $this->assertArrayHasKey( 'replies', $embeds['_embedded'] ); 1017 $this->assertArrayHasKey( 'wp:term', $embeds['_embedded'] ); 1018 1019 // ?_embed=true should return all expected embeds. In this case 'author', 'replies' and 'wp:term'. 1020 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1021 $request->set_param( '_embed', 'true' ); 1022 $response = $this->server->dispatch( $request ); 1023 $data = $response->get_data(); 1024 $data = array_shift( $data ); 1025 $embeds = $this->server->embed_links( $data, $request ); 1026 1027 $this->assertArrayHasKey( 'author', $embeds['_embedded'] ); 1028 $this->assertArrayHasKey( 'replies', $embeds['_embedded'] ); 1029 $this->assertArrayHasKey( 'wp:term', $embeds['_embedded'] ); 1030 1031 // ?_embed=author,wp:term should return only relevant embeds. 1032 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1033 $request->set_param( '_embed', 'author,wp:term' ); 1034 $response = $this->server->dispatch( $request ); 1035 $data = $response->get_data(); 1036 $data = array_shift( $data ); 1037 $embeds = $this->server->embed_links( $data, $request ); 1038 1039 $this->assertArrayHasKey( 'author', $embeds['_embedded'] ); 1040 $this->assertArrayNotHasKey( 'replies', $embeds['_embedded'] ); 1041 $this->assertArrayHasKey( 'wp:term', $embeds['_embedded'] ); 1042 1043 // ?_embed[]=author&_embed[]=replies should return only relevant embeds. 1044 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 1045 $response = $this->server->dispatch( $request ); 1046 $request->set_param( '_embed', array( 'author', 'replies' ) ); 1047 $data = $response->get_data(); 1048 $data = array_shift( $data ); 1049 $embeds = $this->server->embed_links( $data, $request ); 1050 1051 $this->assertArrayHasKey( 'author', $embeds['_embedded'] ); 1052 $this->assertArrayHasKey( 'replies', $embeds['_embedded'] ); 1053 $this->assertArrayNotHasKey( 'wp:term', $embeds['_embedded'] ); 1054 } 1055 997 1056 public function test_get_post_without_permission() { 998 1057 $draft_id = $this->factory->post->create( array( 999 1058 'post_status' => 'draft',