WordPress.org

Make WordPress Core

Changeset 46435


Ignore:
Timestamp:
10/08/2019 01:41:29 PM (6 weeks ago)
Author:
kadamwhite
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-post-type.php

    r46272 r46435  
    344344     * @var WP_REST_Controller $rest_controller
    345345     */
    346     private $rest_controller;
     346    public $rest_controller;
    347347
    348348    /**
     
    723723        }
    724724
     725        if ( ! ( $this->rest_controller instanceof $class ) ) {
     726            return null;
     727        }
     728
    725729        return $this->rest_controller;
    726730    }
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r46272 r46435  
    1616 */
    1717class WP_REST_Posts_Controller extends WP_REST_Controller {
    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 
    2718    /**
    2819     * Post type.
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r46434 r46435  
    46184618    }
    46194619
     4620    /**
     4621     * @ticket 45677
     4622     */
     4623    public function test_get_for_post_type_returns_null_for_invalid_provided_controller() {
     4624        register_post_type(
     4625            'test',
     4626            array(
     4627                'show_in_rest'    => true,
     4628                'rest_controller' => new \stdClass(),
     4629            )
     4630        );
     4631
     4632        $this->assertNull( get_post_type_object( 'test' )->get_rest_controller() );
     4633    }
     4634
     4635    /**
     4636     * @ticket 45677
     4637     */
     4638    public function test_get_for_post_type_returns_null_for_controller_class_mismatch() {
     4639        register_post_type(
     4640            'test',
     4641            array(
     4642                'show_in_rest'          => true,
     4643                'rest_controller_class' => WP_REST_Posts_Controller::class,
     4644                'rest_controller'       => new WP_REST_Terms_Controller( 'category' ),
     4645            )
     4646        );
     4647
     4648        $this->assertNull( get_post_type_object( 'test' )->get_rest_controller() );
     4649    }
     4650
    46204651    public function tearDown() {
    46214652        _unregister_post_type( 'private-post' );
Note: See TracChangeset for help on using the changeset viewer.