PluginProbe
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on / 2.0
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on v2.0
2.0 1.7.1 1.2.9 1.3 1.4 1.4.1 1.4.2 1.4.3 1.5 1.6 1.7 trunk 1.0 1.1 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8
import-users / importExtensions / MediaHandling.php

MediaHandling.php in Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on 2.0, at importExtensions/MediaHandling.php

607 lines 22.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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(
84 $wpdb->prepare(
85 "SELECT ID FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' AND guid LIKE %s",
86 '%' . $wpdb->esc_like( $image_title ) . '%'
87 ),
88 ARRAY_A
89 );
90
91 }
92 if(is_array($attachment_id) && !empty($attachment_id[0]['ID']) && $image_type != 'Featured'){
93 $table_name = $wpdb->prefix . 'smackcsv_file_events';
94 $post_title = $wpdb->get_var(
95 $wpdb->prepare(
96 "SELECT post_title FROM {$wpdb->prefix}posts WHERE ID = %d AND post_status != 'trash'",
97 absint( $post_id )
98 )
99 );
100 $file_name = $wpdb->get_var(
101 $wpdb->prepare(
102 "SELECT file_name FROM $table_name WHERE hash_key = %s",
103 $hash_key
104 )
105 );
106 $shortcode_table = $wpdb->prefix . "ultimate_csv_importer_shortcode_manager";
107 $attach_id = $attachment_id[0]['ID'];
108 $check_id = $wpdb->get_results(
109 $wpdb->prepare(
110 "SELECT ID FROM {$wpdb->prefix}posts WHERE ID = %d AND post_title ='image-failed' AND post_type = 'attachment' AND guid LIKE %s",
111 absint( $attach_id ),
112 '%' . $wpdb->esc_like( $image_title ) . '%'
113 ),
114 ARRAY_A
115 );
116 if(!empty($check_id)){
117 $failed_ids = $wpdb->get_results(
118 $wpdb->prepare(
119 "SELECT * FROM {$wpdb->prefix}ultimate_csv_importer_shortcode_manager WHERE post_id = %d AND media_id = %d",
120 absint( $post_id ),
121 absint( $attach_id )
122 )
123 );
124 if(!empty($failed_ids) && $failed_ids[0]->post_id != $post_id){
125 $attach_id = $check_id[0]['ID'];;
126 $insert_status = $wpdb->insert($shortcode_table,
127 array(
128 'post_id' => $post_id,
129 'post_title' => $post_title,
130 'image_shortcode' => $image_type.'_image__'.$acf_wpname_element,
131 'media_id' => $attach_id,
132 'original_image' => $img_url,
133 'indexs' => $indexs,
134 'image_meta' => $acf_image_meta,
135 'hash_key' => $hash_key,
136 'import_type' => $module,
137 'file_name' => $file_name
138 ),
139 array('%d','%s','%s','%d','%s','%d','%s','%s','%s','%s')
140 );
141 if($insert_status){
142 $this->store_failed_image_ids($attach_id);
143 }
144
145 }elseif(($failed_ids[0]->post_id == $post_id) && ($check_id[0]['ID'] == $failed_ids[0]->media_id)){
146 $attach_id = $check_id[0]['ID'];
147 $insert_status = $wpdb->insert($shortcode_table,
148 array(
149 'post_id' => $post_id,
150 'post_title' => $post_title,
151 'image_shortcode' => $image_type.'_image__'.$acf_wpname_element,
152 'media_id' => $attach_id,
153 'original_image' => $img_url,
154 'indexs' => $indexs,
155 'image_meta' => $acf_image_meta,
156 'hash_key' => $hash_key,
157 'import_type' => $module,
158 'file_name' => $file_name
159 ),
160 array('%d','%s','%s','%d','%s','%d','%s','%s','%s','%s')
161 );
162 if($insert_status){
163 $this->store_failed_image_ids($attach_id);
164 }
165 }
166 elseif(empty($failed_ids) ){
167 $insert_status = $wpdb->insert($shortcode_table,
168 array(
169 'post_id' => $post_id,
170 'post_title' => $post_title,
171 'image_shortcode' => $image_type.'_image__'.$acf_wpname_element,
172 'media_id' => $attach_id,
173 'original_image' => $img_url,
174 'indexs'=>$indexs,
175 'hash_key' => $hash_key,
176 'import_type' => $module,
177 'file_name' => $file_name,
178 ),
179 array('%d','%s','%s','%d','%s','%d','%s','%s','%s')
180 );
181 if($insert_status){
182 $this->store_failed_image_ids($attach_id);
183 }
184 }
185
186 //return $attach_id;
187 }
188 }
189 else{
190 $attach_id = $this->image_function($img_url , $post_id , $data_array,'','use_existing_image',$header_array,$value_array);
191 }
192
193 }
194 else{
195 $img_url = is_array($img_url) ? implode(',', array_filter($img_url)) : $img_url;
196 $hash_key = is_array($hash_key) ? implode(',', array_filter($hash_key)) : $hash_key;
197 $templatekey = is_array($templatekey) ? implode(',', array_filter($templatekey)) : $templatekey;
198 $module = is_array($module) ? implode(',', array_filter($module)) : $module;
199 $image_type = is_array($image_type) ? implode(',', array_filter($image_type)) : $image_type;
200 $img_url = esc_sql($img_url);
201 $hash_key = esc_sql($hash_key);
202 $templatekey = esc_sql($templatekey);
203 $module = esc_sql($module);
204 $image_type = esc_sql($image_type);
205
206 $attach_id = $this->image_function($img_url, $post_id, $data_array,'','',$header_array,$value_array);
207 // if ($attach_id != null) {
208 // global $wpdb;
209 // $image_table = $wpdb->prefix . "ultimate_csv_importer_media";
210 // $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));
211 // }
212
213 }
214 return $attach_id;
215 }
216
217 public function image_function($f_img , $post_id , $data_array = null,$option_name = null, $use_existing_image = false,$header_array = null , $value_array = null){
218
219 global $wpdb;
220 $f_img = urldecode($f_img);
221 // $image = explode("?", $f_img);
222 // $f_img=$image[0];
223 $media_handle = get_option('smack_image_options');
224 if(!empty($header_array) && !empty($value_array) ){
225 $media_settings = array_combine($header_array,$value_array);
226 }
227 if(isset($media_handle['media_settings']['alttext'])) {
228 $alttext ['_wp_attachment_image_alt'] = isset($media_settings[$media_handle['media_settings']['alttext']]) ? $media_settings[$media_handle['media_settings']['alttext']] :'';
229 }
230
231 if(preg_match_all('/\b(?:(?:https?|http|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i', $f_img , $matchedlist, PREG_PATTERN_ORDER)) {
232 $f_img = $f_img;
233 }
234 else{
235 $media_dir = wp_get_upload_dir();
236 $names = glob($media_dir['path'].'/'.'*.*');
237 foreach($names as $values){
238 if (strpos($values, $f_img) !== false) {
239 $f_img = $media_dir['url'].'/'.$f_img;
240 }
241 }
242 }
243
244 $image_name = pathinfo($f_img);
245 //if(!empty($media_handle['media_settings']['file_name'])){
246 //$file_type = wp_check_filetype( $f_img, null );
247 //$ext = '.'. $file_type['ext'];
248 // if(!empty($media_settings[$media_handle['media_settings']['file_name']])){
249 // $fimg_name = $media_settings[$media_handle['media_settings']['file_name']].$ext;
250 // }
251 //}
252 //else{
253 $fimg_name = $image_name['basename'];
254 //}
255 $file_type = wp_check_filetype( $fimg_name, null );
256 if($use_existing_image){
257 if(empty($file_type['ext'])){
258 $fimg_name = @basename($f_img);
259 $fimg_name = str_replace(' ', '-', trim($fimg_name));
260 $fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name);
261 }
262 $attachment_id = $wpdb->get_var(
263 $wpdb->prepare(
264 "SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type = 'attachment' AND guid LIKE %s",
265 '%' . $wpdb->esc_like( $fimg_name )
266 )
267 );
268 if($attachment_id){
269 if(!empty($data_array['featured_image'])){
270 set_post_thumbnail( $post_id, $attachment_id );
271 return $attachment_id;
272 }else{
273 return $attachment_id;
274 }
275 }
276 }
277
278 $attachment_title = sanitize_file_name( pathinfo( $fimg_name, PATHINFO_FILENAME ) );
279 $file_type = wp_check_filetype( $fimg_name, null );
280 $dir = wp_upload_dir();
281 $dirname = date('Y') . '/' . date('m');
282 $uploads_use_yearmonth = get_option('uploads_use_yearmonth_folders');
283 if($uploads_use_yearmonth == 1){
284 $uploaddir_paths = $dir ['basedir'] . '/' . $dirname ;
285 $uploaddir_url = $dir ['baseurl'] . '/' . $dirname;
286 }
287 else{
288 $uploaddir_paths = $dir ['basedir'];
289 $uploaddir_url = $dir ['baseurl'];
290 }
291 $f_img = str_replace(" ","%20",$f_img);
292 if(empty($file_type['ext'])){
293 $fimg_name = @basename($f_img);
294 $fimg_name = str_replace(' ', '-', trim($fimg_name));
295 $fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name);
296 }
297 if ($uploaddir_paths != "" && $uploaddir_paths) {
298 if (strpos($fimg_name, ' ') !== false) {
299 $fimg_name = str_replace(' ', '-', $fimg_name);
300 $fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name);
301 }
302 $uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
303 }
304
305 if($file_type['ext'] == 'jpeg'){
306 $response = wp_safe_remote_get($f_img, array( 'timeout' => 30));
307 }else{
308 $response = wp_safe_remote_get($f_img, array( 'timeout' => 10));
309 }
310 if(is_wp_error($response)) {
311 return null;
312 }
313 $rawdata = wp_remote_retrieve_body($response);
314
315 $http_code = wp_remote_retrieve_response_code($response);
316 if($http_code == 404){
317 return null;
318 }
319
320 if ( $http_code != 200 && strpos( $rawdata, 'Not Found' ) != 0 ) {
321 return null;
322 }
323 if(is_plugin_active('exmage-wp-image-links/exmage-wp-image-links.php')){
324 $guid =$fimg_name;
325 }
326 if ($rawdata == false) {
327 return null;
328 } else {
329 if(is_plugin_active('exmage-wp-image-links/exmage-wp-image-links.php')){
330 $link = new \EXMAGE_WP_IMAGE_LINKS;
331 $postID = $link->add_image($data_array['featured_image'],$value);
332 wp_update_post(array(
333 'ID' => $postID['id'],
334 'post_title' => $data_array['title'],
335 'post_content' => $data_array['description'],
336 'post_excerpt' => $data_array['caption']
337 ));
338 // if($postID['id'] != null && isset($data_array['alt_text'])){
339 //update_post_meta($postID['id'], '_wp_attachment_image_alt', $data_array['alt_text']);
340 //}
341 }
342 else{
343 if (file_exists($uploaddir_path)) {
344 $i = 1;
345 $exist = true;
346 while($exist){
347 $fimg_name = $attachment_title . "-" . $i . "." . $file_type['ext'];
348 $uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
349
350 if (file_exists($uploaddir_path)) {
351 $i = $i + 1;
352 }
353 else{
354 $exist = false;
355 }
356 }
357 }
358 $fp = fopen($uploaddir_path, 'x');
359 fwrite($fp, $rawdata);
360 fclose($fp);
361 }
362 }
363 if(empty($file_type['type'])){
364 $file_type['type'] = 'image/jpeg';
365 }
366 if(is_plugin_active('exmage-wp-image-links/exmage-wp-image-links.php')){
367 $guids =$data_array['featured_image'];
368 }
369 else{
370 $guids=$uploaddir_url . "/" . $fimg_name;
371 }
372 if(!empty($data_array['title'])){
373 $attachment_title = $data_array['title'];
374 }else{
375 $attachment_title = str_replace('-', ' ', $attachment_title);
376 }
377 $post_info = array(
378 'guid' => $guids,
379 'post_mime_type' => $file_type['type'],
380 'post_title' => $attachment_title,
381 'post_content' => '',
382 'post_status' => 'inherit',
383 );
384 if(!is_plugin_active('exmage-wp-image-links/exmage-wp-image-links.php')){
385 $attach_id = wp_insert_attachment( $post_info,$uploaddir_path, $post_id );
386 $attach_data = wp_generate_attachment_metadata( $attach_id, $uploaddir_path );
387 wp_update_attachment_metadata( $attach_id, $attach_data );
388 }
389
390 if(!empty($data_array['featured_image'])) {
391 set_post_thumbnail( $post_id, $attach_id );
392 }
393 return $attach_id;
394 }
395
396 public function get_filename_path($image_url){
397 $image_name = pathinfo($image_url);
398 $fimg_name = $image_name['basename'];
399 $fimg_name_without_ext = $image_name['filename'];
400 if(empty($fimg_name_without_ext)){
401 $fimg_name_without_ext = $fimg_name;
402 }
403 $file_type = wp_check_filetype( $fimg_name, null );
404 if(empty($file_type['ext'])){
405 $fimg_name = @basename($image_url);
406 $fimg_name = str_replace(' ', '-', trim($fimg_name));
407 $fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name);
408 }
409
410 if (strstr($image_url, 'https://drive.google.com')){
411 preg_match('/[?&]id=([^&]+)/', $image_url, $matches);
412 $fimg_name = isset($matches[1]) ? $matches[1] : basename($image_url);
413 }
414 $dir = wp_upload_dir();
415 $dirname = date('Y') . '/' . date('m');
416 $uploads_use_yearmonth = get_option('uploads_use_yearmonth_folders');
417 if($uploads_use_yearmonth == 1){
418 $uploaddir_paths = $dir ['basedir'] . '/' . $dirname ;
419 $uploaddir_url = $dir ['baseurl'] . '/' . $dirname;
420 }
421 else{
422 $uploaddir_paths = $dir ['basedir'];
423 $uploaddir_url = $dir ['baseurl'];
424 }
425 if ($uploaddir_paths != "" && $uploaddir_paths) {
426 $uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
427 }
428
429 return ['uploaddir_path' => $uploaddir_path,'uploaddir_url' => $uploaddir_url,'fimg_name' => $fimg_name];
430 }
431 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){
432 global $wpdb;
433 $table_name = $wpdb->prefix . 'smackcsv_file_events';
434 $file_name = $wpdb->get_var(
435 $wpdb->prepare(
436 "SELECT file_name FROM $table_name WHERE hash_key = %s",
437 $hash_key
438 )
439 );
440 $post_title = $wpdb->get_var(
441 $wpdb->prepare(
442 "SELECT post_title FROM {$wpdb->prefix}posts WHERE ID = %d AND post_status != 'trash'",
443 absint( $post_id )
444 )
445 );
446 $failed_ids = $wpdb->get_results(
447 $wpdb->prepare(
448 "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 = %d AND original_image = %s",
449 absint( $post_id ),
450 $acf_csv_name
451 )
452 );
453 //$core_instance = CoreFieldsImport::getInstance();
454 //$core_instance = new WooCommerceCoreImport;
455 $shortcode_table = $wpdb->prefix . "ultimate_csv_importer_shortcode_manager";
456
457 $get_path_values = $this->get_filename_path($acf_csv_name);
458 $uploaddir_path = isset($get_path_values['uploaddir_path']) ? $get_path_values['uploaddir_path'] : '';
459 $uploaddir_url = isset($get_path_values['uploaddir_url']) ? $get_path_values['uploaddir_url'] : '';
460 $fimg_name = isset($get_path_values['fimg_name']) ? $get_path_values['fimg_name'] : '';
461 $file_type = 'image/jpeg';
462 if(isset($post_id) && !empty($acf_csv_name) ){
463
464 /************* custom image functionality ***********/
465 if(strpos($plugin, 'jetengine_') !== false ){
466 $image_meta_value = array(
467 'headerarray' => $header_array,
468 'valuearray' => $value_array,
469 'tablename' => $typecct,
470 'returnformat' => $imgformat
471 );
472 $acf_image_meta =json_encode($image_meta_value);
473 }
474 $acf_image_meta = isset($acf_image_meta) ? $acf_image_meta : null;
475 $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);
476 $failed_id = $wpdb->get_results(
477 $wpdb->prepare(
478 "SELECT post_title,post_id,image_shortcode,media_id,original_image FROM {$wpdb->prefix}ultimate_csv_importer_shortcode_manager WHERE post_id = %d AND original_image = %s AND image_shortcode = %s",
479 absint( $post_id ),
480 $acf_csv_name,
481 $plugin . '_image__' . $acf_wpname_element
482 )
483 );
484 if(!empty($attach_id)){
485 $this->store_image_ids($attach_id ); //store the image id
486 return $attach_id;
487 }
488 elseif(empty($attach_id) || !empty($failed_id) || empty($failed_id)){
489 if(empty($failed_id)){
490 $post_info = array(
491 'guid' => $uploaddir_url . "/" . $fimg_name,
492 'post_mime_type' => $file_type,
493 'post_title' => 'image-failed',
494 'post_content' => '',
495 'post_status' => 'inherit',
496 'post_author' => isset($post_values['author']) ? $post_values['author'] : ''
497 );
498 $attach_id = wp_insert_attachment( $post_info,$uploaddir_path, $post_id );
499 }
500 elseif($failed_id[0]->original_image != $acf_csv_name){
501 $post_info = array(
502 'guid' => $uploaddir_url . "/" . $fimg_name,
503 'post_mime_type' => $file_type,
504 'post_title' => 'image-failed',
505 'post_content' => '',
506 'post_status' => 'inherit',
507 'post_author' => $post_values['author']
508 );
509 $attach_id = wp_insert_attachment( $post_info,$uploaddir_path, $post_id );
510 }
511 elseif(($failed_id[0]->image_shortcode == 'Featured_image_') && ($failed_id[0]->original_image == $acf_csv_name)){
512 $post_info = array(
513 'guid' => $uploaddir_url . "/" . $fimg_name,
514 'post_mime_type' => $file_type,
515 'post_title' => 'image-failed',
516 'post_content' => '',
517 'post_status' => 'inherit',
518 'post_author' => $post_values['author']
519 );
520 $attach_id = wp_insert_attachment( $post_info,$uploaddir_path, $post_id );
521 }
522 if(empty($failed_id)){
523 $insert_status = $wpdb->insert($shortcode_table,
524 array(
525 'post_id' => $post_id,
526 'post_title' => $post_title,
527 'image_shortcode' => $plugin.'_image__'.$acf_wpname_element,
528 'media_id' => $attach_id,
529 'original_image' => $acf_csv_name,
530 'indexs' => $indexs,
531 'image_meta' => $acf_image_meta,
532 'hash_key' => $hash_key,
533 'import_type' => $get_import_type,
534 'file_name' => $file_name
535 ),
536 array('%d','%s','%s','%d','%s','%d','%s','%s','%s','%s')
537 );
538 if($insert_status){
539 $this->store_image_ids($attach_id);//store number of image ids
540 $this->store_failed_image_ids($attach_id);//store failed image ids
541 }
542 }
543 else{
544 $media_id = $failed_id[0]->media_id;
545 $attachment_id = $wpdb->get_results(
546 $wpdb->prepare(
547 "SELECT ID FROM {$wpdb->prefix}posts WHERE ID = %d AND post_title ='image-failed' AND post_type = 'attachment' AND guid LIKE %s",
548 absint( $media_id ),
549 '%' . $wpdb->esc_like( $fimg_name ) . '%'
550 ),
551 ARRAY_A
552 );
553 $attach_id= $attachment_id[0]['ID'];
554 }
555 return isset($attach_id) ? $attach_id : '';
556 }else{
557 $attachment_id = $wpdb->get_results(
558 $wpdb->prepare(
559 "SELECT ID FROM {$wpdb->prefix}posts WHERE post_title ='image-failed' AND post_type = 'attachment' AND guid LIKE %s",
560 '%' . $wpdb->esc_like( $fimg_name ) . '%'
561 ),
562 ARRAY_A
563 );
564 }
565 return $attach_id;
566 }
567 }
568 public function store_image_ids($attach_id){
569 //get number of images count
570 $stored_ids = get_option('total_attachment_ids', '');
571 $att_id = $attach_id;
572 if ($stored_ids === '') {
573 add_option('total_attachment_ids', serialize(array($att_id)));
574 $stored_ids = unserialize(get_option('total_attachment_ids', ''));
575 } else {
576 $get_stored_ids = unserialize(get_option('total_attachment_ids', ''));
577 if (is_array($get_stored_ids) && !empty($att_id)) {
578 $att_id = is_array($att_id) ? $att_id : array($att_id);
579 $stored_ids = array_merge($get_stored_ids,$att_id);
580 } else {
581 $stored_ids = $att_id;
582 }
583 update_option('total_attachment_ids', serialize($stored_ids));
584 $stored_ids = unserialize(get_option('total_attachment_ids', ''));
585 }
586 }
587 public function store_failed_image_ids($attach_id){
588 //get number of failed images count
589 $stored_ids = get_option('failed_attachment_ids', '');
590 $att_id = $attach_id;
591 if ($stored_ids === '') {
592 add_option('failed_attachment_ids', serialize(array($att_id)));
593 $stored_ids = unserialize(get_option('failed_attachment_ids', ''));
594 } else {
595 $get_stored_ids = unserialize(get_option('failed_attachment_ids', ''));
596 if (is_array($get_stored_ids) && !empty($att_id)) {
597 $att_id = is_array($att_id) ? $att_id : array($att_id);
598 $stored_ids = array_merge($get_stored_ids,$att_id);
599 } else {
600 $stored_ids = $att_id;
601 }
602 update_option('failed_attachment_ids', serialize($stored_ids));
603 $stored_ids = unserialize(get_option('failed_attachment_ids', ''));
604 }
605 }
606
607 }