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.
Built from https://develop.svn.wordpress.org/trunk@58201


git-svn-id: http://core.svn.wordpress.org/trunk@57664 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2024-05-27 09:06:16 +00:00
parent eba0df8c7b
commit 3504e15aa9
3 changed files with 47 additions and 31 deletions

View File

@ -670,9 +670,20 @@ final class WP_Post_Type {
}
}
unset( $this->supports );
/*
* 'editor' support implies 'autosave' support for backward compatibility.
* 'autosave' support needs to be explicitly removed if not desired.
*/
if (
post_type_supports( $this->name, 'editor' ) &&
! post_type_supports( $this->name, 'autosave' )
) {
add_post_type_support( $this->name, 'autosave' );
}
} elseif ( false !== $this->supports ) {
// Add default features.
add_post_type_support( $this->name, array( 'title', 'editor' ) );
add_post_type_support( $this->name, array( 'title', 'editor', 'autosave' ) );
}
}
@ -922,6 +933,10 @@ final class WP_Post_Type {
return null;
}
if ( ! post_type_supports( $this->name, 'autosave' ) ) {
return null;
}
if ( 'attachment' === $this->name ) {
return null;
}

View File

@ -570,14 +570,14 @@ function create_initial_post_types() {
register_post_type(
'wp_font_family',
array(
'labels' => array(
'labels' => array(
'name' => __( 'Font Families' ),
'singular_name' => __( 'Font Family' ),
),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'hierarchical' => false,
'capabilities' => array(
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'hierarchical' => false,
'capabilities' => array(
'read' => 'edit_theme_options',
'read_private_posts' => 'edit_theme_options',
'create_posts' => 'edit_theme_options',
@ -589,28 +589,27 @@ function create_initial_post_types() {
'delete_others_posts' => 'edit_theme_options',
'delete_published_posts' => 'edit_theme_options',
),
'map_meta_cap' => true,
'query_var' => false,
'rewrite' => false,
'show_in_rest' => true,
'rest_base' => 'font-families',
'rest_controller_class' => 'WP_REST_Font_Families_Controller',
// Disable autosave endpoints for font families.
'autosave_rest_controller_class' => 'stdClass',
'map_meta_cap' => true,
'query_var' => false,
'rewrite' => false,
'show_in_rest' => true,
'rest_base' => 'font-families',
'rest_controller_class' => 'WP_REST_Font_Families_Controller',
'supports' => array( 'title' ),
)
);
register_post_type(
'wp_font_face',
array(
'labels' => array(
'labels' => array(
'name' => __( 'Font Faces' ),
'singular_name' => __( 'Font Face' ),
),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'hierarchical' => false,
'capabilities' => array(
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'hierarchical' => false,
'capabilities' => array(
'read' => 'edit_theme_options',
'read_private_posts' => 'edit_theme_options',
'create_posts' => 'edit_theme_options',
@ -622,14 +621,13 @@ function create_initial_post_types() {
'delete_others_posts' => 'edit_theme_options',
'delete_published_posts' => 'edit_theme_options',
),
'map_meta_cap' => true,
'query_var' => false,
'rewrite' => false,
'show_in_rest' => true,
'rest_base' => 'font-families/(?P<font_family_id>[\d]+)/font-faces',
'rest_controller_class' => 'WP_REST_Font_Faces_Controller',
// Disable autosave endpoints for font faces.
'autosave_rest_controller_class' => 'stdClass',
'map_meta_cap' => true,
'query_var' => false,
'rewrite' => false,
'show_in_rest' => true,
'rest_base' => 'font-families/(?P<font_family_id>[\d]+)/font-faces',
'rest_controller_class' => 'WP_REST_Font_Faces_Controller',
'supports' => array( 'title' ),
)
);
@ -1719,8 +1717,10 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' )
* 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt',
* 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'.
* Additionally, the 'revisions' feature dictates whether the post type
* will store revisions, and the 'comments' feature dictates whether the
* comments count will show on the edit screen. A feature can also be
* will store revisions, the 'autosave' feature dictates whether the post type
* will be autosaved, and the 'comments' feature dictates whether the
* comments count will show on the edit screen. For backward compatibility reasons,
* adding 'editor' support implies 'autosave' support too. A feature can also be
* specified as an array of arguments to provide additional information
* about supporting that feature.
* Example: `array( 'my_feature', array( 'field' => 'value' ) )`.
@ -2198,7 +2198,8 @@ function _add_post_type_submenus() {
* 'thumbnail', 'custom-fields', and 'post-formats'.
*
* Additionally, the 'revisions' feature dictates whether the post type will
* store revisions, and the 'comments' feature dictates whether the comments
* store revisions, the 'autosave' feature dictates whether the post type
* will be autosaved, and the 'comments' feature dictates whether the comments
* count will show on the edit screen.
*
* A third, optional parameter can also be passed along with a feature to provide

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.6-alpha-58200';
$wp_version = '6.6-alpha-58201';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.