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. Built from https://develop.svn.wordpress.org/trunk@46272 git-svn-id: http://core.svn.wordpress.org/trunk@46084 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
43b86a246c
commit
10772e8499
|
@ -335,6 +335,16 @@ final class WP_Post_Type {
|
||||||
*/
|
*/
|
||||||
public $rest_controller_class;
|
public $rest_controller_class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The controller instance for this post type's REST API endpoints.
|
||||||
|
*
|
||||||
|
* Lazily computed. Should be accessed using {@see WP_Post_Type::get_rest_controller()}.
|
||||||
|
*
|
||||||
|
* @since 5.3.0
|
||||||
|
* @var WP_REST_Controller $rest_controller
|
||||||
|
*/
|
||||||
|
private $rest_controller;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
|
@ -682,4 +692,36 @@ final class WP_Post_Type {
|
||||||
public function remove_hooks() {
|
public function remove_hooks() {
|
||||||
remove_action( 'future_' . $this->name, '_future_post_hook', 5 );
|
remove_action( 'future_' . $this->name, '_future_post_hook', 5 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the REST API controller for this post type.
|
||||||
|
*
|
||||||
|
* Will only instantiate the controller class once per request.
|
||||||
|
*
|
||||||
|
* @since 5.3.0
|
||||||
|
*
|
||||||
|
* @return WP_REST_Controller|null The controller instance, or null if the post type
|
||||||
|
* is set not to show in rest.
|
||||||
|
*/
|
||||||
|
public function get_rest_controller() {
|
||||||
|
if ( ! $this->show_in_rest ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$class = $this->rest_controller_class ? $this->rest_controller_class : WP_REST_Posts_Controller::class;
|
||||||
|
|
||||||
|
if ( ! class_exists( $class ) ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! $this->rest_controller ) {
|
||||||
|
$this->rest_controller = new $class( $this->name );
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->rest_controller;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -192,13 +192,9 @@ function rest_api_default_filters() {
|
||||||
*/
|
*/
|
||||||
function create_initial_rest_routes() {
|
function create_initial_rest_routes() {
|
||||||
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
|
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
|
||||||
$class = ! empty( $post_type->rest_controller_class ) ? $post_type->rest_controller_class : 'WP_REST_Posts_Controller';
|
$controller = $post_type->get_rest_controller();
|
||||||
|
|
||||||
if ( ! class_exists( $class ) ) {
|
if ( ! $controller ) {
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$controller = new $class( $post_type->name );
|
|
||||||
if ( ! is_subclass_of( $controller, 'WP_REST_Controller' ) ) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -59,11 +59,13 @@ class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller {
|
||||||
public function __construct( $parent_post_type ) {
|
public function __construct( $parent_post_type ) {
|
||||||
$this->parent_post_type = $parent_post_type;
|
$this->parent_post_type = $parent_post_type;
|
||||||
$post_type_object = get_post_type_object( $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.
|
if ( ! $parent_controller ) {
|
||||||
$parent_controller_class = ! empty( $post_type_object->rest_controller_class ) ? $post_type_object->rest_controller_class : 'WP_REST_Posts_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->revisions_controller = new WP_REST_Revisions_Controller( $parent_post_type );
|
||||||
$this->rest_namespace = 'wp/v2';
|
$this->rest_namespace = 'wp/v2';
|
||||||
$this->rest_base = 'autosaves';
|
$this->rest_base = 'autosaves';
|
||||||
|
|
|
@ -1592,8 +1592,14 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
|
||||||
* @return bool Whether post can be read.
|
* @return bool Whether post can be read.
|
||||||
*/
|
*/
|
||||||
protected function check_read_post_permission( $post, $request ) {
|
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 );
|
$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;
|
$has_password_filter = false;
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,14 @@
|
||||||
*/
|
*/
|
||||||
class WP_REST_Posts_Controller extends WP_REST_Controller {
|
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.
|
* Post type.
|
||||||
*
|
*
|
||||||
|
|
|
@ -49,11 +49,15 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller {
|
||||||
*/
|
*/
|
||||||
public function __construct( $parent_post_type ) {
|
public function __construct( $parent_post_type ) {
|
||||||
$this->parent_post_type = $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->namespace = 'wp/v2';
|
||||||
$this->rest_base = 'revisions';
|
$this->rest_base = 'revisions';
|
||||||
$post_type_object = get_post_type_object( $parent_post_type );
|
$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_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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '5.3-alpha-46271';
|
$wp_version = '5.3-alpha-46272';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue