|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class acf_revisions { |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* __construct |
|
7
|
|
|
* |
|
8
|
|
|
* A good place to add actions / filters |
|
9
|
|
|
* |
|
10
|
|
|
* @type function |
|
11
|
|
|
* @date 11/08/13 |
|
12
|
|
|
* |
|
13
|
|
|
* @param N/A |
|
14
|
|
|
* @return N/A |
|
15
|
|
|
*/ |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
function __construct() |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
// actions |
|
20
|
|
|
add_action('wp_restore_post_revision', array($this, 'wp_restore_post_revision'), 10, 2 ); |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
// filters |
|
24
|
|
|
add_filter('_wp_post_revision_fields', array($this, 'wp_preview_post_fields') ); |
|
25
|
|
|
add_filter('_wp_post_revision_fields', array($this, 'wp_post_revision_fields') ); |
|
26
|
|
|
add_filter('wp_save_post_revision_check_for_changes', array($this, 'force_save_revision'), 10, 3); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/* |
|
31
|
|
|
* wp_preview_post_fields |
|
32
|
|
|
* |
|
33
|
|
|
* This function is used to trick WP into thinking that one of the $post's fields has changed and |
|
34
|
|
|
* will allow an autosave to be updated. |
|
35
|
|
|
* Fixes an odd bug causing the preview page to render the non autosave post data on every odd attempt |
|
36
|
|
|
* |
|
37
|
|
|
* @type function |
|
38
|
|
|
* @date 21/10/2014 |
|
39
|
|
|
* @since 5.1.0 |
|
40
|
|
|
* |
|
41
|
|
|
* @param $fields (array) |
|
42
|
|
|
* @return $fields |
|
43
|
|
|
*/ |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
function wp_preview_post_fields( $fields ) { |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
// bail early if not previewing a post |
|
48
|
|
|
if( empty($_POST['wp-preview']) || $_POST['wp-preview'] != 'dopreview') { |
|
49
|
|
|
|
|
50
|
|
|
return $fields; |
|
51
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
// add to fields if ACF has changed |
|
56
|
|
|
if( !empty($_POST['_acfchanged']) ) { |
|
57
|
|
|
|
|
58
|
|
|
$fields['_acfchanged'] = 'different than 1'; |
|
59
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
// return |
|
64
|
|
|
return $fields; |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/* |
|
70
|
|
|
* force_save_revision |
|
71
|
|
|
* |
|
72
|
|
|
* This filter will return false and force WP to save a revision. This is required due to |
|
73
|
|
|
* WP checking only post_title, post_excerpt and post_content values, not custom fields. |
|
74
|
|
|
* |
|
75
|
|
|
* @type filter |
|
76
|
|
|
* @date 19/09/13 |
|
77
|
|
|
* |
|
78
|
|
|
* @param $return (boolean) defaults to true |
|
79
|
|
|
* @param $last_revision (object) the last revision that WP will compare against |
|
80
|
|
|
* @param $post (object) the $post that WP will compare against |
|
81
|
|
|
* @return $return (boolean) |
|
82
|
|
|
*/ |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
function force_save_revision( $return, $last_revision, $post ) |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
// preview hack |
|
87
|
|
|
if( isset($_POST['_acfchanged']) && $_POST['_acfchanged'] == '1' ) |
|
88
|
|
|
{ |
|
89
|
|
|
$return = false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
// return |
|
94
|
|
|
return $return; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/* |
|
99
|
|
|
* wp_post_revision_fields |
|
100
|
|
|
* |
|
101
|
|
|
* This filter will add the ACF fields to the returned array |
|
102
|
|
|
* Versions 3.5 and 3.6 of WP feature different uses of the revisions filters, so there are |
|
103
|
|
|
* some hacks to allow both versions to work correctly |
|
104
|
|
|
* |
|
105
|
|
|
* @type filter |
|
106
|
|
|
* @date 11/08/13 |
|
107
|
|
|
* |
|
108
|
|
|
* @param $post_id (int) |
|
109
|
|
|
* @return $post_id (int) |
|
110
|
|
|
*/ |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
function wp_post_revision_fields( $return ) { |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
//globals |
|
116
|
|
|
global $post, $pagenow; |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
// validate |
|
120
|
|
|
$allowed = false; |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
// Normal revisions page |
|
124
|
|
|
if( $pagenow == 'revision.php' ) |
|
125
|
|
|
{ |
|
126
|
|
|
$allowed = true; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
// WP 3.6 AJAX revision |
|
131
|
|
|
if( $pagenow == 'admin-ajax.php' && isset($_POST['action']) && $_POST['action'] == 'get-revision-diffs' ) |
|
132
|
|
|
{ |
|
133
|
|
|
$allowed = true; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
// bail |
|
138
|
|
|
if( !$allowed ) |
|
139
|
|
|
{ |
|
140
|
|
|
return $return; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
// vars |
|
145
|
|
|
$post_id = 0; |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
// determine $post_id |
|
149
|
|
|
if( isset($_POST['post_id']) ) |
|
150
|
|
|
{ |
|
151
|
|
|
$post_id = $_POST['post_id']; |
|
152
|
|
|
} |
|
153
|
|
|
elseif( isset($post->ID) ) |
|
154
|
|
|
{ |
|
155
|
|
|
$post_id = $post->ID; |
|
156
|
|
|
} |
|
157
|
|
|
else |
|
158
|
|
|
{ |
|
159
|
|
|
return $return; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
// setup global array |
|
164
|
|
|
$GLOBALS['acf_revisions_fields'] = array(); |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
// get field objects |
|
168
|
|
|
$custom_fields = get_post_custom( $post_id ); |
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
// populate vars |
|
172
|
|
|
if( !empty($custom_fields) ) |
|
173
|
|
|
{ |
|
174
|
|
|
foreach( $custom_fields as $k => $v ) |
|
175
|
|
|
{ |
|
176
|
|
|
// value is always an array |
|
177
|
|
|
$v = $v[0]; |
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
// bail early if $value is not is a field_key |
|
181
|
|
|
if( !acf_is_field_key($v) ) |
|
182
|
|
|
{ |
|
183
|
|
|
continue; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
// remove prefix '_' field from reference |
|
188
|
|
|
$field_name = substr($k, 1); |
|
189
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
// get field |
|
192
|
|
|
//$field = acf_get_field($v); |
|
193
|
|
|
|
|
194
|
|
|
|
|
195
|
|
|
// append to return |
|
196
|
|
|
$return[ $field_name ] = $field_name; |
|
197
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
// load value |
|
200
|
|
|
add_filter("_wp_post_revision_field_{$field_name}", array($this, 'wp_post_revision_field'), 10, 4); |
|
201
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
// WP 3.5: left vs right |
|
204
|
|
|
// Add a value of the revision ID (as there is no way to determine this within the '_wp_post_revision_field_' filter!) |
|
205
|
|
|
if( isset($_GET['action'], $_GET['left'], $_GET['right']) && $_GET['action'] == 'diff' ) |
|
206
|
|
|
{ |
|
207
|
|
|
global $left_revision, $right_revision; |
|
208
|
|
|
|
|
209
|
|
|
$left_revision->$field_name = 'revision_id=' . $_GET['left']; |
|
210
|
|
|
$right_revision->$field_name = 'revision_id=' . $_GET['right']; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
return $return; |
|
218
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
|
|
222
|
|
|
/* |
|
223
|
|
|
* wp_post_revision_field |
|
224
|
|
|
* |
|
225
|
|
|
* This filter will load the value for the given field and return it for rendering |
|
226
|
|
|
* |
|
227
|
|
|
* @type filter |
|
228
|
|
|
* @date 11/08/13 |
|
229
|
|
|
* |
|
230
|
|
|
* @param $value (mixed) should be false as it has not yet been loaded |
|
231
|
|
|
* @param $field_name (string) The name of the field |
|
232
|
|
|
* @param $post (mixed) Holds the $post object to load from - in WP 3.5, this is not passed! |
|
233
|
|
|
* @param $direction (string) to / from - not used |
|
234
|
|
|
* @return $value (string) |
|
235
|
|
|
*/ |
|
|
|
|
|
|
236
|
|
|
|
|
237
|
|
|
function wp_post_revision_field( $value, $field_name, $post = null, $direction = false) |
|
|
|
|
|
|
238
|
|
|
{ |
|
239
|
|
|
// vars |
|
240
|
|
|
$post_id = 0; |
|
241
|
|
|
|
|
242
|
|
|
|
|
243
|
|
|
// determine $post_id |
|
244
|
|
|
if( isset($post->ID) ) |
|
245
|
|
|
{ |
|
246
|
|
|
// WP 3.6 |
|
247
|
|
|
$post_id = $post->ID; |
|
248
|
|
|
} |
|
249
|
|
|
elseif( isset($_GET['revision']) ) |
|
250
|
|
|
{ |
|
251
|
|
|
// WP 3.5 |
|
252
|
|
|
$post_id = (int) $_GET['revision']; |
|
253
|
|
|
} |
|
254
|
|
|
elseif( strpos($value, 'revision_id=') !== false ) |
|
255
|
|
|
{ |
|
256
|
|
|
// WP 3.5 (left vs right) |
|
257
|
|
|
$post_id = (int) str_replace('revision_id=', '', $value); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
|
|
261
|
|
|
// load field |
|
262
|
|
|
$field = acf_maybe_get_field( $field_name, $post_id ); |
|
263
|
|
|
|
|
264
|
|
|
|
|
265
|
|
|
// update value |
|
266
|
|
|
//$value = $field['value']; |
|
267
|
|
|
|
|
268
|
|
|
|
|
269
|
|
|
// default formatting |
|
270
|
|
|
if( is_array($value) ) { |
|
271
|
|
|
|
|
272
|
|
|
$value = implode(', ', $value); |
|
273
|
|
|
|
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
|
|
277
|
|
|
// format |
|
278
|
|
|
if( !empty($value) ) |
|
279
|
|
|
{ |
|
280
|
|
|
// image || file? |
|
281
|
|
|
if( $field['type'] == 'image' || $field['type'] == 'file' ) |
|
282
|
|
|
{ |
|
283
|
|
|
$url = wp_get_attachment_url($value); |
|
284
|
|
|
$value = $value . ' (' . $url . ')'; |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
|
|
289
|
|
|
// return |
|
290
|
|
|
return $value; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
|
|
294
|
|
|
/* |
|
295
|
|
|
* wp_restore_post_revision |
|
296
|
|
|
* |
|
297
|
|
|
* This action will copy and paste the metadata from a revision to the post |
|
298
|
|
|
* |
|
299
|
|
|
* @type action |
|
300
|
|
|
* @date 11/08/13 |
|
301
|
|
|
* |
|
302
|
|
|
* @param $parent_id (int) the destination post |
|
303
|
|
|
* @return $revision_id (int) the source post |
|
304
|
|
|
*/ |
|
|
|
|
|
|
305
|
|
|
|
|
306
|
|
|
function wp_restore_post_revision( $post_id, $revision_id ) { |
|
|
|
|
|
|
307
|
|
|
|
|
308
|
|
|
// global |
|
309
|
|
|
global $wpdb; |
|
310
|
|
|
|
|
311
|
|
|
|
|
312
|
|
|
// get field objects |
|
313
|
|
|
$custom_fields = get_post_custom( $revision_id ); |
|
314
|
|
|
|
|
315
|
|
|
|
|
316
|
|
|
// populate vars |
|
317
|
|
|
if( !empty($custom_fields) ) |
|
318
|
|
|
{ |
|
319
|
|
|
foreach( $custom_fields as $k => $v ) |
|
320
|
|
|
{ |
|
321
|
|
|
// value is always an array |
|
322
|
|
|
$v = $v[0]; |
|
323
|
|
|
|
|
324
|
|
|
|
|
325
|
|
|
// bail early if $value is not is a field_key |
|
326
|
|
|
if( !acf_is_field_key($v) ) |
|
327
|
|
|
{ |
|
328
|
|
|
continue; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
|
|
332
|
|
|
// remove prefix '_' field from reference |
|
333
|
|
|
$field_name = substr($k, 1); |
|
334
|
|
|
|
|
335
|
|
|
|
|
336
|
|
|
// bail early if value could not be found |
|
337
|
|
|
if( !isset($custom_fields[ $field_name ][0]) ) |
|
338
|
|
|
{ |
|
339
|
|
|
continue; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
|
|
343
|
|
|
update_post_meta( $post_id, $field_name, $custom_fields[ $field_name ][0] ); |
|
344
|
|
|
|
|
345
|
|
|
|
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
|
|
350
|
|
|
|
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
|
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
new acf_revisions(); |
|
357
|
|
|
|
|
358
|
|
|
?> |
|
|
|
|
|
|
359
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.