Make WordPress Core


Ignore:
Timestamp:
01/30/2020 08:20:30 PM (7 years ago)
Author:
kadamwhite
Message:

REST API: Reuse previously-generated embedded objects when building collection response.

Store each generated embedded object in a temporary cache when querying for linked resources so that repeated links to the same resource do not trigger repeated queries or processing.

Props TimothyBlynJacobs.
Fixes #48838.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r47122 r47138  
    7878         */
    7979        protected $route_options = array();
     80
     81        /**
     82         * Caches embedded requests.
     83         *
     84         * @since 5.4.0
     85         * @var array
     86         */
     87        protected $embed_cache = array();
    8088
    8189        /**
    … …  
    463471
    464472                if ( $embed ) {
     473                        $this->embed_cache = array();
    465474                        // Determine if this is a numeric array.
    466475                        if ( wp_is_numeric_array( $data ) ) {
    … …  
    469478                                $data = $this->embed_links( $data );
    470479                        }
     480                        $this->embed_cache = array();
    471481                }
    472482
    … …  
    589599                                }
    590600
    591                                 // Run through our internal routing and serve.
    592                                 $request = WP_REST_Request::from_url( $item['href'] );
    593                                 if ( ! $request ) {
    594                                         $embeds[] = array();
    595                                         continue;
    596                                 }
    597 
    598                                 // Embedded resources get passed context=embed.
    599                                 if ( empty( $request['context'] ) ) {
    600                                         $request['context'] = 'embed';
    601                                 }
    602 
    603                                 $response = $this->dispatch( $request );
    604 
    605                                 /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
    606                                 $response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $this, $request );
    607 
    608                                 $embeds[] = $this->response_to_data( $response, false );
     601                                if ( ! array_key_exists( $item['href'], $this->embed_cache ) ) {
     602                                        // Run through our internal routing and serve.
     603                                        $request = WP_REST_Request::from_url( $item['href'] );
     604                                        if ( ! $request ) {
     605                                                $embeds[] = array();
     606                                                continue;
     607                                        }
     608
     609                                        // Embedded resources get passed context=embed.
     610                                        if ( empty( $request['context'] ) ) {
     611                                                $request['context'] = 'embed';
     612                                        }
     613
     614                                        $response = $this->dispatch( $request );
     615
     616                                        /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
     617                                        $response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $this, $request );
     618
     619                                        $this->embed_cache[ $item['href'] ] = $this->response_to_data( $response, false );
     620                                }
     621
     622                                $embeds[] = $this->embed_cache[ $item['href'] ];
    609623                        }
    610624
Note: See TracChangeset for help on using the changeset viewer.