WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 months ago

#20564 reopened enhancement

Framework for storing revisions of Post Meta

Reported by: alexkingorg Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.4
Component: Revisions Keywords:
Focuses: Cc:

Description

There are a couple of tickets that would seem to benefit from storing revisions of post meta (#11049, #20299). We had a need for this feature in our Carrington Build and RAMP products and built it a few years back.

It handles the storing of post meta along with revisions, as well as restoring those revisions when someone restores an older version of a post(/page/etc.). There is an API for registering the post meta keys you want to track revisions of, it does not track all post meta keys by default.

There are two versions of the code. The bare-bones revisions framework code is here:

https://github.com/crowdfavorite/wp-revision-manager
https://github.com/crowdfavorite/wp-revision-manager/blob/master/cf-revision-manager.php

while I started adding features to a version that would be more user-friendly plugin, allowing the users to select which post meta keys they want to track revisions for:

http://plugins.svn.wordpress.org/revision-manager/
http://plugins.svn.wordpress.org/revision-manager/trunk/cf-revision-manager.php

If this would be a valuable addition to core, I'd be happy to help polish this up in whatever manner is most helpful. I'd recommend breaking out the code I started on for the admin form and having that be the extent of the "revision manager" plugin - basically it becomes a nicer UI for selecting additional post meta keys to track revisions for.

Attachments (38)

20564.diff (5.8 KB) - added by adamsilverstein 2 years ago.
20564+16215.diff (24.3 KB) - added by adamsilverstein 2 years ago.
for testing
20564.2.diff (5.5 KB) - added by adamsilverstein 2 years ago.
updated against current trunk
20564.3.diff (5.5 KB) - added by adamsilverstein 2 years ago.
update against current
20564.4.patch (3.5 KB) - added by WraithKenny 2 years ago.
20564.5.patch (4.5 KB) - added by WraithKenny 2 years ago.
20564.4.diff (4.2 KB) - added by kovshenin 2 years ago.
20564.5.diff (2.5 KB) - added by kovshenin 2 years ago.
20564-6.patch (8.3 KB) - added by azaozz 2 years ago.
20564.6.diff (3.8 KB) - added by kovshenin 2 years ago.
20564.7.diff (4.0 KB) - added by kovshenin 2 years ago.
20564-8.patch (6.3 KB) - added by azaozz 2 years ago.
20564-9.patch (7.0 KB) - added by adamsilverstein 21 months ago.
reintroduce changes removed in 24397
20564-10.diff (11.7 KB) - added by adamsilverstein 17 months ago.
includes unit test
20564-11.diff (11.0 KB) - added by adamsilverstein 17 months ago.
remove unused post format meta values from default array and tests
20564-12.diff (11.1 KB) - added by adamsilverstein 16 months ago.
slight test cleanup
20564-13.diff (11.3 KB) - added by adamsilverstein 16 months ago.
20564-14.diff (9.1 KB) - added by adamsilverstein 16 months ago.
remove post format revisioning
20564.8.diff (9.9 KB) - added by DrewAPicture 16 months ago.
+ docs fixes
20564.9.diff (10.7 KB) - added by adamsilverstein 14 months ago.
Refresh against trunk. remove redundant restore block; update unit tests
20564.10.diff (11.2 KB) - added by adamsilverstein 14 months ago.
add wp_unslash to $_POST comparison
20564-autosave-data.png (148.4 KB) - added by TV productions 14 months ago.
The data that an autosave sent in wp 3.9-alpha-27445-src
20564.11.diff (11.2 KB) - added by adamsilverstein 14 months ago.
Refresh against trunk
20564.12.diff (9.9 KB) - added by adamsilverstein 12 months ago.
refresh against trunk
20564.13.diff (10.3 KB) - added by adamsilverstein 10 months ago.
refresh against trunk
20564.14.diff (12.9 KB) - added by adamsilverstein 10 months ago.
only save changed revisions
20564.15.diff (11.3 KB) - added by adamsilverstein 10 months ago.
don't save blank meta, track revisioned keys
20564.16.diff (12.0 KB) - added by adamsilverstein 10 months ago.
update unit test, restore revision with untracked key
20564.17.diff (12.9 KB) - added by adamsilverstein 10 months ago.
account for the storing of multiple values per key
20564.18.diff (12.9 KB) - added by adamsilverstein 9 months ago.
refresh against trunk
20564.19.diff (12.9 KB) - added by adamsilverstein 7 months ago.
refresh
20564-new-action.diff (537 bytes) - added by adamsilverstein 6 months ago.
hook for autosave
20564-new-action.2.diff (530 bytes) - added by adamsilverstein 6 months ago.
pass entire $new_autosave array
20564.20.diff (7.8 KB) - added by adamsilverstein 6 months ago.
add one filter, unit tests
20564.21.diff (8.2 KB) - added by adamsilverstein 6 months ago.
Adds hook docs
30033.diff (739 bytes) - added by mattheu 6 months ago.
20564.22.diff (8.7 KB) - added by mattheu 6 months ago.
20564.23.diff (2.3 KB) - added by mattheu 6 months ago.

Download all attachments as: .zip

Change History (164)

comment:1 @scribu3 years ago

  • Cc scribu added

comment:2 @toscho3 years ago

  • Cc info@… added

comment:3 @rspindel3 years ago

  • Cc rspindel added

comment:4 @jkudish3 years ago

  • Cc joachim.kudish@… added

comment:5 @Ipstenu3 years ago

  • Cc ipstenu@… added

comment:6 @kovshenin3 years ago

  • Cc kovshenin@… added

comment:7 @hameedullah3 years ago

  • Cc h@… added

comment:8 @ryanduff3 years ago

  • Cc ryan@… added

comment:9 @maor3 years ago

  • Cc maorhaz@… added

comment:10 @travisnorthcutt3 years ago

  • Cc travis@… added

comment:11 @sc0ttkclark3 years ago

  • Cc lol@… added

comment:12 @xyzzy3 years ago

  • Cc dennen@… added

comment:13 @lightningspirit3 years ago

  • Cc lightningspirit@… added

+1

comment:14 @swissspidy2 years ago

  • Cc hello@… added

comment:15 @sennza2 years ago

  • Cc bronson@… added

comment:16 @jaredatch2 years ago

  • Cc jared@… added

comment:17 @bainternet2 years ago

  • Cc admin@… added

comment:18 @jb5102 years ago

  • Cc jbrown510@… added

comment:19 follow-up: @nacin2 years ago

  • Milestone changed from Awaiting Review to 3.6

This and #20299 would block #23314.

comment:20 @johnbillion2 years ago

This and #20299 also potentially block the no-JS fallback for #19570, although MarkJaquith mentioned the possibility of not allowing a post to switch formats after the fact if you have no JS.

Last edited 2 years ago by johnbillion (previous) (diff)

comment:21 in reply to: ↑ 19 ; follow-up: @adamsilverstein2 years ago

Replying to nacin:

This and #20299 would block #23314.

will bring this back into scope, have you reviewed the code attached? if it looks reasonable i can work to apply.

comment:22 @adamsilverstein2 years ago

  • Cc adamsilverstein@… added

comment:23 @goto102 years ago

  • Cc dromsey@… added

comment:24 in reply to: ↑ 21 @westi2 years ago

Replying to adamsilverstein:

Replying to nacin:

This and #20299 would block #23314.

will bring this back into scope, have you reviewed the code attached? if it looks reasonable i can work to apply.

It's still outside the scope that the revisions team will primarily focus on.

comment:25 @kwight2 years ago

  • Cc kwight@… added

comment:26 follow-up: @markjaquith2 years ago

From johnbillion, a very simple take: https://gist.github.com/johnbillion/5225514

comment:27 in reply to: ↑ 26 @adamsilverstein2 years ago

Replying to markjaquith:

From johnbillion, a very simple take: https://gist.github.com/johnbillion/5225514

i have a patch brewing for this ticket and will post for your review monday.

@adamsilverstein2 years ago

@adamsilverstein2 years ago

for testing

comment:28 @adamsilverstein2 years ago

20564.diff

  • adds new wp_save_post_revision_meta() - called in _wp_put_post_revision
  • adds wp_restore_post_revision_meta() - added in restore action
  • also stores post format as revision meta (and restores on revision restore), seemed to be best way to store post_format
  • requires http://core.trac.wordpress.org/attachment/ticket/16215/16215.9.diff to work correctly (stores meta on copy of current revision, only present after patch)
  • wp_revisions_keep_meta filter to override storing of meta
  • initial testing verified post format and related meta stored and restored correctly

20564+16215.diff​ combined patch for testing

@adamsilverstein2 years ago

updated against current trunk

@adamsilverstein2 years ago

update against current

comment:29 follow-up: @markjaquith2 years ago

We need the exact opposite approach of this patch. :-)

We should have a list of meta keys that we WILL revision, not a list of ones we WON'T. There should be no filter to opt out of revisioning of post format postmeta.

comment:30 in reply to: ↑ 29 @adamsilverstein2 years ago

Replying to markjaquith:

We need the exact opposite approach of this patch. :-)

We should have a list of meta keys that we WILL revision, not a list of ones we WON'T. There should be no filter to opt out of revisioning of post format postmeta.

ok, this was the method suggested in #20299 so i went with it. a possible advantage of this method is it will catch new meta options as they are added or used, but i could also see how it could catch fields you didn't need.

i can change the method and include all current used metas, maybe add a filter for plugin authors to register new metas for revisioning?

comment:31 @WraithKenny2 years ago

  • Cc Ken@… added

comment:32 @WraithKenny2 years ago

I think we just need a few new wrapper functions (like add_versioned_meta(), update_versioned_meta(), and delete_versioned_meta() ) that don't check against wp_is_post_revision(). Using these would "opt in" to metadata on revisions. (of course you could currently just use *_metadata('post'...) functions, no?)

then on 'wp_restore_post_revision' (with 2 args)

$meta = get_post_meta( $revision_id, $key );
if ( empty( $meta ) )
    delete_post_meta( $post_id, $key );
else
    update_post_meta( $post_id, $key, $meta );
// or whatnot.

which is pretty straight forward (and also "opt in").
Optionally, core can hook onto 'wp_restore_post_revision' and loop thru a filterable array of "versioned" meta so that the following could work:

add_filter( 'versioned_meta', 'my_versioned_meta' );
function my_versioned_meta( $meta_keys ) {
    $meta_keys[] = 'my_key';
    return $meta_keys;
}

@WraithKenny2 years ago

comment:33 @WraithKenny2 years ago

patch introduces add_versioned_meta(), update_versioned_meta(), and delete_versioned_meta() adds the filter 'versioned_meta' and handles restoring versioned meta data in wp_restore_post_revision() Lightly tested.

comment:34 @WraithKenny2 years ago

  • Keywords has-patch needs-testing added

comment:35 follow-up: @markjaquith2 years ago

I don't think we're ready to bless this for plugin use. As you noted, they can just use the lower-level functions if they know what they're doing. So for this go-around, I'd skip wrapping it up all pretty. Taking a look at your patch now.

@WraithKenny2 years ago

comment:36 @WraithKenny2 years ago

new patch adds get_versioned_meta_keys

comment:37 in reply to: ↑ 35 @WraithKenny2 years ago

Replying to markjaquith:

I don't think we're ready to bless this for plugin use. As you noted, they can just use the lower-level functions if they know what they're doing. So for this go-around, I'd skip wrapping it up all pretty. Taking a look at your patch now.

Yeah, that's cool. I was just working on this because it blocks #20299 which in turn blocks a couple other interesting tickets like #23314 and #23539

Figured if I got a couple working patches up it might move those next round.

@kovshenin2 years ago

comment:38 @kovshenin2 years ago

Taking a stab at this in 20564.4.diff. The patch is quite self-explanatory.

@kovshenin2 years ago

comment:39 @kovshenin2 years ago

20564.5.diff is same as .4, except the *_post_meta functions hi-jacking. Uses the underlying add_metadata where applicable.

comment:40 @markjaquith2 years ago

In 23859:

Revision our post format postmeta.

props kovshenin, WraithKenny. see #20564.

comment:41 @DrewAPicture2 years ago

  • Keywords needs-codex added
Version 0, edited 2 years ago by DrewAPicture (next)

comment:42 follow-up: @azaozz2 years ago

Been seeing this lately in the postmeta table, post IDs 9212, 9213 and 9214 are revisions and all meta is empty (no values):

4040 	9214 	_wp_format_video 	
4039 	9214 	_wp_format_audio 	
4038 	9214 	_wp_format_image 	
4037 	9214 	_wp_format_quote_source 	
4036 	9214 	_wp_format_quote 	
4035 	9214 	_wp_format_url 	
4032 	9213 	_wp_format_video 	
4031 	9213 	_wp_format_audio 	
4030 	9213 	_wp_format_image 	
4029 	9213 	_wp_format_quote_source 	
4028 	9213 	_wp_format_quote 	
4027 	9213 	_wp_format_url 	
4022 	9212 	_wp_format_video 	
4021 	9212 	_wp_format_audio 	
4020 	9212 	_wp_format_image 	
4019 	9212 	_wp_format_quote_source 	
4018 	9212 	_wp_format_quote 	
4017 	9212 	_wp_format_url 	

Seems we are doing isset() and should be doing ! empty() somewhere.

comment:43 @WraithKenny2 years ago

its the if ( false === $meta_values ) line I think

@azaozz2 years ago

comment:44 @azaozz2 years ago

In 20564-6.patch:

  • When saving revisions (including autosaves): save the current post format as revision meta.
  • When restoring from a revision, restore the format too.
  • When saving revisions: add post format meta to the revision post only for the current post format, skip when empty.
Last edited 2 years ago by azaozz (previous) (diff)

comment:45 in reply to: ↑ 42 @kovshenin2 years ago

Replying to azaozz: The original post contains all those meta keys with empty values as well, and the revision post is a complete copy, so I think that's the expected behavior. I also think that there should be a more general approach for all possible meta, rather than post formats only. Thoughts?

comment:46 @azaozz2 years ago

The original post contains all those meta keys with empty values as well...

Not sure that's needed either.

...the revision post is a complete copy, so I think that's the expected behavior.

No, not a complete copy. It's a copy of the post data, most fields are not revisioned. By default only title, content and excerpt are. That's why we have _wp_post_revision_fields().

Imagine a small site with 100 posts, each having 10 revisions. That's (1,100 * 7) nearly 8,000 rows in the post meta table just for post formats. And most will be empty. Now imagine a bigger site with 20,000 posts and 50 revisions each (that's not uncommon). Nearly 8,000,000 rows. And now imagine a really big site... :)

I also think that there should be a more general approach for all possible meta, rather than post formats only.

Yeah, when we open the API to revision post meta (agree with @markjaquith it's out of scope at the moment), thinking it should be opt-in. A lot of the post meta doesn't need to be saved in revisions.

comment:47 @kovshenin2 years ago

I'm pretty sure it's been mentioned somewhere before, but we can probably store all the revision meta in a single serialized _revision_meta key. I agree that adding keys to revisioned meta should be opt-in and that it's out of scope for 3.6, I just dislike the post-format-ish approach at _wp_post_revision_meta_keys(), and the $single assumption in get_post_meta :)

comment:48 follow-up: @azaozz2 years ago

We could, but having separate meta has some advantages. We could do a query to select posts by these meta fields which might be useful in some cases. Also that makes post formats more "futureproof".

Yes, currently the revisions meta is only for post formats, and the fields can only be "single" there. It's not very hard to turn parts of it into an API, perhaps 3.7?

@kovshenin2 years ago

comment:49 in reply to: ↑ 48 ; follow-up: @kovshenin2 years ago

Replying to azaozz: I think what Aaron meant in his comment is useful for posts only, but not really useful for revisions. I took a stab at the _revision_meta approach in 20564.6.diff which should keep the meta table clean and small for revisions. If this works out we can do the same for revisioned terms (#23893). Not really looking to fix this ticket, but rather lay a good foundation for whatever is next in 3.7 and 3.8.

P.S. Sorry for the .6 vs -6 naming conflict, that's trac's auto-naming kicking in :)

comment:50 @WraithKenny2 years ago

The only drawback I can think of for serializing all the meta into one field is size concerns. For formats, it shouldn't be a problem, but if it's opened up later as an API, what if some meta are very large? Potentially, the values would be saved into individual keys, but when combined they maybe too big to store... no? It's an edge case, but who knows.

comment:51 follow-up: @WraithKenny2 years ago

For the unique key issue, if the key is actually unique, then get_post_meta( $revision['ID'], $meta_key ) should only have one value in the array making it unneeded to pass a unique flag to add_post_meta (since the loop only goes once). I had a unique param in my patch originally, but after I saw the commit I thought it unnecessary. Is there a different unique assumption that I'm missing?

comment:52 @travisnorthcutt2 years ago

  • Cc travis@… removed

comment:53 in reply to: ↑ 49 ; follow-up: @azaozz2 years ago

Replying to kovshenin:

I took a stab at the _revision_meta approach in 20564.6.diff which should keep the meta table clean and small for revisions. If this works out we can do the same for revisioned terms (#23893).

Yeah, this works for post formats, not so sure for all meta (only need to add restoring of the post format from 20564-6.patch, perhaps to the same array). Still, having empty keys in the serialized array seems wasteful :)

As long as we know the post supports formats, we can recreate the empty keys programmatically, no need to store them in the DB. Example:

$all_meta = array_merge(
  array(
	'_wp_format_url' => '',
	'_wp_format_quote' => '',
	'_wp_format_quote_source' => '',
	...
  ),
  get_post_meta( $revision->ID, '_revision_meta', true )
);

This brings another interesting question: when restoring from a revision, do we overwrite all data stored in the main post's formats meta? If yes, do empty meta keys from the revision overwrite (i.e. remove) the corresponding values on the main post?

User case:

  • User creates a post, no format set.
  • User saves a draft creating a revision.
  • User edits the post, sets format to quote, enters data in 'url' and 'quote' meta fields.
  • User restores to the first revision. At this point do we delete the data entered in 'url' and 'quote' on the main post or do we keep it? Logically we should delete it, but if we are keeping all post formats data ever entered on the main post, the user may expect to find it if he sets the format to quote again.
Last edited 2 years ago by azaozz (previous) (diff)

comment:54 in reply to: ↑ 51 @azaozz2 years ago

Replying to WraithKenny:

...if the key is actually unique, then get_post_meta( $revision['ID'], $meta_key ) should only have one value in the array making it unneeded to pass a unique flag to add_post_meta.

True, but we will need to do $meta[0] everywhere. In the current code post formats meta doesn't support multiple keys, better to use the meta API properly and get/set a single key.

comment:55 in reply to: ↑ 53 @kovshenin2 years ago

Replying to azaozz:

Only need to add restoring of the post format from 20564-6.patch, perhaps to the same array.

That's taxonomy and terms, not post meta, hence the similar patch in #23893 for taxonomy. If the the serialized _revision_meta approach works well for meta, we can take a similar approach with terms and not "pollute" the term_relationships table with relations to revisions :)

Still, having empty keys in the serialized array seems wasteful :)

Maybe. We can indeed check the emptiness of meta before storing their revisions to save some space, but I have mixed feelings about it. I don't think it will affect post formats all that much, but I keep thinking that a non-existent meta value is not the same as a stored empty string, especially for multiple values under one key. If there was a $default argument to get_post_meta like there is for get_option, not storing the empty values would be a deal-breaker. So I lean towards doing delete_post_meta for empty strings when saving post format metadata. Just thinking out loud here.

This brings another interesting question: when restoring from a revision, do we overwrite all data stored in the main post's formats meta? If yes, do empty meta keys from the revision overwrite (i.e. remove) the corresponding values on the main post?

I can try and answer this with a similar question :) When restoring from a revision, do empty fields (title or content) overwrite the corresponding values in the main post? Create a post with no title, save a few revisions, give it a title, save and restore a previous revision: do we set the title back to an empty string? The answer is yes, and I think post formats (and revisioned post meta in general) should not be any different.

@kovshenin2 years ago

comment:56 @kovshenin2 years ago

In 20564.7.diff:

  • Merged some of the things from Andrew's patch
  • Switched to single value lookups
  • Save and retrieve post format via a _revision_post_format meta
  • Filters get_the_terms to show the revisioned post format during previews
  • Doesn't store empty meta values for revisions

This also covers #23893

comment:57 @azaozz2 years ago

We can indeed check the emptiness of meta before storing their revisions to save some space, but I have mixed feelings about it.

What I mean is at the lowest level: we know the expected keys, so array( 'a' => '', 'b' => '', 'c' => 'something' ); doesn't have to be serialized, saved to the db and unserialized again when getting the data. We can remove the empty keys, save array( 'c' => 'something' ); and then add the empty keys on getting the data. This will work in exactly the same way as saving the original array.

The same is true when getting/setting the post format meta on revision posts. We know there are 7 meta keys, no need to add the empty.

I can try and answer this with a similar question :) When restoring from a revision, do empty fields (title or content) overwrite the corresponding values in the main post?

That's not exactly the same. From the discussion on #19570, we are keeping the unused data for post formats. Basically this is irrelevant post meta unless the user decides to switch back to a previously used post format. The question here is: do we "roll back" that irrelevant data when restoring a revision, and yes, logically we should.

Last edited 2 years ago by azaozz (previous) (diff)

@azaozz2 years ago

comment:58 @azaozz2 years ago

20564-8.patch builds on 20564.7.diff​ and adds updating of post format and post format meta for autosaves.

comment:59 @kovshenin2 years ago

20564-8.patch looks good and works like a charm!

comment:60 @azaozz2 years ago

In 23928:

Revisions:

  • Store the post format as meta on revisions (including autosaves).
  • Add post formats data (post meta) when autosaving.
  • Only add non-empty post formats data to revisions.
  • Correct the post format when previewing a published post.

Props kovshenin, see #19570, see #20564.

comment:61 @kovshenin2 years ago

  • Keywords has-patch removed

@azaozz thanks for the commit! One little thing I noticed is the @since version in the docblock of the new _wp_preview_terms_filter function ([23928]). It says 2.6 but should be 3.6. I mistyped that in my patch, sorry!

comment:62 @ryan2 years ago

[23936] for @since update.

comment:63 @adamsilverstein2 years ago

  • Resolution set to fixed
  • Status changed from new to closed

comment:64 @helen21 months ago

This is so not fixed. Re-open or make new?

comment:65 follow-up: @alexkingorg21 months ago

This ticket seemed to get oddly intertwined with the Post Formats data - perhaps a new ticket would allow it to better exist as a stand-alone framework feature?

comment:66 @adamsilverstein21 months ago

  • Keywords needs-refresh added
  • Resolution fixed deleted
  • Status changed from closed to reopened

i think the code from [23928] will work will work as is. re-opening and will refresh the patch against trunk for testing.

Last edited 21 months ago by SergeyBiryukov (previous) (diff)

comment:67 @SergeyBiryukov21 months ago

  • Milestone changed from 3.6 to Future Release

@adamsilverstein21 months ago

reintroduce changes removed in 24397

comment:68 @adamsilverstein21 months ago

20564-9.patch​ first pass at reintroducing changes removed in [24397].

some small changes:

  • updated selector in autosave.js to current ui selector scheme - seems like a JS hook here would make sense, so plugin developers could use their own UI scheme for post formats
  • includes fix from #24477 (from itthinx)
Last edited 21 months ago by SergeyBiryukov (previous) (diff)

comment:69 @jgwspud21 months ago

  • Cc jgwspud added

comment:70 @c3mdigital21 months ago

#23973 was marked as a duplicate.

comment:71 @doughamlin20 months ago

  • Cc doughamlin@… added

comment:72 @doughamlin20 months ago

  • Cc doughamlin@… removed

comment:73 @doughamlin20 months ago

  • Cc doughamlin@… added

comment:74 in reply to: ↑ 65 @WraithKenny20 months ago

Replying to alexkingorg:

This ticket seemed to get oddly intertwined with the Post Formats data - perhaps a new ticket would allow it to better exist as a stand-alone framework feature?

Indeed, now that post-formats has been tabled, this ticket should revert to it's original intent, a General Framework for saving and restoring metadata from revisions. (Implementation shouldn't focus on post-format data.)

Some notes and considerations collected from various places:

  1. Patches derived from the existing, but removed code, should be abstracted to allow for general use.
  2. Autosaves/Previews needed to be considered: see #20299 #23539
  3. This could enable the feature here: #23314

comment:75 @sillybean20 months ago

  • Cc steph@… added

comment:76 @donutz19 months ago

  • Cc peterherrel@… added

comment:77 @jgwspud19 months ago

  • Cc jgwspud removed

@adamsilverstein17 months ago

includes unit test

comment:78 @adamsilverstein17 months ago

lets revision some post meta!
20564-10.diff​ includes a unit test to verify the meta revisioning and filter functionality as well as improved inline hook docs.

@adamsilverstein17 months ago

remove unused post format meta values from default array and tests

comment:79 @adamsilverstein17 months ago

20564-11.diff​ - removes unused post format meta values - now revisions only 'post_format' itself, plus any meta values added via the wp_post_revision_meta_keys filter

@adamsilverstein16 months ago

slight test cleanup

@adamsilverstein16 months ago

comment:80 @adamsilverstein16 months ago

20564-13.diff​:

  • refresh against trunk
  • added unit test for revisioning of post_format
  • keeping post_format revisioning since post_format a default post meta field

@adamsilverstein16 months ago

remove post format revisioning

comment:81 @adamsilverstein16 months ago

20564-14.diff​:

  • remove all traces of post formats, no more post format revisioning

@DrewAPicture16 months ago

+ docs fixes

comment:82 @DrewAPicture16 months ago

  • Keywords needs-refresh removed
  • Version set to 3.4

20564.8.diff implements docs fixes for 20564-14.diff as requested.

comment:83 @ircbot16 months ago

This ticket was mentioned in IRC in #wordpress-dev by adamsilverstein. View the logs.

comment:85 @ircbot15 months ago

This ticket was mentioned in IRC in #wordpress-dev by adamsilverstein. View the logs.

comment:86 @ircbot14 months ago

This ticket was mentioned in IRC in #wordpress-dev by danielbachhuber. View the logs.

@adamsilverstein14 months ago

Refresh against trunk. remove redundant restore block; update unit tests

comment:87 @adamsilverstein14 months ago

in 20564.9.diff: refreshed against trunk; removed what appears to be redundant meta restore block; updated unit test.

@adamsilverstein14 months ago

add wp_unslash to $_POST comparison

comment:88 @adamsilverstein14 months ago

  • Keywords dev-feedback added

20564.10.diff corrects issue johnbillion pointed out in IRC - $_POST requires wp_unslash in wp_create_post_autosave or the comparison may fail.

comment:89 @ircbot14 months ago

This ticket was mentioned in IRC in #wordpress-dev by adams|mobile. View the logs.

comment:90 @ircbot14 months ago

This ticket was mentioned in IRC in #wordpress-dev by danielbachhuber. View the logs.

comment:91 follow-up: @TV productions14 months ago

For the ability for plugin devs to add custom meta to custom post type revisions (#13382), you do also need a kind of javascript "filter" for the data that is send with the autosave. Something like this http://jsfiddle.net/jTjzK/3/ (just a concept to extend the data var).

comment:92 in reply to: ↑ 91 ; follow-up: @adamsilverstein14 months ago

Replying to TV productions:

For the ability for plugin devs to add custom meta to custom post type revisions (#13382), you do also need a kind of javascript "filter" for the data that is send with the autosave. Something like this http://jsfiddle.net/jTjzK/3/ (just a concept to extend the data var).

I'm not sure this is required. All fields should be 'sent' with the auto update post, and the attached patch filters these fields and revisions ones that are specified. All fields are sent, and the patch allows authors to specify which fields to revision, Does that make sense? If not can you please explain a bit more what you would want to revision that the current patch would not work for?

comment:93 @ircbot14 months ago

This ticket was mentioned in IRC in #wordpress-dev by adamsilverstein. View the logs.

@TV productions14 months ago

The data that an autosave sent in wp 3.9-alpha-27445-src

comment:94 in reply to: ↑ 92 ; follow-up: @TV productions14 months ago

Replying to adamsilverstein:

Replying to TV productions:

For the ability for plugin devs to add custom meta to custom post type revisions (#13382), you do also need a kind of javascript "filter" for the data that is send with the autosave. Something like this http://jsfiddle.net/jTjzK/3/ (just a concept to extend the data var).

I'm not sure this is required. All fields should be 'sent' with the auto update post, and the attached patch filters these fields and revisions ones that are specified. All fields are sent, and the patch allows authors to specify which fields to revision, Does that make sense? If not can you please explain a bit more what you would want to revision that the current patch would not work for?

Just to check, I've pulled the latest code form the repo and applied the latest patch (20564.10.diff).
As you can see in the attached image The data that an autosave sent in wp 3.9-alpha-27445-src the default meta fields are saved.

But what I would like to see is a way to "prepaire" the data that will be saved into the meta field. The post type in the image is a photo album, and instead of "normal" content it saves some image data and the server generates the post_content from this meta field. So I don't have one html input field that matches an meta field, but a lot. There are different solutions to this: (1) create a field that does matches the meta field and that is updated by javascript on autosave (so you need a hook for that) or (2) a "jsfilter" to generate the data and pass it trough.

As I write this, I note that it is kinda focussed to my example, but what I actually would say is: could we make an autosave API where you can add data on client side and extract (and save) it on the server side?

comment:95 in reply to: ↑ 94 ; follow-up: @adamsilverstein14 months ago

Replying to TV productions:

Replying to adamsilverstein:

Replying to TV productions:

For the ability for plugin devs to add custom meta to custom post type revisions (#13382), you do also need a kind of javascript "filter" for the data that is send with the autosave. Something like this http://jsfiddle.net/jTjzK/3/ (just a concept to extend the data var).

I'm not sure this is required. All fields should be 'sent' with the auto update post, and the attached patch filters these fields and revisions ones that are specified. All fields are sent, and the patch allows authors to specify which fields to revision, Does that make sense? If not can you please explain a bit more what you would want to revision that the current patch would not work for?

Just to check, I've pulled the latest code form the repo and applied the latest patch (20564.10.diff).
As you can see in the attached image The data that an autosave sent in wp 3.9-alpha-27445-src the default meta fields are saved.

But what I would like to see is a way to "prepaire" the data that will be saved into the meta field. The post type in the image is a photo album, and instead of "normal" content it saves some image data and the server generates the post_content from this meta field. So I don't have one html input field that matches an meta field, but a lot. There are different solutions to this: (1) create a field that does matches the meta field and that is updated by javascript on autosave (so you need a hook for that) or (2) a "jsfilter" to generate the data and pass it trough.

As I write this, I note that it is kinda focussed to my example, but what I actually would say is: could we make an autosave API where you can add data on client side and extract (and save) it on the server side?

Thank you for the more detailed description and the screenshot.

I think what you are looking for is a way to hook in right before the autosave, or a filter applied to the transmitted autosave data. This type of JavaScript hook or action is currently not standardized in WordPress - see ticket #21170 for more details and a proposed solution.

In the mean time you may be able to hook in using this:

$( document ).on( 'before-autosave', function() { /* Your Function */ } );

(based on this code)

@adamsilverstein14 months ago

Refresh against trunk

comment:96 in reply to: ↑ 95 @TV productions14 months ago

Replying to adamsilverstein:

Replying to TV productions:

Replying to adamsilverstein:

Replying to TV productions:

[...]

[...]

[...]

Thank you for the more detailed description and the screenshot.

I think what you are looking for is a way to hook in right before the autosave, or a filter applied to the transmitted autosave data. This type of JavaScript hook or action is currently not standardized in WordPress - see ticket #21170 for more details and a proposed solution.

In the mean time you may be able to hook in using this:

$( document ).on( 'before-autosave', function() { /* Your Function */ } );

(based on this code)

Okay, thanks for pointing me in the right direction and for the jQuery hook. I am going to dig into #21170.

Last edited 14 months ago by TV productions (previous) (diff)

@adamsilverstein12 months ago

refresh against trunk

comment:97 @adamsilverstein12 months ago

20564.12.diff refresh against trunk, verified unit tests still pass.

comment:98 @ircbot12 months ago

This ticket was mentioned in IRC in #wordpress-dev by adamsilverstein. View the logs.

comment:99 @helen10 months ago

#28553 was marked as a duplicate.

@adamsilverstein10 months ago

refresh against trunk

comment:100 @adamsilverstein10 months ago

20564.13.diff - Refresh against trunk, slightly updated

  • Working on a version that only saves changed revision meta values

@adamsilverstein10 months ago

only save changed revisions

comment:101 @adamsilverstein10 months ago

20564.14.diff

  • Only save metas that have changed
  • Keep a serialized array of the 'whitelisted' (revisioned) meta keys
  • When restoring a revision, traverse back thru past revisions to find the last time the meta was revisioned

While this patch works and the unit tests pass, it seems a little too complicated. I'm going to work on this a bit more - keeping the serialized array of the 'whitelisted' (revisioned) meta keys, but switching to only saving non-blank meta values. This will eliminate the complicated traversal back to find the previously stored meta. We will still be creating rows for each (non-blank) meta which I think is ok as long as we make that fact clear to developers.

@adamsilverstein10 months ago

don't save blank meta, track revisioned keys

comment:102 follow-up: @adamsilverstein10 months ago

In 20564.15.diff:

  • Don't store blank meta values
  • Track meta keys stored for each revision
  • Updated unit test to test restoring blank meta

@adamsilverstein10 months ago

update unit test, restore revision with untracked key

@adamsilverstein10 months ago

account for the storing of multiple values per key

comment:103 @adamsilverstein10 months ago

in 20564.17.diff - account for multiple meta values per key, added test for this, deserves more testing

comment:104 @ircbot10 months ago

This ticket was mentioned in IRC in #wordpress-dev by helen. View the logs.

comment:105 in reply to: ↑ 102 ; follow-up: @p51labs9 months ago

Replying to adamsilverstein:

In 20564.15.diff:

  • Don't store blank meta values
  • Track meta keys stored for each revision
  • Updated unit test to test restoring blank meta

I think that blank meta should be stored if exists on the current post. Sometimes a blank value is the intended value. A case an point would be a field with checkboxes, by setting a blank value I know they purposely didn't select anything from the list.

Thoughts?

Last edited 9 months ago by p51labs (previous) (diff)

comment:106 in reply to: ↑ 105 @adamsilverstein9 months ago

Thanks for the feedback!

The issue is that when retrieving meta, WordPress returns blank if the meta is not set, OR if the meta is blank - so not storing the meta is the same as storing blank meta :) (unless I'm missing something)

the trick here is that because I am tracking which metas are 'revisioned' for each stored revision, i know that a meta that is revisioned and for which no value was stored is in fact blank; therefore, when restoring a revision with a meta that is missing (but which was tracked), that meta is considered blank, and the restored value is set as blank (guess I could just delete it).

Does that make sense? in short, I am tracking blank metas, just not bothering to store them.

Replying to p51labs:

Replying to adamsilverstein:

In 20564.15.diff:

  • Don't store blank meta values
  • Track meta keys stored for each revision
  • Updated unit test to test restoring blank meta

I think that blank meta should be stored if exists on the current post. Sometimes a blank value is the intended value. A case an point would be a field with checkboxes, by setting a blank value I know they purposely didn't select anything from the list.

Thoughts?

Last edited 9 months ago by adamsilverstein (previous) (diff)

comment:107 follow-up: @p51labs9 months ago

Let me expand a little on my previous example. Let's say I have 3 checkboxes, all for one meta_key and the values are 1,2,3. Now by default in my meta box I set 1,3 as checked. Now a user comes in and creates a post and decides none of them should be checked. Now not storing the data is accurate however it doesn't tell me that the user unchecked anything so when I render the field again do I assume I need to load the default values or now leave them all unchecked? I suppose a I could check whether or not the post is new or being edited but is that the best approach?

This solution requires checking for the meta and if the meta key exists.

Just throwing in some thoughts, thanks for taking the time to consider them.

comment:108 in reply to: ↑ 107 @adamsilverstein9 months ago

I think you can solve your problem using metadata_exists to check to the presence of the meta data. I don't think this issue affects the revisioning of post meta, deserves some testing!

Replying to p51labs:

Let me expand a little on my previous example. Let's say I have 3 checkboxes, all for one meta_key and the values are 1,2,3. Now by default in my meta box I set 1,3 as checked. Now a user comes in and creates a post and decides none of them should be checked. Now not storing the data is accurate however it doesn't tell me that the user unchecked anything so when I render the field again do I assume I need to load the default values or now leave them all unchecked? I suppose a I could check whether or not the post is new or being edited but is that the best approach?

This solution requires checking for the meta and if the meta key exists.

Just throwing in some thoughts, thanks for taking the time to consider them.

comment:109 @ircbot9 months ago

This ticket was mentioned in IRC in #wordpress-dev by adamsilverstein. View the logs.

comment:110 @ircbot9 months ago

This ticket was mentioned in IRC in #wordpress-dev by adamsilverstein. View the logs.

@adamsilverstein9 months ago

refresh against trunk

@adamsilverstein7 months ago

refresh

comment:111 follow-up: @adamsilverstein7 months ago

20564.19.diff

  • refresh against trunk
  • verify unit tests pass

comment:112 in reply to: ↑ 111 @Andrew Moawad7 months ago

Last edited 7 months ago by Andrew Moawad (previous) (diff)

comment:113 @ircbot7 months ago

This ticket was mentioned in IRC in #wordpress-dev by johnbillion. View the logs.

comment:114 @ircbot6 months ago

This ticket was mentioned in IRC in #wordpress-dev by adamsilverstein. View the logs.

@adamsilverstein6 months ago

hook for autosave

comment:115 @adamsilverstein6 months ago

I have refactored the latest code on this ticket as a plugin - https://github.com/adamsilverstein/wp-20564

Only one additional hook is required to complete functionality (and only for the or the autosaving of post meta), I added that in 20564-new-action.diff

I haven't tested the plugin carefully yet, my next step is getting the existing unit tests to pass.

@adamsilverstein6 months ago

pass entire $new_autosave array

@adamsilverstein6 months ago

add one filter, unit tests

comment:116 @adamsilverstein6 months ago

One more filter was required: on my list to document these new hooks/filters, the check for change wasn't quite right.

With 20564.20.diff and the plugin ( https://github.com/adamsilverstein/wp-20564 ) metas are revisioned.

I wasn't certain how to run the unit tests when a plugin is required, I found that dropping a copy of wp-20564.php in trunk/tests/phpunit/tests/post/ did the trick for me, with that in place and the patch, all tests pass.

@adamsilverstein6 months ago

Adds hook docs

comment:118 @slackbot6 months ago

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

comment:119 @mattheu6 months ago

I have tested this plugin & related patch and its working well.

I think it is highly desirable for the revisioned meta fields to be included in the revision diff UI.

On this related ticket: #29920 I added a filter that allows revisioned meta fields to be added here. I am updating the patch on this ticket to include this.

I have also made a new version of the plugin to handle this. I will open a PR on github with these changes.

UPDATE - here is the pull request on github: https://github.com/adamsilverstein/wp-20564/pull/2

Last edited 6 months ago by johnbillion (previous) (diff)

@mattheu6 months ago

comment:120 @slackbot6 months ago

This ticket was mentioned in Slack in #core-restapi by rachelbaker. View the logs.

@mattheu6 months ago

comment:121 @mattheu6 months ago

Added the correct patch!

Also thought about the idea of using the current register_meta function - which supports an $args param to register meta keys for revisions.

comment:122 follow-up: @johnbillion6 months ago

  • Milestone changed from Future Release to 4.1

After some discussion at WCSF, moving to 4.1 for the temporary hooks suggested by adamsilverstein and matt_eu above.

comment:123 in reply to: ↑ 122 @mattheu6 months ago

Replying to johnbillion:

After some discussion at WCSF, moving to 4.1 for the temporary hooks suggested by adamsilverstein and matt_eu above.

johnbillion asked me (https://wordpress.slack.com/archives/core/p1414551155002002) to split up the unit tests - however these tests are actually related to the plugin that uses the new hooks and should probably not belong in core. I've updated the patch to remove them.

I will open a PR on github to add the tests to the plugin.

Also added some more documentation.

@mattheu6 months ago

comment:124 @johnbillion6 months ago

In 30091:

Introduce some actions and filters which aid plugins in revisioning post meta.

  • wp_save_post_revision_post_has_changed filter which can be used to determine if a post has been changed, and therefore if a revision should be created for a post.
  • wp_get_revision_ui_diff filter which can be used to filter the fields displayed in the post revision diff UI.
  • wp_creating_autosave action which is fired just before an autosave is created.

See #20564.
Props mattheu, adamsilverstein.

comment:125 @johnbillion6 months ago

  • Keywords needs-testing needs-codex dev-feedback removed
  • Milestone changed from 4.1 to Future Release

comment:126 @adamsilverstein4 months ago

I have bundled up the code for this feature and released to the .org repository, requires trunk or 4.1: https://wordpress.org/plugins/wp-post-meta-revisions/

Please test! Issues and pull requests welcome on the GitHub repo.

comment:127 @slackbot3 months ago

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

Note: See TracTickets for help on using tickets.