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..dbf7547c1d 100644
a
|
b
|
class WP_REST_Server { |
78 | 78 | */ |
79 | 79 | protected $route_options = array(); |
80 | 80 | |
| 81 | /** |
| 82 | * Caches embedded requests. |
| 83 | * |
| 84 | * @since 5.4.0 |
| 85 | * @var array |
| 86 | */ |
| 87 | protected $embed_cache = array(); |
| 88 | |
81 | 89 | /** |
82 | 90 | * Instantiates the REST server. |
83 | 91 | * |
… |
… |
class WP_REST_Server { |
450 | 458 | $data['_links'] = $links; |
451 | 459 | } |
452 | 460 | if ( $embed ) { |
| 461 | $this->embed_cache = array(); |
453 | 462 | // Determine if this is a numeric array. |
454 | 463 | if ( wp_is_numeric_array( $data ) ) { |
455 | 464 | $data = array_map( array( $this, 'embed_links' ), $data ); |
456 | 465 | } else { |
457 | 466 | $data = $this->embed_links( $data ); |
458 | 467 | } |
| 468 | $this->embed_cache = array(); |
459 | 469 | } |
460 | 470 | |
461 | 471 | return $data; |
… |
… |
class WP_REST_Server { |
575 | 585 | continue; |
576 | 586 | } |
577 | 587 | |
578 | | // Run through our internal routing and serve. |
579 | | $request = WP_REST_Request::from_url( $item['href'] ); |
580 | | if ( ! $request ) { |
581 | | $embeds[] = array(); |
582 | | continue; |
583 | | } |
| 588 | if ( ! array_key_exists( $item['href'], $this->embed_cache ) ) { |
| 589 | // Run through our internal routing and serve. |
| 590 | $request = WP_REST_Request::from_url( $item['href'] ); |
| 591 | if ( ! $request ) { |
| 592 | $embeds[] = array(); |
| 593 | continue; |
| 594 | } |
584 | 595 | |
585 | | // Embedded resources get passed context=embed. |
586 | | if ( empty( $request['context'] ) ) { |
587 | | $request['context'] = 'embed'; |
588 | | } |
| 596 | // Embedded resources get passed context=embed. |
| 597 | if ( empty( $request['context'] ) ) { |
| 598 | $request['context'] = 'embed'; |
| 599 | } |
| 600 | |
| 601 | $response = $this->dispatch( $request ); |
589 | 602 | |
590 | | $response = $this->dispatch( $request ); |
| 603 | /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */ |
| 604 | $response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $this, $request ); |
591 | 605 | |
592 | | /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */ |
593 | | $response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $this, $request ); |
| 606 | $this->embed_cache[ $item['href'] ] = $this->response_to_data( $response, false ); |
| 607 | } |
594 | 608 | |
595 | | $embeds[] = $this->response_to_data( $response, false ); |
| 609 | $embeds[] = $this->embed_cache[ $item['href'] ]; |
596 | 610 | } |
597 | 611 | |
598 | 612 | // Determine if any real links were found. |
diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php
index 74d957cbe5..4e1b4baae1 100644
a
|
b
|
class Tests_REST_Server extends WP_Test_REST_TestCase { |
677 | 677 | $this->assertEquals( 403, $up_data['data']['status'] ); |
678 | 678 | } |
679 | 679 | |
| 680 | /** |
| 681 | * @ticket 48838 |
| 682 | */ |
| 683 | public function test_link_embedding_clears_cache() { |
| 684 | $post_id = self::factory()->post->create(); |
| 685 | |
| 686 | $response = new WP_REST_Response(); |
| 687 | $response->add_link( 'post', rest_url( 'wp/v2/posts/' . $post_id ), array( 'embeddable' => true ) ); |
| 688 | |
| 689 | $data = rest_get_server()->response_to_data( $response, true ); |
| 690 | $this->assertArrayHasKey( 'post', $data['_embedded'] ); |
| 691 | $this->assertCount( 1, $data['_embedded']['post'] ); |
| 692 | |
| 693 | wp_update_post( array( |
| 694 | 'ID' => $post_id, |
| 695 | 'post_title' => 'My Awesome Title', |
| 696 | ) ); |
| 697 | |
| 698 | $data = rest_get_server()->response_to_data( $response, true ); |
| 699 | $this->assertArrayHasKey( 'post', $data['_embedded'] ); |
| 700 | $this->assertCount( 1, $data['_embedded']['post'] ); |
| 701 | $this->assertEquals( 'My Awesome Title', $data['_embedded']['post'][0]['title']['rendered'] ); |
| 702 | } |
| 703 | |
680 | 704 | /** |
681 | 705 | * Ensure embedding is a no-op without links in the data. |
682 | 706 | */ |