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