WordPress.org

Make WordPress Core

Ticket #40383: 40383.2.diff

File 40383.2.diff, 8.1 KB (added by TimothyBlynJacobs, 6 months ago)
  • src/wp-includes/rest-api.php

    diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php
    index 7602dd7405..6769221fd5 100644
    a b function rest_api_default_filters() { 
    191191 * @since 4.7.0
    192192 */
    193193function create_initial_rest_routes() {
    194         foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
    195                 $class = ! 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 );
    196196
    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 ) {
    202198                        continue;
    203199                }
    204200
    205201                $controller->register_routes();
    206202
    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 );
    209205                        $revisions_controller->register_routes();
    210206                }
    211207
    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 );
    214210                        $autosaves_controller->register_routes();
    215211                }
    216212        }
  • 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 { 
    5353         * Constructor.
    5454         *
    5555         * @since 5.0.0
     56         * @since 5.3.0 Added optional $parent_controller parameter.
    5657         *
    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.
    5860         */
    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                }
    6265
    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                }
    6569
    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;
    6773                $this->revisions_controller = new WP_REST_Revisions_Controller( $parent_post_type );
    6874                $this->rest_namespace       = 'wp/v2';
    6975                $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 { 
    15821582         * @return bool Whether post can be read.
    15831583         */
    15841584        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 );
    15871592
    15881593                $has_password_filter = false;
    15891594
  • 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  
    1616 */
    1717class WP_REST_Posts_Controller extends WP_REST_Controller {
    1818
     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
    1927        /**
    2028         * Post type.
    2129         *
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    4856                $this->meta = new WP_REST_Post_Meta_Fields( $this->post_type );
    4957        }
    5058
     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
    51102        /**
    52103         * Registers the routes for the objects of the controller.
    53104         *
  • 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 { 
    4444         * Constructor.
    4545         *
    4646         * @since 4.7.0
     47         * @since 5.3.0 Added optional $parent_controller parameter.
    4748         *
    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.
    4951         */
    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
    5161                $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;
    5363                $this->namespace         = 'wp/v2';
    5464                $this->rest_base         = 'revisions';
    5565                $post_type_object        = get_post_type_object( $parent_post_type );