Make WordPress Core

Ticket #48838: 48838.diff

File 48838.diff, 3.7 KB (added by TimothyBlynJacobs, 5 years ago)
  • 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..dbf7547c1d 100644
    a b class WP_REST_Server { 
    7878         */
    7979        protected $route_options = array();
    8080
     81        /**
     82         * Caches embedded requests.
     83         *
     84         * @since 5.4.0
     85         * @var array
     86         */
     87        protected $embed_cache = array();
     88
    8189        /**
    8290         * Instantiates the REST server.
    8391         *
    class WP_REST_Server { 
    450458                        $data['_links'] = $links;
    451459                }
    452460                if ( $embed ) {
     461                        $this->embed_cache = array();
    453462                        // Determine if this is a numeric array.
    454463                        if ( wp_is_numeric_array( $data ) ) {
    455464                                $data = array_map( array( $this, 'embed_links' ), $data );
    456465                        } else {
    457466                                $data = $this->embed_links( $data );
    458467                        }
     468                        $this->embed_cache = array();
    459469                }
    460470
    461471                return $data;
    class WP_REST_Server { 
    575585                                        continue;
    576586                                }
    577587
    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                                        }
    584595
    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 );
    589602
    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 );
    591605
    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                                }
    594608
    595                                 $embeds[] = $this->response_to_data( $response, false );
     609                                $embeds[] = $this->embed_cache[ $item['href'] ];
    596610                        }
    597611
    598612                        // Determine if any real links were found.
  • 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..4e1b4baae1 100644
    a b class Tests_REST_Server extends WP_Test_REST_TestCase { 
    677677                $this->assertEquals( 403, $up_data['data']['status'] );
    678678        }
    679679
     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
    680704        /**
    681705         * Ensure embedding is a no-op without links in the data.
    682706         */