<?php
/**
 * Plugin Name: [Forminator] Upload mapping to ACF image field
 * Description: Solves a bug that prevents upload field mapping to ACF image field through post data form
 * Author: Prashant Singh @ WPMUDEV
 * Author URI: https://premium.wpmudev.org
 * License: GPLv2 or later
 */

if (!defined('ABSPATH')) {
    exit;
}

if (defined('WP_CLI') && WP_CLI) {
    return;
}

add_filter('forminator_custom_form_submit_errors', 'wpmudev_map_uplods_acf_post_test', 10, 3);
function wpmudev_map_uplods_acf_post_test($submit_errors, $form_id, $field_data_array)
{
    $form_ids = array(1684, 2910); //Please change form IDs to your form IDs
    if (!in_array($form_id, $form_ids)) {
        return $submit_errors;
    }

    $submitted_data = Forminator_CForm_Front_Action::$prepared_data;

    if (isset($submitted_data['postdata-1']) && is_array($submitted_data['postdata-1'])) {
        if (isset($submitted_data['postdata-1']['post-custom']) && is_array($submitted_data['postdata-1']['post-custom'])) {
            foreach ($submitted_data['postdata-1']['post-custom'] as $post_key => $post_val) {
                if (strpos($post_val['value'], '{upload') !== false) {
                    $field_name = str_replace('{', '', $post_val['value']);
                    $field_name = str_replace('}', '', $field_name);
                    if (is_array($submitted_data[$field_name]) && isset($submitted_data[$field_name]['file'])) {
                        if (isset($submitted_data[$field_name]['file']['file_url'])) {
                            Forminator_CForm_Front_Action::$prepared_data['postdata-1']['post-custom'][$post_key]['value'] = $submitted_data[$field_name]['file']['file_url'];
                        }
                    }
                }
            }
        }
    }

    return $submit_errors;
}

add_action('forminator_post_data_field_post_saved', 'wpmudev_update_post_acf_uploads_test', 10, 4);
function wpmudev_update_post_acf_uploads_test($post_id, $field, $data, $cls)
{
    $submitted_data = Forminator_CForm_Front_Action::$prepared_data;
    $form_ids = array(1684, 2910); //Please change form IDs to your form IDs
    $acf_fields = ['image_two', 'image_three', 'image_four']; // Change depending on number of uploads fileds.

    if (!in_array($submitted_data['form_id'], $form_ids)) {
        return;
    }

    if (isset($submitted_data['postdata-1']) && is_array($submitted_data['postdata-1'])) {
        if (isset($submitted_data['postdata-1']['post-custom']) && is_array($submitted_data['postdata-1']['post-custom'])) {
            foreach ($submitted_data['postdata-1']['post-custom'] as $post_key => $post_val) {

                foreach ($acf_fields as $field => $key) {
                    if ($post_val['key'] == $key) {
                        if (isset($post_val['value'])) {
                            $attach_id = attachment_url_to_postid($post_val['value']);
                            if ($attach_id) {
                                update_field($post_val['key'], $attach_id, $post_id);
                                $mime_type = wp_get_image_mime($post_val['value']);
                                if ($mime_type) {
                                    $update_data = array(
                                        'ID' => $attach_id,
                                        'post_mime_type' => $mime_type,
                                    );
                                    wp_update_post($update_data);
                                }
                            }
                        }
                    }
                }

            }
        }
    }
}