mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-10-07 08:07:16 +00:00
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.
git-svn-id: https://develop.svn.wordpress.org/trunk@46272 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -59,11 +59,13 @@ class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller {
|
||||
public function __construct( $parent_post_type ) {
|
||||
$this->parent_post_type = $parent_post_type;
|
||||
$post_type_object = get_post_type_object( $parent_post_type );
|
||||
$parent_controller = $post_type_object->get_rest_controller();
|
||||
|
||||
// Ensure that post type-specific controller logic is available.
|
||||
$parent_controller_class = ! empty( $post_type_object->rest_controller_class ) ? $post_type_object->rest_controller_class : 'WP_REST_Posts_Controller';
|
||||
if ( ! $parent_controller ) {
|
||||
$parent_controller = new WP_REST_Posts_Controller( $parent_post_type );
|
||||
}
|
||||
|
||||
$this->parent_controller = new $parent_controller_class( $post_type_object->name );
|
||||
$this->parent_controller = $parent_controller;
|
||||
$this->revisions_controller = new WP_REST_Revisions_Controller( $parent_post_type );
|
||||
$this->rest_namespace = 'wp/v2';
|
||||
$this->rest_base = 'autosaves';
|
||||
|
||||
@@ -1592,8 +1592,14 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
|
||||
* @return bool Whether post can be read.
|
||||
*/
|
||||
protected function check_read_post_permission( $post, $request ) {
|
||||
$posts_controller = new WP_REST_Posts_Controller( $post->post_type );
|
||||
$post_type = get_post_type_object( $post->post_type );
|
||||
$posts_controller = $post_type->get_rest_controller();
|
||||
|
||||
// Ensure the posts controller is specifically a WP_REST_Posts_Controller instance
|
||||
// before using methods specific to that controller.
|
||||
if ( ! $posts_controller instanceof WP_REST_Posts_Controller ) {
|
||||
$posts_controller = new WP_REST_Posts_Controller( $post->post_type );
|
||||
}
|
||||
|
||||
$has_password_filter = false;
|
||||
|
||||
|
||||
@@ -16,6 +16,14 @@
|
||||
*/
|
||||
class WP_REST_Posts_Controller extends WP_REST_Controller {
|
||||
|
||||
/**
|
||||
* Instances of post type controllers keyed by post type.
|
||||
*
|
||||
* @since 5.3.0
|
||||
* @var WP_REST_Controller[]
|
||||
*/
|
||||
private static $post_type_controllers = array();
|
||||
|
||||
/**
|
||||
* Post type.
|
||||
*
|
||||
|
||||
@@ -49,11 +49,15 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller {
|
||||
*/
|
||||
public function __construct( $parent_post_type ) {
|
||||
$this->parent_post_type = $parent_post_type;
|
||||
$this->parent_controller = new WP_REST_Posts_Controller( $parent_post_type );
|
||||
$this->namespace = 'wp/v2';
|
||||
$this->rest_base = 'revisions';
|
||||
$post_type_object = get_post_type_object( $parent_post_type );
|
||||
$this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
|
||||
$this->parent_controller = $post_type_object->get_rest_controller();
|
||||
|
||||
if ( ! $this->parent_controller ) {
|
||||
$this->parent_controller = new WP_REST_Posts_Controller( $parent_post_type );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user