Make WordPress Core


Ignore:
Timestamp:
10/10/2023 02:03:03 PM (21 months ago)
Author:
spacedmonkey
Message:

REST API: Fix issue with Template and Template Part Revision/Autosave REST API controllers.

The Template and Template Part REST API controllers have unique characteristics compared to other post type REST API controllers. They do not rely on integer IDs to reference objects; instead, they use a combination of the theme name and slug of the template, like 'twentytwentyfourhome.' Consequently, when the post types template and template part were introduced in [52062], it led to the registration of REST API endpoints for autosaves and revisions with invalid URL structures.

In this commit, we introduce new functionality to enable custom autosave and revisions endpoints to be registered at the post type level. Similar to the 'rest_controller_class' parameter, developers can now define 'revisions_rest_controller' and 'autosave_rest_controller.' This empowers developers to create custom controllers for these functionalities. Additionally, we introduce a 'late_route_registration' parameter, which proves helpful when dealing with custom URL patterns and regex pattern matching issues.
This commit registers new classes for template and template part autosave and revisions controllers, differentiating them from standard controllers in the following ways:

  • The response shape now matches that of the template controller.
  • Permission checks align with the template controller.
  • A custom URL pattern is introduced to support slug-based identification of templates.

Furthermore, we've updated the utility function '_build_block_template_result_from_post' to support passing revision post objects. This enhancement ensures compatibility with the custom revisions controller.

Props spacedmonkey, revgeorge, andraganescu, hellofromTonya, antonvlasenko, kadamwhite, ironprogrammer, costdev, mukesh27, timothyblynjacobs, adamsilverstein.
Fixes 56922.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/wpPostType.php

    r53126 r56819  
    230230        $this->assertSame( 3, $action->get_call_count() );
    231231    }
     232
     233    /**
     234     * @ticket 56922
     235     *
     236     * @dataProvider data_should_have_correct_custom_revisions_and_autosaves_controllers_properties
     237     *
     238     * @covers WP_Post_Type::set_props
     239     *
     240     * @param string      $property_name           Property name.
     241     * @param string      $property_value          Property value.
     242     * @param string|bool $expected_property_value Expected property value.
     243     */
     244    public function test_should_have_correct_custom_revisions_and_autosaves_controllers_properties( $property_name, $property_value, $expected_property_value ) {
     245        $properties = null === $property_value ? array() : array( $property_name => $property_value );
     246
     247        $post_type = new WP_Post_Type( 'test_post_type', $properties );
     248
     249        $this->assertObjectHasProperty( $property_name, $post_type, "The WP_Post_Type object does not have the expected {$property_name} property." );
     250        $this->assertSame(
     251            $expected_property_value,
     252            $post_type->$property_name,
     253            sprintf( 'Expected the property "%s" to have the %s value.', $property_name, var_export( $expected_property_value, true ) )
     254        );
     255    }
     256
     257    /**
     258     * Data provider for test_should_allow_to_set_custom_revisions_and_autosaves_controllers_properties.
     259     *
     260     * @return array[] Arguments {
     261     *     @type string $property_name           Property name.
     262     *     @type string $property_value          Property value.
     263     *     @type string|bool $expected_property_value Expected property value.
     264     * }
     265     */
     266    public function data_should_have_correct_custom_revisions_and_autosaves_controllers_properties() {
     267        return array(
     268            'autosave_rest_controller_class property'  => array(
     269                'autosave_rest_controller_class',
     270                'My_Custom_Template_Autosaves_Controller',
     271                'My_Custom_Template_Autosaves_Controller',
     272            ),
     273            'autosave_rest_controller_class property (null value)' => array(
     274                'autosave_rest_controller_class',
     275                null,
     276                false,
     277            ),
     278            'revisions_rest_controller_class property' => array(
     279                'revisions_rest_controller_class',
     280                'My_Custom_Template_Revisions_Controller',
     281                'My_Custom_Template_Revisions_Controller',
     282            ),
     283            'revisions_rest_controller_class property (null value)' => array(
     284                'revisions_rest_controller_class',
     285                null,
     286                false,
     287            ),
     288        );
     289    }
     290
     291    /**
     292     * @ticket 56922
     293     *
     294     * @covers WP_Post_Type::get_revisions_rest_controller
     295     *
     296     * @dataProvider data_get_revisions_rest_controller_should_return_correct_values
     297     *
     298     * @param bool        $show_in_rest                    Enables "show_in_rest" support.
     299     * @param bool        $supports_revisions              Enables revisions support.
     300     * @param string|bool $revisions_rest_controller_class Custom revisions REST controller class.
     301     * @param string|null $expected_value                  Expected value.
     302     */
     303    public function test_get_revisions_rest_controller_should_return_correct_values( $show_in_rest, $supports_revisions, $revisions_rest_controller_class, $expected_value ) {
     304        $post_type  = 'test_post_type';
     305        $properties = array(
     306            'show_in_rest'                    => $show_in_rest,
     307            'supports'                        => $supports_revisions ? array( 'revisions' ) : array(),
     308            'revisions_rest_controller_class' => $revisions_rest_controller_class,
     309        );
     310        register_post_type( $post_type, $properties );
     311        $post_type = get_post_type_object( $post_type );
     312
     313        $controller = $post_type->get_revisions_rest_controller();
     314        if ( $expected_value ) {
     315            $this->assertInstanceOf( $expected_value, $controller );
     316
     317            return;
     318        }
     319
     320        $this->assertSame( $expected_value, $controller );
     321    }
     322
     323    /**
     324     * Data provider for test_get_revisions_rest_controller_should_return_correct_values.
     325     *
     326     * @return array[] Arguments {
     327     *     @type bool             $show_in_rest                    Enables "show_in_rest" support.
     328     *     @type bool             $supports_revisions              Enables revisions support.
     329     *     @type string|bool      $revisions_rest_controller_class Custom revisions REST controller class.
     330     *     @type string|null      $expected_value                  Expected value.
     331     * }
     332     */
     333    public function data_get_revisions_rest_controller_should_return_correct_values() {
     334        return array(
     335            'disable show_in_rest'                => array(
     336                false,
     337                false,
     338                false,
     339                null,
     340            ),
     341            'disable revisions support'           => array(
     342                true,
     343                false,
     344                false,
     345                null,
     346            ),
     347            'default rest revisions controller'   => array(
     348                true,
     349                true,
     350                false,
     351                WP_REST_Revisions_Controller::class,
     352            ),
     353            'incorrect rest revisions controller' => array(
     354                true,
     355                true,
     356                stdClass::class,
     357                null,
     358            ),
     359            'correct rest revisions controller'   => array(
     360                true,
     361                true,
     362                WP_REST_Template_Revisions_Controller::class,
     363                WP_REST_Template_Revisions_Controller::class,
     364            ),
     365        );
     366    }
     367
     368    /**
     369     * @ticket 56922
     370     *
     371     * @covers WP_Post_Type::get_autosave_rest_controller
     372     *
     373     * @dataProvider data_get_autosave_rest_controller_should_return_correct_values
     374     *
     375     * @param bool        $show_in_rest                   Enables "show_in_rest" support.
     376     * @param string      $post_type                      Post type.
     377     * @param string|bool $autosave_rest_controller_class Custom autosave REST controller class.
     378     * @param string|null $expected_value                 Expected value.
     379     */
     380    public function test_get_autosave_rest_controller_should_return_correct_values( $show_in_rest, $post_type, $autosave_rest_controller_class, $expected_value ) {
     381        $properties = array(
     382            'show_in_rest'                   => $show_in_rest,
     383            'autosave_rest_controller_class' => $autosave_rest_controller_class,
     384        );
     385        register_post_type( $post_type, $properties );
     386        $post_type = get_post_type_object( $post_type );
     387
     388        $controller = $post_type->get_autosave_rest_controller();
     389        if ( $expected_value ) {
     390            $this->assertInstanceOf( $expected_value, $controller );
     391
     392            return;
     393        }
     394
     395        $this->assertSame( $expected_value, $controller );
     396    }
     397
     398    /**
     399     * Data provider for test_get_autosave_rest_controller_should_return_correct_values.
     400     *
     401     * @return array[] Arguments {
     402     *     @type bool        $show_in_rest                   Enables "show_in_rest" support.
     403     *     @type string      $post_type                      Post type.
     404     *     @type string|bool $autosave_rest_controller_class Custom autosave REST controller class.
     405     *     @type string|null $expected_value                 Expected value.
     406     * }
     407     */
     408    public function data_get_autosave_rest_controller_should_return_correct_values() {
     409        return array(
     410            'disable show_in_rest'               => array(
     411                false,
     412                'attachment',
     413                false,
     414                null,
     415            ),
     416            'invalid post type'                  => array(
     417                true,
     418                'attachment',
     419                false,
     420                null,
     421            ),
     422            'default rest autosave controller'   => array(
     423                true,
     424                'test_post_type',
     425                false,
     426                WP_REST_Autosaves_Controller::class,
     427            ),
     428            'incorrect rest autosave controller' => array(
     429                true,
     430                'test_post_type',
     431                stdClass::class,
     432                null,
     433            ),
     434            'correct rest autosave controller'   => array(
     435                true,
     436                'test_post_type',
     437                WP_REST_Template_Autosaves_Controller::class,
     438                WP_REST_Template_Autosaves_Controller::class,
     439            ),
     440        );
     441    }
    232442}
Note: See TracChangeset for help on using the changeset viewer.