Make WordPress Core


Ignore:
Timestamp:
05/27/2024 09:04:10 AM (4 months ago)
Author:
swissspidy
Message:

Posts, Post Types: Autosave: Allow disabling autosave support per post type.

Not all post types support autosaving. By making autosave a post type feature, support can be more granularly handled without any workarounds or hardcoded allowlists. wp_font_family and wp_font_face are examples of built-in post types which do not support autosave.

For backward compatibility reasons, adding editor support implies autosave support, so one would need to explicitly use remove_post_type_support() to remove it again.

Props swissspidy, youknowriad, hellofromtonya, peterwilsoncc.
Fixes #41172.

File:
1 edited

Legend:

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

    r57987 r58201  
    218218    /**
    219219     * @ticket 21586
     220     * @ticket 41172
    220221     */
    221222    public function test_post_type_with_no_support() {
    222223        register_post_type( 'foo', array( 'supports' => array() ) );
    223         $this->assertTrue( post_type_supports( 'foo', 'editor' ) );
    224         $this->assertTrue( post_type_supports( 'foo', 'title' ) );
     224        $this->assertTrue( post_type_supports( 'foo', 'editor' ), 'Editor support should be enabled by default.' );
     225        $this->assertTrue( post_type_supports( 'foo', 'title' ), 'Title support should be enabled by default.' );
     226        $this->assertTrue( post_type_supports( 'foo', 'autosave' ), 'Autosaves support should be enabled by default.' );
    225227        _unregister_post_type( 'foo' );
    226228
    227229        register_post_type( 'foo', array( 'supports' => false ) );
    228         $this->assertFalse( post_type_supports( 'foo', 'editor' ) );
    229         $this->assertFalse( post_type_supports( 'foo', 'title' ) );
     230        $this->assertFalse( post_type_supports( 'foo', 'editor' ), 'Editor support should be disabled.' );
     231        $this->assertFalse( post_type_supports( 'foo', 'title' ), 'Title support should be disabled.' );
     232        $this->assertFalse( post_type_supports( 'foo', 'autosave' ), 'Autosaves support should be disabled.' );
    230233        _unregister_post_type( 'foo' );
    231234    }
     
    433436        $this->assertSameSetsWithIndex(
    434437            array(
    435                 'editor' => true,
    436                 'author' => true,
    437                 'title'  => true,
     438                'editor'   => true,
     439                'author'   => true,
     440                'title'    => true,
     441                'autosave' => true,
    438442            ),
    439443            $_wp_post_type_features['foo']
     
    590594        $this->assertSameSets( array(), get_post_types_by_support( 'somefeature' ) );
    591595    }
     596
     597    /**
     598     * @ticket 41172
     599     */
     600    public function test_post_type_supports_autosave_based_on_editor_support() {
     601        register_post_type( 'foo', array( 'supports' => array( 'editor' ) ) );
     602        $this->assertTrue( post_type_supports( 'foo', 'autosave' ) );
     603        _unregister_post_type( 'foo' );
     604
     605        register_post_type( 'foo', array( 'supports' => array( 'title' ) ) );
     606        $this->assertFalse( post_type_supports( 'foo', 'autosave' ) );
     607        _unregister_post_type( 'foo' );
     608    }
     609
     610    /**
     611     * @ticket 41172
     612     */
     613    public function test_removing_autosave_support_removes_rest_api_controller() {
     614        register_post_type(
     615            'foo',
     616            array(
     617                'show_in_rest' => true,
     618                'supports'     => array( 'editor' ),
     619            )
     620        );
     621
     622        $post_type_object = get_post_type_object( 'foo' );
     623        $this->assertInstanceOf( 'WP_REST_Autosaves_Controller', $post_type_object->get_autosave_rest_controller(), 'Autosave controller should be set by default.' );
     624
     625        remove_post_type_support( 'foo', 'autosave' );
     626        $post_type_object = get_post_type_object( 'foo' );
     627        $this->assertSame( null, $post_type_object->get_autosave_rest_controller(), 'Autosave controller should be removed.' );
     628        _unregister_post_type( 'foo' );
     629    }
     630
     631    /**
     632     * @ticket 41172
     633     */
     634    public function test_removing_editor_support_does_not_remove_autosave_support() {
     635        register_post_type(
     636            'foo',
     637            array(
     638                'show_in_rest' => true,
     639                'supports'     => array( 'editor' ),
     640            )
     641        );
     642        remove_post_type_support( 'foo', 'editor' );
     643
     644        $this->assertFalse( post_type_supports( 'foo', 'editor' ), 'Post type should not support editor.' );
     645        $this->assertTrue( post_type_supports( 'foo', 'autosave' ), 'Post type should still support autosaves.' );
     646    }
    592647}
Note: See TracChangeset for help on using the changeset viewer.