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

Fix/cleanup coding standards #39

Merged
merged 3 commits into from
Dec 5, 2017
Merged
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
7 changes: 5 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

$_tests_dir = getenv('WP_TESTS_DIR');
if ( !$_tests_dir ) $_tests_dir = '/tmp/wordpress-tests-lib';
$_tests_dir = getenv( 'WP_TESTS_DIR' );

if ( ! $_tests_dir ) {
$_tests_dir = '/tmp/wordpress-tests-lib';
}

require_once $_tests_dir . '/includes/functions.php';

Expand Down
84 changes: 62 additions & 22 deletions tests/test-meta-revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@ function test_revisions_stores_meta_values() {
*/

// Set up a new post
$original_post_id = $post_id = $this->factory->post->create();
$post_id = $this->factory->post->create();
$original_post_id = $post_id;

// And update to store an initial revision
wp_update_post( array( 'post_content' => 'some initial content', 'ID' => $post_id ) );
wp_update_post(
array(
'post_content' => 'some initial content',
'ID' => $post_id,
)
);

// One revision so far
// One revision so far.
$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 1, $revisions );



/**
* First set up a meta value
*/
Expand All @@ -39,31 +44,41 @@ function test_revisions_stores_meta_values() {
update_post_meta( $post_id, 'meta_revision_test', 'original' );

// Update the post, storing a revision
wp_update_post( array( 'post_content' => 'some more content', 'ID' => $post_id ) );
wp_update_post(
array(
'post_content' => 'some more content',
'ID' => $post_id,
)
);

$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 2, $revisions );


// Next, store some updated meta values for the same key
// Next, store some updated meta values for the same key.
update_post_meta( $post_id, 'meta_revision_test', 'update1' );

// Save the post, changing content to force a revision
wp_update_post( array( 'post_content' => 'some updated content', 'ID' => $post_id ) );
// Save the post, changing content to force a revision.
wp_update_post(
array(
'post_content' => 'some updated content',
'ID' => $post_id,
)
);

$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 3, $revisions );


/**
* Now restore the original revision
*/

// Restore the previous revision
$revisions = (Array) wp_get_post_revisions( $post_id );

// Go back two to load the previous revision
array_shift( $revisions );
$last_revision = array_shift( $revisions );

// Restore!
wp_restore_post_revision( $last_revision->ID );

Expand All @@ -80,7 +95,6 @@ function test_revisions_stores_meta_values() {

update_post_meta( $post_id, 'meta_revision_test', 'update2' );


/*
* Test the revisioning of custom meta when enabled by the wp_post_revision_meta_keys filter
*/
Expand All @@ -89,7 +103,12 @@ function test_revisions_stores_meta_values() {
add_filter( 'wp_post_revision_meta_keys', array( $this, 'add_revisioned_keys' ) );

// Save the post, changing content to force a revision
wp_update_post( array( 'post_content' => 'more updated content', 'ID' => $post_id ) );
wp_update_post(
array(
'post_content' => 'more updated content',
'ID' => $post_id,
)
);

$revisions = wp_get_post_revisions( $post_id );
$this->assertCount( 5, $revisions );
Expand All @@ -104,7 +123,11 @@ function test_revisions_stores_meta_values() {
* in post content, because the revisioned post_meta has changed
*
*/
wp_update_post( array( 'ID' => $post_id ) );
wp_update_post(
array(
'ID' => $post_id,
)
);

// This revision contains the existing post meta ('update3')
$revisions = wp_get_post_revisions( $post_id );
Expand All @@ -128,10 +151,18 @@ function test_revisions_stores_meta_values() {

// Try storing a blank meta
update_post_meta( $post_id, 'meta_revision_test', '' );
wp_update_post( array( 'ID' => $post_id ) );
wp_update_post(
array(
'ID' => $post_id,
)
);

update_post_meta( $post_id, 'meta_revision_test', 'update 4' );
wp_update_post( array( 'ID' => $post_id ) );
wp_update_post(
array(
'ID' => $post_id,
)
);

// Restore the previous revision
$revisions = wp_get_post_revisions( $post_id );
Expand All @@ -151,9 +182,19 @@ function test_revisions_stores_meta_values() {

// Meta should no longer be revisioned
update_post_meta( $post_id, 'meta_revision_test', 'update 5' );
wp_update_post( array( 'ID' => $post_id, 'post_content' => 'changed content' ) );
wp_update_post(
array(
'ID' => $post_id,
'post_content' => 'changed content',
)
);
update_post_meta( $post_id, 'meta_revision_test', 'update 6' );
wp_update_post( array( 'ID' => $post_id, 'post_content' => 'go updated content' ) );
wp_update_post(
array(
'ID' => $post_id,
'post_content' => 'go updated content',
)
);

// Restore the previous revision
$revisions = wp_get_post_revisions( $post_id );
Expand Down Expand Up @@ -181,8 +222,8 @@ function test_revisions_stores_meta_values() {

// Update all three values
update_post_meta( $post_id, 'meta_revision_test', 'update 8', 'update 7' );
update_post_meta( $post_id, 'meta_revision_test', 'update 8 number 2', 'update 7 number 2' );
update_post_meta( $post_id, 'meta_revision_test', 'update 8 number 3', 'update 7 number 3' );
update_post_meta( $post_id, 'meta_revision_test', 'update 8 number 2', 'update 7 number 2' );
update_post_meta( $post_id, 'meta_revision_test', 'update 8 number 3', 'update 7 number 3' );
wp_update_post( array( 'ID' => $post_id ) );

// Restore the previous revision
Expand All @@ -196,7 +237,6 @@ function test_revisions_stores_meta_values() {
*/
$this->assertEquals( array( 'update 7', 'update 7 number 2', 'update 7 number 3' ), get_post_meta( $post_id, 'meta_revision_test' ) );


/**
* Test the revisioning of a multidimensional array.
*/
Expand All @@ -213,7 +253,7 @@ function test_revisions_stores_meta_values() {
'b',
'c',
),
'not' => 'ok',
'not' => 'ok',
),
);

Expand Down
34 changes: 21 additions & 13 deletions wp-post-meta-revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct() {
* @since 4.5.0
*/
public function _add_metadata_preview_filter() {
add_filter( 'get_post_metadata', array( $this, '_wp_preview_meta_filter'), 10, 4 );
add_filter( 'get_post_metadata', array( $this, '_wp_preview_meta_filter' ), 10, 4 );
}

/**
Expand All @@ -54,31 +54,37 @@ public function _add_metadata_preview_filter() {
* @param Post object $new_autosave The new post being autosaved.
*/
public function _wp_autosave_post_revisioned_meta_fields( $new_autosave ) {

/**
* The post data arrives as either $_POST['data']['wp_autosave'] or the $_POST
* itself. This sets $posted_data to the correct variable.
*/
$posted_data = isset( $_POST['data'] ) ? $_POST['data']['wp_autosave'] : $_POST;
$posted_data = isset( $_POST['data'] ) ? $_POST['data']['wp_autosave'] : $_POST; // WPCS: CSRF ok. input var ok. sanitization ok.

/**
* Go thru the revisioned meta keys and save them as part of the autosave, if
* the meta key is part of the posted data, the meta value is not blank and
* 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 ] )
&& get_post_meta( $new_autosave['ID'], $meta_key, true ) != wp_unslash( $posted_data[ $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.
*/
Expand Down Expand Up @@ -114,7 +120,7 @@ public function _wp_post_revision_meta_keys() {
*/
public function _wp_check_revisioned_meta_fields_have_changed( $post_has_changed, WP_Post $last_revision, WP_Post $post ) {
foreach ( $this->_wp_post_revision_meta_keys() as $meta_key ) {
if ( get_post_meta( $post->ID, $meta_key ) != get_post_meta( $last_revision->ID, $meta_key ) ) {
if ( get_post_meta( $post->ID, $meta_key ) !== get_post_meta( $last_revision->ID, $meta_key ) ) {
$post_has_changed = true;
break;
}
Expand All @@ -130,6 +136,7 @@ public function _wp_check_revisioned_meta_fields_have_changed( $post_has_changed
public function _wp_save_revisioned_meta_fields( $revision_id ) {
$revision = get_post( $revision_id );
$post_id = $revision->post_parent;

// Save revisioned meta fields.
foreach ( $this->_wp_post_revision_meta_keys() as $meta_key ) {
$meta_value = get_post_meta( $post_id, $meta_key );
Expand All @@ -149,7 +156,7 @@ public function _wp_save_revisioned_meta_fields( $revision_id ) {
*/
public function _wp_restore_post_revision_meta( $post_id, $revision_id ) {
// Restore revisioned meta fields.
$metas_revisioned = $this->_wp_post_revision_meta_keys();
$metas_revisioned = $this->_wp_post_revision_meta_keys();
if ( isset( $metas_revisioned ) && 0 !== sizeof( $metas_revisioned ) ) {
foreach ( $metas_revisioned as $meta_key ) {
// Clear any existing metas
Expand Down Expand Up @@ -185,11 +192,12 @@ public function _wp_restore_post_revision_meta( $post_id, $revision_id ) {
public function _wp_preview_meta_filter( $value, $object_id, $meta_key, $single ) {

$post = get_post();
if ( empty( $post )
|| $post->ID != $object_id
|| ! in_array( $meta_key, $this->_wp_post_revision_meta_keys() )
|| 'revision' == $post->post_type )
{
if (
empty( $post ) ||
$post->ID !== $object_id ||
! in_array( $meta_key, $this->_wp_post_revision_meta_keys(), true ) ||
'revision' === $post->post_type
) {
return $value;
}

Expand All @@ -203,4 +211,4 @@ public function _wp_preview_meta_filter( $value, $object_id, $meta_key, $single
}
}

$wp_Post_Meta_Revisioning = new WP_Post_Meta_Revisioning;
$wp_post_meta_revisioning = new WP_Post_Meta_Revisioning;