helpers.php
4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* @Description : File helpers
* @Package : Drag & Drop Multiple File Upload - Contact Form 7
* @Author : CodeDropz
*/
if ( ! defined( 'ABSPATH' ) || ! defined('dnd_upload_cf7') || ! defined('dnd_upload_cf7_PRO') ) {
exit;
}
class CodeDropz_Uploader_Helpers {
private static $instance = null;
public static function get_instance() {
if( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Get current time based on GMT selected
*/
public function get_date( $format ) {
$string = date( 'Y-m-d H:i:s' );
return get_date_from_gmt( $string, $format );
}
/**
* Get current user information
*/
public function get_user(){
return is_user_logged_in() ? wp_get_current_user() : null;
}
/**
* Delete Preview Thumbnail
* @param : $file_path - basedir
*/
public function delete_files( $file_path ) {
// There's no reason to proceed if - null
if( ! $file_path ) {
return;
}
// Get file info
$file = pathinfo( $file_path );
$dirname = trailingslashit( wp_normalize_path( $file['dirname'] ) );
// Check and validate file type if it's safe to delete...
$safe_to_delete = dndmfu_cf7_validate_type( $file['extension'] );
// @bolean - true if validated
if( $safe_to_delete ) {
// Delete parent file
wp_delete_file( $file_path );
// Delete if there's a thum
if( file_exists( $thumbnail = $dirname . $file['filename'].'-100x100' .'.'. strtolower( $file['extension'] ) ) ) {
wp_delete_file( $thumbnail );
}
}
}
/**
* Setup media file on json response after the successfull upload.
*/
public function media_response( $path, $file_name ) {
// Default placeholder
$preview = false;
// Match to images ext
$is_image = preg_match( '/\.(jpg|jpeg|png|gif|tiff|svg )$/i', $file_name );
// Get file type
$file_type = wp_check_filetype( $file_name );
// Apply preview on images only
if( get_option('drag_n_drop_show_img_preview') === true ) {
$preview = ( $is_image ? true : wp_mime_type_icon( $file_type['type'] ) );
}
$media_files = array(
'path' => $path,
'file' => $file_name,
'preview' => $preview
);
// Display column for preview image
if( get_option('drag_n_drop_thumbnail_display') == 'column' ) {
$media_files['is_image'] = ( $is_image ? true : false );
$media_files['ext'] = pathinfo( $file_name, PATHINFO_EXTENSION );
if( ! $is_image ) {
$media_files['preview'] = wp_mime_type_icon( $file_type['type'] );
}
}
return $media_files;
}
/**
* Get allowed file types @param : form_id
*/
public function allowed_types( $form_id ) {
// Initialize contact form instance
$form = WPCF7_ContactForm::get_instance( $form_id );
// Check if not valid object and null
if( ! $form && ! is_object( $form ) ) {
return false;
}
// Get specific tag (mfile is for dnd file upload)
$tags = $form->scan_form_tags( array( 'type' => array('mfile', 'mfile*') ) );
$supported_types = array();
// Loop all upload tags
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
// Get file types option & remove not allowed character..
$types = preg_replace( '/[^a-zA-Z0-9|\']/', '', $tag->get_option('filetypes','', true ) );
// Assign if filetypes is present otherwise use the default ext list.
$supported_types[ $tag->name ] = ( $types ? $types : $this->default_ext() );
}
}
return $supported_types;
}
/**
* Define custom (safe) file extension.
*/
public function default_ext() {
return apply_filters('dnd_cf7_default_ext', 'jpg|jpeg|JPG|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv|xls' );
}
/**
* Default error message
*/
public function get_error( $error_key ) {
$error_message = CodeDropz_Uploader_Errors::get_instance()->get_error_message();
if( isset( $error_message[ $error_key ] ) ) {
return $error_message[ $error_key ];
}
return false;
}
}