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