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

Support non-unique meta key in autosave #61

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

=== WP-Post-Meta-Revisions ===
* Contributors: adamsilverstein, mattheu, aaemnnosttv
* Requires at least: 4.1
* Requires at least: 4.9.8
* Tested up to: 4.9
* Stable tag: 1.0.0
* License: GPLv2 or later
Expand All @@ -21,7 +21,7 @@ The goal of releasing this code as a plugin is to allow as many people as possib

Further development of the code for this plugin will continue on its <a href="https://github.com/adamsilverstein/wp-post-meta-revisions">GitHub repository</a>. Pull requests welcome!

To use this plugin, you must be running WordPress 4.1 or newer, two hooks were added in 4.1 that are required for this implementation.
To use this plugin, you must be running WordPress 4.9.8 or newer, a function were added in 4.9.8 that are required for this implementation.

To revision a post meta, you add its key via a filter:

Expand Down
4 changes: 2 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== WP-Post-Meta-Revisions ===
Contributors: adamsilverstein
Requires at least: 4.1
Requires at least: 4.9.8
Tested up to: 4.9
Stable tag: 1.0.0
License: GPLv2 or later
Expand All @@ -16,7 +16,7 @@ The goal of releasing this code as a plugin is to allow as many people as possib

Further development of the code for this plugin will continue on its <a href="https://github.com/adamsilverstein/wp-post-meta-revisions">GitHub repository</a>. Pull requests welcome!

To use this plugin, you must be running WordPress 4.1 or newer, two hooks were added in 4.1 that are required for this implementation.
To use this plugin, you must be running WordPress 4.9.8 or newer, a function were added in 4.9.8 that are required for this implementation.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To use this plugin, you must be running WordPress 4.9.8 or newer, a function were added in 4.9.8 that are required for this implementation.
To use this plugin, you must be running WordPress 4.9.8 or newer, a function was added in 4.9.8 that is required for this implementation.


To revision a post meta, you add its key via a filter:

Expand Down
42 changes: 42 additions & 0 deletions tests/test-meta-revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,48 @@ public function test_revisions_stores_meta_values() {

$this->assertEquals( $expect, $stored_array );

/*
* Test autosave saving single/unique metadata.
*/

$_POST = array(
'meta_revision_test' => 'autosave',
);
wp_create_post_autosave(
array(
'post_content' => 'Autosave content.',
'post_ID' => $post_id,
'post_type' => 'post',
)
);
$autosave_post = wp_get_post_autosave( $post_id );
$this->assertEquals( 'autosave', get_post_meta( $autosave_post->ID, 'meta_revision_test', true ) );

/*
* Test autosave saving multiple/non-unique metadata.
*/

// Register `meta_revision_test` as non-unique meta key.
register_post_meta( '', 'meta_revision_test', [ 'single' => false ] );

$_POST = array(
'meta_multiples_test' => array(
'autosave',
'autosave2',
'autosave3',
)
);
wp_create_post_autosave(
array(
'post_content' => 'Autosave content.',
'post_ID' => $post_id,
'post_type' => 'post',
)
);
$autosave_post = wp_get_post_autosave( $post_id );
$this->assertEquals( array( 'autosave', 'autosave2', 'autosave3' ), get_post_meta( $autosave_post->ID, 'meta_revision_test', false ) );


// Cleanup!
wp_delete_post( $original_post_id );
}
Expand Down
48 changes: 29 additions & 19 deletions wp-post-meta-revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,36 @@ public function wp_autosave_post_revisioned_meta_fields( $new_autosave ) {
* the the meta value has changes from the last autosaved value.
*/
foreach ( $this->wp_post_revision_meta_keys() as $meta_key ) {
if ( ! isset( $posted_data[ $meta_key ] ) ) {
continue;
}

// Default the meta value from the $_POST variable that matches the meta key.
$meta_value = isset( $posted_data[ $meta_key ] ) ? wp_unslash( $posted_data[ $meta_key ] ) : NULL;

// Find out if meta key is single/unqiue.
$registered_meta_keys = get_registered_meta_keys( 'post' );
$is_single = isset( $registered_meta_keys[ $meta_key ] ) ? $registered_meta_keys[ $meta_key ][ 'single' ] : false;

if ( get_post_meta( $new_autosave['ID'], $meta_key, $is_single ) === $meta_value ) {
continue;
}

/*
* Use the underlying delete_metadata() and add_metadata() functions
* vs delete_post_meta() and add_post_meta() to make sure we're working
* with the actual revision meta.
*/
delete_metadata( 'post', $new_autosave['ID'], $meta_key );

if (
isset( $posted_data[ $meta_key ] ) &&
get_post_meta( $new_autosave['ID'], $meta_key, true ) !== wp_unslash( $posted_data[ $meta_key ] )
) {
/*
* Use the underlying delete_metadata() and add_metadata() functions
* vs delete_post_meta() and add_post_meta() to make sure we're working
* with the actual revision meta.
*/
delete_metadata( 'post', $new_autosave['ID'], $meta_key );

/*
* One last check to ensure meta value not empty().
*/
if ( ! empty( $posted_data[ $meta_key ] ) ) {
/*
* Add the revisions meta data to the autosave.
*/
add_metadata( 'post', $new_autosave['ID'], $meta_key, $posted_data[ $meta_key ] );
/*
* Add the revisions meta data to the autosave.
*/
if ( $is_single ) {
add_metadata( 'post', $new_autosave['ID'], $meta_key, $meta_value );
} else {
foreach ( (array) $meta_value as $value ) {
add_metadata( 'post', $new_autosave['ID'], $meta_key, $value );
}
}
}
Expand Down