PluginProbe
Comments Extra Fields For Post,Pages and CPT / 4.3
Comments Extra Fields For Post,Pages and CPT v4.3
trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 2.2 2.3 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 5.0 5.1 5.2
wp-comment-fields / inc / files.php

files.php in Comments Extra Fields For Post,Pages and CPT 4.3, at inc/files.php

605 lines 20.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Manage file uploads, thumbs and generate links for uploaded files
4 *
5 **/
6 if( ! defined('ABSPATH') ) die('Not Allowed.');
7
8 // Set/create directory and return path
9 function wpcomment_files_setup_get_directory( $sub_dir=false ) {
10
11 $upload_dir = wp_upload_dir ();
12
13 $parent_dir = $upload_dir ['basedir'] . '/' . WPCOMMENT_UPLOAD_DIR_NAME . '/';
14 $thumb_dir = $parent_dir . 'thumbs/';
15
16 if($sub_dir){
17 $sub_dir = $parent_dir . $sub_dir . '/';
18 if(wp_mkdir_p($sub_dir)){
19 return $sub_dir;
20 }
21 }elseif(wp_mkdir_p($parent_dir)){
22 if(wp_mkdir_p($thumb_dir)){
23 return $parent_dir;
24 }
25 }
26 }
27
28
29 // Return upload file dir path
30 function wpcomment_get_dir_path( $sub_dir=false ) {
31
32 $wpcomment_upload_dir = wpcomment_files_setup_get_directory( $sub_dir );
33 return apply_filters('wpcomment_dir_path', $wpcomment_upload_dir);
34 }
35
36 // Return upload file dir url
37 function wpcomment_get_dir_url( $thumb=false, $legacy=false ) {
38
39 $upload_dir = wp_upload_dir ();
40 $return_url = '';
41
42 $dir_name = $legacy ? 'comment_uploads' : WPCOMMENT_UPLOAD_DIR_NAME;
43 if ( $thumb ) {
44 $return_url = $upload_dir ['baseurl'] . '/' . $dir_name . '/thumbs/';
45 } else {
46 $return_url = $upload_dir ['baseurl'] . '/' . $dir_name . '/';
47 }
48
49 return apply_filters('wpcomment_dir_url', set_url_scheme( $return_url ));
50 }
51
52 // Check if given filenameis image
53 function wpcomment_is_file_image( $file_name ){
54
55 $type = strtolower ( substr ( strrchr ( $file_name , '.' ), 1 ) );
56 if (($type == "gif") || ($type == "jpeg") || ($type == "png") || ($type == "pjpeg") || ($type == "jpg"))
57 return true;
58 else
59 return false;
60 }
61
62 // return html for file thumb
63 function wpcomment_create_thumb_for_meta( $field_meta, $file_name, $cropped=false, $size=null) {
64
65 $file_thumb_url = wpcomment_is_file_image($file_name) ? wpcomment_get_dir_url(true) . $file_name : WPCOMMENT_URL.'/images/file.png';
66 $file_dir_path = wpcomment_get_dir_path() . $file_name;
67
68 if( ! file_exists($file_dir_path) ) return '';
69
70 $file_link = wpcomment_get_dir_url() . $file_name;
71
72 $wpcomment_cart_meta_thumb_size = wpcomment_get_thumbs_size();
73
74 $wpcomment_html = '<p class="wpcomment-file-front-wrapper">';
75 $thumb_html = '<img class="wpcomment-file-thumbnail" style="width:'.esc_attr($wpcomment_cart_meta_thumb_size).'" src="'.esc_url($file_thumb_url).'" alt="'.sprintf(__("%s","wpcomment"), $file_name).'">';
76 $wpcomment_html .= '<a href="'.esc_url($file_link).'" class="wpcomment-file-link" itemprop="image" title="'.esc_attr($file_name).'">' . $thumb_html . '</a>';
77 $wpcomment_html .= '<p class="wpcomment-file-title">'.esc_html($field_meta['title']).'</p>';
78 $wpcomment_html .= '</p>';
79
80 return apply_filters('wpcomment_meta_file_thumb', $wpcomment_html, $file_name);
81 }
82
83
84 function wpcomment_upload_file() {
85
86 header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
87 header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
88 header ( "Cache-Control: no-store, no-cache, must-revalidate" );
89 header ( "Cache-Control: post-check=0, pre-check=0", false );
90 header ( "Pragma: no-cache" );
91
92 $wpcomment_nonce = $_REQUEST['wpcomment_nonce'];
93 $file_upload_nonce_action = "wpcomment_uploading_file_action";
94 if ( ! wp_verify_nonce( $wpcomment_nonce, $file_upload_nonce_action ) && apply_filters('wpcomment_verify_upload_file', true) ) {
95 $response ['status'] = 'error';
96 $response ['message'] = __ ( 'You cannot upload the file at this time, please refresh the page and try again. Note that your current option choices will be reset.', 'wp-comment-fields' );
97 wp_send_json( $response );
98 }
99
100 // setting up some variables
101 $file_dir_path = wpcomment_get_dir_path();
102 $response = array ();
103 if ($file_dir_path == 'errDirectory') {
104
105 $response ['status'] = 'error';
106 $response ['message'] = __ ( 'Error while creating directory', 'wp-comment-fields' );
107 wp_send_json( $response );
108 }
109
110 $file_name = '';
111
112 if( isset($_REQUEST['name']) && $_REQUEST['name'] != '') {
113 $file_name = sanitize_file_name( $_REQUEST['name'] );
114 }elseif( isset($_REQUEST['_file']) && $_REQUEST['_file'] != '') {
115 $file_name = sanitize_file_name( $_REQUEST['_file'] );
116 }
117
118 $file_name = apply_filters('wpcomment_uploaded_filename', $file_name);
119
120 /* ========== Invalid File type checking ========== */
121 $file_type = wp_check_filetype($file_name);
122 $extension = $file_type['ext'];
123
124
125 $default_restricted = 'php,php4,php5,php6,php7,phtml,exe,shtml';
126 $restricted_type = wpcomment_get_option('wpcomment_restricted_file_type', $default_restricted);
127 $restricted_type = explode(',', $restricted_type);
128
129 if( in_array( strtolower($extension), $restricted_type) ){
130 $response ['status'] = 'error';
131 $response ['message'] = __ ( 'File type not valid - '.$extension, 'wp-comment-fields' );
132 wp_send_json( $response );
133 }
134 /* ========== Invalid File type checking ========== */
135
136 $cleanupTargetDir = true; // Remove old files
137 $maxFileAge = 5 * 3600; // Temp file age in seconds
138
139 // 5 minutes execution time
140 @set_time_limit ( 5 * 60 );
141
142 // Uncomment this one to fake upload time
143 // usleep(5000);
144
145 // Get parameters
146 $chunk = isset ( $_REQUEST ["chunk"] ) ? intval ( $_REQUEST ["chunk"] ) : 0;
147 $chunks = isset ( $_REQUEST ["chunks"] ) ? intval ( $_REQUEST ["chunks"] ) : 0;
148 // $file_name = isset ( $_REQUEST ["name"] ) ? sanitize_file_name($_REQUEST ["name"]) : '';
149
150 $file_path_thumb = $file_dir_path . 'thumbs';
151 $file_name = wp_unique_filename($file_path_thumb, $file_name);
152 $file_name = strtolower($file_name);
153 $file_path = $file_dir_path . $file_name;
154
155 // var_dump($file_path); exit;
156
157 // Make sure the fileName is unique but only if chunking is disabled
158 if ($chunks < 2 && file_exists ( $file_path )) {
159 $ext = strrpos ( $file_name, '.' );
160 $file_name_a = substr ( $file_name, 0, $ext );
161 $file_name_b = substr ( $file_name, $ext );
162
163 $count = 1;
164 while ( file_exists ( $file_dir_path . $file_name_a . '_' . $count . $file_name_b ) )
165 $count ++;
166
167 $file_name = $file_name_a . '_' . $count . $file_name_b;
168 $file_path = $file_dir_path . $file_name;
169 }
170
171 // Remove old temp files
172 if ($cleanupTargetDir && is_dir ( $file_dir_path ) && ($dir = opendir ( $file_dir_path ))) {
173 while ( ($file = readdir ( $dir )) !== false ) {
174 $tmpfilePath = $file_dir_path . $file;
175
176 // Remove temp file if it is older than the max age and is not the current file
177 if (preg_match ( '/\.part$/', $file ) && (filemtime ( $tmpfilePath ) < time () - $maxFileAge) && ($tmpfilePath != "{$file_path}.part")) {
178 @unlink ( $tmpfilePath );
179 }
180 }
181
182 closedir ( $dir );
183 } else
184 die ( '{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}' );
185
186
187
188 // Look for the content type header
189 if (isset ( $_SERVER ["HTTP_CONTENT_TYPE"] ))
190 $contentType = $_SERVER ["HTTP_CONTENT_TYPE"];
191
192 if (isset ( $_SERVER ["CONTENT_TYPE"] ))
193 $contentType = $_SERVER ["CONTENT_TYPE"];
194
195 // Handle non multipart uploads older WebKit versions didn't support multipart in HTML5
196 if (strpos ( $contentType, "multipart" ) !== false) {
197 if (isset ( $_FILES ['file'] ['tmp_name'] ) && is_uploaded_file ( $_FILES ['file'] ['tmp_name'] )) {
198 // Open temp file
199 $out = fopen ( "{$file_path}.part", $chunk == 0 ? "wb" : "ab" );
200 if ($out) {
201 // Read binary input stream and append it to temp file
202 $in = fopen ( sanitize_text_field($_FILES ['file'] ['tmp_name']), "rb" );
203
204 if ($in) {
205 while ( $buff = fread ( $in, 4096 ) )
206 fwrite ( $out, $buff );
207 } else
208 die ( '{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}' );
209 fclose ( $in );
210 fclose ( $out );
211 @unlink ( sanitize_text_field($_FILES ['file'] ['tmp_name']) );
212 } else
213 die ( '{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}' );
214 } else
215 die ( '{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}' );
216 } else {
217 // Open temp file
218 $out = fopen ( "{$file_path}.part", $chunk == 0 ? "wb" : "ab" );
219 if ($out) {
220 // Read binary input stream and append it to temp file
221 $in = fopen ( "php://input", "rb" );
222
223 if ($in) {
224 while ( $buff = fread ( $in, 4096 ) )
225 fwrite ( $out, $buff );
226 } else
227 die ( '{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}' );
228
229 fclose ( $in );
230 fclose ( $out );
231 } else
232 die ( '{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}' );
233 }
234
235 // Check if file has been uploaded
236 if (! $chunks || $chunk == $chunks - 1) {
237 // Strip the temp .part suffix off
238 rename ( "{$file_path}.part", $file_path );
239
240 $product_id = intval($_REQUEST['product_id']);
241 $data_name = sanitize_key($_REQUEST['data_name']);
242 $file_meta = wpcomment_get_field_meta_by_dataname($product_id , $data_name );
243
244
245 // making thumb if images
246 if( wpcomment_is_file_image($file_name) ) {
247
248 $thumb_size = wpcomment_get_thumbs_size();
249 $thumb_dir_path = wpcomment_create_image_thumb($file_dir_path, $file_name, $thumb_size);
250 if(file_exists($thumb_dir_path)){
251 list($fw, $fh) = getimagesize( $file_path );
252 $response = array(
253 'file_name' => $file_name,
254 'file_w' => $fw,
255 'file_h' => $fh,
256 'nocache' => time(),
257 'file_url' => wpcomment_get_dir_url() . $file_name,
258 'html' => wpcomment_uploaded_file_preview($file_name, $file_meta),
259 );
260 }else{
261 $response = array(
262 'file_name' => 'ThumbNotFound',
263 );
264 }
265 }else{
266 $response = array(
267 'file_name' => $file_name,
268 'file_w' => 'na',
269 'file_h' => 'na',
270 'html' => wpcomment_uploaded_file_preview($file_name, $file_meta),
271 );
272 }
273 }
274 // Return JSON-RPC response
275 //die ( '{"jsonrpc" : "2.0", "result" : '. json_encode($response) .', "id" : "id"}' );
276 die ( json_encode( apply_filters('wpcomment_file_upload', $response, $file_type, $file_dir_path, $file_name)) );
277 }
278
279 // Deleting file
280 function wpcomment_delete_file() {
281
282 $file_name = sanitize_file_name( $_REQUEST ['file_name'] );
283
284 $wpcomment_nonce = $_REQUEST['wpcomment_nonce'];
285 $file_nonce_action = "wpcomment_deleting_file_action";
286 if ( ! wp_verify_nonce( $wpcomment_nonce, $file_nonce_action ) ) {
287 printf(__("Error while deleting file %s", "wpcomment"), $file_name );
288 die(0);
289 }
290
291 $dir_path = wpcomment_get_dir_path();
292
293 $file_path = $dir_path . $file_name;
294
295 if ( file_exists($file_path) && unlink ( $file_path )) {
296
297 if ( wpcomment_is_file_image($file_name)){
298 $thumb_path = $dir_path . 'thumbs/' . $file_name;
299 if(file_exists($thumb_path))
300 unlink ( $thumb_path );
301
302 $cropped_image_path = $dir_path . 'cropped/' . $file_name;
303 if(file_exists($cropped_image_path))
304 unlink ( $cropped_image_path );
305 }
306
307 // make sure file is removed
308 if( ! file_exists($file_path) ) {
309 _e( 'File removed', 'wp-comment-fields' );
310 } else {
311 printf(__("Error while deleting file %s", "wpcomment"), $file_path );
312 }
313
314
315 } else {
316 printf(__("Error while deleting file %s", "wpcomment"), $file_path );
317 }
318
319 die ( 0 );
320 }
321
322 // Creating thumb for image
323 function wpcomment_create_image_thumb( $file_path, $image_name, $thumb_size ) {
324
325 $thumb_size = intval($thumb_size);
326 $wp_image = wp_get_image_editor ( $file_path . $image_name );
327 $image_destination = $file_path . 'thumbs/' . $image_name;
328 if (! is_wp_error ( $wp_image )) {
329 $wp_image -> resize ( $thumb_size, $thumb_size, true );
330 $wp_image -> save ( $image_destination );
331 }
332
333 return $image_destination;
334 }
335
336 // Get file download url after payment
337 function wpcomment_get_file_download_url( $file_name, $order_id, $product_id ) {
338
339 $base_dir_path = wpcomment_get_dir_path() . $file_name;
340 $confirm_dir = 'confirmed/'.$order_id;
341 $confirmed_dir_path = wpcomment_get_dir_path($confirm_dir);
342 $edits_dir_path = wpcomment_get_dir_path('edits') . $file_name;
343
344 $wpcomment_dir_url = wpcomment_get_dir_url();
345
346 $file_download_url_found = '';
347
348 $file_name = $product_id . '-' . $file_name;
349
350 // Check if file not yet moved to confirm then move it.
351 if( file_exists($base_dir_path) ) {
352 if(rename ( $base_dir_path, $confirmed_dir_path.$file_name)) {
353 $file_download_url_found = $wpcomment_dir_url .'confirmed/' .$order_id .'/' . $file_name;
354 }
355 } else if( file_exists($confirmed_dir_path.$file_name) ) {
356 $file_download_url_found = $wpcomment_dir_url .'confirmed/' .$order_id .'/' . $file_name;
357 } else if( file_exists($edits_dir_path) ) {
358 $file_download_url_found = $wpcomment_dir_url . 'edits/' . $file_name;
359 }
360
361 return apply_filters('wpcomment_file_download_url', $file_download_url_found, $file_name);
362
363 }
364
365 // Generate uploaded file preview
366 function wpcomment_uploaded_file_preview($file_name, $settings){
367
368 $field_type = $settings['type'];
369 $data_name = isset($settings['data_name']) ? $settings['data_name'] : '';
370
371 $file_dir_path = wpcomment_get_dir_path();
372 $file_path = $file_dir_path . $file_name;
373
374 if( !file_exists($file_path) ) return '';
375
376 $is_image = wpcomment_is_file_image($file_name);
377
378 $thumb_url = $file_meta = $file_tools = $html = '';
379
380 // $settings = json_decode(stripslashes($settings), true);
381 // wpcomment_pa($settings);
382 $file_id = 'thumb_'.time();
383
384 if($is_image){
385
386 list($fw, $fh) = getimagesize( $file_path );
387 $file_meta = $fw . '(w) x '.$fh.'(h)';
388 $file_meta .= ' - '.__('Size: ', "wpcomment") . wpcomment_get_filesize_in_kb($file_name);
389
390 $thumb_url = wpcomment_get_dir_url( true ) . $file_name . '?nocache='.time();
391
392 //large view
393 $image_url = wpcomment_get_dir_url() . $file_name . '?nocache='.time();
394 $html .= '<div style="display:none" id="u_i_c_big_'.$file_id.'"><p id="thumb-thickbox"><img src="'.$image_url.'" /></p></div>';
395
396 // Loading Modals
397 // $modal_vars = array('file_id' => $file_id, 'image_full'=>$image_url, 'image_title'=>$file_name);
398 // ob_start();
399 // wpcomment_load_template('v10/file-modals.php', $modal_vars);
400 // $html .= ob_get_clean();
401
402 // Tools group
403 $file_tools .= '<div class="btn-group" role="group" aria-label="Tools" style="text-align: center; display: block;">';
404 // $file_tools .= '<a href="#" class="nm-file-tools btn btn-primary u_i_c_tools_del" title="'.__('Remove', "wpcomment").'"><span class="fa fa-times"></span></a>';
405 $file_tools .= '<a href="#" class="nm-file-tools btn btn-primary u_i_c_tools_del" title="'.__('Remove', "wpcomment").'">'.__('Delete', 'wp-comment-fields').'</span></a>';
406
407 if( apply_filters('wpcomment_show_image_popup', false) ) {
408 $file_tools .= '<a href="#" data-toggle="modal" data-target="#modalFile'.esc_attr($file_id).'" class="btn btn-primary"><span class="fa fa-expand"></span></a>';
409 }
410
411
412 $file_tools .= '</div>';
413
414 }else{
415
416 $file_meta .= __('Size: ', "wpcomment") . wpcomment_get_filesize_in_kb($file_name);
417 $thumb_url = WPCOMMENT_URL . '/images/file.png';
418
419 $file_tools .= '<a class="btn btn-primary nm-file-tools u_i_c_tools_del" href="" title="'.__('Remove', "wpcomment").'"><span class="fa fa-times"></span></a>'; //delete icon
420 // $file_tools .= '<a class="btn btn-primary nm-file-tools u_i_c_tools_del" href="" title="'.__('Remove', "wpcomment").'">Delete</a>'; //delete icon
421 }
422
423
424 $short_name = wpcomment_files_trim_name( $file_name );
425
426 // $html .= '<table class="table table-bordered"><tr>';
427 // $html .= '<td style="vertical-align:middle">';
428 $html .='<label style="margin-top: 8px;display: block;text-align: center;">
429 <div class="pre_upload_image collapse_dropdown_id" data-wpcomment-tooltip="wpcomment_tooltip" title="'.$short_name.'">
430
431 <img class="img-thumbnail" style="width:'.esc_attr(wpcomment_get_thumbs_size()).'" data-filename="'.esc_attr($file_name).'" id="'.esc_attr($file_id).'" src="'.esc_url($thumb_url).'" />
432 </div>
433 </label>';
434 $html .= $file_tools;
435
436
437 // $html .= '</tr></table>';
438
439 // $html .= '<td class="nm-imagetools" style="padding-left: 5px; vertical-align:top"><h4>'.$short_name.'</h4><br>';
440 // $html .= '<span class="file-meta">'.$short_name.'</span><br>';
441 // $html .= '</td>';
442
443
444 return apply_filters('wpcomment_file_preview_html', $html, $file_name, $settings);
445 }
446
447 // Trim long filename to short
448 function wpcomment_files_trim_name( $file_name ) {
449
450 $text_length = strlen($file_name);
451
452 // for different language string
453 $string_utf8 = strlen(utf8_decode($file_name));
454
455 $max_chars = apply_filters('wpcomment_trim_file_maxchar', 20);
456
457 if( $text_length > $max_chars && $text_length == $string_utf8 ) {
458 $trimmed_filename = substr_replace($file_name, '...', $max_chars/2, $text_length-$max_chars);
459 } else {
460 $trimmed_filename = $file_name;
461 }
462 return $trimmed_filename;
463 }
464
465
466 // Save cropped image fro dataUrl to image
467 function wpcomment_save_data_url_to_image($data, $file_name) {
468
469 $data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data));
470
471 $dest_cropped = wpcomment_get_dir_path('cropped') . $file_name;
472 file_put_contents( $dest_cropped, $data);
473 }
474
475 // Building options array for croppie
476 function wpcomment_get_croppie_options( $settings ) {
477
478 // wpcomment_pa($settings);
479
480 $cropping_settigs = array();
481 $viewport_type = !empty($settings['viewport_type']) ? $settings['viewport_type'] : 'square';
482
483
484 $viewport_h = !empty($viewport['height']) ? $viewport['height'] : 300;
485 $viewport_w = !empty($viewport['width']) ? $viewport['width'] : 300;
486
487
488
489 $viewport_settings = wpcomment_get_viewport_settings( $settings );
490
491 // view height, width and type
492 $cropping_settigs['viewport'] = $viewport_settings;
493 // $cropping_settigs['viewport_all'] = $settings['options'];
494
495 // Boundary settings
496
497 $boundary = !empty($settings['boundary']) ? $settings['boundary'] : '200,200';
498 $boundary = explode(",", $boundary);
499
500 $boundary_h = $boundary[0];
501 $boundary_w = $boundary[1];
502
503 if( $viewport_type == 'circle' ) { // If circle then set same heigt and width
504 $boundary_w = $boundary_h;
505 }
506
507 // boundary height, width
508 $cropping_settigs['boundary'] = array('height'=>$boundary_h, 'width'=>$boundary_w);
509
510 $enable_exif = ( isset($settings['enable_exif']) && $settings['enable_exif'] == 'on') ? true : false;
511 // exif
512 $cropping_settigs['enableExif'] = $enable_exif;
513
514 $enforce_boundary = ( isset($settings['enforce_boundary']) && $settings['enforce_boundary'] == 'on') ? true : false;
515 // enforce boundary
516 $cropping_settigs['enforceBoundary'] = $enforce_boundary;
517
518 $enable_zoom = ( isset($settings['enable_zoom']) && $settings['enable_zoom'] == 'on') ? true : false;
519 // show zoomer
520 $cropping_settigs['enableZoom'] = $enable_zoom;
521
522 $show_zoomer = ( isset($settings['show_zoomer']) && $settings['show_zoomer'] == 'on') ? true : false;
523 // show zoomer
524 $cropping_settigs['showZoomer'] = $show_zoomer;
525
526
527 return apply_filters('wpcomment_croppie_options', $cropping_settigs, $settings);
528
529 }
530
531 function wpcomment_get_viewport_settings( $settings ) {
532
533 $viewport_type = !empty($settings['viewport_type']) ? $settings['viewport_type'] : 'square';
534
535 $first_viewport_size = array();
536
537 if( isset($settings['options']) ) {
538 foreach($settings['options'] as $option => $viewport) {
539
540 if( ! isset($viewport['width']) ) continue;
541
542 $first_viewport_size['width'] = $viewport['width'];
543 $first_viewport_size['height'] = $viewport['height'];
544 break;
545 }
546 }
547
548
549 $the_viewport = array('width'=>300,'height'=>300);
550 if( !empty($first_viewport_size['width']) && !empty($first_viewport_size['height'])) {
551
552 $the_viewport = array('width'=>$first_viewport_size['width'],'height'=>$first_viewport_size['height']);
553 }
554
555 $the_viewport['type'] = $viewport_type;
556
557 return $the_viewport;
558
559 }
560
561 /*
562 * removing ununsed order files
563 */
564 function wpcomment_files_removed_unused_images(){
565
566 $dir = wpcomment_get_dir_path();
567
568 if(is_dir($dir) && apply_filters('wpcomment_remove_unused_images', true)){
569
570 $dir_handle = opendir($dir);
571 while ($file = readdir($dir_handle)){
572
573 $file_path = $dir.$file;
574 if( is_file ($file_path) ){
575
576 // Get Files Created Date
577 $file_created_date = date ("Y-m-d H:i:s.", filemtime($file_path));
578 $today = date("Y-m-d H:i:s");
579
580 $day_count = wpcomment_files_uploaded_days_count($file_created_date, $today);
581
582 if ($day_count > 7) {
583 @unlink($file_path);
584 }
585 }
586 }
587 }
588
589 closedir($dir_handle);
590 }
591
592 function wpcomment_files_uploaded_days_count($date1, $date2){
593
594 $diff = strtotime($date2) - strtotime($date1);
595
596 return abs(round($diff / 86400));
597 }
598
599 // Return file name with prefix product id
600 function wpcomment_file_get_name($file_name, $product_id, $cart_item=null) {
601
602 $new_file_name = "{$product_id}-{$file_name}";
603 return apply_filters('wpcomment_file_name_prefix', $new_file_name, $file_name, $product_id, $cart_item);
604 }
605