<?php

if (!defined('ABSPATH')) {
    exit;
}
if (!class_exists('ACF_Media')) {
    class ACF_Media
    {
        public function __construct()
        {
            add_action('acf/enqueue_scripts', array($this, 'enqueue_scripts'));
            add_action('acf/save_post', array($this, 'save_files'), 5, 1);
            add_filter('wp_handle_upload_prefilter', array($this, 'handle_upload_prefilter'), 10, 1);
            add_action('wp_ajax_query-attachments', array($this, 'wp_ajax_query_attachments'), -1);
        }
        public function enqueue_scripts()
        {
            if (wp_script_is('acf-input')) {
                acf_localize_text(array('Select.verb' => _x('Select', 'verb', 'acf'), 'Edit.verb' => _x('Edit', 'verb', 'acf'), 'Update.verb' => _x('Update', 'verb', 'acf'), 'Uploaded to this post' => __('Uploaded to this post', 'acf'), 'Expand Details' => __('Expand Details', 'acf'), 'Collapse Details' => __('Collapse Details', 'acf'), 'Restricted' => __('Restricted', 'acf'), 'All images' => __('All images', 'acf')));
                acf_localize_data(array('mimeTypeIcon' => wp_mime_type_icon(), 'mimeTypes' => get_allowed_mime_types()));
            }
        }
        public function save_files($post_id = 0)
        {
            if (isset($_FILES['acf']['name'])) {
                acf_upload_files();
            }
        }
        public function handle_upload_prefilter($file)
        {
            $field = $this->get_source_field();
            if (!$field) {
                return $file;
            }
            $errors = acf_validate_attachment($file, $field, 'upload');
            $errors = apply_filters("acf/upload_prefilter/type={$field['type']}", $errors, $file, $field);
            $errors = apply_filters("acf/upload_prefilter/name={$field['_name']}", $errors, $file, $field);
            $errors = apply_filters("acf/upload_prefilter/key={$field['key']}", $errors, $file, $field);
            $errors = apply_filters('acf/upload_prefilter', $errors, $file, $field);
            if (!empty($errors)) {
                $file['error'] = implode("\n", $errors);
            }
            add_filter('image_size_names_choose', array($this, 'image_size_names_choose'), 10, 1);
            return $file;
        }
        private function get_source_field()
        {
            $field = false;
            if (isset($_POST['query']['_acfuploader'])) {
                $field_key = (string) $_POST['query']['_acfuploader'];
            } elseif (isset($_POST['_acfuploader'])) {
                $field_key = (string) $_POST['_acfuploader'];
            }
            if (isset($field_key)) {
                $field = acf_get_field($field_key);
            }
            return $field;
        }
        function wp_ajax_query_attachments()
        {
            if ($this->get_source_field()) {
                add_filter('wp_prepare_attachment_for_js', array($this, 'wp_prepare_attachment_for_js'), 10, 3);
                add_filter('image_size_names_choose', array($this, 'image_size_names_choose'), 10, 1);
            } else {
                add_filter('wp_prepare_attachment_for_js', array($this, 'clear_acf_errors_for_core_requests'), 5, 3);
            }
        }
        function clear_acf_errors_for_core_requests($response, $attachment, $meta)
        {
            $response['acf_errors'] = false;
            return $response;
        }
        function wp_prepare_attachment_for_js($response, $attachment, $meta)
        {
            $field = $this->get_source_field();
            $errors = acf_validate_attachment($response, $field, 'prepare');
            $response['acf_errors'] = false;
            if (!empty($errors)) {
                $response['acf_errors'] = implode('<br />', $errors);
            }
            return $response;
        }
        function image_size_names_choose($size_names)
        {
            $field = $this->get_source_field();
            if (isset($field['preview_size'])) {
                $name = (string) $field['preview_size'];
                $size_names[$name] = $name;
            }
            return $size_names;
        }
    }
    acf_new_instance('ACF_Media');
}