PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.3.2
JetFormBuilder — Dynamic Blocks Form Builder v1.3.2
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 4 years ago addons 4 years ago admin 4 years ago blocks 4 years ago classes 4 years ago compatibility 4 years ago dev-mode 4 years ago exceptions 4 years ago form-actions 4 years ago form-messages 4 years ago form-patterns 4 years ago form-response 4 years ago gateways 4 years ago generators 4 years ago integrations 4 years ago presets 4 years ago request 4 years ago shortcodes 4 years ago widgets 4 years ago autoloader.php 4 years ago file-upload.php 4 years ago form-handler.php 4 years ago form-manager.php 4 years ago live-form.php 4 years ago plugin.php 4 years ago post-type.php 4 years ago
file-upload.php
573 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 // If this file is called directly, abort.
9 if ( ! defined( 'WPINC' ) ) {
10 die;
11 }
12
13 /**
14 * @method static File_Upload instance()
15 *
16 * Class description
17 *
18 * @package package_name
19 * @author Cherry Team
20 * @license GPL-2.0+
21 */
22 class File_Upload {
23
24 use Instance_Trait;
25
26 private $nonce_key = 'jet-form-builder-file-upload-nonce-key';
27 private $action = 'jet-form-builder-upload-file';
28 private $errors = array();
29
30 public function __construct() {
31 add_action( 'wp_ajax_' . $this->action, array( $this, 'ajax_file_upload' ) );
32 add_action( 'wp_ajax_nopriv_' . $this->action, array( $this, 'ajax_file_upload' ) );
33 }
34
35
36 /**
37 * Returns data arguments for files wrapper
38 */
39 public function get_files_data_args( $args ) {
40
41 $data_args = array(
42 'max_files' => 1,
43 'insert_attachment' => false,
44 'value_format' => 'url',
45 );
46
47 foreach ( $data_args as $key => $value ) {
48 $data_args[ $key ] = ! empty( $args[ $key ] ) ? $args[ $key ] : $value;
49 }
50
51 return sprintf( ' data-args="%s"', htmlspecialchars( wp_json_encode( $data_args ) ) );
52 }
53
54 /**
55 * Ajax callback for uploading files
56 *
57 * @return [type] [description]
58 */
59 public function ajax_file_upload() {
60
61 $nonce = sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) );
62 $form_id = absint( wp_unslash( $_POST['form_id'] ?? 0 ) );
63 $field = sanitize_text_field( wp_unslash( $_POST['field'] ?? '' ) );
64
65 if ( ! $nonce || ! wp_verify_nonce( $nonce, $this->nonce_key ) ) {
66 wp_send_json_error( __( 'You not allowed to do this', 'jet-form-builder' ) );
67 }
68
69 if ( ! $form_id || ! $field ) {
70 wp_send_json_error( __( 'Required parameters not found in request', 'jet-form-builder' ) );
71 }
72
73 $form_data = Plugin::instance()->form->get_only_form_fields( $form_id );
74
75 if ( ! $form_data ) {
76 wp_send_json_error( __( 'Form data not found', 'jet-form-builder' ) );
77 }
78
79 $field_data = null;
80
81 foreach ( $form_data as $item ) {
82 if ( ! empty( $item['attrs']['name'] ) && $item['attrs']['name'] === $field ) {
83 $field_data = $item['attrs'];
84 break;
85 }
86 }
87
88 if ( ! $field_data ) {
89 wp_send_json_error( __( 'Requested field not found', 'jet-form-builder' ) );
90 }
91
92 $cap = ! empty( $field_data['allowed_user_cap'] ) ? $field_data['allowed_user_cap'] : 'upload_files';
93
94 if ( 'any_user' !== $cap && ! is_user_logged_in() ) {
95 wp_send_json_error( __( 'You are not allowed to upload files', 'jet-form-builder' ) );
96 }
97
98 if ( ! in_array( $cap, array( 'all', 'any_user' ) ) && ! current_user_can( $cap ) ) {
99 wp_send_json_error( __( 'You are not allowed to upload files', 'jet-form-builder' ) );
100 }
101
102 // Prevent non logged-in users insert attachment
103 if ( ! is_user_logged_in() ) {
104 $field_data['insert_attachment'] = false;
105 }
106
107 $settings = array(
108 'max_size' => $this->get_max_size_for_field( $field_data ),
109 );
110
111 $message_builder = Plugin::instance()->form_handler->get_message_builder( $form_id );
112 $settings['messages'] = $message_builder->manager->get_messages();
113
114 $settings = array_merge( $field_data, $settings );
115
116 $result = $this->process_upload( $_FILES, $settings );
117
118 if ( ! $result ) {
119 wp_send_json_error( __( 'Internal error. Plaese check uploaded files and try again.', 'jet-form-builder' ) );
120 }
121
122 wp_send_json_success(
123 array(
124 'files' => $result,
125 'html' => $this->get_result_html( $settings, $result ),
126 'value' => $this->get_result_value( $settings, $result ),
127 'errors' => $this->get_errors_string(),
128 )
129 );
130 }
131
132 /**
133 * Process files upload
134 *
135 * @param boolean $files [description]
136 *
137 * @return [type] [description]
138 */
139 public function process_upload( $files = false, $settings = array() ) {
140
141 $settings = wp_parse_args(
142 $settings,
143 array(
144 'max_size' => wp_max_upload_size(),
145 'max_files' => 1,
146 'insert_attachment' => false,
147 )
148 );
149 $settings['max_files'] = $settings['max_files'] ? $settings['max_files'] : 1;
150
151 $insert_attachment = filter_var( $settings['insert_attachment'], FILTER_VALIDATE_BOOLEAN );
152
153 $files = Tools::sanitize_files( $files );
154
155 if ( empty( $files ) || ! is_array( $files ) ) {
156 return false;
157 }
158
159 if ( count( $files ) > $settings['max_files'] ) {
160 wp_send_json_error( $settings['messages']['upload_max_files'] );
161 }
162
163 $result = array();
164 $index = 0;
165
166 foreach ( $files as $file ) {
167
168 if ( ! $file['size'] > $settings['max_size'] ) {
169 wp_send_json_error( $settings['messages']['upload_max_size'] );
170 }
171
172 if ( ! empty( $settings['mime_types'] ) && ! in_array( $file['type'], $settings['mime_types'] ) ) {
173 wp_send_json_error( $settings['messages']['upload_mime_types'] );
174 }
175
176 $result[] = $this->upload_file( $file, $insert_attachment );
177
178 }
179
180 return $result;
181
182 }
183
184 /**
185 * Upload file
186 *
187 * @return [type] [description]
188 */
189 public function upload_file( $file = array(), $insert_attachment = false ) {
190
191 $result = array();
192
193 if ( ! function_exists( 'wp_handle_upload' ) ) {
194 include_once ABSPATH . 'wp-admin/includes/file.php';
195 include_once ABSPATH . 'wp-admin/includes/media.php';
196 }
197
198 add_filter( 'upload_dir', array( $this, 'apply_upload_dir' ) );
199
200 $upload = wp_handle_upload(
201 $file,
202 array( 'test_form' => false )
203 );
204
205 if ( empty( $upload['error'] ) && $insert_attachment ) {
206
207 $filepath = $upload['file'];
208 $attachment = wp_insert_attachment(
209 array(
210 'guid' => $upload['url'],
211 'post_mime_type' => $upload['type'],
212 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filepath ) ),
213 'post_content' => '',
214 'post_status' => 'publish',
215 ),
216 $filepath,
217 0,
218 true
219 );
220
221 if ( ! is_wp_error( $attachment ) ) {
222 $metadata = wp_generate_attachment_metadata( $attachment, $filepath );
223 wp_update_attachment_metadata( $attachment, $metadata );
224 } else {
225 $this->errors[] = $attachment->get_error_message();
226 }
227
228 $upload['attachment'] = $attachment;
229
230 } elseif ( ! empty( $upload['error'] ) ) {
231 $this->errors[] = $upload['error'];
232 }
233
234 remove_filter( 'upload_dir', array( $this, 'apply_upload_dir' ) );
235
236 return $upload;
237
238 }
239
240 /**
241 * Try to get files array from field data
242 *
243 * @param array $field [description]
244 * @param string $format [description]
245 *
246 * @return [type] [description]
247 */
248 public function get_files_from_field( $field = array(), $format = 'url' ) {
249
250 $files = array();
251 $value = ! empty( $field['default'] ) ? $field['default'] : array();
252
253 if ( ! is_array( $value ) ) {
254 if ( 'both' !== $format ) {
255 $value = explode( ',', str_replace( ', ', ',', $value ) );
256 } else {
257 if ( false !== strpos( $value, '{' ) ) {
258 $value = json_decode( wp_unslash( $value ), true );
259 } else {
260 return $files;
261 }
262 }
263 }
264
265 if ( 'both' === $format ) {
266 $value = isset( $value['id'] ) ? array( $value ) : $value;
267 }
268
269 foreach ( $value as $val ) {
270 switch ( $format ) {
271 case 'id':
272 $files[] = array(
273 'url' => wp_get_attachment_url( $val ),
274 'attachment' => $val,
275 );
276 break;
277
278 case 'url':
279 $files[] = array(
280 'url' => $val,
281 );
282 break;
283
284 case 'both':
285 if ( is_array( $val ) && isset( $val['url'] ) && isset( $val['id'] ) ) {
286 $files[] = array(
287 'url' => $val['url'],
288 'attachment' => $val['id'],
289 );
290 }
291 break;
292 }
293 }
294
295 return $files;
296 }
297
298 /**
299 * Returns formatted HTML result
300 *
301 * @return [type] [description]
302 */
303 public function get_result_html( $field = array(), $files = array() ) {
304
305 if ( ! empty( $field['insert_attachment'] ) ) {
306 $result_format = ! empty( $field['value_format'] ) ? $field['value_format'] : 'url';
307 } else {
308 $result_format = 'url';
309 }
310
311 if ( empty( $files ) ) {
312 $files = $this->get_files_from_field( $field, $result_format );
313 }
314
315 if ( empty( $files ) ) {
316 return '';
317 }
318
319 $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>%4$s</div>';
320
321 $result = '';
322
323 foreach ( $files as $file ) {
324
325 if ( ! empty( $file['attachment'] ) && ! is_wp_error( $file['attachment'] ) ) {
326 $attachment = $file['attachment'];
327 } else {
328 $attachment = 0;
329 }
330
331 $result .= sprintf(
332 $format,
333 $file['url'],
334 $attachment,
335 $result_format,
336 apply_filters( 'jet-form-builder/file-upload/custom-html', '', $file, $field )
337 );
338
339 }
340
341 return $result;
342
343 }
344
345 public function get_loader() {
346 return '<div class="jet-form-builder-file-upload__loader">' . apply_filters(
347 'jet-form-builder/file-upload/loader',
348 '<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>'
349 ) . '</div>';
350 }
351
352 /**
353 * Returns formatted result array
354 *
355 * @param array $field [description]
356 * @param array $files [description]
357 *
358 * @return [type] [description]
359 */
360 public function get_result_value( $field = array(), $files = array() ) {
361
362 if ( ! empty( $field['insert_attachment'] ) ) {
363 $format = ! empty( $field['value_format'] ) ? $field['value_format'] : 'url';
364 } else {
365 $format = 'url';
366 }
367
368 if ( empty( $files ) ) {
369 $files = $this->get_files_from_field( $field, $format );
370 }
371
372 if ( empty( $files ) ) {
373 return '';
374 }
375
376 $limit = ! empty( $field['max_files'] ) ? absint( $field['max_files'] ) : 1;
377 $limit = $limit ? $limit : 1;
378 $result = array();
379
380 foreach ( $files as $file ) {
381
382 if ( isset( $file['attachment'] ) && ! is_wp_error( $file['attachment'] ) ) {
383 $id = $file['attachment'];
384 } else {
385 $id = false;
386 }
387
388 $url = ! empty( $file['url'] ) ? $file['url'] : false;
389
390 switch ( $format ) {
391 case 'id':
392 if ( 1 < $limit ) {
393 $result[] = $id;
394 } else {
395 $result = $id;
396 }
397 break;
398
399 case 'url':
400 if ( 1 < $limit ) {
401 $result[] = $url;
402 } else {
403 $result = $url;
404 }
405 break;
406
407 case 'both':
408 if ( $url && $id ) {
409 if ( 1 < $limit ) {
410 $result[] = array(
411 'id' => $id,
412 'url' => $url,
413 );
414 } else {
415 $result = array(
416 'id' => $id,
417 'url' => $url,
418 );
419 }
420 }
421 break;
422 }
423 }
424
425 return is_array( $result ) ? array_filter( $result ) : $result;
426
427 }
428
429 /**
430 * Returns stringified uploading errors
431 *
432 * @return string
433 */
434 public function get_errors_string() {
435
436 if ( empty( $this->errors ) ) {
437 return null;
438 }
439
440 if ( 1 === count( $this->errors ) ) {
441 return $this->errors[0];
442 } else {
443
444 $result = '';
445
446 foreach ( $this->errors as $error ) {
447 $result .= '- ' . $error . '<br>';
448 }
449
450 return $result;
451
452 }
453
454 }
455
456 /**
457 * Resturns max upload size based on field arguments
458 *
459 * @param array $args [description]
460 *
461 * @return [type] [description]
462 */
463 public function get_max_size_for_field( $args = array() ) {
464
465 $max_size = wp_max_upload_size();
466 $field_max_size = $max_size;
467
468 if ( ! empty( $args['max_size'] ) ) {
469
470 $field_max_size = intval( floatval( $args['max_size'] ) * MB_IN_BYTES );
471
472 if ( $field_max_size > $max_size ) {
473 $field_max_size = $max_size;
474 }
475 }
476
477 return $field_max_size;
478
479 }
480
481
482 /**
483 * Returns upload subdirectory
484 *
485 * @return [type] [description]
486 */
487 public function get_upload_dir() {
488
489 $user_id = get_current_user_id();
490 $user_dir_name = $user_id ? $user_id : 'guest';
491 $user_dir_name = apply_filters( 'jet-form-builder/file-upload/user-dir-name', $user_dir_name );
492
493 return $this->upload_base() . '/' . $user_dir_name;
494 }
495
496 /**
497 * Returns upload base directory
498 *
499 * @return [type] [description]
500 */
501 public function upload_base() {
502 return apply_filters( 'jet-form-builder/file-upload/dir', 'jet-form-builder' );
503 }
504
505 /**
506 * Change upload directory for JetEngine uploads
507 *
508 * @param [type] $pathdata [description]
509 *
510 * @return [type] [description]
511 */
512 public function apply_upload_dir( $pathdata ) {
513
514 $dir = $this->get_upload_dir();
515
516 if ( empty( $pathdata['subdir'] ) ) {
517 $pathdata['path'] = $pathdata['path'] . '/' . $dir;
518 $pathdata['url'] = $pathdata['url'] . '/' . $dir;
519 $pathdata['subdir'] = '/' . $dir;
520 } else {
521 $new_subdir = '/' . $dir . $pathdata['subdir'];
522 $pathdata['path'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] );
523 $pathdata['url'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] );
524 $pathdata['subdir'] = $new_subdir;
525 }
526
527 return $pathdata;
528
529 }
530
531 /**
532 * Register form-specific assets
533 *
534 * @return void
535 */
536 public function enqueue_scripts() {
537 wp_enqueue_script( 'jet-form-builder-sortable' );
538 wp_enqueue_script( 'jet-form-builder-file-upload' );
539
540 $message_builder = Plugin::instance()->form_handler->get_message_builder( Live_Form::instance()->form_id );
541 $messages = $message_builder->manager->get_messages();
542
543 wp_localize_script(
544 'jet-form-builder-file-upload',
545 'JetFormBuilderFileUploadConfig',
546 array(
547 'ajaxurl' => esc_url_raw( admin_url( 'admin-ajax.php' ) ),
548 'action' => $this->action,
549 'nonce' => wp_create_nonce( $this->nonce_key ),
550 'max_upload_size' => wp_max_upload_size(),
551 'errors' => array(
552 'upload_limit' => $messages['upload_max_files'],
553 'file_type' => $messages['upload_mime_types'],
554 'file_size' => $messages['upload_max_size'],
555 'internal' => $messages['internal_error'],
556
557 ),
558 )
559 );
560 }
561
562 public function ensure_media_js( $content, $popup_data = array() ) {
563 ob_start();
564 jet_engine()->frontend->frontend_scripts();
565 $this->enqueue_scripts();
566 wp_scripts()->done[] = 'jet-form-builder-frontend-forms';
567 wp_scripts()->print_scripts( 'jet-form-builder-file-upload' );
568
569 return $content . ob_get_clean();
570 }
571
572 }
573