From e2cf94fa04979f6e932d0ef09ffcf362c411b773 Mon Sep 17 00:00:00 2001 From: "K. Adam White" Date: Tue, 8 Oct 2019 13:41:29 +0000 Subject: [PATCH] REST API: Ensure rest_controller instantiates the post type's declared REST controller class. Ensures that the ::get_rest_controller() method will always return an instanceof the expected controller class, or null. Removes unused private static property $post_type_controllers. Props dlh, TimothyBlynJacobs. Fixes #45677. git-svn-id: https://develop.svn.wordpress.org/trunk@46435 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-post-type.php | 6 +++- .../class-wp-rest-posts-controller.php | 9 ------ .../tests/rest-api/rest-posts-controller.php | 31 +++++++++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/class-wp-post-type.php b/src/wp-includes/class-wp-post-type.php index 0099515050..bb148067c0 100644 --- a/src/wp-includes/class-wp-post-type.php +++ b/src/wp-includes/class-wp-post-type.php @@ -343,7 +343,7 @@ final class WP_Post_Type { * @since 5.3.0 * @var WP_REST_Controller $rest_controller */ - private $rest_controller; + public $rest_controller; /** * Constructor. @@ -722,6 +722,10 @@ final class WP_Post_Type { $this->rest_controller = new $class( $this->name ); } + if ( ! ( $this->rest_controller instanceof $class ) ) { + return null; + } + return $this->rest_controller; } } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 4a00336e34..2b71a76cb0 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -15,15 +15,6 @@ * @see 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. * diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index d1a30c69a4..37ee1ac02c 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -4617,6 +4617,37 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te ); } + /** + * @ticket 45677 + */ + public function test_get_for_post_type_returns_null_for_invalid_provided_controller() { + register_post_type( + 'test', + array( + 'show_in_rest' => true, + 'rest_controller' => new \stdClass(), + ) + ); + + $this->assertNull( get_post_type_object( 'test' )->get_rest_controller() ); + } + + /** + * @ticket 45677 + */ + public function test_get_for_post_type_returns_null_for_controller_class_mismatch() { + register_post_type( + 'test', + array( + 'show_in_rest' => true, + 'rest_controller_class' => WP_REST_Posts_Controller::class, + 'rest_controller' => new WP_REST_Terms_Controller( 'category' ), + ) + ); + + $this->assertNull( get_post_type_object( 'test' )->get_rest_controller() ); + } + public function tearDown() { _unregister_post_type( 'private-post' ); _unregister_post_type( 'youseeme' );