diff --git a/src/wp-includes/class-wp-post-type.php b/src/wp-includes/class-wp-post-type.php index 7a2769ed88327..610297661749c 100644 --- a/src/wp-includes/class-wp-post-type.php +++ b/src/wp-includes/class-wp-post-type.php @@ -670,9 +670,20 @@ public function add_supports() { } } 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 @@ public function get_autosave_rest_controller() { return null; } + if ( ! post_type_supports( $this->name, 'autosave' ) ) { + return null; + } + if ( 'attachment' === $this->name ) { return null; } diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 90a941ac4cc68..dcabd65937837 100644 --- a/src/wp-includes/post.php +++ b/src/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/tests/phpunit/tests/fonts/font-library/postTypes.php b/tests/phpunit/tests/fonts/font-library/postTypes.php new file mode 100644 index 0000000000000..7b24163e46705 --- /dev/null +++ b/tests/phpunit/tests/fonts/font-library/postTypes.php @@ -0,0 +1,45 @@ +assertFalse( post_type_supports( 'wp_font_family', 'autosave' ) ); + } + + /** + * @ticket 41172 + */ + public function test_wp_font_face_does_not_support_autosaves() { + $this->assertFalse( post_type_supports( 'wp_font_face', 'autosave' ) ); + } + + /** + * @ticket 41172 + */ + public function test_wp_font_family_does_not_have_an_autosave_controller() { + $post_type_object = get_post_type_object( 'wp_font_family' ); + $controller = $post_type_object->get_autosave_rest_controller(); + + $this->assertNull( $controller ); + } + + /** + * @ticket 41172 + */ + public function test_wp_font_face_does_not_have_an_autosave_controller() { + $post_type_object = get_post_type_object( 'wp_font_face' ); + $controller = $post_type_object->get_autosave_rest_controller(); + + $this->assertNull( $controller ); + } +} diff --git a/tests/phpunit/tests/post/types.php b/tests/phpunit/tests/post/types.php index 70d2538956aa4..3d3d9f1065f95 100644 --- a/tests/phpunit/tests/post/types.php +++ b/tests/phpunit/tests/post/types.php @@ -217,16 +217,19 @@ public function test_post_type_supports() { /** * @ticket 21586 + * @ticket 41172 */ public function test_post_type_with_no_support() { register_post_type( 'foo', array( 'supports' => array() ) ); - $this->assertTrue( post_type_supports( 'foo', 'editor' ) ); - $this->assertTrue( post_type_supports( 'foo', 'title' ) ); + $this->assertTrue( post_type_supports( 'foo', 'editor' ), 'Editor support should be enabled by default.' ); + $this->assertTrue( post_type_supports( 'foo', 'title' ), 'Title support should be enabled by default.' ); + $this->assertTrue( post_type_supports( 'foo', 'autosave' ), 'Autosaves support should be enabled by default.' ); _unregister_post_type( 'foo' ); register_post_type( 'foo', array( 'supports' => false ) ); - $this->assertFalse( post_type_supports( 'foo', 'editor' ) ); - $this->assertFalse( post_type_supports( 'foo', 'title' ) ); + $this->assertFalse( post_type_supports( 'foo', 'editor' ), 'Editor support should be disabled.' ); + $this->assertFalse( post_type_supports( 'foo', 'title' ), 'Title support should be disabled.' ); + $this->assertFalse( post_type_supports( 'foo', 'autosave' ), 'Autosaves support should be disabled.' ); _unregister_post_type( 'foo' ); } @@ -432,9 +435,10 @@ public function test_unregister_post_type_removes_post_type_supports() { $this->assertSameSetsWithIndex( array( - 'editor' => true, - 'author' => true, - 'title' => true, + 'editor' => true, + 'author' => true, + 'title' => true, + 'autosave' => true, ), $_wp_post_type_features['foo'] ); @@ -589,4 +593,55 @@ public function test_get_post_types_by_support_excluding_features() { public function test_get_post_types_by_support_non_existent_feature() { $this->assertSameSets( array(), get_post_types_by_support( 'somefeature' ) ); } + + /** + * @ticket 41172 + */ + public function test_post_type_supports_autosave_based_on_editor_support() { + register_post_type( 'foo', array( 'supports' => array( 'editor' ) ) ); + $this->assertTrue( post_type_supports( 'foo', 'autosave' ) ); + _unregister_post_type( 'foo' ); + + register_post_type( 'foo', array( 'supports' => array( 'title' ) ) ); + $this->assertFalse( post_type_supports( 'foo', 'autosave' ) ); + _unregister_post_type( 'foo' ); + } + + /** + * @ticket 41172 + */ + public function test_removing_autosave_support_removes_rest_api_controller() { + register_post_type( + 'foo', + array( + 'show_in_rest' => true, + 'supports' => array( 'editor' ), + ) + ); + + $post_type_object = get_post_type_object( 'foo' ); + $this->assertInstanceOf( 'WP_REST_Autosaves_Controller', $post_type_object->get_autosave_rest_controller(), 'Autosave controller should be set by default.' ); + + remove_post_type_support( 'foo', 'autosave' ); + $post_type_object = get_post_type_object( 'foo' ); + $this->assertSame( null, $post_type_object->get_autosave_rest_controller(), 'Autosave controller should be removed.' ); + _unregister_post_type( 'foo' ); + } + + /** + * @ticket 41172 + */ + public function test_removing_editor_support_does_not_remove_autosave_support() { + register_post_type( + 'foo', + array( + 'show_in_rest' => true, + 'supports' => array( 'editor' ), + ) + ); + remove_post_type_support( 'foo', 'editor' ); + + $this->assertFalse( post_type_supports( 'foo', 'editor' ), 'Post type should not support editor.' ); + $this->assertTrue( post_type_supports( 'foo', 'autosave' ), 'Post type should still support autosaves.' ); + } } diff --git a/tests/phpunit/tests/post/wpPostType.php b/tests/phpunit/tests/post/wpPostType.php index 2a8ad42f0a2ca..bfa0fcc83b528 100644 --- a/tests/phpunit/tests/post/wpPostType.php +++ b/tests/phpunit/tests/post/wpPostType.php @@ -24,8 +24,9 @@ public function test_add_supports_defaults() { $this->assertSameSets( array( - 'title' => true, - 'editor' => true, + 'title' => true, + 'editor' => true, + 'autosave' => true, ), $post_type_supports ); @@ -56,6 +57,7 @@ public function test_add_supports_custom() { 'editor' => true, 'comments' => true, 'revisions' => true, + 'autosave' => true, ), $post_type_supports );