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