| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Upload file input field. |
| 9 |
* |
| 10 |
* @since 1.0.0 |
| 11 |
*/ |
| 12 |
class FILEUPFO_WPForms_Field_File extends WPForms_Field { |
| 13 |
|
| 14 |
public $time_now = ''; |
| 15 |
public $attachments = []; |
| 16 |
|
| 17 |
/** |
| 18 |
* Primary class constructor. |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
public function init() { |
| 23 |
$this->name = esc_html__( 'Upload File', 'file-upload-for-wpforms' ); |
| 24 |
$this->type = 'file'; |
| 25 |
$this->icon = 'fa-upload'; |
| 26 |
$this->order = 1000; |
| 27 |
|
| 28 |
add_filter('wpforms_get_form_fields_allowed', [$this, 'form_fields_allowed']); |
| 29 |
add_action('wpforms_ajax_submit_before_processing', [$this, 'before_processing']); |
| 30 |
add_filter('wpforms_emails_send_email_data', [$this, 'add_attachments']); |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
public function before_processing(){ |
| 36 |
|
| 37 |
$this->time_now = time().bin2hex( random_bytes(5) ); |
| 38 |
$upload_dir = wp_upload_dir(); |
| 39 |
$uplod_dirpath = $upload_dir['baseurl'].'/wpxform-uploads'; |
| 40 |
|
| 41 |
$files = isset($_FILES['wpforms']['name']['fields']) ? $_FILES['wpforms']['name']['fields'] : []; |
| 42 |
|
| 43 |
foreach($files as $field_id => $value){ |
| 44 |
if( empty( $value ) ) |
| 45 |
continue; |
| 46 |
$value = sanitize_file_name( $value ); |
| 47 |
$new_file = $uplod_dirpath.'/'.$this->time_now.'-'.$field_id.'-'.$value; |
| 48 |
$_POST['wpforms']['fields'][ $field_id ] = $new_file; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
public function form_fields_allowed( $fields ){ |
| 54 |
$fields[] = 'file'; |
| 55 |
return $fields; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
/** |
| 60 |
* Field options panel inside the builder. |
| 61 |
* |
| 62 |
* @since 1.0.0 |
| 63 |
* |
| 64 |
* @param array $field Field settings. |
| 65 |
*/ |
| 66 |
public function field_options( $field ) { |
| 67 |
/* |
| 68 |
* Basic field options. |
| 69 |
*/ |
| 70 |
|
| 71 |
// Options open markup. |
| 72 |
$this->field_option( |
| 73 |
'basic-options', |
| 74 |
$field, |
| 75 |
[ |
| 76 |
'markup' => 'open', |
| 77 |
] |
| 78 |
); |
| 79 |
|
| 80 |
// Label. |
| 81 |
$this->field_option( 'label', $field ); |
| 82 |
|
| 83 |
// Description. |
| 84 |
$this->field_option( 'description', $field ); |
| 85 |
|
| 86 |
$this->upload_file_options( 'accepted_file_types', $field ); |
| 87 |
|
| 88 |
$this->upload_file_options( 'max_size', $field ); |
| 89 |
|
| 90 |
$this->upload_file_options( 'add_to_email', $field ); |
| 91 |
|
| 92 |
// Required toggle. |
| 93 |
$this->field_option( 'required', $field ); |
| 94 |
|
| 95 |
// Options close markup. |
| 96 |
$this->field_option( |
| 97 |
'basic-options', |
| 98 |
$field, |
| 99 |
[ |
| 100 |
'markup' => 'close', |
| 101 |
] |
| 102 |
); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
/* |
| 107 |
* Advanced field options. |
| 108 |
*/ |
| 109 |
|
| 110 |
// Options open markup. |
| 111 |
$this->field_option( |
| 112 |
'advanced-options', |
| 113 |
$field, |
| 114 |
[ |
| 115 |
'markup' => 'open', |
| 116 |
] |
| 117 |
); |
| 118 |
|
| 119 |
// Custom CSS classes. |
| 120 |
$this->field_option( 'css', $field ); |
| 121 |
|
| 122 |
// Hide label. |
| 123 |
$this->field_option( 'label_hide', $field ); |
| 124 |
|
| 125 |
// Options close markup. |
| 126 |
$this->field_option( |
| 127 |
'advanced-options', |
| 128 |
$field, |
| 129 |
[ |
| 130 |
'markup' => 'close', |
| 131 |
] |
| 132 |
); |
| 133 |
|
| 134 |
$ignore_field_notice = get_option('fileupfo_view_ignore_field_notice'); |
| 135 |
if( empty($ignore_field_notice)){ |
| 136 |
echo '<div id="filenzo-notice" style="display:flex; align-items:center; justify-content:space-between; margin:20px; padding:15px; border-radius:7px; background:#E0E8F0"> |
| 137 |
<p style="margin:0;">Need more file upload capabilities? Unlock premium features with <strong>Filenzo Pro</strong> - |
| 138 |
<a href="https://wpdebuglog.com/downloads/file-uploads-to-wpforms/" target="_blank" style="text-decoration:none"> |
| 139 |
<span>Learn More!</span> |
| 140 |
</a> |
| 141 |
</p> |
| 142 |
<a href="' . $this->get_dismiss_admin_url() . '" class="wpforms-dismiss-button" style="display:inline-flex; align-items:center; justify-content:center; width:24px; height:24px; cursor:pointer; padding-left:5px;text-decoration:none"></a> |
| 143 |
</div>'; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
private function get_dismiss_admin_url() { |
| 150 |
$scheme = is_ssl() ? 'https://' : 'http://'; |
| 151 |
$host = $_SERVER['HTTP_HOST']; |
| 152 |
$request = $_SERVER['REQUEST_URI']; |
| 153 |
return $scheme . $host . $request . '&fileupfo-ignore-field-notice=1'; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Field preview inside the builder. |
| 158 |
* |
| 159 |
* @since 1.0.0 |
| 160 |
* |
| 161 |
* @param array $field Field settings. |
| 162 |
*/ |
| 163 |
public function field_preview( $field ) { |
| 164 |
|
| 165 |
// Define data. |
| 166 |
$placeholder = ! empty( $field['placeholder'] ) ? $field['placeholder'] : ''; |
| 167 |
$default_value = ! empty( $field['default_value'] ) ? $field['default_value'] : ''; |
| 168 |
|
| 169 |
// Label. |
| 170 |
$this->field_preview_option( 'label', $field ); |
| 171 |
|
| 172 |
// Primary input. |
| 173 |
echo '<input type="file" placeholder="' . esc_attr( $placeholder ) . '" value="' . esc_attr( $default_value ) . '" class="primary-input" readonly>'; |
| 174 |
|
| 175 |
// Description. |
| 176 |
$this->field_preview_option( 'description', $field ); |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
public function upload_file_options( $option, $field ){ |
| 182 |
|
| 183 |
switch( $option ){ |
| 184 |
case 'accepted_file_types': |
| 185 |
|
| 186 |
$lbl = $this->field_element( |
| 187 |
'label', |
| 188 |
$field, |
| 189 |
[ |
| 190 |
'slug' => 'supported_files', |
| 191 |
'value' => esc_html__( 'Acceptable file types', 'file-upload-for-wpforms' ), |
| 192 |
'tooltip' => esc_html__( 'Pipe-separated file types list.', 'file-upload-for-wpforms' ), |
| 193 |
], |
| 194 |
false |
| 195 |
); |
| 196 |
|
| 197 |
$fld = $this->field_element( |
| 198 |
'text', |
| 199 |
$field, |
| 200 |
[ |
| 201 |
'slug' => 'supported_files', |
| 202 |
'value' => empty( $field['supported_files'] ) ? 'jpg|jpeg|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv' : $field['supported_files'], |
| 203 |
], |
| 204 |
false |
| 205 |
); |
| 206 |
|
| 207 |
$args = [ |
| 208 |
'slug' => 'supported_files', |
| 209 |
'content' => $lbl . $fld, |
| 210 |
]; |
| 211 |
|
| 212 |
$this->field_element( 'row', $field, $args, true ); |
| 213 |
break; |
| 214 |
|
| 215 |
case 'max_size': |
| 216 |
|
| 217 |
$lbl = $this->field_element( |
| 218 |
'label', |
| 219 |
$field, |
| 220 |
[ |
| 221 |
'slug' => 'max_size', |
| 222 |
'value' => esc_html__( 'File size limit', 'file-upload-for-wpforms' ), |
| 223 |
'tooltip' => esc_html__( 'In bytes. You can use kb and mb suffixes.', 'file-upload-for-wpforms' ), |
| 224 |
], |
| 225 |
false |
| 226 |
); |
| 227 |
|
| 228 |
$fld = $this->field_element( |
| 229 |
'text', |
| 230 |
$field, |
| 231 |
[ |
| 232 |
'slug' => 'max_size', |
| 233 |
'value' => empty( $field['max_size'] ) ? '1mb' : $field['max_size'], |
| 234 |
], |
| 235 |
false |
| 236 |
); |
| 237 |
|
| 238 |
$args = [ |
| 239 |
'slug' => 'max_size', |
| 240 |
'content' => $lbl . $fld, |
| 241 |
]; |
| 242 |
|
| 243 |
$this->field_element( 'row', $field, $args, true ); |
| 244 |
break; |
| 245 |
|
| 246 |
case 'add_to_email': |
| 247 |
$value = isset( $field['add_to_email'] ) ? esc_attr( $field['add_to_email'] ) : 1; |
| 248 |
$tooltip = esc_html__( 'Check this box to include the uploaded file in email notifications.', 'file-upload-for-wpforms' ); |
| 249 |
|
| 250 |
$output = $this->field_element( |
| 251 |
'toggle', |
| 252 |
$field, |
| 253 |
[ |
| 254 |
'slug' => 'add_to_email', |
| 255 |
'value' => $value, |
| 256 |
'desc' => esc_html__( 'Add to Email Attachment', 'file-upload-for-wpforms' ), |
| 257 |
'tooltip' => $tooltip, |
| 258 |
], |
| 259 |
false |
| 260 |
); |
| 261 |
|
| 262 |
$this->field_element( |
| 263 |
'row', |
| 264 |
$field, |
| 265 |
[ |
| 266 |
'slug' => 'required', |
| 267 |
'content' => $output, |
| 268 |
], |
| 269 |
true |
| 270 |
); |
| 271 |
break; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Field display on the form front-end. |
| 277 |
* |
| 278 |
* @since 1.0.0 |
| 279 |
* |
| 280 |
* @param array $field Field settings. |
| 281 |
* @param array $deprecated Deprecated. |
| 282 |
* @param array $form_data Form data and settings. |
| 283 |
*/ |
| 284 |
public function field_display( $field, $deprecated, $form_data ) { |
| 285 |
|
| 286 |
// Define data. |
| 287 |
$primary = $field['properties']['inputs']['primary']; |
| 288 |
|
| 289 |
if ( isset( $field['limit_enabled'] ) ) { |
| 290 |
$limit_count = isset( $field['limit_count'] ) ? absint( $field['limit_count'] ) : 0; |
| 291 |
$limit_mode = isset( $field['limit_mode'] ) ? sanitize_key( $field['limit_mode'] ) : 'characters'; |
| 292 |
|
| 293 |
$primary['data']['form-id'] = $form_data['id']; |
| 294 |
$primary['data']['field-id'] = $field['id']; |
| 295 |
} |
| 296 |
|
| 297 |
// Primary field. |
| 298 |
printf( |
| 299 |
'<input type="file" %s %s>', |
| 300 |
wpforms_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ), |
| 301 |
$primary['required'] // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 302 |
); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
/** |
| 307 |
* Validate field on form submit. |
| 308 |
* |
| 309 |
* @since 1.0.0 |
| 310 |
* |
| 311 |
* @param int $field_id Field ID. |
| 312 |
* @param mixed $field_submit Submitted field value (raw data). |
| 313 |
* @param array $form_data Form data and settings. |
| 314 |
*/ |
| 315 |
public function validate( $field_id, $field_submit, $form_data ) { |
| 316 |
|
| 317 |
$files = $_FILES; |
| 318 |
|
| 319 |
if ( empty( $form_data['fields'][ $field_id ] ) ) { |
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
$field = $form_data['fields'][ $field_id ]; |
| 324 |
$max_size = (int) $this->get_size_option( $field['max_size'] ); |
| 325 |
$max_size_limit = $this->get_size_option( $field['max_size'] ); |
| 326 |
$file_name = $files['wpforms']['name']['fields'][$field_id] ?? ''; |
| 327 |
$file_name = sanitize_file_name( $file_name ); |
| 328 |
$tmp_name = $files['wpforms']['tmp_name']['fields'][$field_id] ?? ''; |
| 329 |
$acceptable_filetypes = explode( '|', $field['supported_files']); |
| 330 |
|
| 331 |
if( empty($field['required']) && empty($file_name) ){ |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
if( ( ! empty($field['required']) ) && empty($file_name) ){ |
| 336 |
wpforms()->obj( 'process' )->errors[ $form_data['id'] ][ $field_id ] = sprintf( __( 'This field is required', 'file-upload-for-wpforms' )); |
| 337 |
return; |
| 338 |
} |
| 339 |
|
| 340 |
if( $max_size < $files['wpforms']['size']['fields'][$field_id]){ |
| 341 |
/* translators: %s is the maximum allowed file size. */ |
| 342 |
wpforms()->obj( 'process' )->errors[ $form_data['id'] ][ $field_id ] = sprintf( __( 'Files size can\'t exceed %s .', 'file-upload-for-wpforms' ), esc_attr( $max_size_limit ) ); |
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
$last_period_pos = strrpos( $file_name, '.' ); |
| 347 |
|
| 348 |
if ( false === $last_period_pos ) { |
| 349 |
wpforms()->obj( 'process' )->errors[ $form_data['id'] ][ $field_id ] = __( 'You are not allowed to upload files of this type.', 'file-upload-for-wpforms' ); |
| 350 |
return; |
| 351 |
} |
| 352 |
|
| 353 |
$suffix = strtolower( substr( $file_name, $last_period_pos ) ); |
| 354 |
$suffix = str_replace( '.', '', $suffix ); |
| 355 |
|
| 356 |
if ( ! in_array( $suffix, $acceptable_filetypes, true ) ) { |
| 357 |
wpforms()->obj( 'process' )->errors[ $form_data['id'] ][ $field_id ] = __( 'You are not allowed to upload files of this type.', 'file-upload-for-wpforms' ); |
| 358 |
return; |
| 359 |
} |
| 360 |
|
| 361 |
$upload_dir = wp_upload_dir(); |
| 362 |
$uplod_dirpath = $upload_dir['basedir'].'/wpxform-uploads'; |
| 363 |
$new_file = $uplod_dirpath.'/'.$this->time_now.'-'.$field_id.'-'.$file_name; |
| 364 |
|
| 365 |
$queue = FILEUPFO_Move_Queue::getInstance(); |
| 366 |
$queue->form_id = $form_data['id']; |
| 367 |
$queue_files = $queue->get_files(); |
| 368 |
$queue_files[ $field_id ] = [ $tmp_name, $new_file]; |
| 369 |
$queue->update_files( $queue_files ); |
| 370 |
|
| 371 |
if( !empty( $field['add_to_email'] ) ){ |
| 372 |
$this->attachments[] = $new_file; |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
public function add_attachments( $data ){ |
| 377 |
$attachments = $data['attachments'] ?? []; |
| 378 |
$attachments = array_merge($attachments, $this->attachments); |
| 379 |
$data['attachments'] = $attachments; |
| 380 |
return $data; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
public function get_size_option( $val, $default_value = MB_IN_BYTES ) { |
| 385 |
$pattern = '/^([1-9][0-9]*)([kKmM]?[bB])?$/'; |
| 386 |
$matches = false; |
| 387 |
|
| 388 |
preg_match( $pattern, $val, $matches ); |
| 389 |
|
| 390 |
if ( $matches ) { |
| 391 |
$size = (int) $matches[1]; |
| 392 |
|
| 393 |
if ( ! empty( $matches[2] ) ) { |
| 394 |
$kbmb = strtolower( $matches[2] ); |
| 395 |
|
| 396 |
if ( 'kb' === $kbmb ) { |
| 397 |
$size *= KB_IN_BYTES; |
| 398 |
} elseif ( 'mb' === $kbmb ) { |
| 399 |
$size *= MB_IN_BYTES; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
return $size; |
| 404 |
} |
| 405 |
|
| 406 |
return (int) $default_value; |
| 407 |
} |
| 408 |
|
| 409 |
} |
| 410 |
|
| 411 |
add_action('init', 'fileupfo_init_instance'); |
| 412 |
|
| 413 |
function fileupfo_init_instance(){ |
| 414 |
new FILEUPFO_WPForms_Field_File(); |
| 415 |
} |