| 1 |
<?php |
| 2 |
/** |
| 3 |
* Import Users plugin file. |
| 4 |
* |
| 5 |
* Copyright (C) 2010-2020, Smackcoders Inc - info@smackcoders.com |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Smackcoders\SMUSERS; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) |
| 11 |
exit; // Exit if accessed directly |
| 12 |
|
| 13 |
class MediaHandling{ |
| 14 |
private static $instance=null,$smack_instance; |
| 15 |
public $header_array,$core_instance; |
| 16 |
public $value_array; |
| 17 |
|
| 18 |
public function __construct(){ |
| 19 |
|
| 20 |
include_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 21 |
add_action('wp_ajax_image_options', array($this , 'imageOptions')); |
| 22 |
add_action('wp_ajax_delete_image' , array($this , 'deleteImage')); |
| 23 |
} |
| 24 |
|
| 25 |
public static function imageOptions(){ |
| 26 |
check_ajax_referer('smack-ultimate-csv-importer', 'securekey'); |
| 27 |
$media_settings['media_handle_option'] = sanitize_text_field($_POST['media_handle_option']); |
| 28 |
$media_settings['use_ExistingImage'] = sanitize_text_field($_POST['use_ExistingImage']); |
| 29 |
$media_settings['enable_postcontent_image'] = sanitize_text_field($_POST['postContent_image_option']); |
| 30 |
$image_info = array( |
| 31 |
'media_settings' => $media_settings |
| 32 |
); |
| 33 |
update_option( 'smack_image_options', $image_info ); |
| 34 |
$result['success'] = 'true'; |
| 35 |
echo wp_json_encode($result); |
| 36 |
wp_die(); |
| 37 |
} |
| 38 |
public static function getInstance() { |
| 39 |
if (MediaHandling::$instance == null) { |
| 40 |
MediaHandling::$instance = new MediaHandling; |
| 41 |
return MediaHandling::$instance; |
| 42 |
} |
| 43 |
return MediaHandling::$instance; |
| 44 |
} |
| 45 |
|
| 46 |
public function deleteImage(){ |
| 47 |
check_ajax_referer('smack-ultimate-csv-importer', 'securekey'); |
| 48 |
$image = sanitize_text_field($_POST['image']); |
| 49 |
$media_dir = wp_get_upload_dir(); |
| 50 |
$names = glob($media_dir['path'].'/'.'*.*'); |
| 51 |
foreach($names as $values){ |
| 52 |
if (strpos($values, $image) !== false) { |
| 53 |
unlink($values); |
| 54 |
} |
| 55 |
} |
| 56 |
$result['success'] = 'true'; |
| 57 |
echo wp_json_encode($result); |
| 58 |
wp_die(); |
| 59 |
} |
| 60 |
public function media_handling($img_url , $post_id , $data_array = null,$module = null, $image_type = null ,$hash_key = null,$templatekey = null,$header_array=null,$value_array=null,$indexs=null,$acf_wpname_element=null,$acf_image_meta=null){ |
| 61 |
$encodedurl = urlencode($img_url); |
| 62 |
$img_url = urldecode($encodedurl); |
| 63 |
$url = parse_url($img_url); |
| 64 |
if($hash_key == null) |
| 65 |
$hash_key = ""; |
| 66 |
$media_handle = get_option('smack_image_options'); |
| 67 |
if(isset($url['scheme']) && $url['scheme'] == 'http' || $url['scheme'] == 'https' ){ |
| 68 |
$image_name = basename($img_url); |
| 69 |
$image_title = sanitize_file_name( pathinfo( $image_name, PATHINFO_FILENAME ) ); |
| 70 |
}else{ |
| 71 |
$image_title=preg_replace('/\\.[^.\\s]{3,4}$/', '', $img_url); |
| 72 |
} |
| 73 |
global $wpdb; |
| 74 |
if($media_handle['media_settings']['use_ExistingImage'] == 'true'){ |
| 75 |
if(is_numeric($img_url)){ |
| 76 |
$attach_id=$img_url; |
| 77 |
if(!empty($data_array['featured_image'])) { |
| 78 |
set_post_thumbnail( $post_id, $attach_id ); |
| 79 |
} |
| 80 |
return $attach_id; |
| 81 |
} |
| 82 |
else{ |
| 83 |
$attachment_id = $wpdb->get_results("SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' AND guid LIKE '%$image_title%'", ARRAY_A); |
| 84 |
|
| 85 |
} |
| 86 |
if(is_array($attachment_id) && !empty($attachment_id[0]['ID']) && $image_type != 'Featured'){ |
| 87 |
$table_name = $wpdb->prefix . 'smackcsv_file_events'; |
| 88 |
$post_title = $wpdb->get_var("SELECT post_title FROM {$wpdb->prefix}posts WHERE ID = '{$post_id}' AND post_status != 'trash'"); |
| 89 |
$file_name = $wpdb->get_var("SELECT file_name FROM $table_name WHERE hash_key = '$hash_key'"); |
| 90 |
$shortcode_table = $wpdb->prefix . "ultimate_csv_importer_shortcode_manager"; |
| 91 |
$attach_id = $attachment_id[0]['ID']; |
| 92 |
$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 '%$image_title%'", ARRAY_A); |
| 93 |
if(!empty($check_id)){ |
| 94 |
$failed_ids = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}ultimate_csv_importer_shortcode_manager WHERE post_id='{$post_id}' AND media_id = '{$attach_id}'"); |
| 95 |
if(!empty($failed_ids) && $failed_ids[0]->post_id != $post_id){ |
| 96 |
$attach_id = $check_id[0]['ID'];; |
| 97 |
$insert_status = $wpdb->insert($shortcode_table, |
| 98 |
array( |
| 99 |
'post_id' => $post_id, |
| 100 |
'post_title' => $post_title, |
| 101 |
'image_shortcode' => $image_type.'_image__'.$acf_wpname_element, |
| 102 |
'media_id' => $attach_id, |
| 103 |
'original_image' => $img_url, |
| 104 |
'indexs' => $indexs, |
| 105 |
'image_meta' => $acf_image_meta, |
| 106 |
'hash_key' => $hash_key, |
| 107 |
'import_type' => $module, |
| 108 |
'file_name' => $file_name |
| 109 |
), |
| 110 |
array('%d','%s','%s','%d','%s','%d','%s','%s','%s','%s') |
| 111 |
); |
| 112 |
if($insert_status){ |
| 113 |
$this->store_failed_image_ids($attach_id); |
| 114 |
} |
| 115 |
|
| 116 |
}elseif(($failed_ids[0]->post_id == $post_id) && ($check_id[0]['ID'] == $failed_ids[0]->media_id)){ |
| 117 |
$attach_id = $check_id[0]['ID']; |
| 118 |
$insert_status = $wpdb->insert($shortcode_table, |
| 119 |
array( |
| 120 |
'post_id' => $post_id, |
| 121 |
'post_title' => $post_title, |
| 122 |
'image_shortcode' => $image_type.'_image__'.$acf_wpname_element, |
| 123 |
'media_id' => $attach_id, |
| 124 |
'original_image' => $img_url, |
| 125 |
'indexs' => $indexs, |
| 126 |
'image_meta' => $acf_image_meta, |
| 127 |
'hash_key' => $hash_key, |
| 128 |
'import_type' => $module, |
| 129 |
'file_name' => $file_name |
| 130 |
), |
| 131 |
array('%d','%s','%s','%d','%s','%d','%s','%s','%s','%s') |
| 132 |
); |
| 133 |
if($insert_status){ |
| 134 |
$this->store_failed_image_ids($attach_id); |
| 135 |
} |
| 136 |
} |
| 137 |
elseif(empty($failed_ids) ){ |
| 138 |
$insert_status = $wpdb->insert($shortcode_table, |
| 139 |
array( |
| 140 |
'post_id' => $post_id, |
| 141 |
'post_title' => $post_title, |
| 142 |
'image_shortcode' => $image_type.'_image__'.$acf_wpname_element, |
| 143 |
'media_id' => $attach_id, |
| 144 |
'original_image' => $img_url, |
| 145 |
'indexs'=>$indexs, |
| 146 |
'hash_key' => $hash_key, |
| 147 |
'import_type' => $module, |
| 148 |
'file_name' => $file_name, |
| 149 |
), |
| 150 |
array('%d','%s','%s','%d','%s','%d','%s','%s','%s') |
| 151 |
); |
| 152 |
if($insert_status){ |
| 153 |
$this->store_failed_image_ids($attach_id); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
//return $attach_id; |
| 158 |
} |
| 159 |
} |
| 160 |
else{ |
| 161 |
$attach_id = $this->image_function($img_url , $post_id , $data_array,'','use_existing_image',$header_array,$value_array); |
| 162 |
} |
| 163 |
|
| 164 |
} |
| 165 |
else{ |
| 166 |
$img_url = is_array($img_url) ? implode(',', array_filter($img_url)) : $img_url; |
| 167 |
$hash_key = is_array($hash_key) ? implode(',', array_filter($hash_key)) : $hash_key; |
| 168 |
$templatekey = is_array($templatekey) ? implode(',', array_filter($templatekey)) : $templatekey; |
| 169 |
$module = is_array($module) ? implode(',', array_filter($module)) : $module; |
| 170 |
$image_type = is_array($image_type) ? implode(',', array_filter($image_type)) : $image_type; |
| 171 |
$img_url = esc_sql($img_url); |
| 172 |
$hash_key = esc_sql($hash_key); |
| 173 |
$templatekey = esc_sql($templatekey); |
| 174 |
$module = esc_sql($module); |
| 175 |
$image_type = esc_sql($image_type); |
| 176 |
|
| 177 |
$attach_id = $this->image_function($img_url, $post_id, $data_array,'','',$header_array,$value_array); |
| 178 |
// if ($attach_id != null) { |
| 179 |
// global $wpdb; |
| 180 |
// $image_table = $wpdb->prefix . "ultimate_csv_importer_media"; |
| 181 |
// $wpdb->query($wpdb->prepare("INSERT INTO $image_table (image_url, attach_id, post_id, hash_key, templatekey, module, image_type, status) VALUES (%s, %d, %d, %s, %s, %s, %s, 'Completed')",$img_url,$attach_id,$post_id,$hash_key,$templatekey,$module,$image_type)); |
| 182 |
// } |
| 183 |
|
| 184 |
} |
| 185 |
return $attach_id; |
| 186 |
} |
| 187 |
|
| 188 |
public function image_function($f_img , $post_id , $data_array = null,$option_name = null, $use_existing_image = false,$header_array = null , $value_array = null){ |
| 189 |
|
| 190 |
global $wpdb; |
| 191 |
$f_img = urldecode($f_img); |
| 192 |
// $image = explode("?", $f_img); |
| 193 |
// $f_img=$image[0]; |
| 194 |
$media_handle = get_option('smack_image_options'); |
| 195 |
if(!empty($header_array) && !empty($value_array) ){ |
| 196 |
$media_settings = array_combine($header_array,$value_array); |
| 197 |
} |
| 198 |
if(isset($media_handle['media_settings']['alttext'])) { |
| 199 |
$alttext ['_wp_attachment_image_alt'] = isset($media_settings[$media_handle['media_settings']['alttext']]) ? $media_settings[$media_handle['media_settings']['alttext']] :''; |
| 200 |
} |
| 201 |
|
| 202 |
if(preg_match_all('/\b(?:(?:https?|http|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i', $f_img , $matchedlist, PREG_PATTERN_ORDER)) { |
| 203 |
$f_img = $f_img; |
| 204 |
} |
| 205 |
else{ |
| 206 |
$media_dir = wp_get_upload_dir(); |
| 207 |
$names = glob($media_dir['path'].'/'.'*.*'); |
| 208 |
foreach($names as $values){ |
| 209 |
if (strpos($values, $f_img) !== false) { |
| 210 |
$f_img = $media_dir['url'].'/'.$f_img; |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
$image_name = pathinfo($f_img); |
| 216 |
//if(!empty($media_handle['media_settings']['file_name'])){ |
| 217 |
//$file_type = wp_check_filetype( $f_img, null ); |
| 218 |
//$ext = '.'. $file_type['ext']; |
| 219 |
// if(!empty($media_settings[$media_handle['media_settings']['file_name']])){ |
| 220 |
// $fimg_name = $media_settings[$media_handle['media_settings']['file_name']].$ext; |
| 221 |
// } |
| 222 |
//} |
| 223 |
//else{ |
| 224 |
$fimg_name = $image_name['basename']; |
| 225 |
//} |
| 226 |
$file_type = wp_check_filetype( $fimg_name, null ); |
| 227 |
if($use_existing_image){ |
| 228 |
if(empty($file_type['ext'])){ |
| 229 |
$fimg_name = @basename($f_img); |
| 230 |
$fimg_name = str_replace(' ', '-', trim($fimg_name)); |
| 231 |
$fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name); |
| 232 |
} |
| 233 |
$attachment_id = $wpdb->get_var("SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type = 'attachment' AND guid LIKE '%$fimg_name'"); |
| 234 |
|
| 235 |
if($attachment_id){ |
| 236 |
if(!empty($data_array['featured_image'])){ |
| 237 |
set_post_thumbnail( $post_id, $attachment_id ); |
| 238 |
return $attachment_id; |
| 239 |
}else{ |
| 240 |
return $attachment_id; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
$attachment_title = sanitize_file_name( pathinfo( $fimg_name, PATHINFO_FILENAME ) ); |
| 246 |
$file_type = wp_check_filetype( $fimg_name, null ); |
| 247 |
$dir = wp_upload_dir(); |
| 248 |
$dirname = date('Y') . '/' . date('m'); |
| 249 |
$uploads_use_yearmonth = get_option('uploads_use_yearmonth_folders'); |
| 250 |
if($uploads_use_yearmonth == 1){ |
| 251 |
$uploaddir_paths = $dir ['basedir'] . '/' . $dirname ; |
| 252 |
$uploaddir_url = $dir ['baseurl'] . '/' . $dirname; |
| 253 |
} |
| 254 |
else{ |
| 255 |
$uploaddir_paths = $dir ['basedir']; |
| 256 |
$uploaddir_url = $dir ['baseurl']; |
| 257 |
} |
| 258 |
$f_img = str_replace(" ","%20",$f_img); |
| 259 |
if(empty($file_type['ext'])){ |
| 260 |
$fimg_name = @basename($f_img); |
| 261 |
$fimg_name = str_replace(' ', '-', trim($fimg_name)); |
| 262 |
$fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name); |
| 263 |
} |
| 264 |
if ($uploaddir_paths != "" && $uploaddir_paths) { |
| 265 |
if (strpos($fimg_name, ' ') !== false) { |
| 266 |
$fimg_name = str_replace(' ', '-', $fimg_name); |
| 267 |
$fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name); |
| 268 |
} |
| 269 |
$uploaddir_path = $uploaddir_paths . "/" . $fimg_name; |
| 270 |
} |
| 271 |
|
| 272 |
if($file_type['ext'] == 'jpeg'){ |
| 273 |
$response = wp_safe_remote_get($f_img, array( 'timeout' => 30)); |
| 274 |
}else{ |
| 275 |
$response = wp_safe_remote_get($f_img, array( 'timeout' => 10)); |
| 276 |
} |
| 277 |
if(is_wp_error($response)) { |
| 278 |
return null; |
| 279 |
} |
| 280 |
$rawdata = wp_remote_retrieve_body($response); |
| 281 |
|
| 282 |
$http_code = wp_remote_retrieve_response_code($response); |
| 283 |
if($http_code == 404){ |
| 284 |
return null; |
| 285 |
} |
| 286 |
|
| 287 |
if ( $http_code != 200 && strpos( $rawdata, 'Not Found' ) != 0 ) { |
| 288 |
return null; |
| 289 |
} |
| 290 |
if(is_plugin_active('exmage-wp-image-links/exmage-wp-image-links.php')){ |
| 291 |
$guid =$fimg_name; |
| 292 |
} |
| 293 |
if ($rawdata == false) { |
| 294 |
return null; |
| 295 |
} else { |
| 296 |
if(is_plugin_active('exmage-wp-image-links/exmage-wp-image-links.php')){ |
| 297 |
$link = new \EXMAGE_WP_IMAGE_LINKS; |
| 298 |
$postID = $link->add_image($data_array['featured_image'],$value); |
| 299 |
wp_update_post(array( |
| 300 |
'ID' => $postID['id'], |
| 301 |
'post_title' => $data_array['title'], |
| 302 |
'post_content' => $data_array['description'], |
| 303 |
'post_excerpt' => $data_array['caption'] |
| 304 |
)); |
| 305 |
// if($postID['id'] != null && isset($data_array['alt_text'])){ |
| 306 |
//update_post_meta($postID['id'], '_wp_attachment_image_alt', $data_array['alt_text']); |
| 307 |
//} |
| 308 |
} |
| 309 |
else{ |
| 310 |
if (file_exists($uploaddir_path)) { |
| 311 |
$i = 1; |
| 312 |
$exist = true; |
| 313 |
while($exist){ |
| 314 |
$fimg_name = $attachment_title . "-" . $i . "." . $file_type['ext']; |
| 315 |
$uploaddir_path = $uploaddir_paths . "/" . $fimg_name; |
| 316 |
|
| 317 |
if (file_exists($uploaddir_path)) { |
| 318 |
$i = $i + 1; |
| 319 |
} |
| 320 |
else{ |
| 321 |
$exist = false; |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
$fp = fopen($uploaddir_path, 'x'); |
| 326 |
fwrite($fp, $rawdata); |
| 327 |
fclose($fp); |
| 328 |
} |
| 329 |
} |
| 330 |
if(empty($file_type['type'])){ |
| 331 |
$file_type['type'] = 'image/jpeg'; |
| 332 |
} |
| 333 |
if(is_plugin_active('exmage-wp-image-links/exmage-wp-image-links.php')){ |
| 334 |
$guids =$data_array['featured_image']; |
| 335 |
} |
| 336 |
else{ |
| 337 |
$guids=$uploaddir_url . "/" . $fimg_name; |
| 338 |
} |
| 339 |
if(!empty($data_array['title'])){ |
| 340 |
$attachment_title = $data_array['title']; |
| 341 |
}else{ |
| 342 |
$attachment_title = str_replace('-', ' ', $attachment_title); |
| 343 |
} |
| 344 |
$post_info = array( |
| 345 |
'guid' => $guids, |
| 346 |
'post_mime_type' => $file_type['type'], |
| 347 |
'post_title' => $attachment_title, |
| 348 |
'post_content' => '', |
| 349 |
'post_status' => 'inherit', |
| 350 |
); |
| 351 |
if(!is_plugin_active('exmage-wp-image-links/exmage-wp-image-links.php')){ |
| 352 |
$attach_id = wp_insert_attachment( $post_info,$uploaddir_path, $post_id ); |
| 353 |
$attach_data = wp_generate_attachment_metadata( $attach_id, $uploaddir_path ); |
| 354 |
wp_update_attachment_metadata( $attach_id, $attach_data ); |
| 355 |
} |
| 356 |
|
| 357 |
if(!empty($data_array['featured_image'])) { |
| 358 |
set_post_thumbnail( $post_id, $attach_id ); |
| 359 |
} |
| 360 |
return $attach_id; |
| 361 |
} |
| 362 |
|
| 363 |
public function get_filename_path($image_url){ |
| 364 |
$image_name = pathinfo($image_url); |
| 365 |
$fimg_name = $image_name['basename']; |
| 366 |
$fimg_name_without_ext = $image_name['filename']; |
| 367 |
if(empty($fimg_name_without_ext)){ |
| 368 |
$fimg_name_without_ext = $fimg_name; |
| 369 |
} |
| 370 |
$file_type = wp_check_filetype( $fimg_name, null ); |
| 371 |
if(empty($file_type['ext'])){ |
| 372 |
$fimg_name = @basename($image_url); |
| 373 |
$fimg_name = str_replace(' ', '-', trim($fimg_name)); |
| 374 |
$fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name); |
| 375 |
} |
| 376 |
|
| 377 |
if (strstr($image_url, 'https://drive.google.com')){ |
| 378 |
preg_match('/[?&]id=([^&]+)/', $image_url, $matches); |
| 379 |
$fimg_name = isset($matches[1]) ? $matches[1] : basename($image_url); |
| 380 |
} |
| 381 |
$dir = wp_upload_dir(); |
| 382 |
$dirname = date('Y') . '/' . date('m'); |
| 383 |
$uploads_use_yearmonth = get_option('uploads_use_yearmonth_folders'); |
| 384 |
if($uploads_use_yearmonth == 1){ |
| 385 |
$uploaddir_paths = $dir ['basedir'] . '/' . $dirname ; |
| 386 |
$uploaddir_url = $dir ['baseurl'] . '/' . $dirname; |
| 387 |
} |
| 388 |
else{ |
| 389 |
$uploaddir_paths = $dir ['basedir']; |
| 390 |
$uploaddir_url = $dir ['baseurl']; |
| 391 |
} |
| 392 |
if ($uploaddir_paths != "" && $uploaddir_paths) { |
| 393 |
$uploaddir_path = $uploaddir_paths . "/" . $fimg_name; |
| 394 |
} |
| 395 |
|
| 396 |
return ['uploaddir_path' => $uploaddir_path,'uploaddir_url' => $uploaddir_url,'fimg_name' => $fimg_name]; |
| 397 |
} |
| 398 |
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){ |
| 399 |
global $wpdb; |
| 400 |
$table_name = $wpdb->prefix . 'smackcsv_file_events'; |
| 401 |
$file_name = $wpdb->get_var("SELECT file_name FROM $table_name WHERE hash_key = '$hash_key'"); |
| 402 |
$post_title = $wpdb->get_var("SELECT post_title FROM {$wpdb->prefix}posts WHERE ID = '{$post_id}' AND post_status != 'trash'"); |
| 403 |
$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}' "); |
| 404 |
//$core_instance = CoreFieldsImport::getInstance(); |
| 405 |
//$core_instance = new WooCommerceCoreImport; |
| 406 |
$shortcode_table = $wpdb->prefix . "ultimate_csv_importer_shortcode_manager"; |
| 407 |
|
| 408 |
$get_path_values = $this->get_filename_path($acf_csv_name); |
| 409 |
$uploaddir_path = isset($get_path_values['uploaddir_path']) ? $get_path_values['uploaddir_path'] : ''; |
| 410 |
$uploaddir_url = isset($get_path_values['uploaddir_url']) ? $get_path_values['uploaddir_url'] : ''; |
| 411 |
$fimg_name = isset($get_path_values['fimg_name']) ? $get_path_values['fimg_name'] : ''; |
| 412 |
$file_type = 'image/jpeg'; |
| 413 |
if(isset($post_id) && !empty($acf_csv_name) ){ |
| 414 |
|
| 415 |
/************* custom image functionality ***********/ |
| 416 |
if(strpos($plugin, 'jetengine_') !== false ){ |
| 417 |
$image_meta_value = array( |
| 418 |
'headerarray' => $header_array, |
| 419 |
'valuearray' => $value_array, |
| 420 |
'tablename' => $typecct, |
| 421 |
'returnformat' => $imgformat |
| 422 |
); |
| 423 |
$acf_image_meta =json_encode($image_meta_value); |
| 424 |
} |
| 425 |
$acf_image_meta = isset($acf_image_meta) ? $acf_image_meta : null; |
| 426 |
$attach_id = $this->media_handling($acf_csv_name, $post_id, $post_values,$get_import_type,$plugin,$hash_key,'',$header_array,$value_array,$indexs,$acf_wpname_element,$acf_image_meta); |
| 427 |
$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) . "' "); |
| 428 |
if(!empty($attach_id)){ |
| 429 |
$this->store_image_ids($attach_id ); //store the image id |
| 430 |
return $attach_id; |
| 431 |
} |
| 432 |
elseif(empty($attach_id) || !empty($failed_id) || empty($failed_id)){ |
| 433 |
if(empty($failed_id)){ |
| 434 |
$post_info = array( |
| 435 |
'guid' => $uploaddir_url . "/" . $fimg_name, |
| 436 |
'post_mime_type' => $file_type, |
| 437 |
'post_title' => 'image-failed', |
| 438 |
'post_content' => '', |
| 439 |
'post_status' => 'inherit', |
| 440 |
'post_author' => isset($post_values['author']) ? $post_values['author'] : '' |
| 441 |
); |
| 442 |
$attach_id = wp_insert_attachment( $post_info,$uploaddir_path, $post_id ); |
| 443 |
} |
| 444 |
elseif($failed_id[0]->original_image != $acf_csv_name){ |
| 445 |
$post_info = array( |
| 446 |
'guid' => $uploaddir_url . "/" . $fimg_name, |
| 447 |
'post_mime_type' => $file_type, |
| 448 |
'post_title' => 'image-failed', |
| 449 |
'post_content' => '', |
| 450 |
'post_status' => 'inherit', |
| 451 |
'post_author' => $post_values['author'] |
| 452 |
); |
| 453 |
$attach_id = wp_insert_attachment( $post_info,$uploaddir_path, $post_id ); |
| 454 |
} |
| 455 |
elseif(($failed_id[0]->image_shortcode == 'Featured_image_') && ($failed_id[0]->original_image == $acf_csv_name)){ |
| 456 |
$post_info = array( |
| 457 |
'guid' => $uploaddir_url . "/" . $fimg_name, |
| 458 |
'post_mime_type' => $file_type, |
| 459 |
'post_title' => 'image-failed', |
| 460 |
'post_content' => '', |
| 461 |
'post_status' => 'inherit', |
| 462 |
'post_author' => $post_values['author'] |
| 463 |
); |
| 464 |
$attach_id = wp_insert_attachment( $post_info,$uploaddir_path, $post_id ); |
| 465 |
} |
| 466 |
if(empty($failed_id)){ |
| 467 |
$insert_status = $wpdb->insert($shortcode_table, |
| 468 |
array( |
| 469 |
'post_id' => $post_id, |
| 470 |
'post_title' => $post_title, |
| 471 |
'image_shortcode' => $plugin.'_image__'.$acf_wpname_element, |
| 472 |
'media_id' => $attach_id, |
| 473 |
'original_image' => $acf_csv_name, |
| 474 |
'indexs' => $indexs, |
| 475 |
'image_meta' => $acf_image_meta, |
| 476 |
'hash_key' => $hash_key, |
| 477 |
'import_type' => $get_import_type, |
| 478 |
'file_name' => $file_name |
| 479 |
), |
| 480 |
array('%d','%s','%s','%d','%s','%d','%s','%s','%s','%s') |
| 481 |
); |
| 482 |
if($insert_status){ |
| 483 |
$this->store_image_ids($attach_id);//store number of image ids |
| 484 |
$this->store_failed_image_ids($attach_id);//store failed image ids |
| 485 |
} |
| 486 |
} |
| 487 |
else{ |
| 488 |
$media_id = $failed_id[0]->media_id; |
| 489 |
$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); |
| 490 |
$attach_id= $attachment_id[0]['ID']; |
| 491 |
} |
| 492 |
return isset($attach_id) ? $attach_id : ''; |
| 493 |
}else{ |
| 494 |
$attachment_id = $wpdb->get_results("SELECT ID FROM {$wpdb->prefix}posts WHERE post_title ='image-failed' AND post_type = 'attachment' AND guid LIKE '%$fimg_name%'", ARRAY_A); |
| 495 |
} |
| 496 |
return $attach_id; |
| 497 |
} |
| 498 |
} |
| 499 |
public function store_image_ids($attach_id){ |
| 500 |
//get number of images count |
| 501 |
$stored_ids = get_option('total_attachment_ids', ''); |
| 502 |
$att_id = $attach_id; |
| 503 |
if ($stored_ids === '') { |
| 504 |
add_option('total_attachment_ids', serialize(array($att_id))); |
| 505 |
$stored_ids = unserialize(get_option('total_attachment_ids', '')); |
| 506 |
} else { |
| 507 |
$get_stored_ids = unserialize(get_option('total_attachment_ids', '')); |
| 508 |
if (is_array($get_stored_ids) && !empty($att_id)) { |
| 509 |
$att_id = is_array($att_id) ? $att_id : array($att_id); |
| 510 |
$stored_ids = array_merge($get_stored_ids,$att_id); |
| 511 |
} else { |
| 512 |
$stored_ids = $att_id; |
| 513 |
} |
| 514 |
update_option('total_attachment_ids', serialize($stored_ids)); |
| 515 |
$stored_ids = unserialize(get_option('total_attachment_ids', '')); |
| 516 |
} |
| 517 |
} |
| 518 |
public function store_failed_image_ids($attach_id){ |
| 519 |
//get number of failed images count |
| 520 |
$stored_ids = get_option('failed_attachment_ids', ''); |
| 521 |
$att_id = $attach_id; |
| 522 |
if ($stored_ids === '') { |
| 523 |
add_option('failed_attachment_ids', serialize(array($att_id))); |
| 524 |
$stored_ids = unserialize(get_option('failed_attachment_ids', '')); |
| 525 |
} else { |
| 526 |
$get_stored_ids = unserialize(get_option('failed_attachment_ids', '')); |
| 527 |
if (is_array($get_stored_ids) && !empty($att_id)) { |
| 528 |
$att_id = is_array($att_id) ? $att_id : array($att_id); |
| 529 |
$stored_ids = array_merge($get_stored_ids,$att_id); |
| 530 |
} else { |
| 531 |
$stored_ids = $att_id; |
| 532 |
} |
| 533 |
update_option('failed_attachment_ids', serialize($stored_ids)); |
| 534 |
$stored_ids = unserialize(get_option('failed_attachment_ids', '')); |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
} |