PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.2.8
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.2.8
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.8, at includes/class-ajax.php

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