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