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

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