Make WordPress Core

Changeset 58201


Ignore:
Timestamp:
05/27/2024 09:04:10 AM (8 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.

Location:
trunk
Files:
1 added
4 edited

Legend:

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

    r57628 r58201  
    671671            }
    672672            unset( $this->supports );
     673
     674            /*
     675             * 'editor' support implies 'autosave' support for backward compatibility.
     676             * 'autosave' support needs to be explicitly removed if not desired.
     677             */
     678            if (
     679                post_type_supports( $this->name, 'editor' ) &&
     680                ! post_type_supports( $this->name, 'autosave' )
     681            ) {
     682                add_post_type_support( $this->name, 'autosave' );
     683            }
    673684        } elseif ( false !== $this->supports ) {
    674685            // Add default features.
    675             add_post_type_support( $this->name, array( 'title', 'editor' ) );
     686            add_post_type_support( $this->name, array( 'title', 'editor', 'autosave' ) );
    676687        }
    677688    }
     
    920931    public function get_autosave_rest_controller() {
    921932        if ( ! $this->show_in_rest ) {
     933            return null;
     934        }
     935
     936        if ( ! post_type_supports( $this->name, 'autosave' ) ) {
    922937            return null;
    923938        }
  • trunk/src/wp-includes/post.php

    r58200 r58201  
    571571        'wp_font_family',
    572572        array(
    573             'labels'                         => array(
     573            'labels'                => array(
    574574                'name'          => __( 'Font Families' ),
    575575                'singular_name' => __( 'Font Family' ),
    576576            ),
    577             'public'                         => false,
    578             '_builtin'                       => true, /* internal use only. don't use this when registering your own post type. */
    579             'hierarchical'                   => false,
    580             'capabilities'                   => array(
     577            'public'                => false,
     578            '_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
     579            'hierarchical'          => false,
     580            'capabilities'          => array(
    581581                'read'                   => 'edit_theme_options',
    582582                'read_private_posts'     => 'edit_theme_options',
     
    590590                'delete_published_posts' => 'edit_theme_options',
    591591            ),
    592             'map_meta_cap'                   => true,
    593             'query_var'                      => false,
    594             'rewrite'                        => false,
    595             'show_in_rest'                   => true,
    596             'rest_base'                      => 'font-families',
    597             'rest_controller_class'          => 'WP_REST_Font_Families_Controller',
    598             // Disable autosave endpoints for font families.
    599             'autosave_rest_controller_class' => 'stdClass',
     592            'map_meta_cap'          => true,
     593            'query_var'             => false,
     594            'rewrite'               => false,
     595            'show_in_rest'          => true,
     596            'rest_base'             => 'font-families',
     597            'rest_controller_class' => 'WP_REST_Font_Families_Controller',
     598            'supports'              => array( 'title' ),
    600599        )
    601600    );
     
    604603        'wp_font_face',
    605604        array(
    606             'labels'                         => array(
     605            'labels'                => array(
    607606                'name'          => __( 'Font Faces' ),
    608607                'singular_name' => __( 'Font Face' ),
    609608            ),
    610             'public'                         => false,
    611             '_builtin'                       => true, /* internal use only. don't use this when registering your own post type. */
    612             'hierarchical'                   => false,
    613             'capabilities'                   => array(
     609            'public'                => false,
     610            '_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
     611            'hierarchical'          => false,
     612            'capabilities'          => array(
    614613                'read'                   => 'edit_theme_options',
    615614                'read_private_posts'     => 'edit_theme_options',
     
    623622                'delete_published_posts' => 'edit_theme_options',
    624623            ),
    625             'map_meta_cap'                   => true,
    626             'query_var'                      => false,
    627             'rewrite'                        => false,
    628             'show_in_rest'                   => true,
    629             'rest_base'                      => 'font-families/(?P<font_family_id>[\d]+)/font-faces',
    630             'rest_controller_class'          => 'WP_REST_Font_Faces_Controller',
    631             // Disable autosave endpoints for font faces.
    632             'autosave_rest_controller_class' => 'stdClass',
     624            'map_meta_cap'          => true,
     625            'query_var'             => false,
     626            'rewrite'               => false,
     627            'show_in_rest'          => true,
     628            'rest_base'             => 'font-families/(?P<font_family_id>[\d]+)/font-faces',
     629            'rest_controller_class' => 'WP_REST_Font_Faces_Controller',
     630            'supports'              => array( 'title' ),
    633631        )
    634632    );
     
    17201718 *                                                         'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'.
    17211719 *                                                         Additionally, the 'revisions' feature dictates whether the post type
    1722  *                                                         will store revisions, and the 'comments' feature dictates whether the
    1723  *                                                         comments count will show on the edit screen. A feature can also be
     1720 *                                                         will store revisions, the 'autosave' feature dictates whether the post type
     1721 *                                                         will be autosaved, and the 'comments' feature dictates whether the
     1722 *                                                         comments count will show on the edit screen. For backward compatibility reasons,
     1723 *                                                         adding 'editor' support implies 'autosave' support too. A feature can also be
    17241724 *                                                         specified as an array of arguments to provide additional information
    17251725 *                                                         about supporting that feature.
     
    21992199 *
    22002200 * Additionally, the 'revisions' feature dictates whether the post type will
    2201  * store revisions, and the 'comments' feature dictates whether the comments
     2201 * store revisions, the 'autosave' feature dictates whether the post type
     2202 * will be autosaved, and the 'comments' feature dictates whether the comments
    22022203 * count will show on the edit screen.
    22032204 *
  • 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}
  • trunk/tests/phpunit/tests/post/wpPostType.php

    r56819 r58201  
    2525        $this->assertSameSets(
    2626            array(
    27                 'title'  => true,
    28                 'editor' => true,
     27                'title'    => true,
     28                'editor'   => true,
     29                'autosave' => true,
    2930            ),
    3031            $post_type_supports
     
    5758                'comments'  => true,
    5859                'revisions' => true,
     60                'autosave'  => true,
    5961            ),
    6062            $post_type_supports
Note: See TracChangeset for help on using the changeset viewer.