Ticket #45677: 45677.7.diff
File 45677.7.diff, 9.7 KB (added by , 5 years ago) |
---|
-
src/wp-includes/rest-api.php
diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index 5ec557547c..80681d3845 100644
a b function rest_api_default_filters() { 191 191 * @since 4.7.0 192 192 */ 193 193 function create_initial_rest_routes() { 194 foreach ( get_post_types( array( 'show_in_rest' => true ) , 'objects') as $post_type ) {195 $c lass = ! empty( $post_type->rest_controller_class ) ? $post_type->rest_controller_class : 'WP_REST_Posts_Controller';194 foreach ( get_post_types( array( 'show_in_rest' => true ) ) as $post_type ) { 195 $controller = WP_REST_Posts_Controller::get_for_post_type( $post_type ); 196 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' ) ) { 197 if ( ! $controller ) { 202 198 continue; 203 199 } 204 200 205 201 $controller->register_routes(); 206 202 207 if ( post_type_supports( $post_type ->name, 'revisions' ) ) {208 $revisions_controller = new WP_REST_Revisions_Controller( $post_type ->name);203 if ( post_type_supports( $post_type, 'revisions' ) ) { 204 $revisions_controller = new WP_REST_Revisions_Controller( $post_type ); 209 205 $revisions_controller->register_routes(); 210 206 } 211 207 212 if ( 'attachment' !== $post_type ->name) {213 $autosaves_controller = new WP_REST_Autosaves_Controller( $post_type ->name);208 if ( 'attachment' !== $post_type ) { 209 $autosaves_controller = new WP_REST_Autosaves_Controller( $post_type ); 214 210 $autosaves_controller->register_routes(); 215 211 } 216 212 } -
src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php index e55d3a3729..7f54d59f0a 100644
a b class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller { 57 57 * @param string $parent_post_type Post type of the parent. 58 58 */ 59 59 public function __construct( $parent_post_type ) { 60 $this->parent_post_type = $parent_post_type; 61 $post_type_object = get_post_type_object( $parent_post_type ); 60 $parent_controller = WP_REST_Posts_Controller::get_for_post_type( $parent_post_type ); 62 61 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'; 62 if ( ! $parent_controller ) { 63 $parent_controller = new WP_REST_Posts_Controller( $parent_post_type ); 64 } 65 65 66 $this->parent_controller = new $parent_controller_class( $post_type_object->name ); 66 $this->parent_post_type = $parent_post_type; 67 $post_type_object = get_post_type_object( $parent_post_type ); 68 $this->parent_controller = $parent_controller; 67 69 $this->revisions_controller = new WP_REST_Revisions_Controller( $parent_post_type ); 68 70 $this->rest_namespace = 'wp/v2'; 69 71 $this->rest_base = 'autosaves'; -
src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index b730844fde..7e960e8758 100644
a b class WP_REST_Comments_Controller extends WP_REST_Controller { 1582 1582 * @return bool Whether post can be read. 1583 1583 */ 1584 1584 protected function check_read_post_permission( $post, $request ) { 1585 $posts_controller = new WP_REST_Posts_Controller( $post->post_type ); 1586 $post_type = get_post_type_object( $post->post_type ); 1585 $posts_controller = WP_REST_Posts_Controller::get_for_post_type( $post->post_type ); 1586 1587 // We need to specifically assert that the posts controller is a WP_REST_Posts_Controller instance, 1588 // and not just check that we have a valid controller as we use Posts Controller specific methods. 1589 if ( ! $posts_controller instanceof WP_REST_Posts_Controller ) { 1590 $posts_controller = new WP_REST_Posts_Controller( $post->post_type ); 1591 } 1592 1593 $post_type = get_post_type_object( $post->post_type ); 1587 1594 1588 1595 $has_password_filter = false; 1589 1596 -
src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
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 090ca2a5da..8fee7e7110 100644
a b 16 16 */ 17 17 class WP_REST_Posts_Controller extends WP_REST_Controller { 18 18 19 /** 20 * Instances of post type controllers keyed by post type. 21 * 22 * @since 5.3.0 23 * @var WP_REST_Controller[] 24 */ 25 private static $post_type_controllers = array(); 26 19 27 /** 20 28 * Post type. 21 29 * … … class WP_REST_Posts_Controller extends WP_REST_Controller { 48 56 $this->meta = new WP_REST_Post_Meta_Fields( $this->post_type ); 49 57 } 50 58 59 /** 60 * Gets the REST API controller for a post type. 61 * 62 * @since 5.3.0 63 * 64 * @param string $post_type 65 * @return WP_REST_Controller|null The controller instance, or null if the specified is invalid or the post type 66 * is set not to show in rest. 67 */ 68 public static function get_for_post_type( $post_type ) { 69 if ( array_key_exists( $post_type, self::$post_type_controllers ) ) { 70 return self::$post_type_controllers[ $post_type ]; 71 } 72 73 $definition = get_post_type_object( $post_type ); 74 75 if ( ! $definition ) { 76 self::$post_type_controllers[ $post_type ] = null; 77 78 return self::$post_type_controllers[ $post_type ]; 79 } 80 81 $class = $definition->rest_controller_class ? $definition->rest_controller_class : WP_REST_Posts_Controller::class; 82 83 if ( ! class_exists( $class ) ) { 84 self::$post_type_controllers[ $post_type ] = null; 85 86 return self::$post_type_controllers[ $post_type ]; 87 } 88 89 if ( ! is_subclass_of( $class, WP_REST_Controller::class ) ) { 90 self::$post_type_controllers[ $post_type ] = null; 91 92 return self::$post_type_controllers[ $post_type ]; 93 } 94 95 self::$post_type_controllers[ $post_type ] = new $class( $post_type ); 96 97 return self::$post_type_controllers[ $post_type ]; 98 } 99 51 100 /** 52 101 * Registers the routes for the objects of the controller. 53 102 * -
src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php index 52df0f2206..ecced9f6df 100644
a b class WP_REST_Revisions_Controller extends WP_REST_Controller { 48 48 * @param string $parent_post_type Post type of the parent. 49 49 */ 50 50 public function __construct( $parent_post_type ) { 51 $parent_controller = WP_REST_Posts_Controller::get_for_post_type( $parent_post_type ); 52 53 if ( ! $parent_controller ) { 54 $parent_controller = new WP_REST_Posts_Controller( $parent_post_type ); 55 } 56 51 57 $this->parent_post_type = $parent_post_type; 52 $this->parent_controller = new WP_REST_Posts_Controller( $parent_post_type );58 $this->parent_controller = $parent_controller; 53 59 $this->namespace = 'wp/v2'; 54 60 $this->rest_base = 'revisions'; 55 61 $post_type_object = get_post_type_object( $parent_post_type ); -
tests/phpunit/tests/rest-api/rest-posts-controller.php
diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index f03eee8e83..93383c64e5 100644
a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 4277 4277 4278 4278 } 4279 4279 4280 /** 4281 * @ticket 45677 4282 */ 4283 public function test_get_for_post_type_reuses_same_instance() { 4284 $this->assertSame( 4285 WP_REST_Posts_Controller::get_for_post_type( 'post' ), 4286 WP_REST_Posts_Controller::get_for_post_type( 'post' ) 4287 ); 4288 } 4289 4290 /** 4291 * @ticket 45677 4292 */ 4293 public function test_get_for_post_type_returns_null_for_non_existent_post_type() { 4294 $this->assertNull( WP_REST_Posts_Controller::get_for_post_type( 'doesnotexist' ) ); 4295 } 4296 4297 /** 4298 * @ticket 45677 4299 */ 4300 public function test_get_for_post_type_returns_null_if_class_does_not_exist() { 4301 register_post_type( 4302 'class_not_found', 4303 array( 4304 'show_in_rest' => true, 4305 'rest_controller_class' => 'Class_That_Does_Not_Exist', 4306 ) 4307 ); 4308 4309 $this->assertNull( WP_REST_Posts_Controller::get_for_post_type( 'class_not_found' ) ); 4310 } 4311 4312 /** 4313 * @ticket 45677 4314 */ 4315 public function test_get_for_post_type_returns_null_if_class_does_not_subclass_rest_controller() { 4316 register_post_type( 4317 'invalid_class', 4318 array( 4319 'show_in_rest' => true, 4320 'rest_controller_class' => 'WP_Post', 4321 ), 4322 ); 4323 4324 $this->assertNull( WP_REST_Posts_Controller::get_for_post_type( 'invalid_class' ) ); 4325 } 4326 4327 /** 4328 * @ticket 45677 4329 */ 4330 public function test_get_for_post_type_returns_posts_controller_if_custom_class_not_specified() { 4331 register_post_type( 4332 'test', 4333 array( 4334 'show_in_rest' => true, 4335 ) 4336 ); 4337 4338 $this->assertInstanceOf( 4339 WP_REST_Posts_Controller::class, 4340 WP_REST_Posts_Controller::get_for_post_type( 'test' ) 4341 ); 4342 } 4343 4344 /** 4345 * @ticket 45677 4346 */ 4347 public function test_get_for_post_type_returns_provided_controller_class() { 4348 $this->assertInstanceOf( 4349 WP_REST_Blocks_Controller::class, 4350 WP_REST_Posts_Controller::get_for_post_type( 'wp_block' ) 4351 ); 4352 } 4353 4280 4354 public function tearDown() { 4281 4355 _unregister_post_type( 'private-post' ); 4282 4356 _unregister_post_type( 'youseeme' );