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

Allow for whitelisting/blacklisting keys #19

Open
wants to merge 4 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
30 changes: 21 additions & 9 deletions wp-post-meta-revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ] ) )
Expand Down Expand Up @@ -96,15 +97,17 @@ 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 ) );
$existing_meta_keys = array();
/**
* Filter the list of post meta keys to be revisioned.
*
* @since 4.5.0
*
* @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 );
}

/**
Expand All @@ -113,7 +116,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;
Expand All @@ -130,8 +133,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 ( ! in_array( $meta_key, $existing_meta_keys ) ) {
continue;
}

$meta_value = get_post_meta( $post_id, $meta_key );

/*
Expand All @@ -149,7 +161,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
Expand Down Expand Up @@ -187,7 +199,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;
Expand Down