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

First Revision trigger #17

Closed
joelstransky opened this issue Apr 23, 2016 · 7 comments
Closed

First Revision trigger #17

joelstransky opened this issue Apr 23, 2016 · 7 comments

Comments

@joelstransky
Copy link

Create new post, enter title and a custom field. Publish.
Change custom field value. Update

Expected: See Revisions link in Publish meta box.
Results: No Revisions link.

Revisions are not available until a change is made in the native fields.

@tschortsch
Copy link

tschortsch commented Jul 6, 2016

How do you add your meta fields? If you are using a plugin (like CMB2, carbon-fields, ...) they most likely rely on the save_post hook to store the meta fields in the database.
This plugin hooks into the wp_save_post_revision_post_has_changed filter to check if a meta field was changed since last save. This filter gets called by the the wp_save_post_revision() method which again gets called by the post_updated action.
This means that the check if something has changed in your meta fields (which compares the values from the database revisions) gets called before the new fields are stored in the database.

If you're changing a core field (like content) besides a meta field a revision should be created also for the meta field.

For me this is still the root cause I can't really use this plugin to revision meta fields. I also haven't found a proper solution for this since revisions are only created in the post_updated action in the core and all the plugins rely on the save_post hook.

@adamsilverstein
Copy link
Owner

@tschortsch that's a good description of the current situation. One note is that 'save_post' fires twice on a normal save - once for the post and once for the revision. CPT hooks only fire once though (save_post_{post_type}). If you can control it, hook your meta updates on the update hook instead of the save hook. Otherwise, you will find meta is one revision 'behind' the content. Ultimately I think we need an additional hook in core so we can save post meta after plugins update it, getting back to working on that soon!

@adamsilverstein
Copy link
Owner

adamsilverstein commented Feb 1, 2017

@joelstransky I was actually unable to reproduce the issue you described. Was this on a custom post type? did you add the revisioned meta field to the whitelist using the filter?

Here is a screencast where I try the steps you mentioned:

  1. new post with a title ->publish
  2. add to the custom 'my_meta' field -> update

Revisions shows up in publish meta box. I click on revisions, restore the first revision. The meta is gone correctly. I open the revisions browser and restore the 2nd revision, meta is correctly reset to the revisioned value (currently does not show in the revisions ui, working on that).

https://cl.ly/3o0W063C1Y1r/Screen%20Recording%202017-02-01%20at%2001.04%20PM.gif

@joelstransky
Copy link
Author

Interesting. It's been a while since I've looked at this. I'd have go back and look. Not something I'll have time to do in the near future.

@tschortsch
Copy link

I think I found a workaround for the timing issue I described before:

// do not save revision on post_updated since the meta fields are not saved yet.
remove_action( 'post_updated', 'wp_save_post_revision', 10 );

// readd wp_save_post_revision on save_post if needed.
add_action( 'post_updated', 'save_post_revision_on_save_post', 10 );
function save_post_revision_on_save_post() {
	$mycustomfieldsplugin_save_post_priority = 10;

	// wp_save_post_revision has to be executed after the plugin saves its meta fields (higher priority on save_post hook).
	add_action( 'save_post', 'wp_save_post_revision', $mycustomfieldsplugin_save_post_priority + 1, 1 );
}

With this it's not needed to change anything in the core directly. I think it should work with every Custom Field plugin which relies on the save_post hook to save it's meta fields (eg. Custom-Meta-Boxes, CMB2, Carbon Fields).

The only thing which has to be adjusted is the $mycustomfieldsplugin_save_post_priority value. This variable should be set to the priority of the save_post hook of the used plugin.

@adamsilverstein
Copy link
Owner

Great workaround @tschortsch - thanks! made a note of this for the future.

@thenahidul
Copy link

I think I found a workaround for the timing issue I described before:

// do not save revision on post_updated since the meta fields are not saved yet.
remove_action( 'post_updated', 'wp_save_post_revision', 10 );

// readd wp_save_post_revision on save_post if needed.
add_action( 'post_updated', 'save_post_revision_on_save_post', 10 );
function save_post_revision_on_save_post() {
	$mycustomfieldsplugin_save_post_priority = 10;

	// wp_save_post_revision has to be executed after the plugin saves its meta fields (higher priority on save_post hook).
	add_action( 'save_post', 'wp_save_post_revision', $mycustomfieldsplugin_save_post_priority + 1, 1 );
}

With this it's not needed to change anything in the core directly. I think it should work with every Custom Field plugin which relies on the save_post hook to save it's meta fields (eg. Custom-Meta-Boxes, CMB2, Carbon Fields).

The only thing which has to be adjusted is the $mycustomfieldsplugin_save_post_priority value. This variable should be set to the priority of the save_post hook of the used plugin.

This works perfectly unless there's no ACF filed exist. For the ACF field, the updated field appears in the previous revision. Any solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants