Git Product home page Git Product logo

wp-post-meta-revisions's Introduction

Build Status

=== WP-Post-Meta-Revisions ===

Allow selected post meta keys to be tracked in revisions.

== Description ==

Important

Version 2.0.0 of this plugin introduces a streamlined storage format for revisioned meta that is not completely backwards compatible with previous versions of the plugin. Restoring revisions data from a previous version where array data was stored may not work as expected. If you need to be able to restore array data revisioned in previous versions, avoid upgrading or read the full issue to see how your data may be impacted: #56.

This plugin implements a post meta revisioning feature as arrived at in https://core.trac.wordpress.org/ticket/20564.

The goal of releasing this code as a plugin is to allow as many people as possible to easily test the post meta revisioning feature, and also hopefully move towards inclusion of the feature into core, following the Features as Plugins model.

Further development of the code for this plugin will continue on its GitHub repository. 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 revision a post meta, you add its key via a filter:

function add_meta_keys_to_revision( $keys ) {
	$keys[] = 'meta-key-to-revision';
	return $keys;
}
add_filter( 'wp_post_revision_meta_keys', 'add_meta_keys_to_revision' );

Features:

  • Allows for a whitelisted array of 'revisioned' meta keys (which can change at any time)
  • A revision for the meta is stored on save (if the meta value has changed)
  • A meta revision save (if changed) is also triggered during auto-saves
  • Restoring a revision restores the revisioned meta field's values at that revision (including auto-saves)
  • Supports storing of multiple values for a single key (and restoring them)
  • Adds revisioned meta to the preview data via get_post_metadata
  • Includes unit tests demonstrating feature
  • Travis CI tests integrated with GitHub repository, props @mattheu

wp-post-meta-revisions's People

Contributors

aaemnnosttv avatar adamsilverstein avatar bobbravo2 avatar hingst avatar jrfnl avatar mattheu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wp-post-meta-revisions's Issues

Revisions are sometimes missed

Sometimes when previewing changes, neither wp_creating_autosave nor wp_before_creating_autosave fires, and thus the meta isn't saved to the revision. I believe it has something to do with timing; possibly that revisions aren't stored until WP has sent its first 'autosave' heartbeat. I haven't dug into the WP code yet to determine why that is. See demo below:

revisions

(Note, this is with #40 merged, so that meta previews work)

Why are post metas are stored as an array instead of sperarate keys

I noticed for some reason, all meta for a single key is stored as a single meta key (serialized array) in the revision meta, this makes calling get_post_meta on a revision return an array-encapsulated data rather than the data you would get from calling get_post_meta on the parent post. I just wondered why that was?

Revisions meta entries created when not needed

When creating a revision's meta entires, it will create meta values based on whitelist, so even if the copied post didn't have a meta value with that key, the new revision will have a blank key because theres no check to see if that key existed.

Did this ever work?

At this point I'm sure this plugin ever worked.
I already had Advanced Custom Fields installed when I installed this and so I thought was I was seeing was a working plugin. I've since wiped and re-installed WP with this being the only active plugin and I cannot see it work in the least. No matter what I change, I cannot see custom fields in the revision timeline nor to their values come back when I restore old versions.
Can you please help?

Can't get this to work

function add_meta_keys_to_revision( $keys ) {
    array_push( $keys,  'uname', 'suspicion', 'profile_summary' );

    return $keys;
}
add_filter( 'wp_post_revision_meta_keys', 'add_meta_keys_to_revision' );

The function `add_metadata_preview_filter ` doesn't seem to ever be called

The function add_metadata_preview_filter doesn't seem to ever be called. How does this get called or used?

/**
 * Add the revisioned meta to get_post_metadata for preview meta data.
 *
 * @since 1.0.0
 */
public function add_metadata_preview_filter() {
	add_filter( 'get_post_metadata', array( $this, 'wp_preview_meta_filter' ), 10, 4 );
}

First Revision trigger

Create new post, enter title and a custom field. Publish.
Change custom field value. Update

Expected: See Revisions link in Publish meta box.
Results: No Revisions link.

Revisions are not available until a change is made in the native fields.

Preview Metadata Changes

Hi,
I noticed that the _add_metadata_preview_filter method never gets called. Is this intentional? Why is the add_filter function it uses not called in the constructor?

ACF field on Front-End

Hi,

I use ACF PRO and ACF Extended on my site. On posts with a Custom Post Type, I'd also like to save revisions with these Custom Fields that I created with the ACF plugin. Can i do that?

Another question I'm interested in is whether I can get these revisions and information about ACF fields on the Front-End as well?

Allow filtering of the posted meta data before adding it to the revision

The current functionality in the _wp_autosave_post_revisioned_meta_fields() method takes the meta value directly from the $_POST variable. In instances where a complex meta value is built from several post fields, cleanup/sanitation of the post field occurs, or the post field name differs from the meta key name, this approach will not work.

Generic example:

// Assuming 'custom_field' is part of the wp_post_revision_meta_keys filter
$yes_no_prompt = isset( $_POST['custom_field'] ) 
   && '1' === $_POST['custom_field'] ? 'Yes' : 'No';
update_post_meta( $post_id', 'yes_no_prompt', $yes_no_prompt );

The revision post meta for the code above would be the value of whatever $_POST['custom_field'] is and would not have the yes_no_prompt post meta.

A possible solution would be a filter in the _wp_autosave_post_revisioned_meta_fields() method that allowed a theme/plugin to override the posted value with whatever should be saved. I can work on a PR for this.

CMB2 support

please can you add CMB2 support?

It dosent work with Cmb2 fields, can't figure out why, my guess i that have somthing to do with cmb2 hooks.

Preview and autosave does not work

Using this plugin to get meta data storing in revisions on a client's website. Whereas the meta data stores fine after registering their meta keys, I cannot seem to get the preview functionality working.
I noticed that it was turned off by default so I assume this is a known issue. But since the client asked to be able to preview their changes I thought I'd enable it and try and get it working.
Sadly I ran out of time for this but what I found was that the autosave revision does have meta data stored against it, but that the data is empty. It seems that the _wp_autosave_post_revisioned_meta_fields method does not get called when the autosave gets created.

Hope someone will find the above helpful in solving this as previewing the meta changes would be a fairly nice feature to have. If I get more time later I'll give it another go.

_wp_post_revision_meta_keys isn't a function

_wp_post_revision_meta_keys() is used a few times, but because of the class, it's actually a method.

I'd recommend using a namespace instead, as this will make it work a little more nicely. :) (It will drop compat with 5.2, but only when testing using the plugin. This means less changes between the patch and the plugin too, since it should only require adding/removing the single line for the namespace.)

Improve QA setup

Originally from: #43 (comment)

I've had a quick look and would like to propose the following:

  1. Do the whole fixing in a separate PR.
    Existing open PRs will need to be rebased after that PR has been merged, so those will be tested properly.
  2. Do it with a holistic look at the whole devops setup.

Having said that, I'm looking at:

  • Adding a .gitignore file.
  • Adding a .gitattributes file.
  • Adding a composer.json file.
  • Actually running the unit tests in Travis.
  • Linting against various PHP versions in Travis.
  • Running PHPCS in Travis. I propose to run against the WordPress-Extra or WordPress ruleset, as well the PHPCompatibilityWP ruleset.

Questions:

  • What do you think about the above proposal in the first place ?
  • Any feedback on the proposed PHPCS rulesets ?
  • What is the minimum PHP version you want to support at this moment ? 5.2 still ?
  • What is the minimum WP version you want to support ?

I may have additional questions once I start setting things up.

Meta revisions not attached to expected post revision

When carrying out a series of post / post meta updates, it doesn't appear to store the two in sync properly.

Replication
Create a new post.
Add post content 'Content 1'
Add meta in a whitelisted field 'Meta 1'
Save content

Make content change 'Content 2'
Make change to whitelisted meta field 'Meta 2'
Save content

etc.

Click on revisions created and restore.
Post content example 'Content 2'
Meta content example 'Meta 1'

Expected result
I would expect that restoring 'Content 2' would result in also restoring 'Meta 2'

Viewing revisions in the revision screen

I tested this and it is saving the revisions and when restoring a revision it definitely rolls back my assigned custom fields but in the revisions screen I do no see them.
I tried adding this code but it did not help though this code does work when I save meta fields using other code (that I want to drop because of drawbacks):

function dc_dcb_pmr_fields( $fields ) {
    global $post;
    $fields['meta_slugl'] = 'Meta name';
    return $fields;
}
add_filter( '_wp_post_revision_fields',    'dc_dcb_pmr_fields' );

Is this something anyone here got working or is it not supported?

Non-unique meta-key are saved together in autosave

We are using CMB for the custom meta boxes. (I know it is deprecated but waiting for a whole site refresh to migrate away).

There is a non-unique meta key ph_tabs with an array as value. The data is saved as follows in the database
image

The metadata for the autosave is fetched from $_POST.

$posted_data = isset( $_POST['data']['wp_autosave'] ) ? $_POST['data']['wp_autosave'] : $_POST;

Plugins like CMB send the data in a array and this caused for the data to be saved as one big array instead of seperate rows for each array.

This is how the $_POST looks like:

Array (
[ph_tabs] => Array (
    [cmb-group-0] => Array (
            [ph_tab_title] => Array (
                    [cmb-field-0] => New tab
            )
            [ph_tab_content] => Array  (
                    [cmb-field-0] => Content
            )
    )
    [cmb-group-2] => Array (
            [ph_tab_title] => Array  (
                    [cmb-field-0] => Tab Title
             )
            [ph_tab_content] => Array (
                    [cmb-field-0] => Content second tab
             )
     )
)
)

I will create a PR with a fix.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.