PluginProbe
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on / 1.3
Import Users & Customers with Meta | WP Ultimate CSV Importer Add-on v1.3
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 +104 -57 1.21.3 View file →
@@ -1,11 +1,10 @@
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' ) )
@@ -22,28 +21,28 @@
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 46 'media_settings' => $media_settings
48 47 );
49 48 update_option( 'smack_image_options', $image_info );
@@ -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,32 +137,8 @@
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 - }
163 -
164 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){
165 142
166 143 global $wpdb;
167 144 $media_handle = get_option('smack_image_options');
@@ -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;
@@ -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,19 +337,85 @@
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
@@ -384,9 +431,9 @@
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 434 'ID' => $attach_id,
388 - 'post_title' => $data_array['file_name'],
435 + 'post_title' => $data_array['title'],
389 436 'post_content' => $data_array['description'],
390 437 'post_excerpt' => $data_array['caption']
391 438 ));
392 439 if($attach_id != null && isset($alttext['_wp_attachment_image_alt'])){
@@ -432,9 +479,9 @@
432 479 }
433 480
434 481 $original_file_perms = fileperms($current_file) & 0777;
435 482
436 - $this->emr_delete_current_files( $current_file, $current_metadata , $post_id );
483 + $this->emr_delete_current_files( $current_file, $post_id, $current_metadata );
437 484
438 485 $new_filename = wp_unique_filename( $current_path, $new_filename );
439 486 $new_file = $current_path . "/" . $new_filename;
440 487 file_put_contents($new_file, $data);
@@ -513,9 +560,9 @@
513 560
514 561 }
515 562
516 563
517 - function emr_delete_current_files( $current_file, $metadta = null , $post_id ) {
564 + function emr_delete_current_files( $current_file, $post_id , $metadta = null ) {
518 565 // Delete old file
519 566
520 567 // Find path of current file
521 568 $current_path = substr($current_file, 0, (strrpos($current_file, "/")));