| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
if ( ! class_exists( 'acf_revisions' ) ) : |
| 8 |
|
| 9 |
class acf_revisions { |
| 10 |
|
| 11 |
// vars |
| 12 |
var $cache = array(); |
| 13 |
|
| 14 |
|
| 15 |
/* |
| 16 |
* __construct |
| 17 |
* |
| 18 |
* A good place to add actions / filters |
| 19 |
* |
| 20 |
* @type function |
| 21 |
* @date 11/08/13 |
| 22 |
* |
| 23 |
* @param N/A |
| 24 |
* @return N/A |
| 25 |
*/ |
| 26 |
|
| 27 |
function __construct() { |
| 28 |
|
| 29 |
// actions |
| 30 |
add_action( 'wp_restore_post_revision', array( $this, 'wp_restore_post_revision' ), 10, 2 ); |
| 31 |
|
| 32 |
// filters |
| 33 |
add_filter( 'wp_save_post_revision_check_for_changes', array( $this, 'wp_save_post_revision_check_for_changes' ), 10, 3 ); |
| 34 |
add_filter( '_wp_post_revision_fields', array( $this, 'wp_preview_post_fields' ), 10, 2 ); |
| 35 |
add_filter( '_wp_post_revision_fields', array( $this, 'wp_post_revision_fields' ), 10, 2 ); |
| 36 |
add_filter( 'acf/validate_post_id', array( $this, 'acf_validate_post_id' ), 10, 2 ); |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
/* |
| 42 |
* wp_preview_post_fields |
| 43 |
* |
| 44 |
* This function is used to trick WP into thinking that one of the $post's fields has changed and |
| 45 |
* will allow an autosave to be updated. |
| 46 |
* Fixes an odd bug causing the preview page to render the non autosave post data on every odd attempt |
| 47 |
* |
| 48 |
* @type function |
| 49 |
* @date 21/10/2014 |
| 50 |
* @since 5.1.0 |
| 51 |
* |
| 52 |
* @param $fields (array) |
| 53 |
* @return $fields |
| 54 |
*/ |
| 55 |
|
| 56 |
function wp_preview_post_fields( $fields ) { |
| 57 |
|
| 58 |
// bail early if not previewing a post |
| 59 |
if ( acf_maybe_get_POST( 'wp-preview' ) !== 'dopreview' ) { |
| 60 |
return $fields; |
| 61 |
} |
| 62 |
|
| 63 |
// add to fields if ACF has changed |
| 64 |
if ( acf_maybe_get_POST( '_acf_changed' ) ) { |
| 65 |
|
| 66 |
$fields['_acf_changed'] = 'different than 1'; |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
// return |
| 71 |
return $fields; |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
/* |
| 77 |
* wp_save_post_revision_check_for_changes |
| 78 |
* |
| 79 |
* This filter will return false and force WP to save a revision. This is required due to |
| 80 |
* WP checking only post_title, post_excerpt and post_content values, not custom fields. |
| 81 |
* |
| 82 |
* @type filter |
| 83 |
* @date 19/09/13 |
| 84 |
* |
| 85 |
* @param $return (boolean) defaults to true |
| 86 |
* @param $last_revision (object) the last revision that WP will compare against |
| 87 |
* @param $post (object) the $post that WP will compare against |
| 88 |
* @return $return (boolean) |
| 89 |
*/ |
| 90 |
|
| 91 |
function wp_save_post_revision_check_for_changes( $return, $last_revision, $post ) { |
| 92 |
|
| 93 |
// if acf has changed, return false and prevent WP from performing 'compare' logic |
| 94 |
if ( acf_maybe_get_POST( '_acf_changed' ) ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
// return |
| 99 |
return $return; |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/* |
| 105 |
* wp_post_revision_fields |
| 106 |
* |
| 107 |
* This filter will add the ACF fields to the returned array |
| 108 |
* Versions 3.5 and 3.6 of WP feature different uses of the revisions filters, so there are |
| 109 |
* some hacks to allow both versions to work correctly |
| 110 |
* |
| 111 |
* @type filter |
| 112 |
* @date 11/08/13 |
| 113 |
* |
| 114 |
* @param $post_id (int) |
| 115 |
* @return $post_id (int) |
| 116 |
*/ |
| 117 |
|
| 118 |
function wp_post_revision_fields( $fields, $post = null ) { |
| 119 |
|
| 120 |
// validate page |
| 121 |
if ( acf_is_screen( 'revision' ) || acf_is_ajax( 'get-revision-diffs' ) ) { |
| 122 |
|
| 123 |
// bail early if is restoring |
| 124 |
if ( acf_maybe_get_GET( 'action' ) === 'restore' ) { |
| 125 |
return $fields; |
| 126 |
} |
| 127 |
|
| 128 |
// allow |
| 129 |
|
| 130 |
} else { |
| 131 |
|
| 132 |
// bail early (most likely saving a post) |
| 133 |
return $fields; |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
// vars |
| 138 |
$append = array(); |
| 139 |
$order = array(); |
| 140 |
$post_id = acf_maybe_get( $post, 'ID' ); |
| 141 |
|
| 142 |
// compatibility with WP < 4.5 (test) |
| 143 |
if ( ! $post_id ) { |
| 144 |
|
| 145 |
global $post; |
| 146 |
$post_id = $post->ID; |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
// get all postmeta |
| 151 |
$meta = get_post_meta( $post_id ); |
| 152 |
|
| 153 |
// bail early if no meta |
| 154 |
if ( ! $meta ) { |
| 155 |
return $fields; |
| 156 |
} |
| 157 |
|
| 158 |
// loop |
| 159 |
foreach ( $meta as $name => $value ) { |
| 160 |
|
| 161 |
// attempt to find key value |
| 162 |
$key = acf_maybe_get( $meta, '_' . $name ); |
| 163 |
|
| 164 |
// bail ealry if no key |
| 165 |
if ( ! $key ) { |
| 166 |
continue; |
| 167 |
} |
| 168 |
|
| 169 |
// update vars |
| 170 |
$value = $value[0]; |
| 171 |
$key = $key[0]; |
| 172 |
|
| 173 |
// Load field. |
| 174 |
$field = acf_get_field( $key ); |
| 175 |
if ( ! $field ) { |
| 176 |
continue; |
| 177 |
} |
| 178 |
|
| 179 |
// get field |
| 180 |
$field_title = $field['label'] . ' (' . $name . ')'; |
| 181 |
$field_order = $field['menu_order']; |
| 182 |
$ancestors = acf_get_field_ancestors( $field ); |
| 183 |
|
| 184 |
// ancestors |
| 185 |
if ( ! empty( $ancestors ) ) { |
| 186 |
|
| 187 |
// vars |
| 188 |
$count = count( $ancestors ); |
| 189 |
$oldest = acf_get_field( $ancestors[ $count - 1 ] ); |
| 190 |
|
| 191 |
// update vars |
| 192 |
$field_title = str_repeat( '- ', $count ) . $field_title; |
| 193 |
$field_order = $oldest['menu_order'] . '.1'; |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
// append |
| 198 |
$append[ $name ] = $field_title; |
| 199 |
$order[ $name ] = $field_order; |
| 200 |
|
| 201 |
// hook into specific revision field filter and return local value |
| 202 |
add_filter( "_wp_post_revision_field_{$name}", array( $this, 'wp_post_revision_field' ), 10, 4 ); |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
// append |
| 207 |
if ( ! empty( $append ) ) { |
| 208 |
|
| 209 |
// vars |
| 210 |
$prefix = '_'; |
| 211 |
|
| 212 |
// add prefix |
| 213 |
$append = acf_add_array_key_prefix( $append, $prefix ); |
| 214 |
$order = acf_add_array_key_prefix( $order, $prefix ); |
| 215 |
|
| 216 |
// sort by name (orders sub field values correctly) |
| 217 |
array_multisort( $order, $append ); |
| 218 |
|
| 219 |
// remove prefix |
| 220 |
$append = acf_remove_array_key_prefix( $append, $prefix ); |
| 221 |
|
| 222 |
// append |
| 223 |
$fields = $fields + $append; |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
// return |
| 228 |
return $fields; |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
/* |
| 234 |
* wp_post_revision_field |
| 235 |
* |
| 236 |
* This filter will load the value for the given field and return it for rendering |
| 237 |
* |
| 238 |
* @type filter |
| 239 |
* @date 11/08/13 |
| 240 |
* |
| 241 |
* @param $value (mixed) should be false as it has not yet been loaded |
| 242 |
* @param $field_name (string) The name of the field |
| 243 |
* @param $post (mixed) Holds the $post object to load from - in WP 3.5, this is not passed! |
| 244 |
* @param $direction (string) to / from - not used |
| 245 |
* @return $value (string) |
| 246 |
*/ |
| 247 |
|
| 248 |
function wp_post_revision_field( $value, $field_name, $post = null, $direction = false ) { |
| 249 |
|
| 250 |
// bail ealry if is empty |
| 251 |
if ( empty( $value ) ) { |
| 252 |
return $value; |
| 253 |
} |
| 254 |
|
| 255 |
// value has not yet been 'maybe_unserialize' |
| 256 |
$value = maybe_unserialize( $value ); |
| 257 |
|
| 258 |
// vars |
| 259 |
$post_id = $post->ID; |
| 260 |
|
| 261 |
// load field |
| 262 |
$field = acf_maybe_get_field( $field_name, $post_id ); |
| 263 |
|
| 264 |
// default formatting |
| 265 |
if ( is_array( $value ) ) { |
| 266 |
|
| 267 |
$value = implode( ', ', $value ); |
| 268 |
|
| 269 |
} elseif ( is_object( $value ) ) { |
| 270 |
|
| 271 |
$value = serialize( $value ); |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
// image |
| 276 |
if ( $field['type'] == 'image' || $field['type'] == 'file' ) { |
| 277 |
|
| 278 |
$url = wp_get_attachment_url( $value ); |
| 279 |
$value = $value . ' (' . $url . ')'; |
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
// return |
| 284 |
return $value; |
| 285 |
|
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
/* |
| 290 |
* wp_restore_post_revision |
| 291 |
* |
| 292 |
* This action will copy and paste the metadata from a revision to the post |
| 293 |
* |
| 294 |
* @type action |
| 295 |
* @date 11/08/13 |
| 296 |
* |
| 297 |
* @param $parent_id (int) the destination post |
| 298 |
* @return $revision_id (int) the source post |
| 299 |
*/ |
| 300 |
|
| 301 |
function wp_restore_post_revision( $post_id, $revision_id ) { |
| 302 |
|
| 303 |
// copy postmeta from revision to post (restore from revision) |
| 304 |
acf_copy_postmeta( $revision_id, $post_id ); |
| 305 |
|
| 306 |
// Make sure the latest revision is also updated to match the new $post data |
| 307 |
// get latest revision |
| 308 |
$revision = acf_get_post_latest_revision( $post_id ); |
| 309 |
|
| 310 |
// save |
| 311 |
if ( $revision ) { |
| 312 |
|
| 313 |
// copy postmeta from revision to latest revision (potentialy may be the same, but most likely are different) |
| 314 |
acf_copy_postmeta( $revision_id, $revision->ID ); |
| 315 |
|
| 316 |
} |
| 317 |
|
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
/* |
| 322 |
* acf_validate_post_id |
| 323 |
* |
| 324 |
* This function will modify the $post_id and allow loading values from a revision |
| 325 |
* |
| 326 |
* @type function |
| 327 |
* @date 6/3/17 |
| 328 |
* @since 5.5.10 |
| 329 |
* |
| 330 |
* @param $post_id (int) |
| 331 |
* @param $_post_id (int) |
| 332 |
* @return $post_id (int) |
| 333 |
*/ |
| 334 |
|
| 335 |
function acf_validate_post_id( $post_id, $_post_id ) { |
| 336 |
|
| 337 |
// bail early if no preview in URL |
| 338 |
if ( ! isset( $_GET['preview'] ) ) { |
| 339 |
return $post_id; |
| 340 |
} |
| 341 |
|
| 342 |
// bail early if $post_id is not numeric |
| 343 |
if ( ! is_numeric( $post_id ) ) { |
| 344 |
return $post_id; |
| 345 |
} |
| 346 |
|
| 347 |
// vars |
| 348 |
$k = $post_id; |
| 349 |
$preview_id = 0; |
| 350 |
|
| 351 |
// check cache |
| 352 |
if ( isset( $this->cache[ $k ] ) ) { |
| 353 |
return $this->cache[ $k ]; |
| 354 |
} |
| 355 |
|
| 356 |
// validate |
| 357 |
if ( isset( $_GET['preview_id'] ) ) { |
| 358 |
|
| 359 |
$preview_id = (int) $_GET['preview_id']; |
| 360 |
|
| 361 |
} elseif ( isset( $_GET['p'] ) ) { |
| 362 |
|
| 363 |
$preview_id = (int) $_GET['p']; |
| 364 |
|
| 365 |
} elseif ( isset( $_GET['page_id'] ) ) { |
| 366 |
|
| 367 |
$preview_id = (int) $_GET['page_id']; |
| 368 |
|
| 369 |
} |
| 370 |
|
| 371 |
// bail early id $preview_id does not match $post_id |
| 372 |
if ( $preview_id != $post_id ) { |
| 373 |
return $post_id; |
| 374 |
} |
| 375 |
|
| 376 |
// attempt find revision |
| 377 |
$revision = acf_get_post_latest_revision( $post_id ); |
| 378 |
|
| 379 |
// save |
| 380 |
if ( $revision && $revision->post_parent == $post_id ) { |
| 381 |
|
| 382 |
$post_id = (int) $revision->ID; |
| 383 |
|
| 384 |
} |
| 385 |
|
| 386 |
// set cache |
| 387 |
$this->cache[ $k ] = $post_id; |
| 388 |
|
| 389 |
// return |
| 390 |
return $post_id; |
| 391 |
|
| 392 |
} |
| 393 |
|
| 394 |
} |
| 395 |
|
| 396 |
// initialize |
| 397 |
acf()->revisions = new acf_revisions(); |
| 398 |
|
| 399 |
endif; // class_exists check |
| 400 |
|
| 401 |
|
| 402 |
/* |
| 403 |
* acf_save_post_revision |
| 404 |
* |
| 405 |
* This function will copy meta from a post to it's latest revision |
| 406 |
* |
| 407 |
* @type function |
| 408 |
* @date 26/09/2016 |
| 409 |
* @since 5.4.0 |
| 410 |
* |
| 411 |
* @param $post_id (int) |
| 412 |
* @return n/a |
| 413 |
*/ |
| 414 |
|
| 415 |
function acf_save_post_revision( $post_id = 0 ) { |
| 416 |
|
| 417 |
// get latest revision |
| 418 |
$revision = acf_get_post_latest_revision( $post_id ); |
| 419 |
|
| 420 |
// save |
| 421 |
if ( $revision ) { |
| 422 |
|
| 423 |
acf_copy_postmeta( $post_id, $revision->ID ); |
| 424 |
|
| 425 |
} |
| 426 |
|
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
/* |
| 431 |
* acf_get_post_latest_revision |
| 432 |
* |
| 433 |
* This function will return the latest revision for a given post |
| 434 |
* |
| 435 |
* @type function |
| 436 |
* @date 25/06/2016 |
| 437 |
* @since 5.3.8 |
| 438 |
* |
| 439 |
* @param $post_id (int) |
| 440 |
* @return $post_id (int) |
| 441 |
*/ |
| 442 |
|
| 443 |
function acf_get_post_latest_revision( $post_id ) { |
| 444 |
|
| 445 |
// vars |
| 446 |
$revisions = wp_get_post_revisions( $post_id ); |
| 447 |
|
| 448 |
// shift off and return first revision (will return null if no revisions) |
| 449 |
$revision = array_shift( $revisions ); |
| 450 |
|
| 451 |
// return |
| 452 |
return $revision; |
| 453 |
|
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|