<?php
/**
 * WP Ultimate CSV Importer plugin file.
 *
 * Copyright (C) 2010-2020, Smackcoders Inc - info@smackcoders.com
 */

namespace Smackcoders\UCI\Core;

if ( ! defined( 'ABSPATH' ) )
	exit; // Exit if accessed directly

class MediaHandling{
	private static $instance=null,$smack_instance;
	public $header_array;
	public $value_array;

	/** @var string Last image import failure (logs + detailed log messages). */
	private static $last_image_import_failure = '';

	public function __construct(){
		
		include_once(ABSPATH . 'wp-admin/includes/image.php');
		add_action('wp_ajax_zip_upload' , array($this , 'zipImageUpload'));	
		add_action('wp_ajax_image_options', array($this , 'imageOptions'));
		add_action('wp_ajax_delete_image' , array($this , 'deleteImage'));
	}

	public static function imageOptions()
	{
		check_ajax_referer('smack-ultimate-csv-importer', 'securekey');
		$media_settings['use_ExistingImage'] = sanitize_text_field($_POST['use_ExistingImage'] ?? '');
		$media_settings['overwriteImage'] = sanitize_text_field($_POST['overwriteImage'] ?? '');
		$media_settings['enable_postcontent_image'] = sanitize_text_field($_POST['postContent_image_option'] ?? '');
		$media_settings['newImage'] = sanitize_text_field($_POST['newImage'] ?? '');
		$media_settings['title'] = sanitize_text_field($_POST['title'] ?? '');
		$media_settings['caption'] = sanitize_text_field($_POST['caption'] ?? '');
		$media_settings['alttext'] = sanitize_text_field($_POST['alttext'] ?? '');
		$media_settings['description'] = sanitize_text_field($_POST['description'] ?? '');
		$media_settings['file_name'] = sanitize_text_field($_POST['file_name'] ?? '');
		$media_settings['media_handle_option'] = sanitize_text_field($_POST['media_handle_option'] ?? '');
		$image_info = array(
			'media_settings'  => $media_settings
		);
		update_option('smack_image_options', $image_info);
		$result['success'] = 'true';
		echo wp_json_encode($result);
		wp_die();
	}

	public static function getInstance()
	{
		if (MediaHandling::$instance == null) {
			MediaHandling::$instance = new MediaHandling;
			MediaHandling::$smack_instance = UCICore::getInstance();
			return MediaHandling::$instance;
		}
		return MediaHandling::$instance;
	}




	public function zipImageUpload()
	{
		check_ajax_referer('smack-ultimate-csv-importer', 'securekey');
		// Check if the ZIP extension is loaded
		if (!extension_loaded('zip')) {
			$result['success'] = false;
			$result['message'] = 'The PHP ZIP extension is not installed. Please contact your server administrator to install it.';
			echo wp_json_encode($result);
			wp_die();
		}
		// Check if a file was uploaded
		if (isset($_FILES['zipFile']['name'])) {
			$zip_file_name = $_FILES['zipFile']['name'];
			$file_ext = pathinfo($zip_file_name, PATHINFO_EXTENSION);

			// Validate file extension
			if (strtolower($file_ext) !== 'zip') {
				$result['success'] = false;
				$result['message'] = 'Invalid file format. Please upload a zip file.';
				echo wp_json_encode($result);
				wp_die();
			}

			$hash_key = MediaHandling::$smack_instance->convert_string2hash_key($zip_file_name);
			$upload_dir = MediaHandling::$smack_instance->create_upload_dir();
			$path = $upload_dir . $hash_key . '.zip';
			$extract_base = $this->get_separate_import_images_dir();
			if ($extract_base === '') {
				$result['success'] = false;
				$result['message'] = 'Upload directory is not available.';
				echo wp_json_encode($result);
				wp_die();
			}
			if (!is_dir($extract_base)) {
				wp_mkdir_p($extract_base);
			}
			$index_php = trailingslashit($extract_base) . 'index.php';
			if (!file_exists($index_php)) {
				file_put_contents($index_php, "<?php\n");
			}
			@chmod($extract_base, 0755);
			$extract_path = trailingslashit($extract_base);

			if (file_exists($path)) {
				chmod($path, 0777);
			}

			move_uploaded_file($_FILES['zipFile']['tmp_name'], $path);
			$zip = new \ZipArchive;
			$res = $zip->open($path);

			if ($res === TRUE) {
				$filename = [];
				$size = [];
				$kbsize = [];

				for ($i = 0; $i < $zip->numFiles; $i++) {

					$filename[$i] = $zip->getNameIndex($i);
					if (substr($zip->getNameIndex($i), -1) == '/' || !$this->isValidImageFile($filename[$i])) {
						continue;
					}
					$sanitized_filename = str_replace(' ', '-', basename($filename[$i]));
					$sanitized_filename = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $sanitized_filename);
					$full_extract_path = $extract_path . $sanitized_filename;
					$fp = $zip->getStream($filename[$i]);
					$size[$i] = $zip->statIndex($i)['size'];
					$kbsize[$i] = $this->convertToReadableSize($size[$i]);
					$ofp = fopen($full_extract_path, 'w');
					while (!feof($fp)) {
						fwrite($ofp, fread($fp, 8192));
					}

					fclose($fp);
					fclose($ofp);
				}
				$filename = $this->ValidImageFileAdded($filename);
				$kbsize = $this->ImageFileSizeAdded($filename, $kbsize);
				$zip->close();
				$result['success'] = true;
				$result['zip_file_name'] = $zip_file_name;
				$result['count'] = count($filename);
				$result['filename'] = $filename;
				$result['size'] = $kbsize;
			} else {
				$result['success'] = false;
				$result['message'] = 'Failed to open the zip file.';
			}
		} else {
			$result['success'] = false;
			$result['message'] = 'No file uploaded.';
		}

		echo wp_json_encode($result);
		wp_die();
	}
	public function ImageFileSizeAdded($filenames, $sizes)
	{
		$newSizes = [];
		$sizes = array_values($sizes);
		$sizeIndex = 0;
		foreach ($filenames as $index => $filename) {
			if (substr($filename, -1) === '/') {
				// Skip if it's a directory
				continue;
			} else {
				if (isset($sizes[$sizeIndex])) {
					$newSizes[$index] = $sizes[$sizeIndex];
					$sizeIndex++;
				}
			}
		}
		return $newSizes;
	}
	public function ValidImageFileAdded($filename)
	{
		$validExtensions = ['jpg', 'jpeg', 'png', 'svg','webp','bmp'];
		$filteredFiles = [];
		for ($i = 0; $i < count($filename); $i++) {
			$fileInfo = pathinfo($filename[$i]);
			if ( strpos($filename[$i], '__MACOSX/') === 0 || (substr($fileInfo['basename'], 0, 1) === '.') && (!in_array(strtolower($fileInfo['extension']), $validExtensions))) {
				continue;
			} else {
				if (substr($filename[$i], -1) === '/' || in_array(strtolower($fileInfo['extension']), $validExtensions)) {
					$filteredFiles[$i] = $filename[$i];
				}
			}
		}
		$filenames = array_values($filteredFiles);
		return $filenames;
	}
	public function isValidImageFile($filename)
	{
		$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
		if (in_array($ext, ['jpg', 'jpeg', 'png', 'svg','webp', 'bmp'])) {
			return true;
		} else {
			return false;
		}
	}
	public function convertToReadableSize($size)
	{
		$base = log($size) / log(1024);
		$suffix = array("", "KB", "MB", "GB", "TB");
		$f_base = floor($base);
		return round(pow(1024, $base - floor($base)), 1) . $suffix[$f_base];
	}

	public function deleteImage()
	{
		if (!is_user_logged_in() || !current_user_can('manage_options')) {
			wp_send_json_error(['message' => 'Unauthorized access.'], 403);
			return;
		}
	
		// Verify nonce for CSRF protection
		check_ajax_referer('smack-ultimate-csv-importer', 'securekey');
	
		// Decode JSON safely
		$images = json_decode(stripslashes($_POST['images']), true);
		if (!empty($images)) {
		if (empty($images) || !is_array($images)) {
			wp_send_json_error(['message' => 'Invalid input.'], 400);
			return;
		}
	
		$uploads = wp_upload_dir();
		$basedir_real = !empty($uploads['basedir']) ? realpath($uploads['basedir']) : false;
		$pool_abs = $this->get_separate_import_images_dir();
		$pool_real = ($pool_abs !== '' && is_dir($pool_abs)) ? realpath($pool_abs) : false;
		$media_dir = wp_get_upload_dir();
		$upload_path_month = !empty($media_dir['path']) ? realpath($media_dir['path']) : false;
	
		if (!$basedir_real) {
			wp_send_json_error(['message' => 'Upload directory not found.'], 500);
			return;
		}
	
		foreach ($images as $image) {
			$deleteimage = sanitize_file_name(basename($image));
			$file_path = false;
			if ($pool_real && is_dir($pool_real)) {
				$file_path = realpath(trailingslashit($pool_real) . $deleteimage);
			}
			if ((!$file_path || !is_file($file_path)) && $upload_path_month) {
				$file_path = realpath(trailingslashit($upload_path_month) . $deleteimage);
			}
			if (!$file_path || !is_file($file_path)) {
				continue;
			}
			if (strpos($file_path, $basedir_real) !== 0) {
				wp_send_json_error(['message' => 'Invalid file path.'], 403);
				return;
			}
			unlink($file_path);
			$result['success'] = 'true';
		} 
	}
	else {
			$result['success'] = 'true';
		}


		echo wp_json_encode($result);
		wp_die();
	}

	public function media_handling($img_url, $post_id, $data_array = null, $plugin = null, $import_type = null, $acf_wpname_element = null, $templatekey = null, $unikey = null, $unikey_name = null, $header_array = null, $value_array = null, $wpml_array = null, $image_metas = null, $line_number = null, $indexs = null, $acf_image_meta = null, $media_type = null, $media_id = null, $jet_child_object_id = null, $parent_object_id = null, $media_mode = null,$featured= false)
	{
		global $wpdb;
		$img_url = trim(html_entity_decode(rawurldecode($img_url), ENT_QUOTES, 'UTF-8'));
		$url = parse_url($img_url);
		$media_handle = get_option('smack_image_options');
		if (isset($url['scheme']) && ($url['scheme'] == 'http' || $url['scheme'] == 'https')) {
			if (strstr($img_url, 'https://drive.google.com')) {
				preg_match('~/d/\K[^/]+(?=/)~', $img_url, $result_id);
				$image_title = isset($result_id[0]) ? $result_id[0] : basename($img_url);
			} else {
				$image_name = basename($img_url);
				$image_title = sanitize_file_name(pathinfo($image_name, PATHINFO_FILENAME));
			}
		} else {
			$image_title = preg_replace('/\\.[^.\\s]{3,4}$/', '', $img_url);
		}

		if (strpos($img_url, 'wp-ultimate-csv-importer-pro/assets/images/loading-image.jpg') !== false
			|| strpos($img_url, 'wp-ultimate-csv-importer/assets/images/loading-image.jpg') !== false) {
			$existing_loading_image_id = $wpdb->get_results("SELECT ID FROM {$wpdb->prefix}posts WHERE post_name LIKE 'loading-image%' AND guid LIKE '%loading-image%' ", ARRAY_A);
			if (!empty($existing_loading_image_id[0]['ID'])) {
				$attach_id = $existing_loading_image_id[0]['ID'];
				return $attach_id;
			}
		}

		// Download external images to your media if true
		if ($media_handle['media_settings']['media_handle_option'] == 'true') {
			if ($media_handle['media_settings']['use_ExistingImage'] == 'true') {
				if (empty($img_url)) {
					$attach_id = $img_url;
					if (!empty($data_array)) {
						foreach ($data_array as $values) {
							$attachment = $wpdb->get_var("SELECT meta_value FROM " . $wpdb->prefix . "postmeta WHERE meta_key='$values'");

							if ($attachment) {
								delete_post_thumbnail($post_id);
							} else {
								set_post_thumbnail($post_id, $attach_id);
								$this->imageMetaImport($attach_id, $media_handle, $header_array, $value_array,$featured, $media_type);
							}
							$this->imageMetaImport($attach_id, $media_handle, $header_array, $value_array,$featured, $media_type);
							return $attach_id;
						}
					}
				}

				if (is_numeric($img_url)) {
					$attach_id = $img_url;
					if ($attach_id == 0) {
						delete_post_thumbnail($post_id);
					} else {
						if (!empty($data_array['featured_image'])) {
							set_post_thumbnail($post_id, $attach_id);
						}
					}
					$this->imageMetaImport($attach_id, $media_handle, $header_array, $value_array,$featured, $media_type);
					return $attach_id;
				} else {
					$attachment_id = $wpdb->get_results("select ID from {$wpdb->prefix}posts where post_title='$image_title'", ARRAY_A);
				}
				if (!empty($attachment_id) && empty($media_mode)) {
					$wpml_multi_lang_mode = true;
					foreach ($attachment_id as $value) {
						if (!empty($wpml_array['language_code'])) {
							$attached_id = $value['ID'];
							$lang_code = $wpml_array['language_code'];
							$get_image_element_id = $wpdb->get_results("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type = 'post_attachment' AND element_id = $attached_id AND language_code = '$lang_code' ", ARRAY_A);
							if (!empty($get_image_element_id[0]['element_id'])) {
								$wpml_multi_lang_mode = false;
								$get_lang_image_id = $get_image_element_id[0]['element_id'];
								return $get_lang_image_id;
							}
						} else {
							$attach_id = $value['ID'];
							if (!wp_get_attachment_url($attach_id)) {
								$attach_id = $this->image_function($img_url, $post_id, $data_array, '', 'use_existing_image', $header_array, $value_array, $wpml_array, $unikey, $image_metas, $line_number, $plugin, $import_type, $acf_wpname_element, $templatekey, $indexs, $acf_image_meta, $media_type, $media_id, $jet_child_object_id, $parent_object_id, $media_mode,$featured);
							} else {
								if (!empty($data_array['featured_image'])) {
									set_post_thumbnail($post_id, $attach_id);
								}
							}
						}
					}

					if ((!empty($wpml_array['language_code'])) && ($wpml_multi_lang_mode)) {
						$attach_id = $this->image_function($img_url, $post_id, $data_array, '', 'use_existing_image', $header_array, $value_array, $wpml_array, $unikey, $image_metas, $line_number, $plugin, $import_type, $acf_wpname_element, $templatekey, $indexs, $acf_image_meta, $media_type, $media_id, $jet_child_object_id, $parent_object_id, $media_mode,$featured);
						return $attach_id;
					}

					$this->imageMetaImport($attach_id, $media_handle, $header_array, $value_array,$featured, $media_type);
				} else {
					$attach_id = $this->image_function($img_url, $post_id, $data_array, '', 'use_existing_image', $header_array, $value_array, $wpml_array, $unikey, $image_metas, $line_number, $plugin, $import_type, $acf_wpname_element, $templatekey, $indexs, $acf_image_meta, $media_type, $media_id, $jet_child_object_id, $parent_object_id, $media_mode,$featured);
					if (!empty($attach_id)) {
						$this->imageMetaImport($attach_id, $media_handle, $header_array, $value_array,$featured, $media_type);
					}
				}
			} elseif ($media_handle['media_settings']['overwriteImage'] == 'true') {

				// // Get the file name from the URL
				$fimg_name = @basename($img_url);
				$pathinfo = pathinfo($fimg_name);
				$imagename = $pathinfo['filename'];
				$image_title = str_replace(' ', '-', trim($imagename));
				// Replace dots with dashes and ensure lowercase
				$fil_name = strtolower(str_replace('.', '-', $image_title));
				// Remove any characters that are not alphanumeric, dashes, or underscores
				$fil_name = preg_replace('/[^a-z0-9-_]/', '', $fil_name);
				$fil_name = esc_sql($fil_name);
				$guid_like = esc_sql('%' . $wpdb->esc_like($fimg_name) . '%');
				if (!empty($media_handle['media_settings']['title'])) {
					$image_title   = $media_handle['media_settings']['title'];
				}
				$get_id = $wpdb->get_results("SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' AND post_name = '$fil_name' LIMIT 1", ARRAY_A);
				if (!empty($get_id)) {
					$attach_id = $get_id[0]['ID'];
					$this->overwrite($attach_id, $img_url);
					if (!empty($data_array['featured_image'])) {
						set_post_thumbnail($post_id, $attach_id);
					}
				} else {
					$attach_id = $this->image_function($img_url, $post_id, $data_array, '', '', $header_array, $value_array, [], $unikey, $image_metas, $line_number, $plugin, $import_type, $acf_wpname_element, $templatekey, $indexs, $acf_image_meta, $media_type, $media_id, $jet_child_object_id, $parent_object_id, $media_mode,$featured);
				}
				$this->imageMetaImport($attach_id, $media_handle, $header_array, $value_array,$featured, $media_type);
			} else {
				$attach_id = $this->image_function($img_url, $post_id, $data_array, '', '', $header_array, $value_array, [], $unikey, $image_metas, $line_number, $plugin, $import_type, $acf_wpname_element, $templatekey, $indexs, $acf_image_meta, $media_type, $media_id, $jet_child_object_id, $parent_object_id, $media_mode,$featured);
			}
		} else { // Use local url if available, leave the post as attachment free 

			$guid_url = $img_url;
			$attachment_id = $wpdb->get_results("select ID from {$wpdb->prefix}posts where guid = '$guid_url'", ARRAY_A);

			if (!empty($attachment_id)) {

				foreach ($attachment_id as $value) {
					$attach_id = $value['ID'];
					if ($_wp_attachment_metadata = get_post_meta($attach_id, '_wp_attachment_metadata', true)) {
						// When an attachment is available on Media and not has attachment link
						if (!is_array($_wp_attachment_metadata)) {
							$attach_id = $this->image_function($img_url, $post_id, $data_array, '', '', $header_array, $value_array, [], $unikey, $image_metas, $line_number, $plugin, $import_type, $acf_wpname_element, $templatekey, $indexs, $acf_image_meta, $media_type, $media_id, $jet_child_object_id, $parent_object_id, $media_mode,$featured);
						}
					}
				}
			} else {
				$attach_id = $this->image_function($img_url, $post_id, $data_array, '', '', $header_array, $value_array, [], $unikey, $image_metas, $line_number, $plugin, $import_type, $acf_wpname_element, $templatekey, $indexs, $acf_image_meta, $media_type, $media_id, $jet_child_object_id, $parent_object_id, $media_mode, $featured);
			}
		}
		$attach_id = isset($attach_id) ? $attach_id : '';
		return $attach_id;
	}

	public function mediaReport()
	{
		check_ajax_referer('smack-ultimate-csv-importer', 'securekey');
		global $wpdb;
		$list_of_images = $wpdb->get_results("select * from {$wpdb->prefix}ultimate_csv_importer_media_report  ", ARRAY_A);

		if (!empty($list_of_images)) {
			foreach ($list_of_images as $list_key => $list_val) {
				if (!empty($list_val['hash_key'])) {
					$file_name = $wpdb->get_results("select file_name from {$wpdb->prefix}smackcsv_file_events where hash_key = '{$list_val['hash_key']}'", ARRAY_A);
				}

				$filename[$list_key] = $file_name[0]['file_name'];
				$module[$list_key] = $list_val['module'];
				$success_count[$list_key] = $list_val['success_count'];
				$fail_count[$list_key] = $list_val['fail_count'];
				$image_type[$list_key] = $list_val['image_type'];
				$image_status[$list_key] = $list_val['status'];
			}
			$response['file_name'] = $filename;
			$response['module'] = $module;
			$response['success_count'] = $success_count;
			$response['fail_count'] = $fail_count;
			$response['image_type'] = $image_type;
			$response['status'] = $image_status;
		} else {
			$response['success_count'] = array();
		}
		echo wp_json_encode($response);
		wp_die();
	}

	public function getAuthor($post_author)
	{
		$helpers_instance = ImportHelpers::getInstance();
		if (isset($post_author)) {
			$user_records = $helpers_instance->get_from_user_details($post_author);
			$post_author = $user_records['user_id'];
		}
		return $post_author;
	}

	public static function get_last_image_import_failure()
	{
		return self::$last_image_import_failure;
	}

	private static function set_last_image_import_failure($message)
	{
		self::$last_image_import_failure = (string) $message;
	}

	private function normalize_image_source_value($raw)
	{
		if ($raw === null || $raw === '') {
			return '';
		}
		$s = trim(wp_unslash((string) $raw));
		$s = html_entity_decode(rawurldecode($s), ENT_QUOTES, 'UTF-8');
		$s = trim(str_replace(array("\0"), '', $s));
		if ($s === '') {
			return '';
		}
		if (preg_match('/[,|;]/', $s)) {
			$parts = preg_split('/\s*[,|;]+\s*/', $s, -1, PREG_SPLIT_NO_EMPTY);
			if (!empty($parts[0])) {
				$s = trim($parts[0]);
			}
		}
		return trim($s);
	}

	/**
	 * Make a value usable with wp_remote_get: add https for www/host-only URLs, reject invalid.
	 */
	private function normalize_url_for_http_request($url)
	{
		if ($url === null) {
			return '';
		}
		$url = trim((string) $url);
		$url = trim($url, " \t\n\r\0\x0B\"'<>");
		if ($url === '') {
			return '';
		}
		if (preg_match('#^//#', $url)) {
			$url = 'https:' . $url;
		} elseif (preg_match('#^www\.#i', $url)) {
			$url = 'https://' . $url;
		} elseif (!preg_match('#^https?://#i', $url)) {
			// Use ~ delimiter: a literal # in the pattern must not close a #-delimited regex.
			$no_path = (strpos($url, '/') === false && strpos($url, '?') === false && strpos($url, '#') === false);
			$looks_like_filename = $no_path && preg_match('~\.(jpe?g|png|gif|webp|svg|bmp|ico|avif|tiff?|pdf|mp4|mp3|zip)$~i', $url);
			if (!$looks_like_filename && preg_match('~^[a-z0-9][a-z0-9.-]*\.[a-z]{2,}(/|\?|#|$)~i', $url)) {
				$url = 'https://' . $url;
			}
		}
		if (!preg_match('#^https?://#i', $url)) {
			return '';
		}
		if (function_exists('wp_http_validate_url') && !wp_http_validate_url($url)) {
			return '';
		}
		return $url;
	}

	/**
	 * Turn relative image paths from HTML into absolute http(s) URLs using a base URL.
	 */
	private function resolve_relative_image_url($relative, $base_url)
	{
		$relative = trim((string) $relative);
		if ($relative === '') {
			return '';
		}
		$base_url = str_replace('%20', ' ', rawurldecode($base_url));
		if (preg_match('#^https?://#i', $relative)) {
			return $this->normalize_url_for_http_request($relative);
		}
		if (preg_match('#^//#', $relative)) {
			return $this->normalize_url_for_http_request($relative);
		}
		$base = wp_parse_url($base_url);
		if (empty($base['scheme']) || empty($base['host'])) {
			return '';
		}
		$host = $base['host'];
		if (!empty($base['port'])) {
			$host .= ':' . $base['port'];
		}
		if (isset($relative[0]) && $relative[0] === '/') {
			return $this->normalize_url_for_http_request($base['scheme'] . '://' . $host . $relative);
		}
		$path = isset($base['path']) ? $base['path'] : '/';
		$path = trailingslashit(preg_replace('#/[^/]*$#', '/', $path));
		return $this->normalize_url_for_http_request($base['scheme'] . '://' . $host . $path . $relative);
	}

	/**
	 * Stable directory for images uploaded via the plugin UI (ZIP separate from CSV).
	 */
	private function get_separate_import_images_dir()
	{
		$uploads = wp_upload_dir();
		if (!empty($uploads['error']) || empty($uploads['basedir'])) {
			return '';
		}
		return wp_normalize_path($uploads['basedir'] . '/smack_uci_uploads/import_images');
	}

	private function find_basename_under_dir($dir, $basename)
	{
		if ($dir === '' || $basename === '' || $basename === '.' || $basename === '..') {
			return '';
		}
		$dir = wp_normalize_path(trailingslashit($dir));
		if (!is_dir($dir) || !is_readable($dir)) {
			return '';
		}
		$try = $dir . $basename;
		if (is_file($try) && is_readable($try)) {
			return wp_normalize_path($try);
		}
		$scan = @scandir($dir);
		if (!is_array($scan)) {
			return '';
		}
		$want_lower = strtolower($basename);
		foreach ($scan as $ent) {
			if ($ent === '.' || $ent === '..') {
				continue;
			}
			if (strtolower($ent) !== $want_lower) {
				continue;
			}
			$cand_path = $dir . $ent;
			if (is_file($cand_path) && is_readable($cand_path)) {
				return wp_normalize_path($cand_path);
			}
		}
		return '';
	}

	private function find_uploaded_image_file($candidate, $import_hash_key = null)
	{
		if ($candidate === null || $candidate === '') {
			return '';
		}
		$candidate = str_replace('\\', '/', trim($candidate));
		$candidate = preg_replace('/[?#].*$/', '', $candidate);

		$uploads = wp_upload_dir();
		if (!empty($uploads['error']) || empty($uploads['basedir'])) {
			return '';
		}
		$basedir = wp_normalize_path(trailingslashit($uploads['basedir']));

		if (preg_match('#^https?://#i', $candidate)) {
			$uploads_url = $uploads['baseurl'];
			$url_path = (string) wp_parse_url($candidate, PHP_URL_PATH);
			$uploads_path = (string) wp_parse_url($uploads_url, PHP_URL_PATH);
			if ($uploads_path !== '' && $url_path !== '' && strpos($url_path, $uploads_path) !== false) {
				$rel = substr($url_path, strpos($url_path, $uploads_path) + strlen($uploads_path));
				$rel = ltrim(str_replace('\\', '/', $rel), '/');
				$abs = wp_normalize_path($basedir . $rel);
				if (is_file($abs) && is_readable($abs)) {
					return $abs;
				}
			}
			return '';
		}

		$maybe = wp_normalize_path($candidate);
		if (preg_match('#^/#', $maybe) || preg_match('#^[A-Za-z]:/#', $maybe)) {
			if (strpos($maybe, $basedir) === 0 && is_file($maybe) && is_readable($maybe)) {
				return $maybe;
			}
		}

		$rel = ltrim($candidate, '/');
		$direct = wp_normalize_path($basedir . $rel);
		if (is_file($direct) && is_readable($direct)) {
			return $direct;
		}

		$basename = basename($candidate);
		if ($basename === '' || $basename === '.' || $basename === '..') {
			return '';
		}

		$pool_dir = $this->get_separate_import_images_dir();
		if ($pool_dir !== '') {
			$found = $this->find_basename_under_dir($pool_dir, $basename);
			if ($found !== '') {
				return $found;
			}
		}

		if ($import_hash_key !== null && $import_hash_key !== '') {
			$key = preg_replace('/[^a-fA-F0-9]/', '', (string) $import_hash_key);
			if ($key !== '') {
				$import_dir = wp_normalize_path(trailingslashit($basedir . 'smack_uci_uploads/imports/' . $key));
				$found = $this->find_basename_under_dir($import_dir, $basename);
				if ($found !== '') {
					return $found;
				}
			}
		}

		if (!empty($uploads['path'])) {
			$p = wp_normalize_path(trailingslashit($uploads['path']) . $basename);
			if (is_file($p) && is_readable($p)) {
				return $p;
			}
		}

		$root_try = wp_normalize_path($basedir . $basename);
		if (is_file($root_try) && is_readable($root_try)) {
			return $root_try;
		}

		$patterns = array(
			$basedir . '*/' . $basename,
			$basedir . '*/*/' . $basename,
			$basedir . '*/*/*/' . $basename,
			$basedir . '*/*/*/*/' . $basename,
		);
		foreach ($patterns as $pattern) {
			$hits = glob($pattern, GLOB_NOSORT);
			if (!empty($hits) && is_file($hits[0]) && is_readable($hits[0])) {
				return wp_normalize_path($hits[0]);
			}
		}

		return '';
	}

	private function file_path_to_uploads_url($absolute_path)
	{
		$uploads = wp_upload_dir();
		if (!empty($uploads['error']) || empty($uploads['basedir']) || empty($uploads['baseurl'])) {
			return '';
		}
		$abs = wp_normalize_path($absolute_path);
		$base = wp_normalize_path(trailingslashit($uploads['basedir']));
		if (strpos($abs, $base) !== 0) {
			return '';
		}
		$rel = ltrim(str_replace('\\', '/', substr($abs, strlen($base))), '/');
		return trailingslashit($uploads['baseurl']) . implode('/', array_map('rawurlencode', explode('/', $rel)));
	}

	private function stage_local_image_to_path($source_abs, $dest_abs)
	{
		$source_abs = wp_normalize_path($source_abs);
		$dest_abs = wp_normalize_path($dest_abs);
		if ($source_abs === $dest_abs && is_file($source_abs)) {
			return $dest_abs;
		}
		if (!is_file($source_abs) || !is_readable($source_abs)) {
			return '';
		}
		$dir = dirname($dest_abs);
		if (!wp_mkdir_p($dir)) {
			return '';
		}
		$final = $dest_abs;
		if (is_file($final)) {
			$final = trailingslashit($dir) . wp_unique_filename($dir, basename($final));
		}
		if (!@copy($source_abs, $final)) {
			return '';
		}
		return wp_normalize_path($final);
	}

	/**
	 * Copy a resolved uploads path into the import target and create or update the attachment.
	 *
	 * @param string $local_source_abs Absolute path from find_uploaded_image_file().
	 * @param string $disk_path        Target path under uploads (e.g. .../2026/04/file.jpg).
	 * @return int|null                Attachment ID or null on failure.
	 */
	private function finish_image_from_local_disk($local_source_abs, $disk_path, $uploaddir_url, $f_img, $post_id, $data_array, $media_handle, $header_array, $value_array, $featured, $media_type, $plugin, $media_id)
	{
		global $wpdb;

		$disk_path = (string) $disk_path;
		if ($local_source_abs === '' || $disk_path === '') {
			self::set_last_image_import_failure('Local file missing under uploads or invalid destination path. Value: ' . $f_img);
			return null;
		}
		$staged = $this->stage_local_image_to_path($local_source_abs, $disk_path);
		if ($staged === '') {
			self::set_last_image_import_failure('Could not copy local image into the uploads directory (permissions or disk).');
			return null;
		}
		$disk_path = $staged;
		$fimg_name = basename($staged);
		$file_type = wp_check_filetype($disk_path);
		if (empty($file_type['type']) || empty($file_type['ext'])) {
			$dims = @getimagesize($disk_path);
			if (!empty($dims['mime'])) {
				$file_type['type'] = $dims['mime'];
			}
			$ft2 = wp_check_filetype($disk_path);
			if (empty($file_type['ext']) && !empty($ft2['ext'])) {
				$file_type['ext'] = $ft2['ext'];
			}
		}
		if (empty($file_type['type'])) {
			$file_type['type'] = 'image/jpeg';
		}
		$attachment_title = sanitize_file_name(pathinfo($fimg_name, PATHINFO_FILENAME));
		$guid_for_post = $this->file_path_to_uploads_url($disk_path);
		if ($guid_for_post === '') {
			$guid_for_post = $uploaddir_url . '/' . $fimg_name;
		}
		if ($plugin == 'Media' && !empty($media_id)) {
			$attach_id = $media_id;
			$post_info = array('ID' => $attach_id, 'post_title' => $attachment_title);
			wp_update_post($post_info);
			$wpdb->update($wpdb->posts, array('guid' => $guid_for_post), array('ID' => $attach_id), array('%s'), array('%d'));
			update_attached_file($attach_id, $disk_path);
			$attach_data = wp_generate_attachment_metadata($attach_id, $disk_path);
			wp_update_attachment_metadata($attach_id, $attach_data);
		} else {
			$post_info = array(
				'guid' => $guid_for_post,
				'post_mime_type' => $file_type['type'],
				'post_title' => $attachment_title,
				'post_content' => '',
				'post_status' => 'inherit',
				'post_author' => $data_array['post_author'],
			);
			$attach_id = wp_insert_attachment($post_info, $disk_path, $post_id);
			$attach_data = wp_generate_attachment_metadata($attach_id, $disk_path);
			wp_update_attachment_metadata($attach_id, $attach_data);
		}
		if (!empty($data_array['featured_image'])) {
			set_post_thumbnail($post_id, $attach_id);
		}

		if (strpos($f_img, 'loading-image.jpg') !== false) {
		} else {
			$this->imageMetaImport($attach_id, $media_handle, $header_array, $value_array, $featured, $media_type);
		}
		return (int) $attach_id;
	}

	private function remote_get_with_retries($url, $args, $max_attempts = 3)
	{
		$url = $this->normalize_url_for_http_request($url);
		if ($url === '') {
			return new \WP_Error('http_request_failed', 'A valid URL was not provided.');
		}
		$last = null;
		$response = null;
		for ($i = 0; $i < $max_attempts; $i++) {
			if ($i > 0) {
				usleep(350000 * $i);
			}
			$response = wp_remote_get($url, $args);
			if (is_wp_error($response)) {
				$last = $response;
				continue;
			}
			$code = (int) wp_remote_retrieve_response_code($response);
			if ($code >= 200 && $code < 300) {
				return $response;
			}
			if (in_array($code, array(429, 500, 502, 503, 504), true)) {
				$last = $response;
				continue;
			}
			return $response;
		}
		return is_wp_error($last) ? $last : (isset($response) ? $response : new \WP_Error('http_request_failed', 'No response'));
	}

	/**
	 * Row context for media hooks when not inside main_import_process.
	 *
	 * @since 7.42.0
	 */
	private function build_media_hook_context( $line_number, $hashkey, $import_type ) {
		$context = WpucsvHooks::get_current_context();
		if ( ! empty( $context ) ) {
			return $context;
		}
		return WpucsvHooks::build_context(
			array(
				'line_number'   => $line_number,
				'hash_key'      => $hashkey,
				'selected_type' => $import_type,
			)
		);
	}

	/**
	 * @since 7.42.0
	 */
	private function build_media_hook_args( $url, $post_id, $line_number, $hashkey, $featured, $import_type, $media_type ) {
		return array(
			'url'         => $url,
			'post_id'     => $post_id,
			'line_number' => $line_number,
			'hash_key'    => $hashkey,
			'featured'    => $featured,
			'import_type' => $import_type,
			'media_type'  => $media_type,
		);
	}

	/**
	 * Resolve disk path and public URL for imported media.
	 *
	 * Honors WordPress upload_dir filter and wpucsv_before_media_upload overrides:
	 * upload_subdir (e.g. "2025/05"), or upload_year + upload_month.
	 *
	 * @since 7.42.0
	 *
	 * @param string               $f_img      Image source URL (optional, for legacy fallback).
	 * @param array<string,mixed>  $media_args From wpucsv_before_media_upload.
	 * @return array{path: string, url: string, subdir: string}
	 */
	private function resolve_media_upload_paths( $f_img = '', $media_args = array() ) {
		$dir = wp_upload_dir();

		if ( ! empty( $media_args['upload_subdir'] ) ) {
			$subdir = '/' . ltrim( (string) $media_args['upload_subdir'], '/' );
			return array(
				'path'   => $dir['basedir'] . $subdir,
				'url'    => $dir['baseurl'] . $subdir,
				'subdir' => $subdir,
			);
		}

		if ( ! empty( $media_args['upload_year'] ) && ! empty( $media_args['upload_month'] ) ) {
			$subdir = '/' . $media_args['upload_year'] . '/' . $media_args['upload_month'];
			return array(
				'path'   => $dir['basedir'] . $subdir,
				'url'    => $dir['baseurl'] . $subdir,
				'subdir' => $subdir,
			);
		}

		if ( empty( $dir['error'] ) && ! empty( $dir['path'] ) && ! empty( $dir['url'] ) ) {
			return array(
				'path'   => $dir['path'],
				'url'    => $dir['url'],
				'subdir' => isset( $dir['subdir'] ) ? (string) $dir['subdir'] : '',
			);
		}

		$uploads_use_yearmonth = (int) get_option( 'uploads_use_yearmonth_folders' );
		if ( 1 === $uploads_use_yearmonth ) {
			$dirname = date( 'Y' ) . '/' . date( 'm' );
		} else {
			$parsed_url = parse_url( $f_img, PHP_URL_PATH );
			if ( preg_match( '#uploads/(\d{4}/\d{2})/#', (string) $parsed_url, $matches ) ) {
				$dirname = $matches[1];
			} else {
				$dirname = '';
			}
		}

		if ( $dirname !== '' ) {
			$subdir = '/' . $dirname;
			return array(
				'path'   => $dir['basedir'] . $subdir,
				'url'    => $dir['baseurl'] . $subdir,
				'subdir' => $subdir,
			);
		}

		return array(
			'path'   => $dir['basedir'],
			'url'    => $dir['baseurl'],
			'subdir' => '',
		);
	}

	/**
	 * @since 7.42.0
	 */
	private function complete_media_upload_hook( array $hook_context, array $media_args, $attach_id ) {
		$error = null;
		if ( empty( $attach_id ) ) {
			$fail = self::get_last_image_import_failure();
			if ( is_string( $fail ) && $fail !== '' ) {
				$error = $fail;
			}
		}
		WpucsvHooks::after_media_upload( $hook_context, $media_args, $attach_id ? $attach_id : 0, $error );
	}

	/**
	 * Download / attach one image with developer API hooks (Issue #395).
	 *
	 * @since 7.42.0
	 */
	public function image_function($f_img, $post_id, $data_array = null, $option_name = null, $use_existing_image = false, $header_array = null, $value_array = null, $wpml_array = null, $unikey = null, $image_metas = null, $line_number = null, $plugin = null, $import_type = null, $acf_wpname_element = null, $hashkey = null, $indexs = null, $acf_image_meta = null, $media_type = null, $media_id = null, $jet_child_object_id = null, $parent_object_id = null, $media_mode = null,$featured = false)
	{
		$hook_context = $this->build_media_hook_context( $line_number, $hashkey, $import_type );
		$media_args   = $this->build_media_hook_args( $f_img, $post_id, $line_number, $hashkey, $featured, $import_type, $media_type );
		$media_args   = WpucsvHooks::before_media_upload( $hook_context, $media_args );
		if ( ! empty( $media_args['url'] ) ) {
			$f_img = $media_args['url'];
		}
		if ( isset( $media_args['post_id'] ) ) {
			$post_id = (int) $media_args['post_id'];
		}

		$attach_id = $this->image_function_import(
			$f_img,
			$post_id,
			$data_array,
			$option_name,
			$use_existing_image,
			$header_array,
			$value_array,
			$wpml_array,
			$unikey,
			$image_metas,
			$line_number,
			$plugin,
			$import_type,
			$acf_wpname_element,
			$hashkey,
			$indexs,
			$acf_image_meta,
			$media_type,
			$media_id,
			$jet_child_object_id,
			$parent_object_id,
			$media_mode,
			$featured,
			$media_args
		);

		$this->complete_media_upload_hook( $hook_context, $media_args, $attach_id );
		return $attach_id;
	}

	/**
	 * Core image download / attach (no hook wrapper).
	 */
	private function image_function_import($f_img, $post_id, $data_array = null, $option_name = null, $use_existing_image = false, $header_array = null, $value_array = null, $wpml_array = null, $unikey = null, $image_metas = null, $line_number = null, $plugin = null, $import_type = null, $acf_wpname_element = null, $hashkey = null, $indexs = null, $acf_image_meta = null, $media_type = null, $media_id = null, $jet_child_object_id = null, $parent_object_id = null, $media_mode = null,$featured = false, $media_args = array())
	{
		global $wpdb;

		self::set_last_image_import_failure('');
		$f_img = $this->normalize_image_source_value($f_img);
		if ($f_img === '') {
			self::set_last_image_import_failure('Empty image field after trim/decode.');
			return null;
		}

		$local_source_abs = '';
		$uploads_for_local = wp_upload_dir();
		$uploads_baseurl = isset($uploads_for_local['baseurl']) ? $uploads_for_local['baseurl'] : '';

		$allowed_extensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'pdf', 'mp4', 'mp3', 'zip', 'svg');
		$file_ext = strtolower(pathinfo($f_img, PATHINFO_EXTENSION));
		if (!empty($file_ext) && !in_array($file_ext, $allowed_extensions)) {
			self::set_last_image_import_failure('Unsupported file extension: ' . $file_ext);
			return null;
		}

		$user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36';
		if (isset($data_array['post_author'])) {
			$data_array['post_author'] = $this->getAuthor($data_array['post_author']);
		} else {
			if (!empty($data_array) && is_array($data_array)) {
				$data_array['post_author'] = "admin";
			} else {
				$data_array = array();
				$data_array['post_author'] = "admin";
			}
		}
		$media_handle = get_option('smack_image_options');
		if (!is_array($media_handle)) {
			$media_handle = array('media_settings' => array());
		}
		if (!isset($media_handle['media_settings']) || !is_array($media_handle['media_settings'])) {
			$media_handle['media_settings'] = array();
		}
		$media_settings = [];
		if (!empty($header_array) && !empty($value_array)) {
			if (count($header_array) !== count($value_array)) {
				if (count($value_array) < count($header_array)) {
					$value_array = array_pad($value_array, count($header_array), '');
				} else {
					$value_array = array_slice($value_array, 0, count($header_array));
				}
			}
			$media_settings = array_combine($header_array, $value_array);
		} else {
			if (!empty($unikey) && !is_array($unikey)) {
				$get_array_value = get_option('smack_media_seo' . $unikey);
				if (!empty($get_array_value)) {
					$header_array = $get_array_value['header_array'];
					$value_array = $get_array_value['value_array'];
					if (count($header_array) !== count($value_array)) {
						if (count($value_array) < count($header_array)) {
							$value_array = array_pad($value_array, count($header_array), '');
						} else {
							$value_array = array_slice($value_array, 0, count($header_array));
						}
					}
					$media_settings = array_combine($header_array, $value_array);
				}
			}
		}

		if (!empty($media_handle['media_settings']['alttext'])) {
			$alttext['_wp_attachment_image_alt'] = $media_handle['media_settings']['alttext'];
		}
		if (!empty($media_handle['postcontent_image_alt'])) {
			$alttext['_wp_postcontent_image_alt'] = $media_handle['postcontent_image_alt'];
		}

		$is_url_like = (bool) preg_match_all('/\b(?:(?:https?|http|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i', $f_img, $matchedlist, PREG_PATTERN_ORDER);
		if ($is_url_like) {
			$f_img = trim($f_img);
			$local_source_abs = $this->find_uploaded_image_file($f_img, $hashkey);
			if ($local_source_abs !== '') {
				$built = $this->file_path_to_uploads_url($local_source_abs);
				if ($built !== '') {
					$f_img = $built;
				}
			}
		} else {
			$local_source_abs = $this->find_uploaded_image_file($f_img, $hashkey);
			if ($local_source_abs !== '') {
				$built = $this->file_path_to_uploads_url($local_source_abs);
				if ($built !== '') {
					$f_img = $built;
				}
			} elseif (!$use_existing_image) {
				self::set_last_image_import_failure('Local image not found under uploads (searched basename and YYYY/MM folders): ' . $f_img);
				return null;
			}
		}
		$image_name = pathinfo($f_img);
		if (!empty($media_handle['media_settings']['file_name']) && ($featured || $media_type == 'External')) {
			$file_type = wp_check_filetype($f_img, null);
			$ext = !empty($file_type['ext']) ? '.' . $file_type['ext'] : '';
		
			if (isset($media_settings[$media_handle['media_settings']['file_name']])) {
				$fimg_name = $media_settings[$media_handle['media_settings']['file_name']] . $ext;
			} else {
				$fimg_name = str_replace(' ', '', $media_handle['media_settings']['file_name'] ?? '');
				$fimg_name .= $ext;
			}
		}
		 elseif (!empty($image_metas)) {
			$file_type = wp_check_filetype($f_img, null);
			$ext = '.' . $file_type['ext'];
			$fimg_name = $image_metas . $ext;
		} else {
			$fimg_name = $image_name['basename'];

			$fimg_name_without_ext = $image_name['filename'];
			if (empty($fimg_name_without_ext)) {
				$fimg_name_without_ext = $fimg_name;
			}
		}
		
		$file_type = wp_check_filetype($fimg_name, null);
		if ($use_existing_image) {
			if (!empty($media_handle['media_settings']['file_name']) && $media_type == 'External') {
				$image_file_name = $media_handle['media_settings']['file_name'];
				if (strpos($image_file_name, ' ') !== false) {
					$image_file_name = str_replace(' ', '-', $image_file_name);
					$image_file_name = preg_replace('/[^a-zA-Z0-9._\-]/', '', $image_file_name);
				}
				$attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND guid LIKE '%" . esc_sql($wpdb->esc_like($image_file_name)) . "%'");
				if ($attachment_id != false) {
					$fimg_name = $image_file_name;
				} else {
					$fimg_name = $image_file_name;
				}
			} else {
				if (empty($file_type['ext']) && empty($media_handle['media_settings']['file_name'])) {
					$fimg_name = @basename($f_img);
					$fimg_name = str_replace(' ', '-', trim($fimg_name));
					$fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name);
				}

				if (strstr($f_img, 'https://drive.google.com')) {
					preg_match('/[?&]id=([^&]+)/', $f_img, $matches);
					$fimg_name = isset($matches[1]) ? $matches[1] : basename($f_img);
				}
			}
			$f_img_check = str_replace(" ", "%20", $fimg_name);
			$attachment_id = null;

			$wp_content_url = content_url();
			if ((!empty($media_mode) || $media_mode == 'MediaUpdate')) {
				$attachment_ids = $wpdb->get_results("SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' AND post_title != 'image-failed' AND guid LIKE '%$f_img_check%'", ARRAY_A);
				if (!empty($attachment_ids[0]['ID'])) {
					return $attachment_ids[0]['ID'];
				}
			} else if (strpos($f_img, $wp_content_url) !== FALSE) {
				$attachment_ids = $wpdb->get_results("SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' AND guid = '$f_img' ", ARRAY_A);
			} else {
				$attachment_ids = $wpdb->get_results("SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' AND guid LIKE '%$f_img_check%'", ARRAY_A);
			}
			if (!empty($attachment_ids[0]['ID'])) {
				if (($media_type == 'Local' || $media_type == 'External') && empty($media_id) && empty($post_id)) {
					return $attachment_ids[0]['ID'];
				} else if ($plugin == 'Featured') {
					return $attachment_ids[0]['ID'];
				} else if (empty($media_id) && empty($media_type)) {
					$attach_id = $attachment_ids[0]['ID'];
					$table_name = $wpdb->prefix . 'smackcsv_file_events';
					$post_title = $wpdb->get_var("SELECT post_title FROM {$wpdb->prefix}posts WHERE ID = '{$post_id}' AND post_status != 'trash'");
					$file_name = $wpdb->get_var("SELECT file_name FROM $table_name WHERE hash_key = '$hashkey'");
					$shortcode_table = $wpdb->prefix . "ultimate_csv_importer_shortcode_manager";
					$check_id = $wpdb->get_results("SELECT ID FROM {$wpdb->prefix}posts WHERE ID ='{$attach_id}' AND post_title ='image-failed' AND post_type = 'attachment' AND guid LIKE '%$f_img_check%'", ARRAY_A);
					if (!empty($check_id)) {
						$failed_ids = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}ultimate_csv_importer_shortcode_manager WHERE post_id='{$post_id}' AND media_id = '{$attach_id}'");
						if (!empty($failed_ids) && $failed_ids[0]->post_id != $post_id) {
							$attach_id = $check_id[0]['ID'];
							$insert_status = $wpdb->insert(
								$shortcode_table,
								array('post_id' => $post_id, 'post_title' => $post_title, 'image_shortcode' => $plugin . '_image__' . $acf_wpname_element, 'media_id' => $attach_id, 'original_image' => $f_img, 'indexs' => $indexs, 'image_meta' => $acf_image_meta, 'hash_key' => $hashkey, 'import_type' => $import_type, 'file_name' => $file_name, 'jet_child_object_id' => $jet_child_object_id, 'jet_parent_object_id' => $parent_object_id),
								array('%d', '%s', '%s', '%d', '%s', '%d', '%s', '%s', '%s', '%s', '%s', '%s')
							);
							if ($insert_status) {
								$this->store_failed_image_ids($attach_id);
								$this->failed_media_data($line_number, $post_id, $post_title, $attach_id, $f_img);
							}
						} elseif (!empty($post_id) && !empty($failed_ids) && ($failed_ids[0]->post_id == $post_id) && ($check_id[0]['ID'] == $failed_ids[0]->media_id)) {
							$attach_id = $check_id[0]['ID'];
							$insert_status = $wpdb->insert(
								$shortcode_table,
								array('post_id' => $post_id, 'post_title' => $post_title, 'image_shortcode' => $plugin . '_image__' . $acf_wpname_element, 'media_id' => $attach_id, 'original_image' => $f_img, 'indexs' => $indexs, 'image_meta' => $acf_image_meta, 'hash_key' => $hashkey, 'import_type' => $import_type, 'file_name' => $file_name, 'jet_child_object_id' => $jet_child_object_id, 'jet_parent_object_id' => $parent_object_id),
								array('%d', '%s', '%s', '%d', '%s', '%d', '%s', '%s', '%s', '%s', '%s', '%s')
							);
							if ($insert_status) {
								$this->store_failed_image_ids($attach_id);
								$this->failed_media_data($line_number, $post_id, $post_title, $attach_id, $f_img);
							}
						} elseif (empty($failed_ids)) {
							$insert_status = $wpdb->insert(
								$shortcode_table,
								array('post_id' => $post_id, 'post_title' => $post_title, 'image_shortcode' => $plugin . '_image__' . $acf_wpname_element, 'media_id' => $attach_id, 'original_image' => $f_img, 'indexs' => $indexs, 'image_meta' => $acf_image_meta, 'hash_key' => $hashkey, 'import_type' => $import_type, 'file_name' => $file_name, 'jet_child_object_id' => $jet_child_object_id, 'jet_parent_object_id' => $parent_object_id),
								array('%d', '%s', '%s', '%d', '%s', '%d', '%s', '%s', '%s', '%s', '%s', '%s')
							);
							if ($insert_status) {
								$this->store_failed_image_ids($attach_id);
								$this->failed_media_data($line_number, $post_id, $post_title, $attach_id, $f_img);
							}
						}
					}
					return $attach_id;
				}
			}

			if (is_array($attachment_ids) && !empty($attachment_ids))
				$attachment_id = $attachment_ids[0]['ID'];
			if (strpos($f_img, 'loading-image.jpg') !== FALSE) {
			} else {
				$this->imageMetaImport($attachment_id, $media_handle, $header_array, $value_array,$featured, $media_type);
			}
			if ($attachment_id) {
				if (!empty($data_array['featured_image'])) {
					set_post_thumbnail($post_id, $attachment_id);
					return $attachment_id;
				} else {

					if (!empty($wpml_array['language_code'])) {
						$lang_code = $wpml_array['language_code'];
						$image_trid_id = $wpdb->get_var("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_type = 'post_attachment' AND element_id = $attachment_id ");
						$image_lang_id = $wpdb->get_var("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type = 'post_attachment' AND trid = $image_trid_id AND language_code = '$lang_code' ");
						return $image_lang_id;
					} else {
						return $attachment_id;
					}
				}
			}
		}

		$attachment_title = sanitize_file_name(pathinfo($fimg_name, PATHINFO_FILENAME));

		$file_type = wp_check_filetype($fimg_name, null);
		$upload_paths    = $this->resolve_media_upload_paths( $f_img, is_array( $media_args ) ? $media_args : array() );
		$uploaddir_paths = $upload_paths['path'];
		$uploaddir_url   = $upload_paths['url'];
		$f_img = str_replace(" ", "%20", $f_img);
		if (!empty($media_handle['media_settings']['file_name']) && isset($media_handle['media_settings']['file_name'])) {
			$file_type = wp_check_filetype($f_img, null);
			if (empty($file_type['ext']) && empty($media_handle['media_settings']['file_name'])) {
				$file_type['ext'] = 'jpeg';
			}
			$ext = '.' . $file_type['ext'];
			if (!empty($media_handle['media_settings']['file_name']) && ($featured || $media_type == 'External')) {
				if (strrpos($media_handle['media_settings']['file_name'], '.')) {
					$fimg_name = $media_handle['media_settings']['file_name'];
				} else {
					$fimg_name = $media_handle['media_settings']['file_name'] . $ext;
				}
			}
		} else if (empty($file_type['ext']) && empty($media_handle['media_settings']['file_name'])) {
			if (strstr($f_img, 'https://drive.google.com')) {
				preg_match('/[?&]id=([^&]+)/', $f_img, $matches);
				$fimg_name = isset($matches[1]) ? $matches[1] : basename($f_img);
			} else {
				$fimg_name = @basename($f_img);
				$fimg_name = str_replace(' ', '-', trim($fimg_name));
				$fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name);
			}
		}
		if ($uploaddir_paths != "" && $uploaddir_paths) {
			if (strpos($fimg_name, ' ') !== false) {
				$fimg_name = str_replace(' ', '-', $fimg_name);
				$fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name);
			}
			$uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
		}
		// if ($uploaddir_paths != "" && $uploaddir_paths) {
		// 	$uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
		// }
		$response = null;
		$rawdata = '';
		$fetch_url = null;
		if (strstr($f_img, 'https://drive.google.com')) {

			if (strstr($f_img, 'https://drive.google.com/uc?export=download&id')) {
				$rawdata = file_get_contents($f_img);
			} else if (strpos($f_img, 'https://drive.google.com/file/d/') !== false) {
				$file_id_start = strpos($f_img, '/d/') + 3;
				$file_id_end = strpos($f_img, '/view', $file_id_start);
				$file_id = substr($f_img, $file_id_start, $file_id_end - $file_id_start);
				$f_img = "https://drive.google.com/uc?export=download&id=$file_id";
				$drive_dl = $this->normalize_url_for_http_request($f_img);
				if ($drive_dl === '') {
					self::set_last_image_import_failure('Invalid Google Drive download URL.');
					return null;
				}
				$response = wp_remote_get($drive_dl, array('timeout' => 30));
				$rawdata = wp_remote_retrieve_body($response);
			} else {
				$page_content = file_get_contents($f_img);
				if (empty($page_content)) {
					preg_match('~/d/\K[^/]+(?=/)~', $f_img, $result_id);
					$image_link_id = isset($result_id[0]) ? $result_id[0] : $f_img;
					$rawdata = file_get_contents(html_entity_decode('https://drive.google.com/uc?export=download&id=' . $image_link_id));
				} else {

					$dom_obj = new \DOMDocument();
					$dom_obj->loadHTML($page_content);
					$meta_val = null;
					foreach ($dom_obj->getElementsByTagName('meta') as $meta) {
						if ($meta->getAttribute('property') == 'og:image') {
							$meta_val = $meta->getAttribute('content');
						}
					}
					$meta_val = $this->normalize_url_for_http_request($meta_val);
					if ($meta_val === '') {
						self::set_last_image_import_failure('Google Drive page did not provide a usable og:image URL.');
						return null;
					}
					$ch = curl_init($meta_val);
					curl_setopt($ch, CURLOPT_HEADER, 0);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
					curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
					$rawdata = curl_exec($ch);
				}
			}
		} elseif (
			$local_source_abs !== ''
			|| (!empty($dir['baseurl']) && stripos($f_img, $dir['baseurl']) !== false)
		) {
			if ($local_source_abs === '') {
				$local_source_abs = $this->find_uploaded_image_file(str_replace('%20', ' ', rawurldecode($f_img)), $hashkey);
			}
			$disk_path = isset($uploaddir_path) ? $uploaddir_path : '';
			$attach_id = $this->finish_image_from_local_disk($local_source_abs, $disk_path, $uploaddir_url, $f_img, $post_id, $data_array, $media_handle, $header_array, $value_array, $featured, $media_type, $plugin, $media_id);
			if ($attach_id === null) {
				return null;
			}
			return $attach_id;
		} else if (strstr($f_img, 'https://www.dropbox.com/')) {
			$page_content = file_get_contents($f_img);
			$dom_obj = new \DOMDocument();
			$dom_obj->loadHTML($page_content);
			$meta_val = null;
			foreach ($dom_obj->getElementsByTagName('meta') as $meta) {
				if ($meta->getAttribute('property') == 'og:image') {
					$meta_val = $meta->getAttribute('content');
				}
			}
			$meta_val = $this->normalize_url_for_http_request($meta_val);
			if ($meta_val === '') {
				self::set_last_image_import_failure('Dropbox (or similar) page did not return a usable og:image URL for download.');
				return null;
			}
			usleep(500000);
			$response = $this->remote_get_with_retries($meta_val, array(
				'timeout' => 60,
				'user-agent' => $user_agent,
				'sslverify' => WpucsvHooks::media_download_sslverify( true, $f_img ),
				'redirection' => 5,
			));
			$rawdata = is_wp_error($response) ? '' : wp_remote_retrieve_body($response);
		} else {
			usleep(500000);
			$fetch_url = $this->normalize_url_for_http_request(str_replace('%20', ' ', rawurldecode($f_img)));
			if ($fetch_url === '') {
				$decoded_f = str_replace('%20', ' ', rawurldecode($f_img));
				$fallback_abs = $this->find_uploaded_image_file($decoded_f, $hashkey);
				if ($fallback_abs !== '') {
					$disk_path_fb = isset($uploaddir_path) ? $uploaddir_path : '';
					$attach_id_fb = $this->finish_image_from_local_disk($fallback_abs, $disk_path_fb, $uploaddir_url, $f_img, $post_id, $data_array, $media_handle, $header_array, $value_array, $featured, $media_type, $plugin, $media_id);
					if ($attach_id_fb !== null) {
						return $attach_id_fb;
					}
					return null;
				}
				self::set_last_image_import_failure('External image value is not a valid HTTP(S) URL. Add https:// or use a full URL (e.g. https://example.com/image.jpg). If the file is on this server, put it under wp-content/uploads (same basename) or switch image import to Local. Value: ' . $f_img);
				return null;
			}
			$f_img = str_replace(' ', '%20', $fetch_url);
			$ext = isset($file_type['ext']) ? $file_type['ext'] : '';
			if ($ext == 'jpeg' || $ext == 'jpg' || $ext == 'bmp') {
				$timeout = 120;
			} elseif ($ext == 'gif' || $ext == 'pdf') {
				$timeout = 300;
			} elseif ($ext == 'mp4') {
				$timeout = 120;
			} else {
				$timeout = 60;
			}
			$req_args = array(
				'timeout' => $timeout,
				'user-agent' => $user_agent,
				'sslverify' => WpucsvHooks::media_download_sslverify( true, $fetch_url ),
				'redirection' => 5,
			);
			$response = $this->remote_get_with_retries($fetch_url, $req_args);
			$rawdata = is_wp_error($response) ? '' : wp_remote_retrieve_body($response);
		}
		$http_code = 0;
		if (is_wp_error($response)) {
			self::set_last_image_import_failure('Download failed: ' . $response->get_error_message());
			return null;
		}
		if ($response !== null) {
			$http_code = (int) wp_remote_retrieve_response_code($response);
		}
		if ($http_code === 0 && is_string($rawdata) && $rawdata !== '') {
			$http_code = 200;
		}
		if (strpos($rawdata, '<img src=') !== false) {
			$raw_file = explode('<img src', $rawdata);
			$urls = rtrim(end($raw_file), '>');
			$urls = ltrim($urls, '=');
			$urls = str_replace(array(' ', '"', "'"), '', $urls);
			$resolve_base = ($fetch_url !== null && $fetch_url !== '') ? $fetch_url : str_replace('%20', ' ', rawurldecode($f_img));
			$urls = $this->resolve_relative_image_url($urls, $resolve_base);
			if ($urls === '') {
				self::set_last_image_import_failure('Could not resolve image URL from HTML response.');
				return null;
			}
			usleep(500000);
			$response = $this->remote_get_with_retries($urls, array(
				'timeout' => 60,
				'user-agent' => $user_agent,
				'sslverify' => WpucsvHooks::media_download_sslverify( true, $urls ),
				'redirection' => 5,
			));
			if (is_wp_error($response)) {
				self::set_last_image_import_failure('Secondary image URL fetch failed: ' . $response->get_error_message());
				return null;
			}
			$rawdata = wp_remote_retrieve_body($response);
			$http_code = (int) wp_remote_retrieve_response_code($response);
		}
		if ($http_code >= 400 && $http_code < 600) {
			self::set_last_image_import_failure('HTTP status ' . $http_code . ' when downloading image.');
			return null;
		}
		if ($http_code > 0 && $http_code < 200) {
			self::set_last_image_import_failure('Unexpected HTTP status ' . $http_code . ' when downloading image.');
			return null;
		}

		if ($rawdata === false || $rawdata === '') {
			self::set_last_image_import_failure('Empty response body when downloading image.');
			return null;
		} else {
			if ($plugin == 'Media' && !empty($media_id)) {
				$old_file_path = get_attached_file($media_id);
				if (file_exists($old_file_path)) {
					unlink($old_file_path);
				}
			} else {
				if (file_exists($uploaddir_path)) {
					$i = 1;
					$exist = true;
					while ($exist) {
						$fimg_name = $attachment_title . "-" . $i . "." . $file_type['ext'];
						$uploaddir_path = $uploaddir_paths . "/" . $fimg_name;

						if (file_exists($uploaddir_path)) {
							$i = $i + 1;
						} else {
							$exist = false;
						}
					}
				}
			}

			$directory_path = dirname($uploaddir_path);
			if (!file_exists($directory_path)) {
				wp_mkdir_p($directory_path);
			}
			$fp = @fopen($uploaddir_path, 'x');
			if ($fp === false && !empty($uploaddir_paths) && is_file($uploaddir_path)) {
				$uploaddir_path = trailingslashit($uploaddir_paths) . wp_unique_filename($uploaddir_paths, basename($uploaddir_path));
				$fimg_name = basename($uploaddir_path);
				$fp = @fopen($uploaddir_path, 'x');
			}
			if ($fp !== false) {
				fwrite($fp, $rawdata);
				fclose($fp);
			} else {
				self::set_last_image_import_failure('Could not write downloaded file to uploads (exists or permissions). Path: ' . $uploaddir_path);
				return null;
			}
		}

		if (empty($file_type['type'])) {
			$file_type['type'] = 'image/jpeg';
		}
		if ($plugin == 'Media' && !empty($media_id)) {
			$attach_id = $media_id;
			$post_info = array('ID' => $attach_id, 'post_title'   => $attachment_title);
			wp_update_post($post_info);
			$new_guid = $uploaddir_url . '/' . $fimg_name;
			$wpdb->update($wpdb->posts, array('guid' => $new_guid), array('ID' => $attach_id), array('%s'), array('%d'));
			update_attached_file($attach_id, $uploaddir_path);
			$attach_data = wp_generate_attachment_metadata($attach_id, $uploaddir_path);
			wp_update_attachment_metadata($attach_id, $attach_data);
		} else {
			$post_info = array(
				'guid'           => $uploaddir_url . "/" .  $fimg_name,
				'post_mime_type' => $file_type['type'],
				'post_title'     => $attachment_title,
				'post_content'   => '',
				'post_status'    => 'inherit',
				'post_author'  => $data_array['post_author']
			);
			$attach_id = wp_insert_attachment($post_info, $uploaddir_path, $post_id);
			$attach_data = wp_generate_attachment_metadata($attach_id, $uploaddir_path);

			wp_update_attachment_metadata($attach_id,  $attach_data);
		}
		//added..
		if (strstr($f_img, 'https://drive.google.com')) {
			$finfo = finfo_open(FILEINFO_MIME_TYPE);
			$mime = finfo_file($finfo, $uploaddir_path);

			if ($mime == 'image/jpeg') {
				$extension = 'jpg';
			} elseif ($mime == 'image/png') {
				$extension = 'png';
			} else {
				$explode_ext = explode('/', $mime);
				$extension = $explode_ext[1];
			}
			$image_file_path = get_attached_file($attach_id);
			if ($image_file_path) {
				$extension_exists = '.' . $extension;
				if (strpos($image_file_path, $extension_exists) !== false) {
				} else {
					if (substr($image_file_path, -1) == '.') {
						$new_image_file_path = $image_file_path . $extension;
					} else {
						$new_image_file_path = $image_file_path . '.' . $extension;
					}

					rename($image_file_path, $new_image_file_path);
					update_attached_file($attach_id, $new_image_file_path);
				}
			}
		}
		if (strpos($f_img, 'loading-image.jpg') !== FALSE) {
		} else {
			$this->imageMetaImport($attach_id, $media_handle, $header_array, $value_array,$featured, $media_type);
		}

		if (!empty($data_array['featured_image'])) {
			set_post_thumbnail($post_id, $attach_id);
		}

		if (!empty($wpml_array['language_code']) && empty($media_id)) {
			$lang_code = $wpml_array['language_code'];
			$image_trid_id = $wpdb->get_var("SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_type = 'post_attachment' AND element_id = $attach_id ");

			$image_lang_id = $wpdb->get_var("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type = 'post_attachment' AND trid = $image_trid_id AND language_code = '$lang_code' ");
			return $image_lang_id;
		} else {
			return $attach_id;
		}
	}

	public function imageMetaImport($attach_id, $media_handle, $header_array = null, $value_array = null , $featured = false , $media_type = null)
	{
		$media_handle = get_option('smack_image_options');
		if ( $attach_id && ( $featured || $media_type == 'Local' || $media_type == 'External' ) ) {
			if ($media_handle['media_settings']['media_handle_option']) {
				if (isset($media_handle['media_settings']['alttext'])) {
					$alttext['_wp_attachment_image_alt'] = $media_handle['media_settings']['alttext'];
				}
				if (isset($media_handle['postcontent_image_alt'])) {
					$alttext['_wp_postcontent_image_alt'] = $media_handle['postcontent_image_alt'];
				}
				if (isset($media_handle['media_settings']['caption']) || isset($media_handle['media_settings']['description'])) {
					wp_update_post(array(
						'ID'           => $attach_id,
						'post_content' => $media_handle['media_settings']['description'],
						'post_excerpt' => $media_handle['media_settings']['caption']
					));
				}
				if (!empty($media_handle['media_settings']['title'])) {
					wp_update_post(array(
						'ID'           => $attach_id,
						'post_title'   => $media_handle['media_settings']['title']
					));
				}
				if ($attach_id != null && isset($alttext['_wp_attachment_image_alt'])) {
					update_post_meta($attach_id, '_wp_attachment_image_alt', $alttext['_wp_attachment_image_alt']);
				}
	
				if ($attach_id != null && isset($alttext['_wp_postcontent_image_alt'])) {
					update_post_meta($attach_id, '_wp_attachment_image_alt', $alttext['_wp_postcontent_image_alt']);
				}
			}
		}

	}

	public function acfgalleryMetaImports($attach_id, $media_handle, $plugin)
	{
		if (is_array($attach_id)) {

			foreach ($attach_id as $attachid => $attachvalid) {
				if (isset($media_handle[$plugin . '_gallery_caption'][$attachid]) || isset($media_handle[$plugin . '_gallery_description'][$attachid])) {
					wp_update_post(array(
						'ID'           => $attachvalid,
						'post_content' => $media_handle[$plugin . '_gallery_description'][$attachid],
						'post_excerpt' => $media_handle[$plugin . '_gallery_caption'][$attachid]
					));
				}
				if (isset($media_handle[$plugin . '_gallery_title'][$attachid])) {
					wp_update_post(array(
						'ID'           => $attachvalid,
						'post_title'   => $media_handle[$plugin . '_gallery_title'][$attachid]
					));
				}
				if ($attachvalid != null && isset($media_handle[$plugin . '_gallery_file_name'][$attachid])) {
					global $wpdb;
					$guid = $wpdb->get_results("select guid from {$wpdb->prefix}posts where ID= '$attachvalid'", ARRAY_A);
					$dirname = date('Y') . '/' . date('m');
					$file_type = wp_check_filetype($guid[0]['guid'], null);
					$ext = '.' . $file_type['ext'];
					$dir = wp_upload_dir();

					// $uploaddir_paths = $dir ['basedir'] . '/' . $dirname ;
					// $uploaddir_url = $dir ['baseurl'] . '/' . $dirname;
					$uploads_use_yearmonth = get_option('uploads_use_yearmonth_folders');
					if ($uploads_use_yearmonth == 1) {
						$uploaddir_paths = $dir['basedir'] . '/' . $dirname;
						$uploaddir_url = $dir['baseurl'] . '/' . $dirname;
					} else {
						$uploaddir_paths = $dir['basedir'];
						$uploaddir_url = $dir['baseurl'];
					}
					$guids = $uploaddir_url . '/' . $media_handle[$plugin . '_gallery_file_name'][$attachid] . $ext;
					$wpdb->update($wpdb->posts, ['guid' => $guids], ['ID' => $attachvalid]);
					$fimg_name = $dirname . '/' . $media_handle[$plugin . '_gallery_file_name'][$attachid] . $ext;
					update_post_meta($attachvalid, '_wp_attached_file', $fimg_name);
				}
				if ($attachvalid != null && isset($media_handle[$plugin . '_gallery_alt_text'][$attachid])) {
					update_post_meta($attachvalid, '_wp_attachment_image_alt', $media_handle[$plugin . '_gallery_alt_text'][$attachid]);
				}
			}
		} else {

			if (isset($media_handle[$plugin . '_gallery_caption'][0]) || isset($media_handle[$plugin . '_gallery_description'][0])) {
				wp_update_post(array(
					'ID'           => $attach_id,
					'post_content' => $media_handle[$plugin . '_gallery_description'][0],
					'post_excerpt' => $media_handle[$plugin . '_gallery_caption'][0]
				));
			}
			if (isset($media_handle[$plugin . '_title'][0])) {
				wp_update_post(array(
					'ID'           => $attach_id,
					'post_title'   => $media_handle[$plugin . '_gallery_title'][0]
				));
			}
			if ($attach_id != null && isset($media_handle[$plugin . '_gallery_file_name'][0])) {
				global $wpdb;
				$guid = $wpdb->get_results("select guid from {$wpdb->prefix}posts where ID= '$attach_id'", ARRAY_A);
				$dirname = date('Y') . '/' . date('m');
				$file_type = wp_check_filetype($guid[0]['guid'], null);
				$ext = '.' . $file_type['ext'];
				$dir = wp_upload_dir();

				// $uploaddir_paths = $dir ['basedir'] . '/' . $dirname ;
				// $uploaddir_url = $dir ['baseurl'] . '/' . $dirname;
				$uploads_use_yearmonth = get_option('uploads_use_yearmonth_folders');
				if ($uploads_use_yearmonth == 1) {
					$uploaddir_paths = $dir['basedir'] . '/' . $dirname;
					$uploaddir_url = $dir['baseurl'] . '/' . $dirname;
				} else {
					$uploaddir_paths = $dir['basedir'];
					$uploaddir_url = $dir['baseurl'];
				}
				$guids = $uploaddir_url . '/' . $media_handle[$plugin . '_gallery_file_name'][0] . $ext;
				$wpdb->update($wpdb->posts, ['guid' => $guids], ['ID' => $attach_id]);
				$fimg_name = $dirname . '/' . $media_handle[$plugin . '_gallery_file_name'][0] . $ext;
				update_post_meta($attach_id, '_wp_attached_file', $fimg_name);
			}
			if ($attach_id != null && isset($media_handle[$plugin . '_gallery_alt_text'][0])) {
				update_post_meta($attach_id, '_wp_attachment_image_alt', $media_handle[$plugin . '_gallery_alt_text'][0]);
			}
		}
	}

	public function acfimageMetaImports($attach_id, $media_handle, $plugin)
	{
		if (is_array($attach_id)) {
			foreach ($attach_id as $attachid => $attachvalid) {
				if (isset($media_handle[$plugin . '_caption'][$attachid]) || isset($media_handle[$plugin . '_description'][$attachid])) {
					wp_update_post(array(
						'ID'           => $attachvalid,
						'post_content' => $media_handle[$plugin . '_description'][$attachid],
						'post_excerpt' => $media_handle[$plugin . '_caption'][$attachid]
					));
				}
				if (isset($media_handle[$plugin . '_title'][$attachid])) {
					wp_update_post(array(
						'ID'           => $attachvalid,
						'post_title'   => $media_handle[$plugin . '_title'][$attachid]
					));
				}
				if ($attachvalid != null && isset($media_handle[$plugin . '_file_name'][$attachid])) {
					global $wpdb;
					$guid = $wpdb->get_results("select guid from {$wpdb->prefix}posts where ID= '$attachvalid'", ARRAY_A);
					$dirname = date('Y') . '/' . date('m');
					$file_type = wp_check_filetype($guid[0]['guid'], null);
					$ext = '.' . $file_type['ext'];
					$dir = wp_upload_dir();

					// $uploaddir_paths = $dir ['basedir'] . '/' . $dirname ;
					// $uploaddir_url = $dir ['baseurl'] . '/' . $dirname;
					$uploads_use_yearmonth = get_option('uploads_use_yearmonth_folders');
					if ($uploads_use_yearmonth == 1) {
						$uploaddir_paths = $dir['basedir'] . '/' . $dirname;
						$uploaddir_url = $dir['baseurl'] . '/' . $dirname;
					} else {
						$uploaddir_paths = $dir['basedir'];
						$uploaddir_url = $dir['baseurl'];
					}
					$guids = $uploaddir_url . '/' . $media_handle[$plugin . '_file_name'][$attachid] . $ext;
					$wpdb->update($wpdb->posts, ['guid' => $guids], ['ID' => $attachvalid]);
					$fimg_name = $dirname . '/' . $media_handle[$plugin . '_file_name'][$attachid] . $ext;
					update_post_meta($attachvalid, '_wp_attached_file', $fimg_name);
				}
				if ($attachvalid != null && isset($media_handle[$plugin . '_alt_text'][$attachid])) {
					update_post_meta($attachvalid, '_wp_attachment_image_alt', $media_handle[$plugin . '_alt_text'][$attachid]);
				}
			}
		} else {

			if (isset($media_handle[$plugin . '_caption'][0]) || isset($media_handle[$plugin . '_description'][0])) {
				wp_update_post(array(
					'ID'           => $attach_id,
					'post_content' => $media_handle[$plugin . '_description'][0],
					'post_excerpt' => $media_handle[$plugin . '_caption'][0]
				));
			}
			if (isset($media_handle[$plugin . '_title'][0])) {
				wp_update_post(array(
					'ID'           => $attach_id,
					'post_title'   => $media_handle[$plugin . '_title'][0]
				));
			}
			if ($attach_id != null && isset($media_handle[$plugin . '_file_name'][0])) {
				global $wpdb;
				$guid = $wpdb->get_results("select guid from {$wpdb->prefix}posts where ID= '$attach_id'", ARRAY_A);
				$dirname = date('Y') . '/' . date('m');
				$file_type = wp_check_filetype($guid[0]['guid'], null);
				$ext = '.' . $file_type['ext'];
				$dir = wp_upload_dir();

				// $uploaddir_paths = $dir ['basedir'] . '/' . $dirname ;
				// $uploaddir_url = $dir ['baseurl'] . '/' . $dirname;
				$uploads_use_yearmonth = get_option('uploads_use_yearmonth_folders');
				if ($uploads_use_yearmonth == 1) {
					$uploaddir_paths = $dir['basedir'] . '/' . $dirname;
					$uploaddir_url = $dir['baseurl'] . '/' . $dirname;
				} else {
					$uploaddir_paths = $dir['basedir'];
					$uploaddir_url = $dir['baseurl'];
				}
				$guids = $uploaddir_url . '/' . $media_handle[$plugin . '_file_name'][0] . $ext;
				$wpdb->update($wpdb->posts, ['guid' => $guids], ['ID' => $attach_id]);
				$fimg_name = $dirname . '/' . $media_handle[$plugin . '_file_name'][0] . $ext;
				update_post_meta($attach_id, '_wp_attached_file', $fimg_name);
			}
			if ($attach_id != null && isset($media_handle[$plugin . '_alt_text'][0])) {
				update_post_meta($attach_id, '_wp_attachment_image_alt', $media_handle[$plugin . '_alt_text'][0]);
			}
		}
	}
	function overwrite($post_id, $img_url)
	{
		global $wpdb;
		$sql = "SELECT post_mime_type FROM {$wpdb->prefix}posts WHERE ID = $post_id";
		list($current_filetype) = $wpdb->get_row($sql, ARRAY_N);
		$current_filename = wp_get_attachment_url($post_id);
		$current_guid = $current_filename;
		$current_filename = substr($current_filename, (strrpos($current_filename, "/") + 1));
		$ID = $post_id;
		$current_file = get_attached_file($ID);
		$current_path = substr($current_file, 0, (strrpos($current_file, "/")));
		$current_file = preg_replace("|(?<!:)/{2,}|", "/", $current_file);
		$current_filename = basename($current_file);
		$current_metadata = wp_get_attachment_metadata($post_id);

		$new_filename = basename($img_url);
		$file_type = wp_check_filetype($new_filename, null);
		$new_filetype = $file_type["type"];
		if (empty($new_filetype['ext'])) {
			$new_filename = @basename($img_url);
			$new_filename = str_replace(' ', '-', trim($new_filename));
			$new_filename = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $new_filename);
		}
		if (empty($new_filetype)) {
			$new_filetype = 'image/jpeg';
		}
		if (preg_match_all('/\b(?:(?:https?|http|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i', $img_url, $matchedlist, PREG_PATTERN_ORDER)) {
			$img_url = $img_url;
		} else {
			$media_dir = wp_get_upload_dir();
			$names = glob($media_dir['path'] . '/' . '*.*');
			foreach ($names as $values) {
				$exp = basename($values);
				if ($exp == $img_url) {
					$attach_data = wp_generate_attachment_metadata($ID, $values);
					wp_update_attachment_metadata($ID,  $attach_data);
				}

				if (!empty($f_img) && strpos($values, $img_url) !== false) {
					$img_url = $media_dir['url'] . '/' . $img_url;
				}
			}
			return true;
		}
		$original_file_perms = fileperms($current_file) & 0777;
		$this->emr_delete_current_files($current_file, $post_id, $current_metadata);
		//	$new_filename = wp_unique_filename( $current_path, $new_filename );
		$new_file = $current_path . "/" . $new_filename;

		$data = file_get_contents($img_url);
		file_put_contents($new_file, $data);
		@chmod($current_file, $original_file_perms);
		$new_filetitle = preg_replace('/\.[^.]+$/', '', basename($new_file));
		$new_guid = str_replace($current_filename, $new_filename, $current_guid);
		$post_date = gmdate('Y-m-d H:i:s');
		$sql = $wpdb->prepare(
			"UPDATE {$wpdb->prefix}posts SET post_title = '$new_filetitle', post_name = '$new_filetitle', guid = '$new_guid', post_mime_type = '$new_filetype', post_date = '$post_date', post_date_gmt = '$post_date' WHERE ID = %d;",
			$post_id
		);
		$wpdb->query($sql);
		$sql = $wpdb->prepare(
			"SELECT meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
			$post_id
		);
		$old_meta_name = $wpdb->get_row($sql, ARRAY_A);
		$old_meta_name = $old_meta_name["meta_value"];
		// Make new postmeta _wp_attached_file
		$new_meta_name = str_replace($current_filename, $new_filename, $old_meta_name);
		$sql = $wpdb->prepare(
			"UPDATE {$wpdb->prefix}postmeta SET meta_value = '$new_meta_name' WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
			$post_id
		);
		$wpdb->query($sql);
		$new_metadata = wp_generate_attachment_metadata($post_id, $new_file);
		wp_update_attachment_metadata($post_id, $new_metadata);
		$current_base_url = $this->emr_get_match_url($current_guid); //  .wp-contet.uplodas/ dae name without ext
		$sql = $wpdb->prepare(
			"SELECT ID, post_content FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_content LIKE %s;",
			'%' . $current_base_url . '%'
		);
		$rs = $wpdb->get_results($sql, ARRAY_A);
		$number_of_updates = 0;

		if (! empty($rs)) {
			$search_urls  = $this->emr_get_file_urls($current_guid, $current_metadata);
			$replace_urls = $this->emr_get_file_urls($new_guid, $new_metadata);
			$replace_urls = $this->emr_normalize_file_urls($search_urls, $replace_urls);
			foreach ($rs as $rows) {
				$number_of_updates = $number_of_updates + 1;
				$post_content = $rows["post_content"];
				$post_content = addslashes(str_replace($search_urls, $replace_urls, $post_content));
				$sql = $wpdb->prepare(
					"UPDATE {$wpdb->prefix}posts SET post_content = '$post_content' WHERE ID = %d;",
					$rows["ID"]
				);
				$wpdb->query($sql);
			}
		}
		update_attached_file($post_id, $new_file);
	}

	function emr_delete_current_files($current_file, $post_id, $metadata = null)
	{
		$current_path = substr($current_file, 0, (strrpos($current_file, "/")));
		if (file_exists($current_file)) {
			clearstatcache();
			if (is_writable($current_file)) {
				unlink($current_file);
			} else {
				printf(esc_html__('The file %1$s can not be deleted by the web server, most likely because the permissions on the file are wrong.'), $current_file);
				exit;
			}
		}
		// Delete old resized versions if this was an image
		$suffix = substr($current_file, (strlen($current_file) - 4));
		$prefix = substr($current_file, 0, (strlen($current_file) - 4));
		if (strtolower($suffix) === ".pdf") {
			$prefix .= "-pdf";
			$suffix = ".jpg";
		}
		$imgAr = array(".png", ".gif", ".jpg", ".jpeg");
		if (in_array($suffix, $imgAr)) {
			// It's a png/gif/jpg based on file name
			// Get thumbnail filenames from metadata
			if (empty($metadata)) {
				$metadata = wp_get_attachment_metadata($post_id);
			}
			if (is_array($metadata)) { // Added fix for error messages when there is no metadata (but WHY would there not be? I don't know…)
				foreach ($metadata["sizes"] as $thissize) {
					// Get all filenames and do an unlink() on each one;
					$thisfile = $thissize["file"];
					// Create array with all old sizes for replacing in posts later
					$oldfilesAr[] = $thisfile;
					// Look for files and delete them
					if (strlen($thisfile)) {
						$thisfile = $current_path . "/" . $thissize["file"];
						if (file_exists($thisfile)) {
							unlink($thisfile);
						}
					}
				}
			}
		}
	}

	function emr_get_match_url($url)
	{
		$url = $this->emr_remove_scheme($url);
		$url = $this->emr_maybe_remove_query_string($url);
		$url = $this->emr_remove_size_from_filename($url, true);
		$url = $this->emr_remove_domain_from_filename($url);
		return $url;
	}

	function emr_remove_scheme($url)
	{
		return preg_replace('/^(?:http|https):/', '', $url);
	}

	function emr_maybe_remove_query_string($url)
	{
		$parts = explode('?', $url);
		return reset($parts);
	}

	function emr_remove_size_from_filename($url, $remove_extension = false)
	{
		$url = preg_replace('/^(\S+)-[0-9]{1,4}x[0-9]{1,4}(\.[a-zA-Z0-9\.]{2,})?/', '$1$2', $url);
		if ($remove_extension) {
			$ext = pathinfo($url, PATHINFO_EXTENSION);
			$url = str_replace(".$ext", '', $url);
		}
		return $url;
	}

	function emr_remove_domain_from_filename($url)
	{
		$url = str_replace($this->emr_remove_scheme(get_bloginfo('url')), '', $url);
		return $url;
	}


	function emr_get_file_urls($guid, $metadata)
	{
		$urls = array();
		$guid = $this->emr_remove_scheme($guid);
		$guid = $this->emr_remove_domain_from_filename($guid);
		$urls['guid'] = $guid;
		if (empty($metadata)) {
			return $urls;
		}
		$base_url = dirname($guid);
		if (! empty($metadata['file'])) {
			$urls['file'] = trailingslashit($base_url) . wp_basename($metadata['file']);
		}

		if (! empty($metadata['sizes'])) {
			foreach ($metadata['sizes'] as $key => $value) {
				$urls[$key] = trailingslashit($base_url) . wp_basename($value['file']);
			}
		}
		return $urls;
	}

	function emr_normalize_file_urls($old, $new)
	{
		$result = array();
		if (empty($new['guid'])) {
			return $result;
		}
		$guid = $new['guid'];
		foreach ($old as $key => $value) {
			$result[$key] = empty($new[$key]) ? $guid : $new[$key];
		}
		return $result;
	}

	public function get_filename_path($image_url, $media_type = null)
	{
		$media_handle = get_option('smack_image_options');
		if (!empty($media_handle['media_settings']['file_name']) && $media_type == 'External') {
			$fimg_name = $media_handle['media_settings']['file_name'];
			$fimg_name = str_replace(' ', '-', trim($fimg_name));
			$fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name);
			$upload_paths    = $this->resolve_media_upload_paths( $image_url );
			$uploaddir_paths = $upload_paths['path'];
			$uploaddir_url   = $upload_paths['url'];
			if ($uploaddir_paths != "" && $uploaddir_paths) {
				$uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
			}
			return ['uploaddir_path' => $uploaddir_path, 'uploaddir_url' => $uploaddir_url, 'fimg_name' => $fimg_name];
		} else {
			$image_name = pathinfo($image_url);
			$fimg_name = $image_name['basename'];
			$fimg_name_without_ext = $image_name['filename'];
			if (empty($fimg_name_without_ext)) {
				$fimg_name_without_ext = $fimg_name;
			}
			$file_type = wp_check_filetype($fimg_name, null);
			if (empty($file_type['ext'])) {
				$fimg_name = @basename($image_url);
				$fimg_name = str_replace(' ', '-', trim($fimg_name));
				$fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name);
			}

			if (strstr($image_url, 'https://drive.google.com')) {
				preg_match('/[?&]id=([^&]+)/', $image_url, $matches);
				$fimg_name = isset($matches[1]) ? $matches[1] : basename($image_url);
			}
			$upload_paths    = $this->resolve_media_upload_paths( $image_url );
			$uploaddir_paths = $upload_paths['path'];
			$uploaddir_url   = $upload_paths['url'];
			if ($uploaddir_paths != "" && $uploaddir_paths) {
				$uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
			}

			return ['uploaddir_path' => $uploaddir_path, 'uploaddir_url' => $uploaddir_url, 'fimg_name' => $fimg_name];
		}
	}
	public function image_meta_table_entry($line_number, $post_values, $post_id, $acf_wpname_element, $acf_csv_name, $hash_key, $plugin, $get_import_type, $templatekey = null, $gmode = null, $header_array = null, $value_array = null, $imgformat = null, $typecct = null, $indexs = null, $media_type = null, $jet_child_object_id = null, $parent_object_id = null)
	{
		global $wpdb;
		$acf_wpname_element = isset($acf_wpname_element) ? $acf_wpname_element : '';
		$table_name = $wpdb->prefix . 'smackcsv_file_events';
		$file_name = $wpdb->get_var("SELECT file_name FROM $table_name WHERE hash_key = '$hash_key'");
		$get_path_values = $this->get_filename_path($acf_csv_name, $media_type);
		$uploaddir_path = $get_path_values['uploaddir_path'] ?? '';
		$uploaddir_url = $get_path_values['uploaddir_url'] ?? '';
		$fimg_name = $get_path_values['fimg_name'] ?? '';
		$file_type = 'image/jpeg';

		if ($plugin == 'Media') {
			return $this->handle_media_image($acf_csv_name, $post_id, $post_values, $plugin, $get_import_type, $acf_wpname_element, $hash_key, $header_array, $value_array, $line_number, $indexs, $media_type, $uploaddir_url, $fimg_name, $file_type, $uploaddir_path);
		}

		if (isset($post_id) && !empty($acf_csv_name) && !empty($plugin)) {
			$post_title = $wpdb->get_var("SELECT post_title FROM {$wpdb->prefix}posts WHERE ID = '{$post_id}' AND post_status != 'trash'");
			$shortcode_table = $wpdb->prefix . "ultimate_csv_importer_shortcode_manager";
			$failed_ids = $wpdb->get_results("SELECT post_title, post_id, image_shortcode, media_id, original_image FROM {$wpdb->prefix}ultimate_csv_importer_shortcode_manager WHERE image_shortcode = 'Featured_image_' AND post_id = '{$post_id}' AND original_image = '{$acf_csv_name}'");
			if ($plugin == 'inline') {
				return $this->handle_inline_image($acf_csv_name, $post_id, $post_values, $plugin, $get_import_type, $acf_wpname_element, $hash_key, $header_array, $value_array, $line_number, $indexs, $uploaddir_url, $fimg_name, $file_type, $uploaddir_path, $shortcode_table, $post_title, $file_name);
			}

			if ($plugin == 'Featured') {
				return $this->handle_featured_image($acf_csv_name, $post_id, $post_values, $plugin, $get_import_type, $acf_wpname_element, $hash_key, $header_array, $value_array, $line_number, $uploaddir_url, $fimg_name, $file_type, $uploaddir_path, $shortcode_table, $post_title, $file_name, $failed_ids);
			}

			return $this->handle_custom_image($acf_csv_name, $post_id, $post_values, $plugin, $get_import_type, $acf_wpname_element, $hash_key, $header_array, $value_array, $line_number, $imgformat, $typecct, $indexs, $uploaddir_url, $fimg_name, $file_type, $uploaddir_path, $shortcode_table, $post_title, $file_name, $jet_child_object_id, $parent_object_id);
		}

		return '';
	}

	public function handle_media_image($acf_csv_name, $post_id, $post_values, $plugin, $get_import_type, $acf_wpname_element, $hash_key, $header_array, $value_array, $line_number, $indexs, $media_type, $uploaddir_url, $fimg_name, $file_type, $uploaddir_path)
	{
		$media_handle = get_option('smack_image_options');
		self::set_last_image_import_failure('');
		$attach_id = $this->media_handling($acf_csv_name, $post_id, $post_values, $plugin, $get_import_type, $acf_wpname_element, $hash_key, '', '', $header_array, $value_array, '', '', $line_number, $indexs, '', $media_type);
		if (!empty($attach_id)) {
			return $attach_id;
		}

		if (self::get_last_image_import_failure() === '') {
			self::set_last_image_import_failure('No attachment was created. Verify the file exists under Media uploads or the URL is reachable.');
		}

		$post_info = array(
			'guid' => $uploaddir_url . "/" . $fimg_name,
			'post_mime_type' => $file_type,
			'post_title' => 'image-failed',
			'post_content' => '',
			'post_status' => 'inherit',
			'post_author' => $post_values['author'] ?? ''
		);
		$attach_id = wp_insert_attachment($post_info, $uploaddir_path, $post_id);
		if (!empty($media_handle)) {
			$this->imageMetaImport($attach_id, $media_handle, $header_array, $value_array, '', $media_type);
		}
		return $attach_id;
	}

	public function handle_inline_image($acf_csv_name, $post_id, $post_values, $plugin, $get_import_type, $acf_wpname_element, $hash_key, $header_array, $value_array, $line_number, $indexs, $uploaddir_url, $fimg_name, $file_type, $uploaddir_path, $shortcode_table, $post_title, $file_name)
	{
		global $wpdb;
		$failed_inline_ids = $wpdb->get_results("SELECT post_title, post_id, image_shortcode, media_id, original_image FROM {$wpdb->prefix}ultimate_csv_importer_shortcode_manager WHERE image_shortcode = 'inline_image_' AND post_id = '{$post_id}'");

		$attach_id = $this->media_handling($acf_csv_name, $post_id, $post_values, $plugin, $get_import_type, $acf_wpname_element, $hash_key, '', '', $header_array, $value_array, '', '', $line_number, $indexs);
		if (!empty($attach_id)) {
			return $attach_id;
		}

		if (empty($failed_inline_ids) || $failed_inline_ids[0]->original_image != $acf_csv_name) {
			$post_info = array(
				'guid' => $uploaddir_url . "/" . $fimg_name,
				'post_mime_type' => $file_type,
				'post_title' => 'image-failed',
				'post_content' => '',
				'post_status' => 'inherit',
				'post_author' => $post_values['author'] ?? ''
			);
			$attach_id = wp_insert_attachment($post_info, $uploaddir_path, $post_id);
		}

		if (empty($failed_inline_ids)) {
			$insert_status = $wpdb->insert($shortcode_table, array(
				'post_id' => $post_id,
				'post_title' => $post_title,
				'image_shortcode' => $plugin . '_image_' . $acf_wpname_element,
				'media_id' => $attach_id,
				'original_image' => $acf_csv_name,
				'indexs' => $indexs,
				'hash_key' => $hash_key,
				'import_type' => $get_import_type,
				'file_name' => $file_name
			), array('%d', '%s', '%s', '%d', '%s', '%d', '%s', '%s', '%s'));

			if ($insert_status) {
				$this->store_failed_image_ids($attach_id);
				$this->failed_media_data($line_number, $post_id, $post_title, $attach_id, $acf_csv_name);
			}
		} elseif (isset($failed_inline_ids[0]->post_id) && $failed_inline_ids[0]->post_id == $post_id) {
			$this->store_failed_image_ids($failed_inline_ids[0]->media_id);
			$this->failed_media_data($line_number, $failed_inline_ids[0]->post_id, $failed_inline_ids[0]->post_title, $failed_inline_ids[0]->media_id, $failed_inline_ids[0]->original_image);
		}

		return $attach_id;
	}

	public function handle_featured_image($acf_csv_name, $post_id, $post_values, $plugin, $get_import_type, $acf_wpname_element, $hash_key, $header_array, $value_array, $line_number, $uploaddir_url, $fimg_name, $file_type, $uploaddir_path, $shortcode_table, $post_title, $file_name, $failed_ids)
	{
		global $wpdb;
		if (empty($failed_ids)) {
			$attach_id = $this->media_handling($acf_csv_name, $post_id, $post_values, $plugin, $get_import_type, $acf_wpname_element, $hash_key, '', '', $header_array, $value_array, '', '', $line_number,'','','','','','','',$featured_image = true);
			if (!empty($attach_id)) {
				return $attach_id;
			}
		}

		if (empty($attach_id) || !empty($failed_ids)) {
			$post_info = array(
				'guid' => $uploaddir_url . "/" . $fimg_name,
				'post_mime_type' => $file_type,
				'post_title' => 'image-failed',
				'post_content' => '',
				'post_status' => 'inherit',
				'post_author' => $post_values['author'] ?? ''
			);
			$attach_id = wp_insert_attachment($post_info, $uploaddir_path, $post_id);
			if (empty($failed_ids)) {
				$insert_status = $wpdb->insert($shortcode_table, array(
					'post_id' => $post_id,
					'post_title' => $post_title,
					'image_shortcode' => $plugin . '_image_' . $acf_wpname_element,
					'media_id' => $attach_id,
					'original_image' => $acf_csv_name,
					'hash_key' => $hash_key,
					'import_type' => $get_import_type,
					'file_name' => $file_name
				), array('%d', '%s', '%s', '%d', '%s', '%s', '%s', '%s'));

				if ($insert_status) {
					$this->store_failed_image_ids($attach_id);
					$this->failed_media_data($line_number, $post_id, $post_title, $attach_id, $acf_csv_name);
				}
			}
		}

		return $attach_id;
	}

	public function handle_custom_image($acf_csv_name, $post_id, $post_values, $plugin, $get_import_type, $acf_wpname_element, $hash_key, $header_array, $value_array, $line_number, $imgformat, $typecct, $indexs, $uploaddir_url, $fimg_name, $file_type, $uploaddir_path, $shortcode_table, $post_title, $file_name, $jet_child_object_id, $parent_object_id)
	{
		global $wpdb;

		if (strpos($plugin, 'jetengine_') !== false) {
			if (isset($header_array) && isset($value_array)) {
				$image_meta_value = array(
					'headerarray' => $header_array,
					'valuearray' => $value_array,
					'tablename' => $typecct,
					'returnformat' => $imgformat
				);
				$acf_image_meta  = json_encode($image_meta_value);
			}
		}
		$acf_wpname_element = isset($acf_wpname_element) ? $acf_wpname_element : '';
		$acf_image_meta = isset($acf_image_meta) ? $acf_image_meta : '';
		$failed_id = $wpdb->get_results("SELECT post_title, post_id, image_shortcode, media_id, original_image FROM {$wpdb->prefix}ultimate_csv_importer_shortcode_manager WHERE post_id = '{$post_id}' AND original_image = '{$acf_csv_name}' AND image_shortcode = '" . esc_sql($plugin . '_image__' . $acf_wpname_element) . "'");
		$attach_id = $this->media_handling($acf_csv_name, $post_id, $post_values, $plugin, $get_import_type, $acf_wpname_element, $hash_key, '', '', $header_array, $value_array, '', '', $line_number, $indexs, $acf_image_meta, '', '', $jet_child_object_id, $parent_object_id);

		if (!empty($attach_id)) {
			return $attach_id;
		}

		if (empty($failed_id)) {
			$post_info = array(
				'guid' => $uploaddir_url . "/" . $fimg_name,
				'post_mime_type' => $file_type,
				'post_title' => 'image-failed',
				'post_content' => '',
				'post_status' => 'inherit',
				'post_author' => $post_values['author'] ?? ''
			);
			$attach_id = wp_insert_attachment($post_info, $uploaddir_path, $post_id);

			$insert_status = $wpdb->insert($shortcode_table, array(
				'post_id' => $post_id,
				'post_title' => $post_title,
				'image_shortcode' => $plugin . '_image__' . $acf_wpname_element,
				'media_id' => $attach_id,
				'original_image' => $acf_csv_name,
				'indexs' => $indexs,
				'image_meta' => $acf_image_meta,
				'hash_key' => $hash_key,
				'import_type' => $get_import_type,
				'file_name' => $file_name,
				'jet_child_object_id' => $jet_child_object_id,
				'jet_parent_object_id' => $parent_object_id
			), array('%d', '%s', '%s', '%d', '%s', '%d', '%s', '%s', '%s', '%s', '%s', '%s'));

			if ($insert_status) {
				$this->store_failed_image_ids($attach_id);
				$this->failed_media_data($line_number, $post_id, $post_title, $attach_id, $acf_csv_name);
			}
		} else {
			$media_id = $failed_id[0]->media_id;
			$attachment_id = $wpdb->get_results("SELECT ID FROM {$wpdb->prefix}posts WHERE ID = $media_id AND post_title = 'image-failed' AND post_type = 'attachment' AND guid LIKE '%$fimg_name%'", ARRAY_A);
			$attach_id = $attachment_id[0]['ID'];
		}

		return $attach_id;
	}

	public function store_image_ids($attach_id)
	{
		//get number of images count
		$stored_ids = get_option('total_attachment_ids', '');
		$att_id = $attach_id;
		if ($stored_ids === '') {
			add_option('total_attachment_ids', serialize(array($att_id)));
			$stored_ids = unserialize(get_option('total_attachment_ids', ''));
		} else {
			$get_stored_ids = unserialize(get_option('total_attachment_ids', ''));
			if (is_array($get_stored_ids) && !empty($att_id)) {
				$att_id = is_array($att_id) ? $att_id : array($att_id);
				$stored_ids = array_merge($get_stored_ids, $att_id);
			} else {
				$stored_ids = $att_id;
			}
			update_option('total_attachment_ids', serialize($stored_ids));
			$stored_ids = unserialize(get_option('total_attachment_ids', ''));
		}
	}
	public function store_failed_data($data, $option_name)
	{
		$stored_data = get_option($option_name, '');
		// Initialize array for new data if no stored data exists
		if ($stored_data === '') {
			add_option($option_name, serialize(array($data)));
			$stored_data = unserialize(get_option($option_name, ''));
		} else {
			// Unserialize stored data
			$stored_data = unserialize($stored_data);
			$data = is_array($data) ? $data : array($data);
			$stored_data = array_merge($stored_data, $data);
			update_option($option_name, serialize($stored_data));
			$stored_data = unserialize(get_option($option_name, ''));
		}

		return $stored_data;
	}

	public function store_failed_image_ids($attach_id)
	{
		$option_name = 'failed_attachment_ids';
		$stored_data = $this->store_failed_data($attach_id, $option_name);
		return $stored_data;
	}

	public function failed_media_data($line_number, $post_id, $post_title, $media_id, $actual_url)
	{
		global $core_instance;
		$core_instance = CoreFieldsImport::getInstance();

		$option_name = 'failed_line_number';
		$stored_ids = $this->store_failed_data($media_id, $option_name);

		$count = count($stored_ids);

		$data = array('post_id' => $post_id, 'post_title' => $post_title, 'media_id' => $media_id, 'actual_url' => $actual_url);
		$core_instance->failed_media_data[$count] = $data;
	}
}
