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
← All changes | importExtensions/MediaHandling.php +463 -578 1.2.2 → 2.1 View file →
@@ -1,52 +1,36 @@
1 1 <?php
2 -/******************************************************************************************
3 - * Copyright (C) Smackcoders. - All Rights Reserved under Smackcoders Proprietary License
4 - * Unauthorized copying of this file, via any medium is strictly prohibited
5 - * Proprietary and confidential
6 - * You can contact Smackcoders at email address [email protected].
7 - *******************************************************************************************/
2 +/**
3 + * Import Users plugin file.
4 + *
5 + * Copyright (C) 2010-2020, Smackcoders Inc - [email protected]
6 + */
8 7
9 8 namespace Smackcoders\SMUSERS;
10 9
11 10 if ( ! defined( 'ABSPATH' ) )
12 -exit; // Exit if accessed directly
11 + exit; // Exit if accessed directly
13 12
14 13 class MediaHandling{
15 - private static $instance=null;
16 - public $header_array;
14 + private static $instance=null,$smack_instance;
15 + public $header_array,$core_instance;
17 16 public $value_array;
18 17
19 18 public function __construct(){
20 19
21 - require_once(ABSPATH.'wp-load.php');
22 - require_once(ABSPATH . 'wp-admin/includes/image.php');
23 - require_once(ABSPATH . 'wp-admin/includes/file.php');
20 + include_once(ABSPATH . 'wp-admin/includes/image.php');
24 21 add_action('wp_ajax_image_options', array($this , 'imageOptions'));
25 22 add_action('wp_ajax_delete_image' , array($this , 'deleteImage'));
26 - add_action('wp_ajax_media_report' , array($this , 'mediaReport'));
27 23 }
28 24
29 - public static function imageOptions(){
30 - $media_settings['use_ExistingImage'] = $_POST['use_ExistingImage'];
31 - $media_settings['overwriteImage'] = $_POST['overwriteImage'];
32 - $media_settings['title'] = $_POST['title'];
33 - $media_settings['caption'] = $_POST['caption'];
34 - $media_settings['alttext'] = $_POST['alttext'];
35 - $media_settings['description'] = $_POST['description'];
36 - $media_settings['file_name'] = $_POST['file_name'];
37 - $media_settings['thumbnail'] = $_POST['thumbnail'];
38 - $media_settings['media_handle_option'] = $_POST['media_handle_option'];
39 - $media_settings['medium'] = $_POST['medium'];
40 - $media_settings['medium_large'] = $_POST['medium_large'];
41 - $media_settings['large'] = $_POST['large'];
42 - $media_settings['custom'] = $_POST['custom'];
43 - $media_settings['custom_slug'] = $_POST['custom_slug'];
44 - $media_settings['custom_slug'] = $_POST['custom_width'];
45 - $media_settings['custom_height'] = $_POST['custom_height'];
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']);
46 30 $image_info = array(
47 - 'media_settings' => $media_settings
48 - );
31 + 'media_settings' => $media_settings
32 + );
49 33 update_option( 'smack_image_options', $image_info );
50 34 $result['success'] = 'true';
51 35 echo wp_json_encode($result);
52 36 wp_die();
@@ -53,9 +37,8 @@
53 37 }
54 38 public static function getInstance() {
55 39 if (MediaHandling::$instance == null) {
56 40 MediaHandling::$instance = new MediaHandling;
57 - //MediaHandling::$instance->featured_image();
58 41 return MediaHandling::$instance;
59 42 }
60 43 return MediaHandling::$instance;
61 44 }
@@ -60,9 +43,10 @@
60 43 return MediaHandling::$instance;
61 44 }
62 45
63 46 public function deleteImage(){
64 - $image = $_POST['image'];
47 + check_ajax_referer('smack-ultimate-csv-importer', 'securekey');
48 + $image = sanitize_text_field($_POST['image']);
65 49 $media_dir = wp_get_upload_dir();
66 50 $names = glob($media_dir['path'].'/'.'*.*');
67 51 foreach($names as $values){
68 52 if (strpos($values, $image) !== false) {
@@ -72,105 +56,180 @@
72 56 $result['success'] = 'true';
73 57 echo wp_json_encode($result);
74 58 wp_die();
75 59 }
76 - public function media_handling($img_url , $post_id , $data_array = null ,$module = null, $image_type = null ,$hash_key = null, $header_array = null , $value_array = null){
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){
77 61 $encodedurl = urlencode($img_url);
78 62 $img_url = urldecode($encodedurl);
63 + $url = parse_url($img_url);
64 + if($hash_key == null)
65 + $hash_key = "";
79 66 $media_handle = get_option('smack_image_options');
80 - $image_name = basename($img_url);
81 - $image_title = sanitize_file_name( pathinfo( $image_name, PATHINFO_FILENAME ) );
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 + }
82 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 + );
83 90
84 - if($media_handle['media_settings']['media_handle_option'] == 'true'){
85 - if($media_handle['media_settings']['use_ExistingImage'] == 'true'){
86 - $guid_url = $img_url;
87 - $attachment_id = $wpdb->get_results("select ID from {$wpdb->prefix}posts where guid = '$guid_url'" ,ARRAY_A);
88 - if(!empty($attachment_id)){
89 - foreach($attachment_id as $value){
90 - $attach_id = $value['ID'];
91 - if(!wp_get_attachment_url($attach_id)){
92 - $attach_id = $this->image_function($img_url , $post_id , $data_array, '', 'use_existing_image',$header_array , $value_array );
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 + }
93 165 }
94 - }
95 - }
96 - else{
97 - $attach_id = $this->image_function($img_url , $post_id , $data_array , '', 'use_existing_image',$header_array , $value_array);
98 - }
99 - }
100 - elseif($media_handle['media_settings']['overwriteImage'] == 'true'){
101 - $get_id = $wpdb->get_results("SELECT ID FROM {$wpdb->prefix}posts WHERE post_title = '$image_title' AND post_type = 'attachment' limit 1");
102 - if(!empty($get_id)){
103 - foreach($get_id as $value){
104 - $attach_id = $value->ID;
105 - $this->overwrite($attach_id , $img_url);
106 - if(!empty($data_array['featured_image'])) {
107 - set_post_thumbnail( $post_id, $attach_id );
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 + }
108 184 }
109 - }
110 - }
111 - else{
112 - $attach_id = $this->image_function($img_url , $post_id , $data_array,'','',$header_array , $value_array);
113 - }
185 +
186 + //return $attach_id;
187 + }
114 188 }
115 189 else{
116 - $attach_id = $this->image_function($img_url , $post_id , $data_array,'','',$header_array , $value_array);
190 + $attach_id = $this->image_function($img_url , $post_id , $data_array,'','use_existing_image',$header_array,$value_array);
117 191 }
192 +
118 193 }
119 194 else{
120 - $guid_url = $img_url;
121 - $attachment_id = $wpdb->get_results("select ID from {$wpdb->prefix}posts where guid = '$guid_url'" ,ARRAY_A);
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 + // }
122 212
123 - if(!empty($attachment_id)){
124 -
125 - foreach($attachment_id as $value){
126 - $attach_id = $value['ID'];
127 - if($_wp_attachment_metadata = get_post_meta($attach_id, '_wp_attachment_metadata', true)){
128 - // When an attachment is available on Media and not has attachment link
129 - if(!is_array($_wp_attachment_metadata)){
130 - $attach_id = $this->image_function($img_url , $post_id , $data_array,'','',$header_array , $value_array);
131 - }
132 - }
133 - //set_post_thumbnail( $post_id, $attach_id );
134 - }
135 - }
136 213 }
137 214 return $attach_id;
138 215 }
139 216
140 - public function mediaReport(){
141 - global $wpdb;
142 - $list_of_images = $wpdb->get_results("select * from {$wpdb->prefix}ultimate_csv_importer_media GROUP BY `hash_key`,`image_type` ",ARRAY_A);
143 - foreach( $list_of_images as $list_key => $list_val )
144 - {
145 - if(!empty($list_val['hash_key'])){
146 - $file_name = $wpdb->get_results("select file_name from {$wpdb->prefix}smackcsv_file_events where hash_key = '{$list_val['hash_key']}'",ARRAY_A);
147 - }
148 - $filename[$list_key]= $file_name[0]['file_name'];
149 - $module[$list_key] = $list_val['module'];
150 - $image_type[$list_key] = $list_val['image_type'];
151 - $image_status[$list_key] = $list_val['status'];
152 - $number_of_images = $wpdb->get_results("select image_url from {$wpdb->prefix}ultimate_csv_importer_media where hash_key = '{$list_val['hash_key']}' and image_type = '{$image_type[$list_key]}' ",ARRAY_A);
153 - $count[$list_key] = count($number_of_images);
154 - }
155 - $response['file_name'] = $filename ;
156 - $response['module'] = $module ;
157 - $response['count'] = $count;
158 - $response['image_type'] = $image_type;
159 - $response['status'] = $image_status;
160 - echo wp_json_encode($response);
161 - wp_die();
162 - }
163 -
164 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){
165 -
218 +
166 219 global $wpdb;
220 + $f_img = urldecode($f_img);
221 + // $image = explode("?", $f_img);
222 + // $f_img=$image[0];
167 223 $media_handle = get_option('smack_image_options');
168 - $media_settings = array_combine($header_array,$value_array);
224 + if(!empty($header_array) && !empty($value_array) ){
225 + $media_settings = array_combine($header_array,$value_array);
226 + }
169 227 if(isset($media_handle['media_settings']['alttext'])) {
170 - $alttext ['_wp_attachment_image_alt'] = $media_settings[$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']] :'';
171 229 }
172 - if(preg_match_all('/\b(?:(?:https?|?:http?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i', $f_img , $matchedlist, PREG_PATTERN_ORDER)) {
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)) {
173 232 $f_img = $f_img;
174 233 }
175 234 else{
176 235 $media_dir = wp_get_upload_dir();
@@ -180,18 +239,20 @@
180 239 $f_img = $media_dir['url'].'/'.$f_img;
181 240 }
182 241 }
183 242 }
243 +
184 244 $image_name = pathinfo($f_img);
185 - if(!empty($media_handle['media_settings']['file_name'])){
186 - $file_type = wp_check_filetype( $f_img, null );
187 - $ext = '.'. $file_type['ext'];
188 - $fimg_name = $media_settings[$media_handle['media_settings']['file_name']].$ext;
189 - }
190 - else{
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{
191 253 $fimg_name = $image_name['basename'];
192 - }
193 -
254 + //}
194 255 $file_type = wp_check_filetype( $fimg_name, null );
195 256 if($use_existing_image){
196 257 if(empty($file_type['ext'])){
197 258 $fimg_name = @basename($f_img);
@@ -197,10 +258,14 @@
197 258 $fimg_name = @basename($f_img);
198 259 $fimg_name = str_replace(' ', '-', trim($fimg_name));
199 260 $fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name);
200 261 }
201 - $attachment_id = $wpdb->get_var("SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type = 'attachment' AND guid LIKE '%$fimg_name'");
202 -
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 + );
203 268 if($attachment_id){
204 269 if(!empty($data_array['featured_image'])){
205 270 set_post_thumbnail( $post_id, $attachment_id );
206 271 return $attachment_id;
@@ -210,13 +275,20 @@
210 275 }
211 276 }
212 277
213 278 $attachment_title = sanitize_file_name( pathinfo( $fimg_name, PATHINFO_FILENAME ) );
214 - $file_type = wp_check_filetype( $fimg_name, null );
279 + $file_type = wp_check_filetype( $fimg_name, null );
215 280 $dir = wp_upload_dir();
216 281 $dirname = date('Y') . '/' . date('m');
217 - $uploaddir_paths = $dir ['basedir'] . '/' . $dirname ;
218 - $uploaddir_url = $dir ['baseurl'] . '/' . $dirname;
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 + }
219 291 $f_img = str_replace(" ","%20",$f_img);
220 292 if(empty($file_type['ext'])){
221 293 $fimg_name = @basename($f_img);
222 294 $fimg_name = str_replace(' ', '-', trim($fimg_name));
@@ -222,13 +294,25 @@
222 294 $fimg_name = str_replace(' ', '-', trim($fimg_name));
223 295 $fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name);
224 296 }
225 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 + }
226 302 $uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
227 303 }
228 - //Removed curl and added wordpress http api
229 - $response = wp_remote_get($f_img, array( 'timeout' => 10));
230 - $rawdata = wp_remote_retrieve_body($response);
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 +
231 315 $http_code = wp_remote_retrieve_response_code($response);
232 316 if($http_code == 404){
233 317 return null;
234 318 }
@@ -233,494 +317,295 @@
233 317 return null;
234 318 }
235 319
236 320 if ( $http_code != 200 && strpos( $rawdata, 'Not Found' ) != 0 ) {
237 - $rawdata = null;
321 + return null;
238 322 }
323 + if(is_plugin_active('exmage-wp-image-links/exmage-wp-image-links.php')){
324 + $guid =$fimg_name;
325 + }
239 326 if ($rawdata == false) {
240 327 return null;
241 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;
242 353
243 - if (file_exists($uploaddir_path)) {
244 - $i = 1;
245 - $exist = true;
246 - while($exist){
247 - $fimg_name = $attachment_title . "-" . $i . "." . $file_type['ext'];
248 - $uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
249 -
250 - if (file_exists($uploaddir_path)) {
251 - $i = $i + 1;
354 + if (file_exists($uploaddir_path)) {
355 + $i = $i + 1;
356 + }
357 + else{
358 + $exist = false;
359 + }
252 360 }
253 - else{
254 - $exist = false;
255 - }
256 361 }
362 + $fp = fopen($uploaddir_path, 'x');
363 + fwrite($fp, $rawdata);
364 + fclose($fp);
257 365 }
258 -
259 - $fp = fopen($uploaddir_path, 'x');
260 - fwrite($fp, $rawdata);
261 - fclose($fp);
262 366 }
263 367 if(empty($file_type['type'])){
264 368 $file_type['type'] = 'image/jpeg';
265 369 }
266 - $post_info = array(
267 - 'guid' => $uploaddir_url . "/" . $fimg_name,
268 - 'post_mime_type' => $file_type['type'],
269 - 'post_title' => $attachment_title,
270 - 'post_content' => '',
271 - 'post_status' => 'inherit',
272 - );
273 - //unset( $post_info['ID'] );
274 - $attach_id = wp_insert_attachment( $post_info,$uploaddir_path, $post_id );
275 - $attach_data = wp_generate_attachment_metadata( $attach_id, $uploaddir_path );
276 - wp_update_attachment_metadata( $attach_id, $attach_data );
277 - if($media_handle['media_settings']['thumbnail'] = 'true') {
278 - $thumbnail_width = get_option('thumbnail_size_w');
279 - $thumbnail_height = get_option('thumbnail_size_h');
280 - $metadata = wp_get_attachment_metadata($attach_id);
281 - $metadata['width'] = $thumbnail_width;
282 - $metadata['height'] = $thumbnail_height;
283 - wp_update_attachment_metadata($attach_id,$metadata);
370 + if(is_plugin_active('exmage-wp-image-links/exmage-wp-image-links.php')){
371 + $guids =$data_array['featured_image'];
284 372 }
285 - elseif($media_handle['media_settings']['medium_'] = 'true'){
286 - $medium_width = get_option('medium_size_w');
287 - $medium_height = get_option('medium_size_h');
288 - $metadata = wp_get_attachment_metadata($attach_id);
289 - $metadata['width'] = $medium_width;
290 - $metadata['height'] = $medium_height;
291 - wp_update_attachment_metadata($attach_id,$metadata);
373 + else{
374 + $guids=$uploaddir_url . "/" . $fimg_name;
292 375 }
293 - elseif($media_handle['media_settings']['medium_large'] = 'true') {
294 - $medium_large_width = get_option('medium_large_size_w');
295 - $medium_large_height = get_option('medium_large_size_h');
296 - //add_image_size( 'medium_large', $medium_large_width, $medium_large_height, true );
297 - $metadata = wp_get_attachment_metadata($attach_id);
298 - $metadata['width'] = $medium_large_width;
299 - $metadata['height'] = $medium_large_height;
300 - wp_update_attachment_metadata($attach_id,$metadata);
376 + if(!empty($data_array['title'])){
377 + $attachment_title = $data_array['title'];
378 + }else{
379 + $attachment_title = str_replace('-', ' ', $attachment_title);
301 380 }
302 - elseif($media_handle['media_settings']['large'] = 'true'){
303 - $large_width = get_option('large_size_w');
304 - $large_height = get_option('large_size_h');
305 - $metadata = wp_get_attachment_metadata($attach_id);
306 - $metadata['width'] = $large_width;
307 - $metadata['height'] = $large_height;
308 - wp_update_attachment_metadata($attach_id,$metadata);
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 );
309 392 }
310 - if(isset($media_handle['media_settings']['description'])){
311 - $media_handle['media_settings']['description'] = $media_settings[$media_handle['media_settings']['description']];
312 - }
313 - if(isset($media_handle['media_settings']['caption'])){
314 - $media_handle['media_settings']['caption'] = $media_settings[$media_handle['media_settings']['caption']];
315 - }
316 - if(isset($media_handle['media_settings']['title'])){
317 - $media_handle['media_settings']['title'] = $media_settings[$media_handle['media_settings']['title']];
318 - }
319 - if(isset($media_handle['media_settings']['caption']) || isset($media_handle['media_settings']['description'])){
320 - wp_update_post(array(
321 - 'ID' =>$attach_id,
322 - 'post_content' =>$media_handle['media_settings']['description'],
323 - 'post_excerpt' =>$media_handle['media_settings']['caption']
324 - ));
325 - }
326 - if(isset($media_handle['media_settings']['title'])){
327 - wp_update_post(array(
328 - 'ID' =>$attach_id,
329 - 'post_title' =>$media_handle['media_settings']['title']
330 - ));
331 - }
332 - if($attach_id != null && isset($alttext['_wp_attachment_image_alt'])){
333 - update_post_meta($attach_id, '_wp_attachment_image_alt', $alttext['_wp_attachment_image_alt']);
334 - }
393 +
335 394 if(!empty($data_array['featured_image'])) {
336 395 set_post_thumbnail( $post_id, $attach_id );
337 396 }
338 - delete_option( $option_name );
339 397 return $attach_id;
340 398 }
341 399
342 -
343 - public function image_import($data_array){
344 - $encodedurl = urlencode($data_array['featured_image']);
345 - $data_array['featured_image'] = urldecode($encodedurl);
346 - if(isset($data_array['alt_text'])) {
347 - $alttext ['_wp_attachment_image_alt'] = $data_array['alt_text'];
348 - }
349 - if(preg_match_all('/\b(?:(?:https?|http|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i', $data_array['featured_image'] , $matchedlist, PREG_PATTERN_ORDER))
350 - {
351 - $f_img = $data_array['featured_image'];
352 - }
353 - else{
354 - $media_dir = wp_get_upload_dir();
355 - $names = glob($media_dir['path'].'/'.'*.*');
356 - $f_img = $data_array['featured_image'];
357 - foreach($names as $values){
358 - if (strpos($values, $f_img) !== false) {
359 - $f_img = $media_dir['url'].'/'.$f_img;
360 - }
361 - }
362 - }
363 - $image_name = pathinfo($f_img);
364 - $attachment_title = sanitize_file_name( pathinfo( $f_img, PATHINFO_FILENAME ) );
365 - $file_type = wp_check_filetype( $f_img, null );
366 - $dir = wp_upload_dir();
367 - $dirname = date('Y') . '/' . date('m');
368 - $uploaddir_paths = $dir ['basedir'] . '/' . $dirname ;
369 - $uploaddir_url = $dir ['baseurl'] . '/' . $dirname;
370 - if(empty($file_type['ext']) || empty($data_array['file_name'])){
371 - $fimg_name = @basename($f_img);
372 - $fimg_name = str_replace(' ', '-', trim($fimg_name));
373 - $fimg_name = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $fimg_name);
374 - }
375 - if(!empty($data_array['file_name'])){
376 - $file_type = wp_check_filetype( $f_img, null );
377 - $ext = '.'. $file_type['ext'];
378 - $fimg_name = $data_array['file_name'].$ext;
379 - }
380 - if ($uploaddir_paths != "" && $uploaddir_paths) {
381 - $uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
382 - }
383 - if (strstr($f_img, 'https://drive.google.com')){
384 - $page_content = file_get_contents($f_img);
385 - $dom_obj = new \DOMDocument();
386 - $dom_obj->loadHTML($page_content);
387 - $meta_val = null;
388 - foreach($dom_obj->getElementsByTagName('meta') as $meta) {
389 - if($meta->getAttribute('property')=='og:image'){
390 - $meta_val = $meta->getAttribute('content');
391 - }
392 - }
393 - $ch = curl_init($meta_val);
394 - curl_setopt($ch, CURLOPT_HEADER, 0);
395 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
396 - curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
397 - $rawdata=curl_exec ($ch);
398 - }
399 - //Removed curl and added wordpress http api
400 - else{
401 - if($file_type['ext'] == 'jpeg'){
402 - $response = wp_remote_get($f_img, array( 'timeout' => 30));
403 - }else{
404 - $response = wp_remote_get($f_img, array( 'timeout' => 10));
405 - }
406 - $rawdata = wp_remote_retrieve_body($response);
407 - }
408 - $http_code = wp_remote_retrieve_response_code($response);
409 -
410 - if($http_code == 404){
411 - return null;
412 - }
413 -
414 - // When
415 - if ( $http_code != 200 && strpos( $rawdata, 'Not Found' ) != 0 ) {
416 - return null;
417 - }
418 -
419 - if ($rawdata == false) {
420 -
421 - return null;
422 - } else {
423 -
424 - if (file_exists($uploaddir_path)) {
425 - $i = 1;
426 - $exist = true;
427 - while($exist){
428 - $fimg_name = $attachment_title . "-" . $i . "." . $file_type['ext'];
429 - $uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
430 -
431 - if (file_exists($uploaddir_path)) {
432 - $i = $i + 1;
433 - }
434 - else{
435 - $exist = false;
436 - }
437 - }
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;
438 406 }
439 - $fp = fopen($uploaddir_path, 'x');
440 - fwrite($fp, $rawdata);
441 - fclose($fp);
442 - }
443 - if(empty($file_type['type'])){
444 - $file_type['type'] = 'image/jpeg';
445 - }
446 - //Removed curl and added wordpress http api
447 - $post_info = array(
448 - 'guid' => $uploaddir_url . "/" . $fimg_name,
449 - 'post_mime_type' => $file_type['type'],
450 - 'post_title' => $attachment_title,
451 - 'post_content' => '',
452 - 'post_status' => 'inherit',
453 - );
454 - unset( $post_info['ID'] );
455 - $attach_id = wp_insert_attachment( $post_info,$uploaddir_path );
456 - $attach_data = wp_generate_attachment_metadata( $attach_id, $uploaddir_path );
457 - wp_update_attachment_metadata( $attach_id, $attach_data );
458 - wp_update_post(array(
459 - 'ID' => $attach_id,
460 - 'post_title' => $data_array['title'],
461 - 'post_content' => $data_array['description'],
462 - 'post_excerpt' => $data_array['caption']
463 - ));
464 - if($attach_id != null && isset($alttext['_wp_attachment_image_alt'])){
465 - update_post_meta($attach_id, '_wp_attachment_image_alt', $alttext['_wp_attachment_image_alt']);
466 - }
467 - return $attach_id;
468 - }
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 + }
469 413
470 -
471 - function overwrite($post_id , $img_url){
472 -
473 - global $wpdb;
474 - $sql = "SELECT post_mime_type FROM {$wpdb->prefix}posts WHERE ID = $post_id";
475 - list($current_filetype) = $wpdb->get_row($sql, ARRAY_N);
476 - $current_filename = wp_get_attachment_url($post_id);
477 -
478 - $current_guid = $current_filename;
479 - $current_filename = substr($current_filename, (strrpos($current_filename, "/") + 1));
480 -
481 - $ID = $post_id;
482 -
483 - //$current_file = get_attached_file($ID, apply_filters( 'emr_unfiltered_get_attached_file', true ));
484 - $current_file = get_attached_file($ID);
485 - $current_path = substr($current_file, 0, (strrpos($current_file, "/")));
486 - $current_file = preg_replace("|(?<!:)/{2,}|", "/", $current_file);
487 - $current_filename = basename($current_file);
488 - $current_metadata = wp_get_attachment_metadata( $post_id );
489 -
490 - // $original_file_perms = fileperms($current_file) & 0777;
491 -
492 - $data = file_get_contents($img_url);
493 -
494 - $new_filename = basename($img_url);
495 - $file_type = wp_check_filetype( $new_filename, null );
496 - $new_filetype = $file_type["type"];
497 - if(empty($new_filetype['ext'])){
498 - $new_filename = @basename($img_url);
499 - $new_filename = str_replace(' ', '-', trim($new_filename));
500 - $new_filename = preg_replace('/[^a-zA-Z0-9._\-\s]/', '', $new_filename);
501 - }
502 - if(empty($new_filetype)){
503 - $new_filetype = 'image/jpeg';
504 - }
505 -
506 - $original_file_perms = fileperms($current_file) & 0777;
507 -
508 - $this->emr_delete_current_files( $current_file, $current_metadata , $post_id );
509 -
510 - $new_filename = wp_unique_filename( $current_path, $new_filename );
511 - $new_file = $current_path . "/" . $new_filename;
512 - file_put_contents($new_file, $data);
513 -
514 - @chmod($current_file, $original_file_perms);
515 -
516 - $new_filetitle = preg_replace('/\.[^.]+$/', '', basename($new_file));
517 - $new_guid = str_replace($current_filename, $new_filename, $current_guid);
518 -
519 - $post_date = gmdate( 'Y-m-d H:i:s' );
520 -
521 - $sql = $wpdb->prepare(
522 - "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;",
523 - $post_id
524 - );
525 - $wpdb->query($sql);
526 -
527 -
528 - $sql = $wpdb->prepare(
529 - "SELECT meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
530 - $post_id
531 - );
532 -
533 - $old_meta_name = $wpdb->get_row($sql, ARRAY_A);
534 - $old_meta_name = $old_meta_name["meta_value"];
535 -
536 - // Make new postmeta _wp_attached_file
537 - $new_meta_name = str_replace($current_filename, $new_filename, $old_meta_name);
538 - $sql = $wpdb->prepare(
539 - "UPDATE {$wpdb->prefix}postmeta SET meta_value = '$new_meta_name' WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
540 - $post_id
541 - );
542 - $wpdb->query($sql);
543 -
544 - $new_metadata = wp_generate_attachment_metadata( $post_id, $new_file );
545 - wp_update_attachment_metadata( $post_id, $new_metadata );
546 -
547 -
548 -
549 - $current_base_url = $this->emr_get_match_url( $current_guid ); // .wp-contet.uplodas/ dae name without ext
550 -
551 - $sql = $wpdb->prepare(
552 - "SELECT ID, post_content FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_content LIKE %s;",
553 - '%' . $current_base_url . '%'
554 - );
555 -
556 -
557 - $rs = $wpdb->get_results( $sql, ARRAY_A );
558 -
559 - $number_of_updates = 0;
560 -
561 -
562 - if ( ! empty( $rs ) ) {
563 - $search_urls = $this->emr_get_file_urls( $current_guid, $current_metadata );
564 - $replace_urls = $this->emr_get_file_urls( $new_guid, $new_metadata );
565 - $replace_urls = $this->emr_normalize_file_urls( $search_urls, $replace_urls );
566 -
567 - foreach ( $rs AS $rows ) {
568 -
569 - $number_of_updates = $number_of_updates + 1;
570 -
571 - // replace old URLs with new URLs.
572 - $post_content = $rows["post_content"];
573 -
574 - $post_content = addslashes( str_replace( $search_urls, $replace_urls, $post_content ) );
575 -
576 - $sql = $wpdb->prepare(
577 - "UPDATE {$wpdb->prefix}posts SET post_content = '$post_content' WHERE ID = %d;",
578 - $rows["ID"]
579 - );
580 -
581 - $wpdb->query( $sql );
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);
582 417 }
583 - }
584 - update_attached_file( $post_id, $new_file );
585 -
586 - }
587 -
588 -
589 - function emr_delete_current_files( $current_file, $metadta = null , $post_id ) {
590 - // Delete old file
591 -
592 - // Find path of current file
593 - $current_path = substr($current_file, 0, (strrpos($current_file, "/")));
594 -
595 - // Check if old file exists first
596 - if (file_exists($current_file)) {
597 - // Now check for correct file permissions for old file
598 - clearstatcache();
599 - if (is_writable($current_file)) {
600 - // Everything OK; delete the file
601 - unlink($current_file);
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;
602 424 }
603 - else {
604 - // File exists, but has wrong permissions. Let the user know.
605 - 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);
606 - exit;
425 + else{
426 + $uploaddir_paths = $dir ['basedir'];
427 + $uploaddir_url = $dir ['baseurl'];
607 428 }
608 - }
609 -
610 - // Delete old resized versions if this was an image
611 - $suffix = substr($current_file, (strlen($current_file)-4));
612 - $prefix = substr($current_file, 0, (strlen($current_file)-4));
613 -
614 - if (strtolower($suffix) === ".pdf") {
615 - $prefix .= "-pdf";
616 - $suffix = ".jpg";
617 - }
618 -
619 - $imgAr = array(".png", ".gif", ".jpg", ".jpeg");
620 - if (in_array($suffix, $imgAr)) {
621 - // It's a png/gif/jpg based on file name
622 - // Get thumbnail filenames from metadata
623 - if ( empty( $metadata ) ) {
624 - $metadata = wp_get_attachment_metadata( $post_id );
429 + if ($uploaddir_paths != "" && $uploaddir_paths) {
430 + $uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
625 431 }
626 - // var_dump($metadata);exit;
627 432
628 - if (is_array($metadata)) { // Added fix for error messages when there is no metadata (but WHY would there not be? I don't know…)
629 - foreach($metadata["sizes"] AS $thissize) {
630 - // Get all filenames and do an unlink() on each one;
631 - $thisfile = $thissize["file"];
632 - // Create array with all old sizes for replacing in posts later
633 - $oldfilesAr[] = $thisfile;
634 - // Look for files and delete them
635 - if (strlen($thisfile)) {
636 - $thisfile = $current_path . "/" . $thissize["file"];
637 - if (file_exists($thisfile)) {
638 - unlink($thisfile);
639 - }
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;
640 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 + );
641 568 }
642 - }
643 -
569 + return $attach_id;
644 570 }
645 571 }
646 -
647 - function emr_get_match_url($url) {
648 - $url = $this->emr_remove_scheme($url);
649 - $url = $this->emr_maybe_remove_query_string($url);
650 - $url = $this->emr_remove_size_from_filename($url, true);
651 - $url = $this->emr_remove_domain_from_filename($url);
652 -
653 - return $url;
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 + }
654 590 }
655 -
656 - function emr_remove_scheme( $url ) {
657 - return preg_replace( '/^(?:http|https):/', '', $url );
658 - }
659 -
660 - function emr_maybe_remove_query_string( $url ) {
661 - $parts = explode( '?', $url );
662 -
663 - return reset( $parts );
664 - }
665 -
666 - function emr_remove_size_from_filename( $url, $remove_extension = false ) {
667 - $url = preg_replace( '/^(\S+)-[0-9]{1,4}x[0-9]{1,4}(\.[a-zA-Z0-9\.]{2,})?/', '$1$2', $url );
668 -
669 - if ( $remove_extension ) {
670 - $ext = pathinfo( $url, PATHINFO_EXTENSION );
671 - $url = str_replace( ".$ext", '', $url );
672 - }
673 -
674 - return $url;
675 - }
676 -
677 - function emr_remove_domain_from_filename($url) {
678 - // Holding place for possible future function
679 - $url = str_replace($this->emr_remove_scheme(get_bloginfo('url')), '', $url);
680 - return $url;
681 - }
682 -
683 -
684 - function emr_get_file_urls( $guid, $metadata ) {
685 - $urls = array();
686 -
687 - $guid = $this->emr_remove_scheme( $guid );
688 - $guid= $this->emr_remove_domain_from_filename($guid);
689 -
690 - $urls['guid'] = $guid;
691 -
692 - if ( empty( $metadata ) ) {
693 - return $urls;
694 - }
695 -
696 - $base_url = dirname( $guid );
697 -
698 - if ( ! empty( $metadata['file'] ) ) {
699 - $urls['file'] = trailingslashit( $base_url ) . wp_basename( $metadata['file'] );
700 - }
701 -
702 - if ( ! empty( $metadata['sizes'] ) ) {
703 - foreach ( $metadata['sizes'] as $key => $value ) {
704 - $urls[ $key ] = trailingslashit( $base_url ) . wp_basename( $value['file'] );
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', ''));
705 608 }
706 - }
707 -
708 - return $urls;
709 609 }
710 610
711 - function emr_normalize_file_urls( $old, $new ) {
712 - $result = array();
713 -
714 - if ( empty( $new['guid'] ) ) {
715 - return $result;
716 - }
717 -
718 - $guid = $new['guid'];
719 -
720 - foreach ( $old as $key => $value ) {
721 - $result[ $key ] = empty( $new[ $key ] ) ? $guid : $new[ $key ];
722 - }
723 -
724 - return $result;
725 - }
726 -}
611 +}