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