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