123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- <?php
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- if ( ! class_exists( 'acf_revisions' ) ) :
- class acf_revisions {
-
- var $cache = array();
-
- function __construct() {
-
- add_action( 'wp_restore_post_revision', array( $this, 'wp_restore_post_revision' ), 10, 2 );
-
- add_filter( 'wp_save_post_revision_check_for_changes', array( $this, 'wp_save_post_revision_check_for_changes' ), 10, 3 );
- add_filter( '_wp_post_revision_fields', array( $this, 'wp_preview_post_fields' ), 10, 2 );
- add_filter( '_wp_post_revision_fields', array( $this, 'wp_post_revision_fields' ), 10, 2 );
- add_filter( 'acf/validate_post_id', array( $this, 'acf_validate_post_id' ), 10, 2 );
- }
-
- function wp_preview_post_fields( $fields ) {
-
- if ( acf_maybe_get_POST( 'wp-preview' ) !== 'dopreview' ) {
- return $fields;
- }
-
- if ( acf_maybe_get_POST( '_acf_changed' ) ) {
- $fields['_acf_changed'] = 'different than 1';
- }
-
- return $fields;
- }
-
- function wp_save_post_revision_check_for_changes( $return, $last_revision, $post ) {
-
- if ( acf_maybe_get_POST( '_acf_changed' ) ) {
- return false;
- }
-
- return $return;
- }
-
- function wp_post_revision_fields( $fields, $post = null ) {
-
- if ( acf_is_screen( 'revision' ) || acf_is_ajax( 'get-revision-diffs' ) ) {
-
- if ( acf_maybe_get_GET( 'action' ) === 'restore' ) {
- return $fields;
- }
-
- } else {
-
- return $fields;
- }
-
- $append = array();
- $order = array();
- $post_id = acf_maybe_get( $post, 'ID' );
-
- if ( ! $post_id ) {
- global $post;
- $post_id = $post->ID;
- }
-
- $meta = get_post_meta( $post_id );
-
- if ( ! $meta ) {
- return $fields;
- }
-
- foreach ( $meta as $name => $value ) {
-
- $key = acf_maybe_get( $meta, '_' . $name );
-
- if ( ! $key ) {
- continue;
- }
-
- $value = $value[0];
- $key = $key[0];
-
- $field = acf_get_field( $key );
- if ( ! $field ) {
- continue;
- }
-
- $field_title = $field['label'] . ' (' . $name . ')';
- $field_order = $field['menu_order'];
- $ancestors = acf_get_field_ancestors( $field );
-
- if ( ! empty( $ancestors ) ) {
-
- $count = count( $ancestors );
- $oldest = acf_get_field( $ancestors[ $count - 1 ] );
-
- $field_title = str_repeat( '- ', $count ) . $field_title;
- $field_order = $oldest['menu_order'] . '.1';
- }
-
- $append[ $name ] = $field_title;
- $order[ $name ] = $field_order;
-
- add_filter( "_wp_post_revision_field_{$name}", array( $this, 'wp_post_revision_field' ), 10, 4 );
- }
-
- if ( ! empty( $append ) ) {
-
- $prefix = '_';
-
- $append = acf_add_array_key_prefix( $append, $prefix );
- $order = acf_add_array_key_prefix( $order, $prefix );
-
- array_multisort( $order, $append );
-
- $append = acf_remove_array_key_prefix( $append, $prefix );
-
- $fields = $fields + $append;
- }
-
- return $fields;
- }
-
- function wp_post_revision_field( $value, $field_name, $post = null, $direction = false ) {
-
- if ( empty( $value ) ) {
- return $value;
- }
- $value = maybe_unserialize( $value );
- $post_id = $post->ID;
-
- $field = acf_maybe_get_field( $field_name, $post_id );
-
- if ( is_array( $value ) ) {
- $value = implode( ', ', $value );
- } elseif ( is_object( $value ) ) {
- $value = serialize( $value );
- }
-
- if ( is_array( $field ) && isset( $field['type'] ) && ( $field['type'] === 'image' || $field['type'] === 'file' ) ) {
- $url = wp_get_attachment_url( $value );
- $value = $value . ' (' . $url . ')';
- }
- return $value;
- }
-
- function wp_restore_post_revision( $post_id, $revision_id ) {
-
- acf_copy_postmeta( $revision_id, $post_id );
-
-
- $revision = acf_get_post_latest_revision( $post_id );
-
- if ( $revision ) {
-
- acf_copy_postmeta( $revision_id, $revision->ID );
- }
- }
-
- function acf_validate_post_id( $post_id, $_post_id ) {
-
-
- if ( ! isset( $_GET['preview'] ) ) {
- return $post_id;
- }
-
- if ( ! is_numeric( $post_id ) ) {
- return $post_id;
- }
-
- $k = $post_id;
- $preview_id = 0;
-
- if ( isset( $this->cache[ $k ] ) ) {
- return $this->cache[ $k ];
- }
-
- if ( isset( $_GET['preview_id'] ) ) {
- $preview_id = (int) $_GET['preview_id'];
- } elseif ( isset( $_GET['p'] ) ) {
- $preview_id = (int) $_GET['p'];
- } elseif ( isset( $_GET['page_id'] ) ) {
- $preview_id = (int) $_GET['page_id'];
- }
-
-
- if ( $preview_id != $post_id ) {
- return $post_id;
- }
-
- $revision = acf_get_post_latest_revision( $post_id );
-
- if ( $revision && $revision->post_parent == $post_id ) {
- $post_id = (int) $revision->ID;
- }
-
- $this->cache[ $k ] = $post_id;
-
- return $post_id;
- }
- }
-
- acf()->revisions = new acf_revisions();
- endif;
- function acf_save_post_revision( $post_id = 0 ) {
-
- $revision = acf_get_post_latest_revision( $post_id );
-
- if ( $revision ) {
- acf_copy_postmeta( $post_id, $revision->ID );
- }
- }
- function acf_get_post_latest_revision( $post_id ) {
-
- $revisions = wp_get_post_revisions( $post_id );
-
- $revision = array_shift( $revisions );
-
- return $revision;
- }
|