From 6e8872fec0c1e5030da7a625ca44328403168ec9 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 5 Dec 2017 00:25:18 -0500 Subject: [PATCH] Coding standards fixes for phpcs; whitespace. --- tests/bootstrap.php | 7 ++- tests/test-meta-revisions.php | 84 ++++++++++++++++++++++++++--------- wp-post-meta-revisions.php | 34 ++++++++------ 3 files changed, 88 insertions(+), 37 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d425763..199264b 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,7 +1,10 @@ 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 */ @@ -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 ); @@ -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 */ @@ -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 ); @@ -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 ); @@ -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 ); @@ -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 ); @@ -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 @@ -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. */ @@ -213,7 +253,7 @@ function test_revisions_stores_meta_values() { 'b', 'c', ), - 'not' => 'ok', + 'not' => 'ok', ), ); diff --git a/wp-post-meta-revisions.php b/wp-post-meta-revisions.php index de02b12..a980027 100644 --- a/wp-post-meta-revisions.php +++ b/wp-post-meta-revisions.php @@ -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 ); } /** @@ -54,11 +54,13 @@ 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 @@ -66,19 +68,23 @@ public function _wp_autosave_post_revisioned_meta_fields( $new_autosave ) { */ 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. */ @@ -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; } @@ -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 ); @@ -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 @@ -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; } @@ -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;