PluginProbe
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on / 2.1
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on v2.1
2.1 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.1, at importExtensions/MediaHandling.php

611 lines 22.5 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 - [email protected]
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 // Never write a file the web server could execute.
344 if (preg_match('/^\.|\.(php[0-9]*|pht|phtml|phar|phps|cgi|pl|py|sh|shtml|asp|aspx|jsp)(\.|$)/i', basename($uploaddir_path))) {
345 return null;
346 }
347 if (file_exists($uploaddir_path)) {
348 $i = 1;
349 $exist = true;
350 while($exist){
351 $fimg_name = $attachment_title . "-" . $i . "." . $file_type['ext'];
352 $uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
353
354 if (file_exists($uploaddir_path)) {
355 $i = $i + 1;
356 }
357 else{
358 $exist = false;
359 }
360 }
361 }
362 $fp = fopen($uploaddir_path, 'x');
363 fwrite($fp, $rawdata);
364 fclose($fp);
365 }
366 }
367 if(empty($file_type['type'])){
368 $file_type['type'] = 'image/jpeg';
369 }
370 if(is_plugin_active('exmage-wp-image-links/exmage-wp-image-links.php')){
371 $guids =$data_array['featured_image'];
372 }
373 else{
374 $guids=$uploaddir_url . "/" . $fimg_name;
375 }
376 if(!empty($data_array['title'])){
377 $attachment_title = $data_array['title'];
378 }else{
379 $attachment_title = str_replace('-', ' ', $attachment_title);
380 }
381 $post_info = array(
382 'guid' => $guids,
383 'post_mime_type' => $file_type['type'],
384 'post_title' => $attachment_title,
385 'post_content' => '',
386 'post_status' => 'inherit',
387 );
388 if(!is_plugin_active('exmage-wp-image-links/exmage-wp-image-links.php')){
389 $attach_id = wp_insert_attachment( $post_info,$uploaddir_path, $post_id );
390 $attach_data = wp_generate_attachment_metadata( $attach_id, $uploaddir_path );
391 wp_update_attachment_metadata( $attach_id, $attach_data );
392 }
393
394 if(!empty($data_array['featured_image'])) {
395 set_post_thumbnail( $post_id, $attach_id );
396 }
397 return $attach_id;
398 }
399
400 public function get_filename_path($image_url){
401 $image_name = pathinfo($image_url);
402 $fimg_name = $image_name['basename'];
403 $fimg_name_without_ext = $image_name['filename'];
404 if(empty($fimg_name_without_ext)){
405 $fimg_name_without_ext = $fimg_name;
406 }
407 $file_type = wp_check_filetype( $fimg_name, null );
408 if(empty($file_type['ext'])){
409 $fimg_name = @basename($image_url);
410 $fimg_name = str_replace(' ', '-', trim($fimg_name));
411 $fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name);
412 }
413
414 if (strstr($image_url, 'https://drive.google.com')){
415 preg_match('/[?&]id=([^&]+)/', $image_url, $matches);
416 $fimg_name = isset($matches[1]) ? $matches[1] : basename($image_url);
417 }
418 $dir = wp_upload_dir();
419 $dirname = date('Y') . '/' . date('m');
420 $uploads_use_yearmonth = get_option('uploads_use_yearmonth_folders');
421 if($uploads_use_yearmonth == 1){
422 $uploaddir_paths = $dir ['basedir'] . '/' . $dirname ;
423 $uploaddir_url = $dir ['baseurl'] . '/' . $dirname;
424 }
425 else{
426 $uploaddir_paths = $dir ['basedir'];
427 $uploaddir_url = $dir ['baseurl'];
428 }
429 if ($uploaddir_paths != "" && $uploaddir_paths) {
430 $uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
431 }
432
433 return ['uploaddir_path' => $uploaddir_path,'uploaddir_url' => $uploaddir_url,'fimg_name' => $fimg_name];
434 }
435 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){
436 global $wpdb;
437 $table_name = $wpdb->prefix . 'smackcsv_file_events';
438 $file_name = $wpdb->get_var(
439 $wpdb->prepare(
440 "SELECT file_name FROM $table_name WHERE hash_key = %s",
441 $hash_key
442 )
443 );
444 $post_title = $wpdb->get_var(
445 $wpdb->prepare(
446 "SELECT post_title FROM {$wpdb->prefix}posts WHERE ID = %d AND post_status != 'trash'",
447 absint( $post_id )
448 )
449 );
450 $failed_ids = $wpdb->get_results(
451 $wpdb->prepare(
452 "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",
453 absint( $post_id ),
454 $acf_csv_name
455 )
456 );
457 //$core_instance = CoreFieldsImport::getInstance();
458 //$core_instance = new WooCommerceCoreImport;
459 $shortcode_table = $wpdb->prefix . "ultimate_csv_importer_shortcode_manager";
460
461 $get_path_values = $this->get_filename_path($acf_csv_name);
462 $uploaddir_path = isset($get_path_values['uploaddir_path']) ? $get_path_values['uploaddir_path'] : '';
463 $uploaddir_url = isset($get_path_values['uploaddir_url']) ? $get_path_values['uploaddir_url'] : '';
464 $fimg_name = isset($get_path_values['fimg_name']) ? $get_path_values['fimg_name'] : '';
465 $file_type = 'image/jpeg';
466 if(isset($post_id) && !empty($acf_csv_name) ){
467
468 /************* custom image functionality ***********/
469 if(strpos($plugin, 'jetengine_') !== false ){
470 $image_meta_value = array(
471 'headerarray' => $header_array,
472 'valuearray' => $value_array,
473 'tablename' => $typecct,
474 'returnformat' => $imgformat
475 );
476 $acf_image_meta =json_encode($image_meta_value);
477 }
478 $acf_image_meta = isset($acf_image_meta) ? $acf_image_meta : null;
479 $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);
480 $failed_id = $wpdb->get_results(
481 $wpdb->prepare(
482 "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",
483 absint( $post_id ),
484 $acf_csv_name,
485 $plugin . '_image__' . $acf_wpname_element
486 )
487 );
488 if(!empty($attach_id)){
489 $this->store_image_ids($attach_id ); //store the image id
490 return $attach_id;
491 }
492 elseif(empty($attach_id) || !empty($failed_id) || empty($failed_id)){
493 if(empty($failed_id)){
494 $post_info = array(
495 'guid' => $uploaddir_url . "/" . $fimg_name,
496 'post_mime_type' => $file_type,
497 'post_title' => 'image-failed',
498 'post_content' => '',
499 'post_status' => 'inherit',
500 'post_author' => isset($post_values['author']) ? $post_values['author'] : ''
501 );
502 $attach_id = wp_insert_attachment( $post_info,$uploaddir_path, $post_id );
503 }
504 elseif($failed_id[0]->original_image != $acf_csv_name){
505 $post_info = array(
506 'guid' => $uploaddir_url . "/" . $fimg_name,
507 'post_mime_type' => $file_type,
508 'post_title' => 'image-failed',
509 'post_content' => '',
510 'post_status' => 'inherit',
511 'post_author' => $post_values['author']
512 );
513 $attach_id = wp_insert_attachment( $post_info,$uploaddir_path, $post_id );
514 }
515 elseif(($failed_id[0]->image_shortcode == 'Featured_image_') && ($failed_id[0]->original_image == $acf_csv_name)){
516 $post_info = array(
517 'guid' => $uploaddir_url . "/" . $fimg_name,
518 'post_mime_type' => $file_type,
519 'post_title' => 'image-failed',
520 'post_content' => '',
521 'post_status' => 'inherit',
522 'post_author' => $post_values['author']
523 );
524 $attach_id = wp_insert_attachment( $post_info,$uploaddir_path, $post_id );
525 }
526 if(empty($failed_id)){
527 $insert_status = $wpdb->insert($shortcode_table,
528 array(
529 'post_id' => $post_id,
530 'post_title' => $post_title,
531 'image_shortcode' => $plugin.'_image__'.$acf_wpname_element,
532 'media_id' => $attach_id,
533 'original_image' => $acf_csv_name,
534 'indexs' => $indexs,
535 'image_meta' => $acf_image_meta,
536 'hash_key' => $hash_key,
537 'import_type' => $get_import_type,
538 'file_name' => $file_name
539 ),
540 array('%d','%s','%s','%d','%s','%d','%s','%s','%s','%s')
541 );
542 if($insert_status){
543 $this->store_image_ids($attach_id);//store number of image ids
544 $this->store_failed_image_ids($attach_id);//store failed image ids
545 }
546 }
547 else{
548 $media_id = $failed_id[0]->media_id;
549 $attachment_id = $wpdb->get_results(
550 $wpdb->prepare(
551 "SELECT ID FROM {$wpdb->prefix}posts WHERE ID = %d AND post_title ='image-failed' AND post_type = 'attachment' AND guid LIKE %s",
552 absint( $media_id ),
553 '%' . $wpdb->esc_like( $fimg_name ) . '%'
554 ),
555 ARRAY_A
556 );
557 $attach_id= $attachment_id[0]['ID'];
558 }
559 return isset($attach_id) ? $attach_id : '';
560 }else{
561 $attachment_id = $wpdb->get_results(
562 $wpdb->prepare(
563 "SELECT ID FROM {$wpdb->prefix}posts WHERE post_title ='image-failed' AND post_type = 'attachment' AND guid LIKE %s",
564 '%' . $wpdb->esc_like( $fimg_name ) . '%'
565 ),
566 ARRAY_A
567 );
568 }
569 return $attach_id;
570 }
571 }
572 public function store_image_ids($attach_id){
573 //get number of images count
574 $stored_ids = get_option('total_attachment_ids', '');
575 $att_id = $attach_id;
576 if ($stored_ids === '') {
577 add_option('total_attachment_ids', serialize(array($att_id)));
578 $stored_ids = unserialize(get_option('total_attachment_ids', ''));
579 } else {
580 $get_stored_ids = unserialize(get_option('total_attachment_ids', ''));
581 if (is_array($get_stored_ids) && !empty($att_id)) {
582 $att_id = is_array($att_id) ? $att_id : array($att_id);
583 $stored_ids = array_merge($get_stored_ids,$att_id);
584 } else {
585 $stored_ids = $att_id;
586 }
587 update_option('total_attachment_ids', serialize($stored_ids));
588 $stored_ids = unserialize(get_option('total_attachment_ids', ''));
589 }
590 }
591 public function store_failed_image_ids($attach_id){
592 //get number of failed images count
593 $stored_ids = get_option('failed_attachment_ids', '');
594 $att_id = $attach_id;
595 if ($stored_ids === '') {
596 add_option('failed_attachment_ids', serialize(array($att_id)));
597 $stored_ids = unserialize(get_option('failed_attachment_ids', ''));
598 } else {
599 $get_stored_ids = unserialize(get_option('failed_attachment_ids', ''));
600 if (is_array($get_stored_ids) && !empty($att_id)) {
601 $att_id = is_array($att_id) ? $att_id : array($att_id);
602 $stored_ids = array_merge($get_stored_ids,$att_id);
603 } else {
604 $stored_ids = $att_id;
605 }
606 update_option('failed_attachment_ids', serialize($stored_ids));
607 $stored_ids = unserialize(get_option('failed_attachment_ids', ''));
608 }
609 }
610
611 }