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