<?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 ($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();
}
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;
}