WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/23/2019 08:24:59 PM (2 months ago)
Author:
kadamwhite
Message:

REST API: Introduce WP_Post_Type::get_rest_controller() caching method to prevent unnecessary REST controller construction.

Cache REST controller references on their associated post type object to prevent unnecessary controller re-instantiation, which previously caused "rest_prepare_{$post_type}" and "rest_{$post_type}_query" to run twice per request.

Props TimothyBlynJacobs, patrelentlesstechnologycom.
Fixes #45677.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php

    r46088 r46272  
    6060        $this->parent_post_type = $parent_post_type;
    6161        $post_type_object       = get_post_type_object( $parent_post_type );
    62 
    63         // Ensure that post type-specific controller logic is available.
    64         $parent_controller_class = ! empty( $post_type_object->rest_controller_class ) ? $post_type_object->rest_controller_class : 'WP_REST_Posts_Controller';
    65 
    66         $this->parent_controller    = new $parent_controller_class( $post_type_object->name );
     62        $parent_controller      = $post_type_object->get_rest_controller();
     63
     64        if ( ! $parent_controller ) {
     65            $parent_controller = new WP_REST_Posts_Controller( $parent_post_type );
     66        }
     67
     68        $this->parent_controller    = $parent_controller;
    6769        $this->revisions_controller = new WP_REST_Revisions_Controller( $parent_post_type );
    6870        $this->rest_namespace       = 'wp/v2';
Note: See TracChangeset for help on using the changeset viewer.