Make WordPress Core

Opened 6 years ago

Last modified 6 years ago

#44786 new enhancement

Autosave: Conditionally allow revisions to be saved

Reported by: jblz's profile jblz Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Revisions Keywords: has-patch has-unit-tests reporter-feedback
Focuses: Cc:

Description

As is, when the DOING_AUTOSAVE constant is truthy, calling wp_save_post_revision immediately exits.

One of the first things that endpoints responsible for autosaves are doing is set this constant -- making it impossible to save a revision using the Revisions API during the same request context.

See:

Endpoints (& other plugins) would benefit from a filter allowing revisions to be saved.

A particular use case is to occasionally save revisions for drafts during an autosave call (so there is less chance of irrecoverable data loss when the autosave is overwritten).

Attachments (3)

autosave-allow-revision.diff (834 bytes) - added by jblz 6 years ago.
44786.withtests.patch (3.8 KB) - added by jblz 6 years ago.
Inverts the semantics of the filter (block vs. allow), pass $autosave as the default value, pass $post_id, include unit test
44786.withtests.2.patch (3.8 KB) - added by jblz 6 years ago.
Change filter name -- "prevent" instead of "block" to reduce confusion

Download all attachments as: .zip

Change History (10)

#1 in reply to: ↑ description @jblz
6 years ago

Replying to jblz:

...calling wp_save_post_revision immediately exits.

Should say ...immediately returns instead. (ref)

Last edited 6 years ago by jblz (previous) (diff)

This ticket was mentioned in Slack in #core by jblz. View the logs.


6 years ago

#3 @adamsilverstein
6 years ago

  • Keywords needs-unit-tests added

Hey @jblz - I think the filter is fine here. A unit test that validated the filter would be nice.

Note that the use case you describe "to occasionally save revisions for drafts during an autosave call" is similar to what we have discussed a little and started working on in https://core.trac.wordpress.org/ticket/43316?cversion=0&cnum_hist=65#comment:32 - it would be great to have something like this built into the core autosave endpoint.

@jblz
6 years ago

Inverts the semantics of the filter (block vs. allow), pass $autosave as the default value, pass $post_id, include unit test

#4 @jblz
6 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed

Thanks for taking a look, @adamsilverstein & for pointing me at #43316.

it would be great to have something like this built into the core autosave endpoint.

+1. It looks like WP_REST_Autosaves_Controller::create_item is in sync w/ Gutenberg -- I'll follow up w/ a PR there so we can see the usage in action a bit.

The latest patch introduces a new test to cover the filter.


@jorbin Thanks for the suggestions.

I inverted the filter, chose a different filter name that is more in line with the rest of the surrounding code, & included the $post_id in the filter application.

#5 @jblz
6 years ago

Gutenberg autosaves usage patch in progress here: https://github.com/WordPress/gutenberg/pull/9043

@jblz
6 years ago

Change filter name -- "prevent" instead of "block" to reduce confusion

#6 @azaozz
6 years ago

  • Keywords reporter-feedback added

@jblz as @adamsilverstein mentions above we've discussed this in core before and came up with a bit more "expanded" implementation, see #43760. It checks the changes/differences between the previously saved post data and the autosave, and creates a post revision. That check is of course filterable, and seems to do pretty much the same as the patch on this ticket.

Can you have a look at https://core.trac.wordpress.org/attachment/ticket/43760/43760.patch and see if it fits the needs for this ticket too? If yes, perhaps we can close this in favour of #43760.

#7 @pento
6 years ago

  • Version trunk deleted
Note: See TracTickets for help on using tickets.