Make WordPress Core

Changeset 58201

Timestamp:
05/27/2024 09:04:10 AM (2 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
     676
     677
     678
     679
     680
     681
     682
     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' ) );
    676687        }
    677688    }
     
    920931    public function get_autosave_rest_controller() {
    921932        if ( ! $this->show_in_rest ) {
     933
     934
     935
     936
    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
    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
     599
     600
     601
     602
     603
     604
     605
     606
     607
     608
     609
     610
     611
     612
     613
     614
     615
     616
     617
     618
     619
     620
     621
     622
     623
     624
     625
     626
     627
     628
     629
     630
     631
     632
     633
     634
     635
     636
     637
     638
     639
     640
     641
     642
     643
     644
     645
     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
    5961            ),
    6062            $post_type_supports
Note: See TracChangeset for help on using the changeset viewer.