From 3504e15aa96623d24bef7a3d57af2d3e5fce1ed7 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 27 May 2024 09:06:16 +0000 Subject: [PATCH] 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 --- wp-includes/class-wp-post-type.php | 17 ++++++++- wp-includes/post.php | 59 +++++++++++++++--------------- wp-includes/version.php | 2 +- 3 files changed, 47 insertions(+), 31 deletions(-) diff --git a/wp-includes/class-wp-post-type.php b/wp-includes/class-wp-post-type.php index 7a2769ed88..6102976617 100644 --- a/wp-includes/class-wp-post-type.php +++ b/wp-includes/class-wp-post-type.php @@ -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; } diff --git a/wp-includes/post.php b/wp-includes/post.php index 90a941ac4c..dcabd65937 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -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[\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[\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 diff --git a/wp-includes/version.php b/wp-includes/version.php index 1b68857619..7fcbe873b1 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.