From de49eea2039f77f6585f115543f823ba9ae6f682 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 6 Feb 2024 10:44:17 +0100 Subject: [PATCH 1/7] Allow disabling autosave post type support --- src/wp-includes/class-wp-post-type.php | 17 ++++++++++++++++- src/wp-includes/post.php | 6 ++++-- 2 files changed, 20 insertions(+), 3 deletions(-) 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 eb0d8c1926190..aa89ddd2e66b1 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -1705,7 +1705,8 @@ 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 + * 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. A feature can also be * specified as an array of arguments to provide additional information * about supporting that feature. @@ -2184,7 +2185,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 From 1a018b7c80d83b90ac9587b87f02497b44f08d73 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 6 Feb 2024 10:55:21 +0100 Subject: [PATCH 2/7] Update tests --- tests/phpunit/tests/post/types.php | 7 ++++--- tests/phpunit/tests/post/wpPostType.php | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/post/types.php b/tests/phpunit/tests/post/types.php index df1543fa0c9ca..b9a60b98f1a06 100644 --- a/tests/phpunit/tests/post/types.php +++ b/tests/phpunit/tests/post/types.php @@ -432,9 +432,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'] ); 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 ); From 1a6fc1e4d5bfc539ee69d6376d09edd957f754a1 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 30 Apr 2024 11:16:25 +0200 Subject: [PATCH 3/7] Remove `stdClass` workaround --- src/wp-includes/post.php | 48 ++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 1b112fd96b67e..fa53b71b366af 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -569,14 +569,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', @@ -588,28 +588,26 @@ 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', ) ); 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', @@ -621,14 +619,12 @@ 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', ) ); From 248c8abcb963cd2e0c7b5aaae79cda3b049395a3 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 30 Apr 2024 11:26:49 +0200 Subject: [PATCH 4/7] Add some more specific tests --- src/wp-includes/post.php | 2 + .../tests/fonts/font-library/postTypes.php | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/phpunit/tests/fonts/font-library/postTypes.php diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index fa53b71b366af..e8212f7732100 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -594,6 +594,7 @@ function create_initial_post_types() { 'show_in_rest' => true, 'rest_base' => 'font-families', 'rest_controller_class' => 'WP_REST_Font_Families_Controller', + 'supports' => array( 'title' ), ) ); @@ -625,6 +626,7 @@ function create_initial_post_types() { 'show_in_rest' => true, 'rest_base' => 'font-families/(?P[\d]+)/font-faces', 'rest_controller_class' => 'WP_REST_Font_Faces_Controller', + 'supports' => array( 'title' ), ) ); 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..cf1dc8edeb3f5 --- /dev/null +++ b/tests/phpunit/tests/fonts/font-library/postTypes.php @@ -0,0 +1,45 @@ +assertFalse( post_type_supports( 'wp_font_family', 'autosave' ) ); + } + + /** + * @ticket 59043 + */ + public function test_wp_font_face_does_not_support_autosaves() { + $this->assertFalse( post_type_supports( 'wp_font_face', 'autosave' ) ); + } + + /** + * @ticket 59043 + */ + 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 59043 + */ + 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 ); + } +} From 08fd1280765994c15d90e56f26f4ca8f1c0d67c0 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 24 May 2024 10:19:19 +0200 Subject: [PATCH 5/7] Fix ticket annotation --- tests/phpunit/tests/fonts/font-library/postTypes.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/fonts/font-library/postTypes.php b/tests/phpunit/tests/fonts/font-library/postTypes.php index cf1dc8edeb3f5..7b24163e46705 100644 --- a/tests/phpunit/tests/fonts/font-library/postTypes.php +++ b/tests/phpunit/tests/fonts/font-library/postTypes.php @@ -10,21 +10,21 @@ */ class Tests_Fonts_Post_Types extends WP_UnitTestCase { /** - * @ticket 59043 + * @ticket 41172 */ public function test_wp_font_family_does_not_support_autosaves() { $this->assertFalse( post_type_supports( 'wp_font_family', 'autosave' ) ); } /** - * @ticket 59043 + * @ticket 41172 */ public function test_wp_font_face_does_not_support_autosaves() { $this->assertFalse( post_type_supports( 'wp_font_face', 'autosave' ) ); } /** - * @ticket 59043 + * @ticket 41172 */ public function test_wp_font_family_does_not_have_an_autosave_controller() { $post_type_object = get_post_type_object( 'wp_font_family' ); @@ -34,7 +34,7 @@ public function test_wp_font_family_does_not_have_an_autosave_controller() { } /** - * @ticket 59043 + * @ticket 41172 */ public function test_wp_font_face_does_not_have_an_autosave_controller() { $post_type_object = get_post_type_object( 'wp_font_face' ); From 09dd6dd8d1048d157d5a98462e59611e37796fed Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 24 May 2024 10:20:43 +0200 Subject: [PATCH 6/7] Add more tests Props to Peter --- tests/phpunit/tests/post/types.php | 62 ++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/post/types.php b/tests/phpunit/tests/post/types.php index 0ddea89d421ec..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' ); } @@ -590,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.' ); + } } From 6b593f28c355c54f202dffe5ba5060aecb2e163c Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 27 May 2024 10:43:06 +0200 Subject: [PATCH 7/7] Improve docblock --- src/wp-includes/post.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 297722b6a649e..dcabd65937837 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -1719,7 +1719,8 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) * Additionally, the 'revisions' feature dictates whether the post type * 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. A feature can also be + * 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' ) )`.