Ticket #45677: 45677.3.diff
File 45677.3.diff, 8.1 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..0631fadf37 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, $controller ); 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, $controller ); 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..42bad39e33 100644
a b class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller { 53 53 * Constructor. 54 54 * 55 55 * @since 5.0.0 56 * @since 5.3.0 Added optional $parent_controller parameter. 56 57 * 57 * @param string $parent_post_type Post type of the parent. 58 * @param string $parent_post_type Post type of the parent. 59 * @param WP_REST_Controller|null $parent_controller Controller for the Posts endpoint. 58 60 */ 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 ); 61 public function __construct( $parent_post_type, WP_REST_Controller $parent_controller = null ) { 62 if ( ! $parent_controller ) { 63 $parent_controller = WP_REST_Posts_Controller::get_for_post_type( $parent_post_type ); 64 } 62 65 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'; 66 if ( ! $parent_controller ) { 67 $parent_controller = new WP_REST_Posts_Controller( $parent_post_type ); 68 } 65 69 66 $this->parent_controller = new $parent_controller_class( $post_type_object->name ); 70 $this->parent_post_type = $parent_post_type; 71 $post_type_object = get_post_type_object( $parent_post_type ); 72 $this->parent_controller = $parent_controller; 67 73 $this->revisions_controller = new WP_REST_Revisions_Controller( $parent_post_type ); 68 74 $this->rest_namespace = 'wp/v2'; 69 75 $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..5ca9ce35bb 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 if ( ! $posts_controller instanceof WP_REST_Posts_Controller ) { 1588 $posts_controller = new WP_REST_Posts_Controller( $post->post_type ); 1589 } 1590 1591 $post_type = get_post_type_object( $post->post_type ); 1587 1592 1588 1593 $has_password_filter = false; 1589 1594 -
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..00cf4f5ff6 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 $controller = new $class( $post_type ); 90 91 if ( ! is_subclass_of( $controller, WP_REST_Controller::class ) ) { 92 self::$post_type_controllers[ $post_type ] = null; 93 94 return self::$post_type_controllers[ $post_type ]; 95 } 96 97 self::$post_type_controllers[ $post_type ] = $controller; 98 99 return self::$post_type_controllers[ $post_type ]; 100 } 101 51 102 /** 52 103 * Registers the routes for the objects of the controller. 53 104 * -
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..0755df10b7 100644
a b class WP_REST_Revisions_Controller extends WP_REST_Controller { 44 44 * Constructor. 45 45 * 46 46 * @since 4.7.0 47 * @since 5.3.0 Added optional $parent_controller parameter. 47 48 * 48 * @param string $parent_post_type Post type of the parent. 49 * @param string $parent_post_type Post type of the parent. 50 * @param WP_REST_Controller|null $parent_controller Controller for the Posts endpoint. 49 51 */ 50 public function __construct( $parent_post_type ) { 52 public function __construct( $parent_post_type, WP_REST_Controller $parent_controller = null ) { 53 if ( ! $parent_controller ) { 54 $parent_controller = WP_REST_Posts_Controller::get_for_post_type( $parent_post_type ); 55 } 56 57 if ( ! $parent_controller ) { 58 $parent_controller = new WP_REST_Posts_Controller( $parent_post_type ); 59 } 60 51 61 $this->parent_post_type = $parent_post_type; 52 $this->parent_controller = new WP_REST_Posts_Controller( $parent_post_type );62 $this->parent_controller = $parent_controller; 53 63 $this->namespace = 'wp/v2'; 54 64 $this->rest_base = 'revisions'; 55 65 $post_type_object = get_post_type_object( $parent_post_type );