PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.4.2
JetFormBuilder — Dynamic Blocks Form Builder v1.4.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-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
571 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 $settings['messages'] = jet_form_builder()->msg_router->get_manager( array(
112 'form_id' => $form_id
113 ) );
114
115 $settings = array_merge( $field_data, $settings );
116
117 $result = $this->process_upload( $_FILES, $settings );
118
119 if ( ! $result ) {
120 wp_send_json_error( $settings['messages']['internal'] );
121 }
122
123 wp_send_json_success(
124 array(
125 'files' => $result,
126 'html' => $this->get_result_html( $settings, $result ),
127 'value' => $this->get_result_value( $settings, $result ),
128 'errors' => $this->get_errors_string(),
129 )
130 );
131 }
132
133 /**
134 * Process files upload
135 *
136 * @param boolean $files [description]
137 *
138 * @return [type] [description]
139 */
140 public function process_upload( $files = false, $settings = array() ) {
141
142 $settings = wp_parse_args(
143 $settings,
144 array(
145 'max_size' => wp_max_upload_size(),
146 'max_files' => 1,
147 'insert_attachment' => false,
148 )
149 );
150 $settings['max_files'] = $settings['max_files'] ? $settings['max_files'] : 1;
151
152 $insert_attachment = filter_var( $settings['insert_attachment'], FILTER_VALIDATE_BOOLEAN );
153
154 $files = Tools::sanitize_files( $files );
155
156 if ( empty( $files ) || ! is_array( $files ) ) {
157 return false;
158 }
159
160 if ( count( $files ) > $settings['max_files'] ) {
161 wp_send_json_error( $settings['messages']['upload_max_files'] );
162 }
163
164 $result = array();
165 $index = 0;
166
167 foreach ( $files as $file ) {
168
169 if ( ! $file['size'] > $settings['max_size'] ) {
170 wp_send_json_error( $settings['messages']['upload_max_size'] );
171 }
172
173 if ( ! empty( $settings['mime_types'] ) && ! in_array( $file['type'], $settings['mime_types'] ) ) {
174 wp_send_json_error( $settings['messages']['upload_mime_types'] );
175 }
176
177 $result[] = $this->upload_file( $file, $insert_attachment );
178
179 }
180
181 return $result;
182
183 }
184
185 /**
186 * Upload file
187 *
188 * @return [type] [description]
189 */
190 public function upload_file( $file = array(), $insert_attachment = false ) {
191
192 $result = array();
193
194 if ( ! function_exists( 'wp_handle_upload' ) ) {
195 include_once ABSPATH . 'wp-admin/includes/file.php';
196 include_once ABSPATH . 'wp-admin/includes/media.php';
197 }
198
199 add_filter( 'upload_dir', array( $this, 'apply_upload_dir' ) );
200
201 $upload = wp_handle_upload(
202 $file,
203 array( 'test_form' => false )
204 );
205
206 if ( empty( $upload['error'] ) && $insert_attachment ) {
207
208 $filepath = $upload['file'];
209 $attachment = wp_insert_attachment(
210 array(
211 'guid' => $upload['url'],
212 'post_mime_type' => $upload['type'],
213 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filepath ) ),
214 'post_content' => '',
215 'post_status' => 'publish',
216 ),
217 $filepath,
218 0,
219 true
220 );
221
222 if ( ! is_wp_error( $attachment ) ) {
223 $metadata = wp_generate_attachment_metadata( $attachment, $filepath );
224 wp_update_attachment_metadata( $attachment, $metadata );
225 } else {
226 $this->errors[] = $attachment->get_error_message();
227 }
228
229 $upload['attachment'] = $attachment;
230
231 } elseif ( ! empty( $upload['error'] ) ) {
232 $this->errors[] = $upload['error'];
233 }
234
235 remove_filter( 'upload_dir', array( $this, 'apply_upload_dir' ) );
236
237 return $upload;
238
239 }
240
241 /**
242 * Try to get files array from field data
243 *
244 * @param array $field [description]
245 * @param string $format [description]
246 *
247 * @return [type] [description]
248 */
249 public function get_files_from_field( $field = array(), $format = 'url' ) {
250
251 $files = array();
252 $value = ! empty( $field['default'] ) ? $field['default'] : array();
253
254 if ( ! is_array( $value ) ) {
255 if ( 'both' !== $format ) {
256 $value = explode( ',', str_replace( ', ', ',', $value ) );
257 } else {
258 if ( false !== strpos( $value, '{' ) ) {
259 $value = json_decode( wp_unslash( $value ), true );
260 } else {
261 return $files;
262 }
263 }
264 }
265
266 if ( 'both' === $format ) {
267 $value = isset( $value['id'] ) ? array( $value ) : $value;
268 }
269
270 foreach ( $value as $val ) {
271 switch ( $format ) {
272 case 'id':
273 $files[] = array(
274 'url' => wp_get_attachment_url( $val ),
275 'attachment' => $val,
276 );
277 break;
278
279 case 'url':
280 $files[] = array(
281 'url' => $val,
282 );
283 break;
284
285 case 'both':
286 if ( is_array( $val ) && isset( $val['url'] ) && isset( $val['id'] ) ) {
287 $files[] = array(
288 'url' => $val['url'],
289 'attachment' => $val['id'],
290 );
291 }
292 break;
293 }
294 }
295
296 return $files;
297 }
298
299 /**
300 * Returns formatted HTML result
301 *
302 * @return [type] [description]
303 */
304 public function get_result_html( $field = array(), $files = array() ) {
305
306 if ( ! empty( $field['insert_attachment'] ) ) {
307 $result_format = ! empty( $field['value_format'] ) ? $field['value_format'] : 'url';
308 } else {
309 $result_format = 'url';
310 }
311
312 if ( empty( $files ) ) {
313 $files = $this->get_files_from_field( $field, $result_format );
314 }
315
316 if ( empty( $files ) ) {
317 return '';
318 }
319
320 $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>';
321
322 $result = '';
323
324 foreach ( $files as $file ) {
325
326 if ( ! empty( $file['attachment'] ) && ! is_wp_error( $file['attachment'] ) ) {
327 $attachment = $file['attachment'];
328 } else {
329 $attachment = 0;
330 }
331
332 $result .= sprintf(
333 $format,
334 $file['url'],
335 $attachment,
336 $result_format,
337 apply_filters( 'jet-form-builder/file-upload/custom-html', '', $file, $field )
338 );
339
340 }
341
342 return $result;
343
344 }
345
346 public function get_loader() {
347 return '<div class="jet-form-builder-file-upload__loader">' . apply_filters(
348 'jet-form-builder/file-upload/loader',
349 '<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>'
350 ) . '</div>';
351 }
352
353 /**
354 * Returns formatted result array
355 *
356 * @param array $field [description]
357 * @param array $files [description]
358 *
359 * @return [type] [description]
360 */
361 public function get_result_value( $field = array(), $files = array() ) {
362
363 if ( ! empty( $field['insert_attachment'] ) ) {
364 $format = ! empty( $field['value_format'] ) ? $field['value_format'] : 'url';
365 } else {
366 $format = 'url';
367 }
368
369 if ( empty( $files ) ) {
370 $files = $this->get_files_from_field( $field, $format );
371 }
372
373 if ( empty( $files ) ) {
374 return '';
375 }
376
377 $limit = ! empty( $field['max_files'] ) ? absint( $field['max_files'] ) : 1;
378 $limit = $limit ? $limit : 1;
379 $result = array();
380
381 foreach ( $files as $file ) {
382
383 if ( isset( $file['attachment'] ) && ! is_wp_error( $file['attachment'] ) ) {
384 $id = $file['attachment'];
385 } else {
386 $id = false;
387 }
388
389 $url = ! empty( $file['url'] ) ? $file['url'] : false;
390
391 switch ( $format ) {
392 case 'id':
393 if ( 1 < $limit ) {
394 $result[] = $id;
395 } else {
396 $result = $id;
397 }
398 break;
399
400 case 'url':
401 if ( 1 < $limit ) {
402 $result[] = $url;
403 } else {
404 $result = $url;
405 }
406 break;
407
408 case 'both':
409 if ( $url && $id ) {
410 if ( 1 < $limit ) {
411 $result[] = array(
412 'id' => $id,
413 'url' => $url,
414 );
415 } else {
416 $result = array(
417 'id' => $id,
418 'url' => $url,
419 );
420 }
421 }
422 break;
423 }
424 }
425
426 return is_array( $result ) ? array_filter( $result ) : $result;
427
428 }
429
430 /**
431 * Returns stringified uploading errors
432 *
433 * @return string
434 */
435 public function get_errors_string() {
436
437 if ( empty( $this->errors ) ) {
438 return null;
439 }
440
441 if ( 1 === count( $this->errors ) ) {
442 return $this->errors[0];
443 } else {
444
445 $result = '';
446
447 foreach ( $this->errors as $error ) {
448 $result .= '- ' . $error . '<br>';
449 }
450
451 return $result;
452
453 }
454
455 }
456
457 /**
458 * Resturns max upload size based on field arguments
459 *
460 * @param array $args [description]
461 *
462 * @return [type] [description]
463 */
464 public function get_max_size_for_field( $args = array() ) {
465
466 $max_size = wp_max_upload_size();
467 $field_max_size = $max_size;
468
469 if ( ! empty( $args['max_size'] ) ) {
470
471 $field_max_size = intval( floatval( $args['max_size'] ) * MB_IN_BYTES );
472
473 if ( $field_max_size > $max_size ) {
474 $field_max_size = $max_size;
475 }
476 }
477
478 return $field_max_size;
479
480 }
481
482
483 /**
484 * Returns upload subdirectory
485 *
486 * @return [type] [description]
487 */
488 public function get_upload_dir() {
489
490 $user_id = get_current_user_id();
491 $user_dir_name = $user_id ? $user_id : 'guest';
492 $user_dir_name = apply_filters( 'jet-form-builder/file-upload/user-dir-name', $user_dir_name );
493
494 return $this->upload_base() . '/' . $user_dir_name;
495 }
496
497 /**
498 * Returns upload base directory
499 *
500 * @return [type] [description]
501 */
502 public function upload_base() {
503 return apply_filters( 'jet-form-builder/file-upload/dir', 'jet-form-builder' );
504 }
505
506 /**
507 * Change upload directory for JetEngine uploads
508 *
509 * @param [type] $pathdata [description]
510 *
511 * @return [type] [description]
512 */
513 public function apply_upload_dir( $pathdata ) {
514
515 $dir = $this->get_upload_dir();
516
517 if ( empty( $pathdata['subdir'] ) ) {
518 $pathdata['path'] = $pathdata['path'] . '/' . $dir;
519 $pathdata['url'] = $pathdata['url'] . '/' . $dir;
520 $pathdata['subdir'] = '/' . $dir;
521 } else {
522 $new_subdir = '/' . $dir . $pathdata['subdir'];
523 $pathdata['path'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] );
524 $pathdata['url'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] );
525 $pathdata['subdir'] = $new_subdir;
526 }
527
528 return $pathdata;
529
530 }
531
532 /**
533 * Register form-specific assets
534 *
535 * @return void
536 */
537 public function enqueue_scripts() {
538 wp_enqueue_script( 'jet-form-builder-sortable' );
539 wp_enqueue_script( 'jet-form-builder-file-upload' );
540
541 $messages = wp_json_encode( jet_form_builder()->msg_router->get_manager()->get_messages() );
542 $form_id = (int) Live_Form::instance()->form_id;
543
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, $popup_data = array() ) {
561 ob_start();
562 jet_engine()->frontend->frontend_scripts();
563 $this->enqueue_scripts();
564 wp_scripts()->done[] = 'jet-form-builder-frontend-forms';
565 wp_scripts()->print_scripts( 'jet-form-builder-file-upload' );
566
567 return $content . ob_get_clean();
568 }
569
570 }
571