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.php

    r46249 r46272  
    193193function create_initial_rest_routes() {
    194194    foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
    195         $class = ! empty( $post_type->rest_controller_class ) ? $post_type->rest_controller_class : 'WP_REST_Posts_Controller';
    196 
    197         if ( ! class_exists( $class ) ) {
    198             continue;
    199         }
    200         $controller = new $class( $post_type->name );
    201         if ( ! is_subclass_of( $controller, 'WP_REST_Controller' ) ) {
     195        $controller = $post_type->get_rest_controller();
     196
     197        if ( ! $controller ) {
    202198            continue;
    203199        }
Note: See TracChangeset for help on using the changeset viewer.