PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.2.1
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.2.1
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / class-ajax.php

class-ajax.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.2.1, at includes/class-ajax.php

923 lines 30.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The ajax handler class
5 */
6 class WeForms_Ajax {
7
8 public function __construct() {
9
10 // backend requests
11 add_action( 'wp_ajax_weforms_form_list', array( $this, 'get_contact_forms' ) );
12 add_action( 'wp_ajax_weforms_form_names', array( $this, 'get_contact_form_names' ) );
13 add_action( 'wp_ajax_weforms_form_create', array( $this, 'create_form' ) );
14 add_action( 'wp_ajax_weforms_form_delete', array( $this, 'delete_form' ) );
15 add_action( 'wp_ajax_weforms_form_delete_bulk', array( $this, 'delete_form_bulk' ) );
16 add_action( 'wp_ajax_weforms_form_duplicate', array( $this, 'duplicate_form' ) );
17
18 // create from a template
19 add_filter( 'wp_ajax_weforms_contact_form_template', array( $this, 'create_form_from_template' ) );
20
21 // form settings
22 add_action( 'wp_ajax_weforms_save_settings', array( $this, 'save_settings' ) );
23 add_action( 'wp_ajax_weforms_get_settings', array( $this, 'get_settings' ) );
24
25 // import
26 add_action( 'wp_ajax_weforms_import_form', array( $this, 'import_form' ) );
27
28 // form editing
29 add_action( 'wp_ajax_weforms_get_form', array( $this, 'get_form' ) );
30 add_action( 'wp_ajax_wpuf_form_builder_save_form', array( $this, 'save_form' ) );
31
32 // entries
33 add_action( 'wp_ajax_weforms_form_entries', array( $this, 'get_entries' ) );
34
35 add_action( 'wp_ajax_weforms_form_entry_details', array( $this, 'get_entry_detail' ) );
36 add_action( 'wp_ajax_weforms_form_entry_trash', array( $this, 'trash_entry' ) );
37 add_action( 'wp_ajax_weforms_form_entry_trash_bulk', array( $this, 'bulk_delete_entry' ) );
38
39 // frontend requests
40 add_action( 'wp_ajax_weforms_frontend_submit', array( $this, 'handle_frontend_submission' ) );
41 add_action( 'wp_ajax_nopriv_weforms_frontend_submit', array( $this, 'handle_frontend_submission' ) );
42 }
43
44 /**
45 * Administrator validation
46 *
47 * @return void
48 */
49 public function check_admin() {
50 if ( !current_user_can( weforms_form_access_capability() ) ) {
51 wp_send_json_error( __( 'You do not have sufficient permission.', 'weforms' ) );
52 }
53 }
54
55 /**
56 * Get a form to edit
57 *
58 * @return void
59 */
60 public function get_form() {
61
62 $this->check_admin();
63
64 $form_id = isset( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : 0;
65
66 $form = weforms()->form->get( $form_id );
67
68 $data = array(
69 'post' => $form->data,
70 'form_fields' => $form->get_fields(),
71 'settings' => $form->get_settings(),
72 'notifications' => $form->get_notifications(),
73 'integrations' => $form->get_integrations()
74 );
75
76 wp_send_json_success( $data );
77 }
78
79 /**
80 * Save the form
81 *
82 * @return void
83 */
84 public function save_form() {
85 parse_str( $_POST['form_data'], $form_data );
86
87 if ( ! wp_verify_nonce( $form_data['wpuf_form_builder_nonce'], 'wpuf_form_builder_save_form' ) ) {
88 wp_send_json_error( __( 'Unauthorized operation', 'wpuf' ) );
89 }
90
91 if ( empty( $form_data['wpuf_form_id'] ) ) {
92 wp_send_json_error( __( 'Invalid form id', 'wpuf' ) );
93 }
94
95 $form_fields = isset( $_POST['form_fields'] ) ? $_POST['form_fields'] : '';
96 $notifications = isset( $_POST['notifications'] ) ? $_POST['notifications'] : '';
97 $settings = array();
98 $integrations = array();
99
100 if ( isset( $_POST['settings'] ) ) {
101 $settings = (array) json_decode( wp_unslash( $_POST['settings'] ) );
102 } else {
103 $settings = isset( $form_data['wpuf_settings'] ) ? $form_data['wpuf_settings'] : array();
104 }
105
106 if ( isset( $_POST['integrations'] ) ) {
107 $integrations = (array) json_decode( wp_unslash( $_POST['integrations'] ) );
108 }
109
110 $form_fields = wp_unslash( $form_fields );
111 $notifications = wp_unslash( $notifications );
112
113 $form_fields = json_decode( $form_fields, true );
114 $notifications = json_decode( $notifications, true );
115
116 $data = array(
117 'form_id' => absint( $form_data['wpuf_form_id'] ),
118 'post_title' => sanitize_text_field( $form_data['post_title'] ),
119 'form_fields' => $form_fields,
120 'form_settings' => $settings,
121 'form_settings_key' => isset( $form_data['form_settings_key'] ) ? $form_data['form_settings_key'] : '',
122 'notifications' => $notifications,
123 'integrations' => $integrations
124 );
125
126 $form_fields = weforms()->form->save( $data );
127
128 wp_send_json_success( array( 'form_fields' => $form_fields ) );
129 }
130
131 /**
132 * Get all contact forms
133 *
134 * @return void
135 */
136 public function get_contact_forms() {
137 check_ajax_referer( 'weforms' );
138
139 $this->check_admin();
140
141 $args = array(
142 'posts_per_page' => 10,
143 'paged' => isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 1,
144 'order' => 'DESC',
145 'orderby' => 'post_date'
146 );
147
148 $args = apply_filters( 'weforms_ajax_get_contact_forms_args', $args );
149
150 $contact_forms = weforms()->form->get_forms( $args );
151
152 array_map( function($form) {
153 $form->entries = $form->num_form_entries();
154 $form->views = $form->num_form_views();
155 $form->payments = $form->num_form_payments();
156 }, $contact_forms['forms'] );
157
158 $contact_forms = apply_filters( 'weforms_ajax_get_contact_forms', $contact_forms );
159
160 wp_send_json_success( $contact_forms );
161 }
162
163 /**
164 * Get the names of contact forms for generating dropdown
165 *
166 * @return void
167 */
168 public function get_contact_form_names() {
169 check_ajax_referer( 'weforms' );
170
171 $this->check_admin();
172
173 $contact_forms = weforms()->form->all();
174 $response = array();
175
176 foreach ($contact_forms['forms'] as $form) {
177 $response[] = array(
178 'id' => $form->get_id(),
179 'title' => $form->get_name() . ' (#' . $form->get_id() . ')'
180 );
181 }
182
183 wp_send_json_success( $response );
184 }
185
186 /**
187 * Create a form
188 *
189 * @return void
190 */
191 public function create_form() {
192 check_ajax_referer( 'weforms' );
193
194 $this->check_admin();
195
196 $form_name = isset( $_POST['form_name'] ) ? sanitize_text_field( $_POST['form_name'] ) : '';
197
198 if ( empty( $form_name ) ) {
199 wp_send_json_error( __( 'Please provide a form name', 'weforms' ) );
200 }
201
202 $form_id = weforms()->form->create( $form_name );
203
204 if ( is_wp_error( $form_id )) {
205 wp_send_json_error( $form_id->get_error_message() );
206 }
207
208 wp_send_json_success( array(
209 'form_id' => $form_id,
210 'form_name' => $form_name
211 ) );
212 }
213
214 /**
215 * Delete a form
216 *
217 * @return void
218 */
219 public function delete_form() {
220 check_ajax_referer( 'weforms' );
221
222 $this->check_admin();
223
224 $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0;
225
226 if ( ! $form_id ) {
227 wp_send_json_error( __( 'No form id provided!', 'weforms' ) );
228 }
229
230 weforms()->form->delete( $form_id );
231
232 wp_send_json_success();
233 }
234
235 public function delete_form_bulk() {
236 check_ajax_referer( 'weforms' );
237
238 $this->check_admin();
239
240 $form_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : array();
241
242 if ( ! $form_ids ) {
243 wp_send_json_error( __( 'No form ids provided!', 'weforms' ) );
244 }
245
246 foreach ($form_ids as $form_id) {
247 weforms()->form->delete( $form_id );
248 }
249
250 wp_send_json_success();
251 }
252
253 /**
254 * Duplicate a form
255 *
256 * @return voiud
257 */
258 public function duplicate_form() {
259 check_ajax_referer( 'weforms' );
260
261 $this->check_admin();
262
263 $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0;
264
265 $form = weforms()->form->duplicate( $form_id );
266
267 wp_send_json_success( $form );
268 }
269
270 /**
271 * Create form from a template
272 *
273 * @return void
274 */
275 public function create_form_from_template() {
276 check_ajax_referer( 'weforms' );
277
278 $template = isset( $_REQUEST['template'] ) ? sanitize_text_field( $_REQUEST['template'] ) : '';
279
280 $form_id = weforms()->templates->create( $template );
281
282 if ( is_wp_error( $form_id ) ) {
283 wp_send_json_error( __( 'Could not create the form', 'weforms' ) );
284 }
285
286 wp_send_json_success( array(
287 'id' => $form_id
288 ) );
289 }
290
291 /**
292 * Save the weForms settings
293 *
294 * @return void
295 */
296 public function save_settings() {
297 check_ajax_referer( 'weforms' );
298
299 $this->check_admin();
300
301 $requires_wpuf_update = false;
302 $wpuf_update_array = array();
303 $settings = (array) json_decode( wp_unslash( $_POST['settings'] ) );
304
305 update_option( 'weforms_settings', $settings );
306
307 // wpuf settings sync
308 if ( isset( $settings['gmap_api'] ) ) {
309 $requires_wpuf_update = true;
310 $wpuf_update_array['gmap_api_key'] = $settings['gmap_api'];
311 }
312
313 if ( isset( $settings['recaptcha'] ) ) {
314 $requires_wpuf_update = true;
315 $wpuf_update_array['recaptcha_public'] = $settings['recaptcha']->key;
316 $wpuf_update_array['recaptcha_private'] = $settings['recaptcha']->secret;
317 }
318
319 if ( $requires_wpuf_update ) {
320 $wpuf_settings = get_option( 'wpuf_general', array() );
321
322 $wpuf_settings = array_merge( $wpuf_settings, $wpuf_update_array );
323 update_option( 'wpuf_general', $wpuf_settings );
324 }
325
326 do_action( 'weforms_save_settings', $settings );
327
328 $settings = apply_filters( 'weforms_after_save_settings', $settings );
329
330 wp_send_json_success( $settings );
331 }
332
333 /**
334 * Get the weForms Settings
335 *
336 * @return void
337 */
338 public function get_settings() {
339 check_ajax_referer( 'weforms' );
340
341 $this->check_admin();
342
343 $settings = weforms_get_settings();
344
345 // checking to prevent js error, will be removed in future
346 if ( ! isset( $settings['credit'] ) ) {
347 $settings['credit'] = false;
348 }
349
350 if ( ! isset( $settings['permission'] ) ) {
351 $settings['permission'] = 'manage_options';
352 }
353
354 wp_send_json_success( $settings );
355 }
356
357 /**
358 * Get all entries
359 *
360 * @return void
361 */
362 public function get_entries() {
363 check_ajax_referer( 'weforms' );
364
365 $this->check_admin();
366
367 $form_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
368 $current_page = isset( $_REQUEST['page'] ) ? intval( $_REQUEST['page'] ) : 1;
369 $per_page = 20;
370 $offset = ( $current_page - 1 ) * $per_page;
371
372 if ( ! $form_id ) {
373 wp_send_json_error( __( 'No form id provided!', 'weforms' ) );
374 }
375
376 $entries = weforms_get_form_entries( $form_id, array(
377 'number' => $per_page,
378 'offset' => $offset
379 ) );
380
381 $columns = weforms_get_entry_columns( $form_id );
382 $total_entries = weforms_count_form_entries( $form_id );
383
384 array_map( function( $entry ) use ($columns) {
385 $entry_id = $entry->id;
386 $entry->fields = array();
387
388 foreach ($columns as $meta_key => $label) {
389 $value = weforms_get_entry_meta( $entry_id, $meta_key, true );
390 $entry->fields[$meta_key] = str_replace( WeForms::$field_separator, ' ', $value );
391 }
392 }, $entries );
393
394 $entries = apply_filters('weforms_get_entries', $entries, $form_id );
395
396 $response = array(
397 'columns' => $columns,
398 'entries' => $entries,
399 'form_title' => get_post_field( 'post_title', $form_id ),
400 'pagination' => array(
401 'total' => $total_entries,
402 'per_page' => $per_page,
403 'pages' => ceil( $total_entries / $per_page ),
404 'current' => $current_page
405 )
406 );
407
408 wp_send_json_success( $response );
409 }
410
411
412
413 /**
414 * Get an entry details
415 *
416 * @return void
417 */
418 public function get_entry_detail() {
419 check_ajax_referer( 'weforms' );
420
421 $this->check_admin();
422
423 $form_id = isset( $_REQUEST['form_id'] ) ? intval( $_REQUEST['form_id'] ) : 0;
424 $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0;
425
426 $form = weforms()->form->get( $form_id );
427 $entry = $form->entries()->get( $entry_id );
428 $fields = $entry->get_fields();
429 $metadata = $entry->get_metadata();
430 $payment = $entry->get_payment_data();
431
432 if ( isset($payment->payment_data) && is_serialized( $payment->payment_data ) ) {
433 $payment->payment_data = unserialize( $payment->payment_data );
434 }
435
436 if ( false === $fields ) {
437 wp_send_json_error( __( 'No form fields found!', 'weforms' ) );
438 }
439
440 $response = array(
441 'form_fields' => $fields,
442 'meta_data' => $metadata,
443 'payment_data' => $payment,
444 );
445
446 wp_send_json_success( $response );
447 }
448
449 /**
450 * Trash an entry
451 *
452 * @return void
453 */
454 public function trash_entry() {
455 check_ajax_referer( 'weforms' );
456
457 $this->check_admin();
458
459 $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0;
460
461 weforms_change_entry_status( $entry_id, 'trash' );
462 wp_send_json_success();
463 }
464
465 /**
466 * Bulk trash entries
467 *
468 * @return void
469 */
470 public function bulk_delete_entry() {
471 check_ajax_referer( 'weforms' );
472
473 $this->check_admin();
474
475 $entry_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : array();
476
477 if ( ! $entry_ids ) {
478 wp_send_json_error( __( 'No entry ids provided!', 'weforms' ) );
479 }
480
481 foreach ($entry_ids as $entry_id) {
482 weforms_change_entry_status( $entry_id, 'trash' );
483 }
484
485 wp_send_json_success();
486 }
487
488 /**
489 * Handle the frontend submission
490 *
491 * @return void
492 */
493 public function handle_frontend_submission() {
494 check_ajax_referer( 'wpuf_form_add' );
495
496 $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0;
497 $page_id = isset( $_POST['page_id'] ) ? intval( $_POST['page_id'] ) : 0;
498
499 $form = weforms()->form->get( $form_id );
500 $form_settings = $form->get_settings();
501 $form_fields = $form->get_fields();
502 $entry_fields = $form->prepare_entries();
503 $form_entries = weforms_get_form_entries( $form_id, array( 'number' => '', 'offset' => '' ) );
504
505 if ( $form_fields && count( $form_entries ) && count( $entry_fields ) ) {
506
507 foreach ( $entry_fields as $field_key => $field_value ) {
508 $duplicate_check = false;
509 $field_label = 'This';
510 foreach ( $form_fields as $form_field ) {
511 if ( in_array( $form_field['template'], array( 'text_field', 'website_url', 'numeric_text_field', 'email_address' ) ) && $form_field['name'] == $field_key && isset( $form_field['duplicate'] ) && 'no' == $form_field['duplicate'] ) {
512 $duplicate_check = true;
513 $field_label = $form_field['label'];
514 }
515 }
516
517 if ( $duplicate_check ) {
518 foreach ( $form_entries as $entry ) {
519 $existing = weforms_get_entry_meta( $entry->id, $field_key, true );
520 if ( $existing && $field_value == $existing ) {
521 wp_send_json( array(
522 'success' => false,
523 'error' => sprintf( __( '"%s" field requires a unique entry and "%s" has already been used.', 'weforms' ), $field_label, $field_value )
524 ) );
525 }
526 }
527 }
528 }
529 }
530
531 if ( !$form_fields ) {
532 wp_send_json( array(
533 'success' => false,
534 'error' => __( 'No form field was found.', 'weforms' ),
535 ) );
536 }
537
538 if ( $form->has_field( 'recaptcha' ) ) {
539 $this->validate_reCaptcha();
540 }
541
542 // vaidate submission
543 $this->validate_submission( $entry_fields, $form, $form_settings, $form_fields );
544
545 $entry_fields = apply_filters( 'weforms_before_entry_submission', $entry_fields, $form, $form_settings, $form_fields);
546
547 $entry_id = weforms_insert_entry( array(
548 'form_id' => $form_id
549 ), $entry_fields );
550
551 if ( is_wp_error( $entry_id ) ) {
552 wp_send_json( array(
553 'success' => false,
554 'error' => $entry_id->get_error_message(),
555 ) );
556 }
557
558 // redirect URL
559 $show_message = false;
560 $redirect_to = false;
561
562 if ( $form_settings['redirect_to'] == 'page' ) {
563 $redirect_to = get_permalink( $form_settings['page_id'] );
564 } elseif ( $form_settings['redirect_to'] == 'url' ) {
565 $redirect_to = $form_settings['url'];
566 } elseif ( $form_settings['redirect_to'] == 'same' ) {
567 $show_message = true;
568 } else {
569 $redirect_to = get_permalink( $post_id );
570 }
571
572 // Fire a hook for integration
573 do_action( 'weforms_entry_submission', $entry_id, $form_id, $page_id, $form_settings );
574
575 // send the response
576 $response = apply_filters( 'weforms_entry_submission_response', array(
577 'success' => true,
578 'redirect_to' => $redirect_to,
579 'show_message' => $show_message,
580 'message' => $form_settings['message'],
581 'data' => $_POST,
582 'form_id' => $form_id,
583 'entry_id' => $entry_id,
584 ) );
585
586 $notification = new WeForms_Notification( array(
587 'form_id' => $form_id,
588 'page_id' => $page_id,
589 'entry_id' => $entry_id
590 ) );
591
592 $notification->send_notifications();
593
594 weforms_clear_buffer();
595 wp_send_json( $response );
596 }
597
598 /**
599 * reCaptcha Validation
600 *
601 * @return void
602 */
603 function validate_reCaptcha() {
604
605 // add reCaptcha library if not found
606 if ( !function_exists( 'recaptcha_get_html' ) ) {
607 require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib.php';
608 require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib_noCaptcha.php';
609 }
610
611
612 $invisible = isset( $_POST["g-recaptcha-response"] ) ? false : true;
613
614 $recaptcha_settings = weforms_get_settings( 'recaptcha' );
615 $secret = isset( $recaptcha_settings->secret ) ? $recaptcha_settings->secret : '';
616
617 if ( ! $invisible ) {
618
619 $response = null;
620 $reCaptcha = new ReCaptcha($secret);
621
622 $resp = $reCaptcha->verifyResponse(
623 $_SERVER["REMOTE_ADDR"],
624 $_POST["g-recaptcha-response"]
625 );
626
627 if ( !$resp->success ) {
628 wp_send_json( array(
629 'success' => false,
630 'error' => __( 'reCAPTCHA validation failed', 'wpuf' ),
631 ) );
632 }
633
634 } else {
635
636 $recap_challenge = isset( $_POST['recaptcha_challenge_field'] ) ? $_POST['recaptcha_challenge_field'] : '';
637 $recap_response = isset( $_POST['recaptcha_response_field'] ) ? $_POST['recaptcha_response_field'] : '';
638
639 $resp = recaptcha_check_answer( $secret, $_SERVER["REMOTE_ADDR"], $recap_challenge, $recap_response );
640
641 if ( !$resp->is_valid ) {
642
643 ob_clean();
644
645 wp_send_json( array(
646 'success' => false,
647 'error' => __( 'reCAPTCHA validation failed', 'wpuf' ),
648 ) );
649 }
650 }
651
652 }
653
654 /**
655 * Validate submission
656 *
657 * @param array $entry_fields
658 * @param object $form
659 * @param array $form_settings
660 * @param array $form_fields
661 *
662 * @return bool|json
663 */
664 function validate_submission( $entry_fields, $form, $form_settings, $form_fields ) {
665
666 foreach ( $form_fields as $key => $field ) {
667
668 $value = $entry_fields[$field['name']];
669
670 // if ( isset( $field['required'] ) && $field['required'] && empty( $value ) ) {
671
672 // wp_send_json( array(
673 // 'success' => false,
674 // 'error' => __( sprintf( '%s field is required', $field['label'] ), 'weforms' ),
675 // ) );
676 // }
677
678 if ( 'single_product' === $field['template'] ) {
679
680 if ( ! $value ) {
681 $value = array();
682 }
683
684 $value['price'] = isset( $value['price'] ) ? floatval( $value['price'] ) : 0;
685 $value['quantity'] = isset( $value['quantity'] ) ? floatval( $value['quantity'] ) : 0;
686 $quantity = isset( $field['quantity'] ) ? $field['quantity'] : array();
687 $price = isset( $field['price'] ) ? $field['price'] : array();
688
689
690 if ( isset($price['is_flexible']) && $price['is_flexible'] ) {
691
692 $min = isset( $price['min'] ) ? floatval( $price['min'] ) : 0;
693 $max = isset( $price['max'] ) ? floatval( $price['max'] ) : 0;
694
695 if ( $value['price'] < $min ) {
696
697 wp_send_json( array(
698 'success' => false,
699 'error' => __( sprintf(
700 '%s price must be equal or greater than %s',
701 $field['label'],
702 $min),
703 'weforms' ),
704 ) );
705 }
706
707
708 if ( $max && $value['price'] > $max ) {
709
710 wp_send_json( array(
711 'success' => false,
712 'error' => __( sprintf(
713 '%s price must be equal or less than %s',
714 $field['label'],
715 $max),
716 'weforms' ),
717 ) );
718 }
719 }
720
721 if ( isset($quantity['status']) && $quantity['status'] ) {
722
723 $min = isset( $quantity['min'] ) ? floatval( $quantity['min'] ) : 0;
724 $max = isset( $quantity['max'] ) ? floatval( $quantity['max'] ) : 0;
725
726 if ( $value['quantity'] < $min ) {
727
728 wp_send_json( array(
729 'success' => false,
730 'error' => __( sprintf(
731 '%s quantity must be equal or greater than %s',
732 $field['label'],
733 $min),
734 'weforms' ),
735 ) );
736 }
737
738 if ( $max && $value['quantity'] > $max ) {
739
740 wp_send_json( array(
741 'success' => false,
742 'error' => __( sprintf(
743 '%s quantity must be equal or less than %s',
744 $field['label'],
745 $max),
746 'weforms' ),
747 ) );
748 }
749 }
750
751 }
752 }
753 }
754
755 public static function prepare_meta_fields( $meta_vars ) {
756 // loop through custom fields
757 // skip files, put in a key => value paired array for later executation
758 // process repeatable fields separately
759 // if the input is array type, implode with separator in a field
760
761 $files = array();
762 $meta_key_value = array();
763 $multi_repeated = array(); //multi repeated fields will in sotre duplicated meta key
764
765 foreach ($meta_vars as $key => $value) {
766
767 switch ( $value['template'] ) {
768
769 // put files in a separate array, we'll process it later
770 case 'file_upload':
771 case 'image_upload':
772
773 $files[] = array(
774 'name' => $value['name'],
775 'value' => isset( $_POST['wpuf_files'][$value['name']] ) ? $_POST['wpuf_files'][$value['name']] : array(),
776 'count' => $value['count']
777 );
778 break;
779
780 case 'repeat_field':
781
782 // if it is a multi column repeat field
783 if ( isset( $value['multiple'] ) && $value['multiple'] == 'true' ) {
784
785 // if there's any items in the array, process it
786 if ( $_POST[$value['name']] ) {
787
788 $ref_arr = array();
789 $cols = count( $value['columns'] );
790 $first = array_shift( array_values( $_POST[$value['name']] ) ); //first element
791 $rows = count( $first );
792
793 // loop through columns
794 for ($i = 0; $i < $rows; $i++) {
795
796 // loop through the rows and store in a temp array
797 $temp = array();
798 for ($j = 0; $j < $cols; $j++) {
799
800 $temp[] = $_POST[$value['name']][$j][$i];
801 }
802
803 // store all fields in a row with WeForms::$field_separator separated
804 $ref_arr[] = implode( WeForms::$field_separator, $temp );
805 }
806
807 // now, if we found anything in $ref_arr, store to $multi_repeated
808 if ( $ref_arr ) {
809 $multi_repeated[$value['name']] = array_slice( $ref_arr, 0, $rows );
810 }
811 }
812 } else {
813 $meta_key_value[$value['name']] = implode( WeForms::$field_separator, $_POST[$value['name']] );
814 }
815
816 break;
817
818 case 'address_field':
819
820 if ( isset( $_POST[ $value['name'] ] ) && is_array( $_POST[ $value['name'] ] ) ) {
821 foreach ( $_POST[ $value['name'] ] as $address_field => $field_value ) {
822 $meta_key_value[ $value['name'] ][ $address_field ] = sanitize_text_field( $field_value );
823 }
824 }
825
826 break;
827
828 case 'text_field':
829 case 'email_address':
830 case 'numeric_text_field':
831 case 'date_field':
832
833 $meta_key_value[$value['name']] = sanitize_text_field( trim( $_POST[$value['name']] ) );
834
835 break;
836
837 case 'textarea_field':
838
839 $meta_key_value[$value['name']] = wp_kses_post( $_POST[$value['name']] );
840
841 break;
842
843 case 'dropdown_field':
844 case 'radio_field':
845
846 $val = $_POST[$value['name']];
847 $meta_key_value[$value['name']] = isset( $value['options'][$val] ) ? $value['options'][$val] : '';
848 break;
849
850 case 'multiple_select':
851 case 'checkbox_field':
852
853 $val = ( is_array( $_POST[$value['name']] ) && $_POST[$value['name']] ) ? $_POST[$value['name']] : array();
854 $meta_key_value[$value['name']] = $val;
855
856 if ( $val ) {
857 $new_val = array();
858
859 foreach ($val as $option_key) {
860 $new_val[] = isset( $value['options'][$option_key] ) ? $value['options'][$option_key] : '';
861 }
862
863 $meta_key_value[$value['name']] = implode( WeForms::$field_separator, $new_val );
864 }
865 break;
866
867 default:
868 // if it's an array, implode with this->separator
869 if ( is_array( $_POST[$value['name']] ) ) {
870
871 $meta_key_value[$value['name']] = implode( WeForms::$field_separator, $_POST[$value['name']] );
872
873 } else {
874 $meta_key_value[$value['name']] = trim( $_POST[$value['name']] );
875 }
876
877 break;
878 }
879
880 } //end foreach
881
882 return array($meta_key_value, $multi_repeated, $files);
883 }
884
885 /**
886 * Import a form from a JSON file
887 *
888 * @return void
889 */
890 public function import_form() {
891 check_ajax_referer( 'weforms' );
892
893 $this->check_admin();
894
895 $the_file = isset( $_FILES['importFile'] ) ? $_FILES['importFile'] : false;
896
897 if ( ! $the_file ) {
898 wp_send_json_error( __( 'No file found to import.', 'weforms' ) );
899 }
900
901 $file_ext = pathinfo( $the_file['name'], PATHINFO_EXTENSION );
902
903 if ( ! class_exists( 'WeForms_Admin_Tools' ) ) {
904 require_once dirname( __FILE__ ) . '/admin/class-admin-tools.php';
905 }
906
907 if ( ( $file_ext == 'json' ) && ( $the_file['size'] < 500000 ) ) {
908
909 $status = WeForms_Admin_Tools::import_json_file( $the_file['tmp_name'] );
910
911 if ( $status ) {
912 wp_send_json_success( __( 'The forms have been imported successfully!', 'weforms' ) );
913 } else {
914 wp_send_json_error( __( 'Something went wrong importing the file.', 'weforms' ) );
915 }
916
917 } else {
918 wp_send_json_error( __( 'Invalid file or file size too big.', 'weforms' ) );
919 }
920 }
921
922 }
923