PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.2.3
JetFormBuilder — Dynamic Blocks Form Builder v1.2.3
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / file-upload.php
jetformbuilder / includes Last commit date
actions 5 years ago admin 5 years ago blocks 5 years ago classes 5 years ago compatibility 5 years ago dev-mode 5 years ago exceptions 5 years ago form-actions 5 years ago form-messages 5 years ago form-patterns 5 years ago form-response 5 years ago gateways 5 years ago generators 5 years ago integrations 5 years ago license 5 years ago presets 5 years ago request 5 years ago shortcodes 5 years ago widgets 5 years ago autoloader.php 5 years ago file-upload.php 5 years ago form-handler.php 5 years ago form-manager.php 5 years ago live-form.php 5 years ago plugin.php 5 years ago post-type.php 5 years ago
file-upload.php
560 lines
1 <?php
2
3 namespace Jet_Form_Builder;
4
5 use Jet_Form_Builder\Classes\Instance_Trait;
6 use Jet_Form_Builder\Classes\Tools;
7
8 /**
9 * Class description
10 *
11 * @package package_name
12 * @author Cherry Team
13 * @license GPL-2.0+
14 */
15
16 // If this file is called directly, abort.
17 if ( ! defined( 'WPINC' ) ) {
18 die;
19 }
20
21 class File_Upload {
22
23 use Instance_Trait;
24
25 private $nonce_key = 'jet-form-builder-file-upload-nonce-key';
26 private $action = 'jet-form-builder-upload-file';
27 private $errors = array();
28
29 public function __construct() {
30 add_action( 'wp_ajax_' . $this->action, array( $this, 'ajax_file_upload' ) );
31 add_action( 'wp_ajax_nopriv_' . $this->action, array( $this, 'ajax_file_upload' ) );
32 }
33
34
35 /**
36 * Returns data arguments for files wrapper
37 */
38 public function get_files_data_args( $args ) {
39
40 $data_args = array(
41 'max_files' => 1,
42 'insert_attachment' => false,
43 'value_format' => 'url',
44 );
45
46 foreach ( $data_args as $key => $value ) {
47 $data_args[ $key ] = ! empty( $args[ $key ] ) ? $args[ $key ] : $value;
48 }
49
50 return sprintf( ' data-args="%s"', htmlspecialchars( json_encode( $data_args ) ) );
51 }
52
53 /**
54 * Ajax callback for uploading files
55 *
56 * @return [type] [description]
57 */
58 public function ajax_file_upload() {
59
60 $nonce = ! empty( $_REQUEST['nonce'] ) ? $_REQUEST['nonce'] : false;
61 $form_id = ! empty( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : false;
62 $field = ! empty( $_REQUEST['field'] ) ? sanitize_key( $_REQUEST['field'] ) : false;
63
64 if ( ! $nonce || ! wp_verify_nonce( $nonce, $this->nonce_key ) ) {
65 wp_send_json_error( __( 'You not allowed to do this', 'jet-form-builder' ) );
66 }
67
68 if ( ! $form_id || ! $field ) {
69 wp_send_json_error( __( 'Required parameters not found in request', 'jet-form-builder' ) );
70 }
71
72 $form_data = Plugin::instance()->form->get_only_form_fields( $form_id );
73
74 if ( ! $form_data ) {
75 wp_send_json_error( __( 'Form data not found', 'jet-form-builder' ) );
76 }
77
78 $field_data = null;
79
80 foreach ( $form_data as $item ) {
81 if ( ! empty( $item['attrs']['name'] ) && $item['attrs']['name'] === $field ) {
82 $field_data = $item['attrs'];
83 break;
84 }
85 }
86
87 if ( ! $field_data ) {
88 wp_send_json_error( __( 'Requested field not found', 'jet-form-builder' ) );
89 }
90
91 $cap = ! empty( $field_data['allowed_user_cap'] ) ? $field_data['allowed_user_cap'] : 'upload_files';
92
93 if ( 'any_user' !== $cap && ! is_user_logged_in() ) {
94 wp_send_json_error( __( 'You are not allowed to upload files', 'jet-form-builder' ) );
95 }
96
97 if ( ! in_array( $cap, array( 'all', 'any_user' ) ) && ! current_user_can( $cap ) ) {
98 wp_send_json_error( __( 'You are not allowed to upload files', 'jet-form-builder' ) );
99 }
100
101 // Prevent non logged-in users insert attachment
102 if ( ! is_user_logged_in() ) {
103 $field_data['insert_attachment'] = false;
104 }
105
106 $settings = array(
107 'max_size' => $this->get_max_size_for_field( $field_data ),
108 );
109
110 $message_builder = Plugin::instance()->form_handler->get_message_builder( $form_id );
111 $settings['messages'] = $message_builder->manager->get_messages();
112
113 $settings = array_merge( $field_data, $settings );
114
115 $result = $this->process_upload( $_FILES, $settings );
116
117 if ( ! $result ) {
118 wp_send_json_error( __( 'Internal error. Plaese check uploaded files and try again.', 'jet-form-builder' ) );
119 }
120
121 wp_send_json_success( array(
122 'files' => $result,
123 'html' => $this->get_result_html( $settings, $result ),
124 'value' => $this->get_result_value( $settings, $result ),
125 'errors' => $this->get_errors_string(),
126 ) );
127
128 }
129
130 /**
131 * Process files upload
132 *
133 * @param boolean $files [description]
134 *
135 * @return [type] [description]
136 */
137 public function process_upload( $files = false, $settings = array() ) {
138
139 $settings = wp_parse_args( $settings, array(
140 'max_size' => wp_max_upload_size(),
141 'max_files' => 1,
142 'insert_attachment' => false,
143 ) );
144 $settings['max_files'] = $settings['max_files'] ? $settings['max_files'] : 1;
145
146 $insert_attachment = filter_var( $settings['insert_attachment'], FILTER_VALIDATE_BOOLEAN );
147
148 $files = Tools::sanitize_files( $files );
149
150 if ( empty( $files ) || ! is_array( $files ) ) {
151 return false;
152 }
153
154 if ( count( $files ) > $settings['max_files'] ) {
155 wp_send_json_error( $settings['messages']['upload_max_files'] );
156 }
157
158 $result = array();
159 $index = 0;
160
161 foreach ( $files as $file ) {
162
163 if ( ! $file['size'] > $settings['max_size'] ) {
164 wp_send_json_error( $settings['messages']['upload_max_size'] );
165 }
166
167 if ( ! empty( $settings['mime_types'] ) && ! in_array( $file['type'], $settings['mime_types'] ) ) {
168 wp_send_json_error( $settings['messages']['upload_mime_types'] );
169 }
170
171 $result[] = $this->upload_file( $file, $insert_attachment );
172
173 }
174
175 return $result;
176
177 }
178
179 /**
180 * Upload file
181 *
182 * @return [type] [description]
183 */
184 public function upload_file( $file = array(), $insert_attachment = false ) {
185
186 $result = array();
187
188 if ( ! function_exists( 'wp_handle_upload' ) ) {
189 include_once ABSPATH . 'wp-admin/includes/file.php';
190 include_once ABSPATH . 'wp-admin/includes/media.php';
191 }
192
193 add_filter( 'upload_dir', array( $this, 'apply_upload_dir' ) );
194
195 $upload = wp_handle_upload(
196 $file,
197 array( 'test_form' => false )
198 );
199
200 if ( empty( $upload['error'] ) && $insert_attachment ) {
201
202 $filepath = $upload['file'];
203 $attachment = wp_insert_attachment(
204 array(
205 'guid' => $upload['url'],
206 'post_mime_type' => $upload['type'],
207 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filepath ) ),
208 'post_content' => '',
209 'post_status' => 'publish'
210 ),
211 $filepath,
212 0,
213 true
214 );
215
216 if ( ! is_wp_error( $attachment ) ) {
217 $metadata = wp_generate_attachment_metadata( $attachment, $filepath );
218 wp_update_attachment_metadata( $attachment, $metadata );
219 } else {
220 $this->errors[] = $attachment->get_error_message();
221 }
222
223 $upload['attachment'] = $attachment;
224
225 } elseif ( ! empty( $upload['error'] ) ) {
226 $this->errors[] = $upload['error'];
227 }
228
229
230 remove_filter( 'upload_dir', array( $this, 'apply_upload_dir' ) );
231
232 return $upload;
233
234 }
235
236 /**
237 * Try to get files array from field data
238 *
239 * @param array $field [description]
240 * @param string $format [description]
241 *
242 * @return [type] [description]
243 */
244 public function get_files_from_field( $field = array(), $format = 'url' ) {
245
246 $files = array();
247 $value = ! empty( $field['default'] ) ? $field['default'] : array();
248
249 if ( ! is_array( $value ) ) {
250 if ( 'both' !== $format ) {
251 $value = explode( ',', str_replace( ', ', ',', $value ) );
252 } else {
253 if ( false !== strpos( $value, '{' ) ) {
254 $value = json_decode( $value, true );
255 } else {
256 return $files;
257 }
258 }
259 }
260
261 if ( 'both' === $format ) {
262 $value = isset( $value['id'] ) ? array( $value ) : $value;
263 }
264
265 foreach ( $value as $val ) {
266 switch ( $format ) {
267 case 'id':
268 $files[] = array(
269 'url' => wp_get_attachment_url( $val ),
270 'attachment' => $val,
271 );
272 break;
273
274 case 'url':
275 $files[] = array(
276 'url' => $val,
277 );
278 break;
279
280 case 'both':
281 if ( is_array( $val ) && isset( $val['url'] ) && isset( $val['id'] ) ) {
282 $files[] = array(
283 'url' => $val['url'],
284 'attachment' => $val['id'],
285 );
286 }
287 break;
288 }
289 }
290
291 return $files;
292 }
293
294 /**
295 * Returns formatted HTML result
296 *
297 * @return [type] [description]
298 */
299 public function get_result_html( $field = array(), $files = array() ) {
300
301 if ( ! empty( $field['insert_attachment'] ) ) {
302 $result_format = ! empty( $field['value_format'] ) ? $field['value_format'] : 'url';
303 } else {
304 $result_format = 'url';
305 }
306
307
308 if ( empty( $files ) ) {
309 $files = $this->get_files_from_field( $field, $result_format );
310 }
311
312 if ( empty( $files ) ) {
313 return;
314 }
315
316 $format = '<div class="jet-form-builder-file-upload__file" data-file="%1$s" data-id="%2$s" data-format="%3$s"><img src="%1$s" alt=""><div class="jet-form-builder-file-upload__file-remove"><svg width="22" height="22" viewBox="0 0 14 15" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4.375 7H6.125V12.25H4.375V7ZM7.875 7H9.625V12.25H7.875V7ZM10.5 1.75C10.5 1.51302 10.4134 1.30794 10.2402 1.13477C10.0762 0.961589 9.87109 0.875 9.625 0.875H4.375C4.12891 0.875 3.91927 0.961589 3.74609 1.13477C3.58203 1.30794 3.5 1.51302 3.5 1.75V3.5H0V5.25H0.875V14C0.875 14.237 0.957031 14.4421 1.12109 14.6152C1.29427 14.7884 1.50391 14.875 1.75 14.875H12.25C12.4961 14.875 12.7012 14.7884 12.8652 14.6152C13.0384 14.4421 13.125 14.237 13.125 14V5.25H14V3.5H10.5V1.75ZM5.25 2.625H8.75V3.5H5.25V2.625ZM11.375 5.25V13.125H2.625V5.25H11.375Z"></path></svg></div></div>';
317
318 $result = '';
319
320 foreach ( $files as $file ) {
321
322 if ( ! empty( $file['attachment'] ) && ! is_wp_error( $file['attachment'] ) ) {
323 $attachment = $file['attachment'];
324 } else {
325 $attachment = 0;
326 }
327
328 $result .= sprintf( $format, $file['url'], $attachment, $result_format );
329
330 }
331
332 return $result;
333
334 }
335
336 public function get_loader() {
337 return '<div class="jet-form-builder-file-upload__loader">' . apply_filters(
338 'jet-form-builder/file-upload/loader',
339 '<svg xmlns="http://www.w3.org/2000/svg" width="38" height="38" viewBox="0 0 38 38" stroke="#fff"><g fill="none" fill-rule="evenodd"><g transform="translate(1 1)" stroke-width="2"><circle stroke-opacity=".5" cx="18" cy="18" r="18"/><path d="M36 18c0-9.94-8.06-18-18-18" transform="rotate(137.826 18 18)"><animateTransform attributeName="transform" type="rotate" from="0 18 18" to="360 18 18" dur="1s" repeatCount="indefinite"/></path></g></g></svg>'
340 ) . '</div>';
341 }
342
343 /**
344 * Returns formatted result array
345 *
346 * @param array $field [description]
347 * @param array $files [description]
348 *
349 * @return [type] [description]
350 */
351 public function get_result_value( $field = array(), $files = array() ) {
352
353 if ( ! empty( $field['insert_attachment'] ) ) {
354 $format = ! empty( $field['value_format'] ) ? $field['value_format'] : 'url';
355 } else {
356 $format = 'url';
357 }
358
359 if ( empty( $files ) ) {
360 $files = $this->get_files_from_field( $field, $format );
361 }
362
363 if ( empty( $files ) ) {
364 return '';
365 }
366
367 $limit = ! empty( $field['max_files'] ) ? absint( $field['max_files'] ) : 1;
368 $limit = $limit ? $limit : 1;
369 $result = array();
370
371 foreach ( $files as $file ) {
372
373 if ( isset( $file['attachment'] ) && ! is_wp_error( $file['attachment'] ) ) {
374 $id = $file['attachment'];
375 } else {
376 $id = false;
377 }
378
379 $url = ! empty( $file['url'] ) ? $file['url'] : false;
380
381 switch ( $format ) {
382 case 'id':
383 if ( 1 < $limit ) {
384 $result[] = $id;
385 } else {
386 $result = $id;
387 }
388 break;
389
390 case 'url':
391 if ( 1 < $limit ) {
392 $result[] = $url;
393 } else {
394 $result = $url;
395 }
396 break;
397
398 case 'both':
399 if ( $url && $id ) {
400 if ( 1 < $limit ) {
401 $result[] = array(
402 'id' => $id,
403 'url' => $url,
404 );
405 } else {
406 $result = array(
407 'id' => $id,
408 'url' => $url,
409 );
410 }
411 }
412 break;
413 }
414 }
415
416 return is_array( $result ) ? array_filter( $result ) : $result;
417
418 }
419
420 /**
421 * Returns stringified uploading errors
422 *
423 * @return string
424 */
425 public function get_errors_string() {
426
427 if ( empty( $this->errors ) ) {
428 return null;
429 }
430
431 if ( 1 === count( $this->errors ) ) {
432 return $this->errors[0];
433 } else {
434
435 $result = '';
436
437 foreach ( $this->errors as $error ) {
438 $result .= '- ' . $error . '<br>';
439 }
440
441 return $result;
442
443 }
444
445 }
446
447 /**
448 * Resturns max upload size based on field arguments
449 *
450 * @param array $args [description]
451 *
452 * @return [type] [description]
453 */
454 public function get_max_size_for_field( $args = array() ) {
455
456 $max_size = wp_max_upload_size();
457 $field_max_size = $max_size;
458
459 if ( ! empty( $args['max_size'] ) ) {
460
461 $field_max_size = intval( floatval( $args['max_size'] ) * MB_IN_BYTES );
462
463 if ( $field_max_size > $max_size ) {
464 $field_max_size = $max_size;
465 }
466
467 }
468
469 return $field_max_size;
470
471 }
472
473
474 /**
475 * Returns upload subdirectory
476 *
477 * @return [type] [description]
478 */
479 public function get_upload_dir() {
480
481 $user_id = get_current_user_id();
482 $user_dir_name = $user_id ? $user_id : 'guest';
483 $user_dir_name = apply_filters( 'jet-form-builder/file-upload/user-dir-name', $user_dir_name );
484
485 return $this->upload_base() . '/' . $user_dir_name;
486 }
487
488 /**
489 * Returns upload base directory
490 *
491 * @return [type] [description]
492 */
493 public function upload_base() {
494 return apply_filters( 'jet-form-builder/file-upload/dir', 'jet-form-builder' );
495 }
496
497 /**
498 * Change upload directory for JetEngine uploads
499 *
500 * @param [type] $pathdata [description]
501 *
502 * @return [type] [description]
503 */
504 public function apply_upload_dir( $pathdata ) {
505
506 $dir = $this->get_upload_dir();
507
508 if ( empty( $pathdata['subdir'] ) ) {
509 $pathdata['path'] = $pathdata['path'] . '/' . $dir;
510 $pathdata['url'] = $pathdata['url'] . '/' . $dir;
511 $pathdata['subdir'] = '/' . $dir;
512 } else {
513 $new_subdir = '/' . $dir . $pathdata['subdir'];
514 $pathdata['path'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] );
515 $pathdata['url'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] );
516 $pathdata['subdir'] = $new_subdir;
517 }
518
519 return $pathdata;
520
521 }
522
523 /**
524 * Register form-specific assets
525 *
526 * @return void
527 */
528 public function enqueue_scripts() {
529 wp_enqueue_script( 'jet-form-builder-sortable' );
530 wp_enqueue_script( 'jet-form-builder-file-upload' );
531
532 $message_builder = Plugin::instance()->form_handler->get_message_builder( Live_Form::instance()->form_id );
533 $messages = $message_builder->manager->get_messages();
534
535 wp_localize_script( 'jet-form-builder-file-upload', 'JetFormBuilderFileUploadConfig', array(
536 'ajaxurl' => esc_url( admin_url( 'admin-ajax.php' ) ),
537 'action' => $this->action,
538 'nonce' => wp_create_nonce( $this->nonce_key ),
539 'max_upload_size' => wp_max_upload_size(),
540 'errors' => array(
541 'upload_limit' => $messages['upload_max_files'],
542 'file_type' => $messages['upload_mime_types'],
543 'file_size' => $messages['upload_max_size'],
544 'internal' => $messages['internal_error'],
545
546 ),
547 ) );
548 }
549
550 public function ensure_media_js( $content, $popup_data = array() ) {
551 ob_start();
552 jet_engine()->frontend->frontend_scripts();
553 $this->enqueue_scripts();
554 wp_scripts()->done[] = 'jet-form-builder-frontend-forms';
555 wp_scripts()->print_scripts( 'jet-form-builder-file-upload' );
556
557 return $content . ob_get_clean();
558 }
559
560 }