From a1951206aa9fb1fcbf4f9086953ce1264da37b12 Mon Sep 17 00:00:00 2001 From: Ivan Kruchkoff Date: Mon, 25 Apr 2016 16:04:28 -0300 Subject: [PATCH 1/4] Pass post_id to _wp_post_revision_meta_keys to allow for blacklisting rather than whitelisting keys --- wp-post-meta-revisions.php | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/wp-post-meta-revisions.php b/wp-post-meta-revisions.php index de02b12..2c11cf6 100644 --- a/wp-post-meta-revisions.php +++ b/wp-post-meta-revisions.php @@ -59,12 +59,13 @@ public function _wp_autosave_post_revisioned_meta_fields( $new_autosave ) { * itself. This sets $posted_data to the correct variable. */ $posted_data = isset( $_POST['data'] ) ? $_POST['data']['wp_autosave'] : $_POST; + $id = array_key_exists( 'post_id', $posted_data ) ? $posted_data[ 'post_id' ] : null; /** * 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 ) { + foreach ( $this->_wp_post_revision_meta_keys( $id ) as $meta_key ) { if ( isset( $posted_data[ $meta_key ] ) && get_post_meta( $new_autosave['ID'], $meta_key, true ) != wp_unslash( $posted_data[ $meta_key ] ) ) @@ -96,7 +97,8 @@ public function _wp_autosave_post_revisioned_meta_fields( $new_autosave ) { * * @return array An array of meta keys to be revisioned. */ - public function _wp_post_revision_meta_keys() { + public function _wp_post_revision_meta_keys( $post_id = null ) { + $existing_meta_keys = is_null( $post_id ) ? array() : array_keys( get_post_meta( $post_id ) ); /** * Filter the list of post meta keys to be revisioned. * @@ -104,7 +106,7 @@ public function _wp_post_revision_meta_keys() { * * @param array $keys An array of default meta fields to be revisioned. */ - return apply_filters( 'wp_post_revision_meta_keys', array() ); + return apply_filters( 'wp_post_revision_meta_keys', $existing_meta_keys ); } /** @@ -113,7 +115,7 @@ public function _wp_post_revision_meta_keys() { * @since 4.5.0 */ 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 ) { + foreach ( $this->_wp_post_revision_meta_keys( $post->ID ) as $meta_key ) { if ( get_post_meta( $post->ID, $meta_key ) != get_post_meta( $last_revision->ID, $meta_key ) ) { $post_has_changed = true; break; @@ -130,8 +132,17 @@ 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 ) { + + $existing_meta_keys = is_null( $post_id ) ? array() : array_keys( get_post_meta( $post_id ) ); + + foreach ( $this->_wp_post_revision_meta_keys( $post_id ) as $meta_key ) { + /** + * Avoid fetching the meta that doesn't exist in the source post. + */ + if ( ! array_key_exists( $meta_key, $existing_meta_keys ) ) { + continue; + } + $meta_value = get_post_meta( $post_id, $meta_key ); /* @@ -149,7 +160,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( $revision_id ); if ( isset( $metas_revisioned ) && 0 !== sizeof( $metas_revisioned ) ) { foreach ( $metas_revisioned as $meta_key ) { // Clear any existing metas @@ -187,7 +198,7 @@ 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() ) + || ! in_array( $meta_key, $this->_wp_post_revision_meta_keys( $post->ID ) ) || 'revision' == $post->post_type ) { return $value; From 73ed92174599692ccb1829caeb501971a221cbae Mon Sep 17 00:00:00 2001 From: Ivan Kruchkoff Date: Mon, 25 Apr 2016 16:09:09 -0300 Subject: [PATCH 2/4] Add self to contributors --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 165d231..6ef9e92 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ [![Build Status](https://travis-ci.org/adamsilverstein/wp-post-meta-revisions.svg?branch=master)](https://travis-ci.org/adamsilverstein/wp-post-meta-revisions) === WP-Post-Meta-Revisions === -* Contributors: adamsilverstein, mattheu +* Contributors: adamsilverstein, mattheu, ivankk * Requires at least: 4.1 * Tested up to: 4.5 * Stable tag: 0.2.2 From 940154fdb6dccf4580cc5823fb6ed36f5c9aba88 Mon Sep 17 00:00:00 2001 From: Ivan Kruchkoff Date: Mon, 25 Apr 2016 16:48:13 -0300 Subject: [PATCH 3/4] Minor version bump --- wp-post-meta-revisions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-post-meta-revisions.php b/wp-post-meta-revisions.php index 2c11cf6..f24409f 100644 --- a/wp-post-meta-revisions.php +++ b/wp-post-meta-revisions.php @@ -3,7 +3,7 @@ * Plugin Name: Post Meta Revisions * Plugin URI: https://github.com/adamsilverstein/wp-post-meta-revisions * Description: Post Meta Revisions - * Version: 0.2.2 + * Version: 0.2.3 * Author: Adam Silverstein - code developed with others * at https://core.trac.wordpress.org/ticket/20564 * License: GPLv2 or later From 44705f15fdb97667b0d028a30140438c3a14eaa9 Mon Sep 17 00:00:00 2001 From: Ivan Kruchkoff Date: Mon, 25 Apr 2016 18:05:00 -0300 Subject: [PATCH 4/4] s/array_key_exists/in_array/ --- wp-post-meta-revisions.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wp-post-meta-revisions.php b/wp-post-meta-revisions.php index f24409f..6c1a5d6 100644 --- a/wp-post-meta-revisions.php +++ b/wp-post-meta-revisions.php @@ -99,6 +99,7 @@ public function _wp_autosave_post_revisioned_meta_fields( $new_autosave ) { */ public function _wp_post_revision_meta_keys( $post_id = null ) { $existing_meta_keys = is_null( $post_id ) ? array() : array_keys( get_post_meta( $post_id ) ); + $existing_meta_keys = array(); /** * Filter the list of post meta keys to be revisioned. * @@ -139,7 +140,7 @@ public function _wp_save_revisioned_meta_fields( $revision_id ) { /** * Avoid fetching the meta that doesn't exist in the source post. */ - if ( ! array_key_exists( $meta_key, $existing_meta_keys ) ) { + if ( ! in_array( $meta_key, $existing_meta_keys ) ) { continue; }