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

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