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

When autosaving drafts, don't create revisions. #4062

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion editor/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,10 @@ export function editPost( edits ) {
};
}

export function savePost() {
export function savePost( options ) {
return {
type: 'REQUEST_POST_UPDATE',
options,
};
}

Expand Down
11 changes: 9 additions & 2 deletions editor/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getDefaultBlockName,
} from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
Expand Down Expand Up @@ -76,7 +77,13 @@ export default {
} );
dispatch( removeNotice( SAVE_POST_NOTICE_ID ) );
const Model = wp.api.getPostTypeModel( getCurrentPostType( state ) );
new Model( toSend ).save().done( ( newPost ) => {
const newModel = new Model( toSend );

// Tag the autosave action to avoid creating revisions.
if ( action.options && action.options.autosave ) {
newModel.url = addQueryArgs( newModel.url(), { 'gutenberg_autosave': '1' } );
}
newModel.save().done( ( newPost ) => {
dispatch( {
type: 'RESET_POST',
post: newPost,
Expand Down Expand Up @@ -278,7 +285,7 @@ export default {
dispatch( editPost( { status: 'draft' } ) );
}

dispatch( savePost() );
dispatch( savePost( { 'autosave': 1 } ) );
},
SETUP_EDITOR( action ) {
const { post, settings } = action;
Expand Down
16 changes: 16 additions & 0 deletions gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,19 @@ function gutenberg_replace_default_add_new_button() {
function gutenberg_add_admin_body_class( $classes ) {
return "$classes gutenberg-editor-page";
}

/**
* Filter the number of revisions for posts, skipping revisions when autosaving.
*
* @param int $count The number of revisions to save.
*
* @return int The original count, or zero if this is a Gutenberg autosave.
*/
function gutenberg_filter_revisions( $count ) {

if ( isset( $_REQUEST['gutenberg_autosave'] ) && '1' === $_REQUEST['gutenberg_autosave'] ) {
return 0;
}
return $count;
}
add_filter( 'wp_revisions_to_keep', 'gutenberg_filter_revisions' );