-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcf-revision-manager.php
201 lines (176 loc) · 5.94 KB
/
cf-revision-manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/*
Plugin Name: CF Revision Manager
Plugin URI: http://crowdfavorite.com
Description: Revision management functionality so that plugins can add metadata to revisions as well as restore that metadata from revisions
Version: 1.0.2
Author: Crowd Favorite
Author URI: http://crowdfavorite.com
*/
if (!class_exists('cf_revisions')) {
define('CF_REVISIONS_DEBUG', false);
function cfr_register_metadata($postmeta_key, $display_func = '') {
static $cfr;
if (empty($cfr)) {
$cfr = cf_revisions::get_instance();
}
return $cfr->register($postmeta_key, $display_func);
}
class cf_revisions {
private static $_instance;
protected $postmeta_keys = array();
public function __construct() {
# save & restore
add_action('save_post', array($this, 'save_post_revision'), 10, 2);
add_action('wp_restore_post_revision', array($this, 'restore_post_revision'), 10, 2);
if (is_admin()) {
# revision display
global $pagenow;
if ($pagenow == 'revision.php') {
add_filter('_wp_post_revision_fields', array($this, 'post_revision_fields'), 10, 1);
add_filter('_wp_post_revision_field_postmeta', array($this, 'post_revision_field'), 1, 2);
}
}
}
public function register($postmeta_key, $display_func = '') {
if (!in_array($postmeta_key, $this->postmeta_keys, true)) {
$this->postmeta_keys[] = compact('postmeta_key', 'display_func');
}
return true;
}
/**
* This is a paranoid check. There will be no object to register the
* actions and filters if nobody adds any postmeta to be handled
*
* @return bool
*/
public function have_keys() {
return (bool) count($this->postmeta_keys);
}
/**
* The opposite of WordPress stripslashes_deep, since wp_slash
* only works on arrays of strings.
**/
public function slash_deep($value) {
if (is_array($value)) {
$value = array_map(array($this, 'slash_deep'), $value);
}
else if (is_object($value)) {
$vars = get_object_vars($value);
foreach ($vars as $key => $data) {
$value->{$key} = $this->slash_deep($data);
}
} else if (is_string($value)) {
$value = wp_slash($value);
}
return $value;
}
/**
* Save the revision data
*
* @param int $post_id
* @param object $post
* @return void
*/
public function save_post_revision($post_id, $post) {
if ($post->post_type != 'revision' || !$this->have_keys()) {
return false;
}
foreach ($this->postmeta_keys as $postmeta_type) {
$postmeta_key = $postmeta_type['postmeta_key'];
if ($postmeta_values = get_post_meta($post->post_parent, $postmeta_key)) {
foreach ($postmeta_values as $postmeta_value) {
add_metadata('post', $post_id, $this->slash_deep($postmeta_key), $this->slash_deep($postmeta_value));
}
$this->log('Added postmeta for: '.$postmeta_key.' to revision: '.$post_id.' from post: '.$post->post_parent);
}
}
}
/**
* Revert the revision data
*
* @param int $post_id
* @param int $revision_id
* @return void
*/
public function restore_post_revision($post_id, $revision_id) {
if (!$this->have_keys()) {
return false;
}
foreach ($this->postmeta_keys as $postmeta_type) {
$postmeta_key = $postmeta_type['postmeta_key'];
delete_metadata('post', $post_id, $this->slash_deep($postmeta_key));
// get_metadata does not unslash
if ($postmeta_values = get_metadata('post', $revision_id, $postmeta_key)) {
foreach ($postmeta_values as $postmeta_value) {
$this->log('Setting postmeta: '.$postmeta_key.' for post: '.$post_id);
add_metadata('post', $post_id, $this->slash_deep($postmeta_key), $this->slash_deep($postmeta_value), true);
}
$this->log('Restored post_id: '.$post_id.' metadata from: '.$postmeta_key);
}
}
}
public function post_revision_fields($fields) {
$fields['postmeta'] = __('Post Meta');
return $fields;
}
public function post_revision_field($field_id, $field) {
if ($field != 'postmeta' || !$this->have_keys()) {
return;
}
remove_filter('_wp_post_revision_field_postmeta', 'htmlspecialchars', 10, 2);
$html = '<ul style="white-space: normal; margin-left: 1.5em; list-style: disc outside;">';
foreach ($this->postmeta_keys as $postmeta_type) {
$postmeta_html = '';
$postmeta_key = $postmeta_type['postmeta_key'];
$postmeta_values = get_metadata('post', intval($_GET['revision']), $postmeta_key);
if (is_array($postmeta_values)) {
foreach ($postmeta_values as $postmeta_value) {
$postmeta_html .= '<div>';
$postmeta_value = maybe_unserialize($postmeta_value);
if (!empty($postmeta_value)) {
if (!empty($postmeta_type['display_func']) && function_exists($postmeta_type['display_func'])) {
$postmeta_html .= $postmeta_type['display_func']($postmeta_value);
}
else {
$postmeta_rendered = (is_array($postmeta_value) || is_object($postmeta_value) ? print_r($postmeta_value, true) : $postmeta_value);
$postmeta_html .= apply_filters('_wp_post_revision_field_postmeta_display', htmlspecialchars($postmeta_rendered), $postmeta_key, $postmeta_value);
}
}
$postmeta_html .= '</div>';
}
}
else {
$postmeta_html .= '*empty postmeta value*';
}
$html .= '
<li>
<h3><a href="#postmeta-'.$postmeta_key.'" onclick="jQuery(\'#postmeta-'.$postmeta_key.'\').slideToggle(); return false;">'.$postmeta_key.'</a></h3>
<div id="postmeta-'.$postmeta_key.'" style="display: none;">'.$postmeta_html.'</div>
</li>
';
}
$html .= '</ul>';
return $html;
}
/**
* Singleton
*
* @return object
*/
public static function get_instance() {
if (!(self::$_instance instanceof cf_revisions)) {
self::$_instance = new cf_revisions;
}
return self::$_instance;
}
protected function log($message) {
if (CF_REVISIONS_DEBUG) {
error_log($message);
}
}
}
if (defined('CF_REVISIONS_DEBUG') && CF_REVISIONS_DEBUG) {
include('tests.php');
}
}