Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling autosave post type support #6035

Closed
17 changes: 16 additions & 1 deletion src/wp-includes/class-wp-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
} 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' ) );
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
56 changes: 28 additions & 28 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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' ),
)
);

Expand Down Expand Up @@ -1719,7 +1717,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
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
* 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.
Expand Down Expand Up @@ -2198,7 +2197,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
Expand Down
45 changes: 45 additions & 0 deletions tests/phpunit/tests/fonts/font-library/postTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Test the wp_font_family and wp_font_face post types.
*
* @package WordPress
* @subpackage Font Library
*
* @group fonts
* @group font-library
*/
class Tests_Fonts_Post_Types extends WP_UnitTestCase {
/**
* @ticket 41172
*/
public function test_wp_font_family_does_not_support_autosaves() {
$this->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 );
}
}
69 changes: 62 additions & 7 deletions tests/phpunit/tests/post/types.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}

Expand Down Expand Up @@ -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']
);
Expand Down Expand Up @@ -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.' );
}
}
6 changes: 4 additions & 2 deletions tests/phpunit/tests/post/wpPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -56,6 +57,7 @@ public function test_add_supports_custom() {
'editor' => true,
'comments' => true,
'revisions' => true,
'autosave' => true,
),
$post_type_supports
);
Expand Down
Loading