PluginProbe
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on / 1.4.2
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on v1.4.2
2.0 1.7.1 1.2.9 1.3 1.4 1.4.1 1.4.2 1.4.3 1.5 1.6 1.7 trunk 1.0 1.1 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8
← All changes | importExtensions/MediaHandling.php +147 -102 1.2.11.4.2 View file →
@@ -1,16 +1,15 @@
1 1 <?php
2 -/******************************************************************************************
3 - * Copyright (C) Smackcoders. - All Rights Reserved under Smackcoders Proprietary License
4 - * Unauthorized copying of this file, via any medium is strictly prohibited
5 - * Proprietary and confidential
6 - * You can contact Smackcoders at email address info@smackcoders.com.
7 - *******************************************************************************************/
2 +/**
3 + * Import Users plugin file.
4 + *
5 + * Copyright (C) 2010-2020, Smackcoders Inc - info@smackcoders.com
6 + */
8 7
9 8 namespace Smackcoders\SMUSERS;
10 9
11 10 if ( ! defined( 'ABSPATH' ) )
12 -exit; // Exit if accessed directly
11 + exit; // Exit if accessed directly
13 12
14 13 class MediaHandling{
15 14 private static $instance=null;
16 15 public $header_array;
@@ -16,37 +15,37 @@
16 15 public $header_array;
17 16 public $value_array;
18 17
19 18 public function __construct(){
20 -
19 +
21 20 require_once(ABSPATH.'wp-load.php');
22 21 require_once(ABSPATH . 'wp-admin/includes/image.php');
23 22 require_once(ABSPATH . 'wp-admin/includes/file.php');
24 23 add_action('wp_ajax_image_options', array($this , 'imageOptions'));
25 24 add_action('wp_ajax_delete_image' , array($this , 'deleteImage'));
26 - add_action('wp_ajax_media_report' , array($this , 'mediaReport'));
27 25 }
28 26
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'];
27 + public static function imageOptions(){
28 + check_ajax_referer('smack-ultimate-csv-importer', 'securekey');
29 + $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']);
46 45 $image_info = array(
47 - 'media_settings' => $media_settings
48 - );
46 + 'media_settings' => $media_settings
47 + );
49 48 update_option( 'smack_image_options', $image_info );
50 49 $result['success'] = 'true';
51 50 echo wp_json_encode($result);
52 51 wp_die();
@@ -60,9 +59,10 @@
60 59 return MediaHandling::$instance;
61 60 }
62 61
63 62 public function deleteImage(){
64 - $image = $_POST['image'];
63 + check_ajax_referer('smack-ultimate-csv-importer', 'securekey');
64 + $image = sanitize_text_field($_POST['image']);
65 65 $media_dir = wp_get_upload_dir();
66 66 $names = glob($media_dir['path'].'/'.'*.*');
67 67 foreach($names as $values){
68 68 if (strpos($values, $image) !== false) {
@@ -72,8 +72,9 @@
72 72 $result['success'] = 'true';
73 73 echo wp_json_encode($result);
74 74 wp_die();
75 75 }
76 +
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){
77 78 $encodedurl = urlencode($img_url);
78 79 $img_url = urldecode($encodedurl);
79 80 $media_handle = get_option('smack_image_options');
@@ -136,34 +137,10 @@
136 137 }
137 138 return $attach_id;
138 139 }
139 140
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 - }
141 + public function image_function($f_img , $post_id , $data_array = null,$option_name = null, $use_existing_image = false,$header_array = null , $value_array = null){
163 142
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 143 global $wpdb;
167 144 $media_handle = get_option('smack_image_options');
168 145 $media_settings = array_combine($header_array,$value_array);
169 146 if(isset($media_handle['media_settings']['alttext'])) {
@@ -210,8 +187,9 @@
210 187 }
211 188 }
212 189
213 190 $attachment_title = sanitize_file_name( pathinfo( $fimg_name, PATHINFO_FILENAME ) );
191 + $file_type = wp_check_filetype( $fimg_name, null );
214 192 $dir = wp_upload_dir();
215 193 $dirname = date('Y') . '/' . date('m');
216 194 $uploaddir_paths = $dir ['basedir'] . '/' . $dirname ;
217 195 $uploaddir_url = $dir ['baseurl'] . '/' . $dirname;
@@ -262,14 +240,14 @@
262 240 if(empty($file_type['type'])){
263 241 $file_type['type'] = 'image/jpeg';
264 242 }
265 243 $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 - );
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 + );
272 250 //unset( $post_info['ID'] );
273 251 $attach_id = wp_insert_attachment( $post_info,$uploaddir_path, $post_id );
274 252 $attach_data = wp_generate_attachment_metadata( $attach_id, $uploaddir_path );
275 253 wp_update_attachment_metadata( $attach_id, $attach_data );
@@ -316,18 +294,18 @@
316 294 $media_handle['media_settings']['title'] = $media_settings[$media_handle['media_settings']['title']];
317 295 }
318 296 if(isset($media_handle['media_settings']['caption']) || isset($media_handle['media_settings']['description'])){
319 297 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 - ));
298 + 'ID' =>$attach_id,
299 + 'post_content' =>$media_handle['media_settings']['description'],
300 + 'post_excerpt' =>$media_handle['media_settings']['caption']
301 + ));
324 302 }
325 303 if(isset($media_handle['media_settings']['title'])){
326 304 wp_update_post(array(
327 - 'ID' =>$attach_id,
328 - 'post_title' =>$media_handle['media_settings']['title']
329 - ));
305 + 'ID' =>$attach_id,
306 + 'post_title' =>$media_handle['media_settings']['title']
307 + ));
330 308 }
331 309 if($attach_id != null && isset($alttext['_wp_attachment_image_alt'])){
332 310 update_post_meta($attach_id, '_wp_attachment_image_alt', $alttext['_wp_attachment_image_alt']);
333 311 }
@@ -339,12 +317,15 @@
339 317 }
340 318
341 319
342 320 public function image_import($data_array){
321 + $encodedurl = urlencode($data_array['featured_image']);
322 + $data_array['featured_image'] = urldecode($encodedurl);
343 323 if(isset($data_array['alt_text'])) {
344 324 $alttext ['_wp_attachment_image_alt'] = $data_array['alt_text'];
345 325 }
346 - if(preg_match("http", $data_array['featured_image'])) {
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 + {
347 328 $f_img = $data_array['featured_image'];
348 329 }
349 330 else{
350 331 $media_dir = wp_get_upload_dir();
@@ -356,40 +337,106 @@
356 337 }
357 338 }
358 339 }
359 340 $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 );
341 + $attachment_title = sanitize_file_name( pathinfo( $f_img, PATHINFO_FILENAME ) );
342 + $file_type = wp_check_filetype( $f_img, null );
363 343 $dir = wp_upload_dir();
364 344 $dirname = date('Y') . '/' . date('m');
365 345 $uploaddir_paths = $dir ['basedir'] . '/' . $dirname ;
366 346 $uploaddir_url = $dir ['baseurl'] . '/' . $dirname;
367 - $f_img = str_replace(" ","%20",$f_img);
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 + }
368 357 if ($uploaddir_paths != "" && $uploaddir_paths) {
369 358 $uploaddir_path = $uploaddir_paths . "/" . $fimg_name;
370 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 + }
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 + }
413 + }
414 + $fp = fopen($uploaddir_path, 'x');
415 + fwrite($fp, $rawdata);
416 + fclose($fp);
417 + }
371 418 if(empty($file_type['type'])){
372 419 $file_type['type'] = 'image/jpeg';
373 420 }
374 421 //Removed curl and added wordpress http api
375 422 $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 - );
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 + );
382 429 unset( $post_info['ID'] );
383 430 $attach_id = wp_insert_attachment( $post_info,$uploaddir_path );
384 431 $attach_data = wp_generate_attachment_metadata( $attach_id, $uploaddir_path );
385 432 wp_update_attachment_metadata( $attach_id, $attach_data );
386 433 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 - ));
434 + 'ID' => $attach_id,
435 + 'post_title' => $data_array['title'],
436 + 'post_content' => $data_array['description'],
437 + 'post_excerpt' => $data_array['caption']
438 + ));
392 439 if($attach_id != null && isset($alttext['_wp_attachment_image_alt'])){
393 440 update_post_meta($attach_id, '_wp_attachment_image_alt', $alttext['_wp_attachment_image_alt']);
394 441 }
395 442 return $attach_id;
@@ -407,9 +454,8 @@
407 454 $current_filename = substr($current_filename, (strrpos($current_filename, "/") + 1));
408 455
409 456 $ID = $post_id;
410 457
411 - //$current_file = get_attached_file($ID, apply_filters( 'emr_unfiltered_get_attached_file', true ));
412 458 $current_file = get_attached_file($ID);
413 459 $current_path = substr($current_file, 0, (strrpos($current_file, "/")));
414 460 $current_file = preg_replace("|(?<!:)/{2,}|", "/", $current_file);
415 461 $current_filename = basename($current_file);
@@ -414,9 +460,8 @@
414 460 $current_file = preg_replace("|(?<!:)/{2,}|", "/", $current_file);
415 461 $current_filename = basename($current_file);
416 462 $current_metadata = wp_get_attachment_metadata( $post_id );
417 463
418 - // $original_file_perms = fileperms($current_file) & 0777;
419 464
420 465 $data = file_get_contents($img_url);
421 466
422 467 $new_filename = basename($img_url);
@@ -432,9 +477,9 @@
432 477 }
433 478
434 479 $original_file_perms = fileperms($current_file) & 0777;
435 480
436 - $this->emr_delete_current_files( $current_file, $current_metadata , $post_id );
481 + $this->emr_delete_current_files( $current_file, $post_id, $current_metadata );
437 482
438 483 $new_filename = wp_unique_filename( $current_path, $new_filename );
439 484 $new_file = $current_path . "/" . $new_filename;
440 485 file_put_contents($new_file, $data);
@@ -446,18 +491,18 @@
446 491
447 492 $post_date = gmdate( 'Y-m-d H:i:s' );
448 493
449 494 $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 - );
495 + "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;",
496 + $post_id
497 + );
453 498 $wpdb->query($sql);
454 499
455 500
456 501 $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 - );
502 + "SELECT meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
503 + $post_id
504 + );
460 505
461 506 $old_meta_name = $wpdb->get_row($sql, ARRAY_A);
462 507 $old_meta_name = $old_meta_name["meta_value"];
463 508
@@ -463,11 +508,11 @@
463 508
464 509 // Make new postmeta _wp_attached_file
465 510 $new_meta_name = str_replace($current_filename, $new_filename, $old_meta_name);
466 511 $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 - );
512 + "UPDATE {$wpdb->prefix}postmeta SET meta_value = '$new_meta_name' WHERE meta_key = '_wp_attached_file' AND post_id = %d;",
513 + $post_id
514 + );
470 515 $wpdb->query($sql);
471 516
472 517 $new_metadata = wp_generate_attachment_metadata( $post_id, $new_file );
473 518 wp_update_attachment_metadata( $post_id, $new_metadata );
@@ -476,11 +521,11 @@
476 521
477 522 $current_base_url = $this->emr_get_match_url( $current_guid ); // .wp-contet.uplodas/ dae name without ext
478 523
479 524 $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 - );
525 + "SELECT ID, post_content FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_content LIKE %s;",
526 + '%' . $current_base_url . '%'
527 + );
483 528
484 529
485 530 $rs = $wpdb->get_results( $sql, ARRAY_A );
486 531
@@ -501,11 +546,11 @@
501 546
502 547 $post_content = addslashes( str_replace( $search_urls, $replace_urls, $post_content ) );
503 548
504 549 $sql = $wpdb->prepare(
505 - "UPDATE {$wpdb->prefix}posts SET post_content = '$post_content' WHERE ID = %d;",
506 - $rows["ID"]
507 - );
550 + "UPDATE {$wpdb->prefix}posts SET post_content = '$post_content' WHERE ID = %d;",
551 + $rows["ID"]
552 + );
508 553
509 554 $wpdb->query( $sql );
510 555 }
511 556 }
@@ -513,9 +558,9 @@
513 558
514 559 }
515 560
516 561
517 - function emr_delete_current_files( $current_file, $metadta = null , $post_id ) {
562 + function emr_delete_current_files( $current_file, $post_id , $metadta = null ) {
518 563 // Delete old file
519 564
520 565 // Find path of current file
521 566 $current_path = substr($current_file, 0, (strrpos($current_file, "/")));