PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.4.6
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.4.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 +1225 -1238 1.6.161.4.6 View file →
@@ -1,1238 +1,1225 @@
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 - continue;
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 - $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0;
707 - $page_id = isset( $_POST['page_id'] ) ? intval( $_POST['page_id'] ) : 0;
708 -
709 - $form = weforms()->form->get( $form_id );
710 - $form_settings = $form->get_settings();
711 - $form_fields = $form->get_fields();
712 - $entry_fields = $form->prepare_entries();
713 - $form_entries = weforms_get_form_entries( $form_id, [ 'number' => '', 'offset' => '' ] );
714 -
715 - if ( $form_fields && count( $form_entries ) && count( $entry_fields ) ) {
716 - foreach ( $entry_fields as $field_key => $field_value ) {
717 - $duplicate_check = false;
718 - $field_label = 'This';
719 -
720 - foreach ( $form_fields as $form_field ) {
721 - 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'] ) {
722 - $duplicate_check = true;
723 - $field_label = $form_field['label'];
724 - }
725 - }
726 -
727 - if ( $duplicate_check ) {
728 - foreach ( $form_entries as $entry ) {
729 - $existing = weforms_get_entry_meta( $entry->id, $field_key, true );
730 -
731 - if ( $existing && $field_value == $existing ) {
732 - wp_send_json( [
733 - 'success' => false,
734 - 'error' => sprintf( __( '"%s" field requires a unique entry and "%s" has already been used.', 'weforms' ), $field_label, $field_value ),
735 - ] );
736 - }
737 - }
738 - }
739 - }
740 - }
741 -
742 - if ( !$form_fields ) {
743 - wp_send_json( [
744 - 'success' => false,
745 - 'error' => __( 'No form field was found.', 'weforms' ),
746 - ] );
747 - }
748 -
749 - if ( $form->has_field( 'recaptcha' ) ) {
750 - $settings = weforms_get_settings( 'recaptcha' );
751 - $type = isset( $settings->type ) ? $settings->type : '';
752 - $secret = isset( $settings->secret ) ? $settings->secret : '';
753 - if( $type == 'v3' ) {
754 - $this->validate_reCaptchav3( $secret );
755 - } else {
756 - $this->validate_reCaptcha();
757 - }
758 - }
759 -
760 - // vaidate submission
761 - $this->validate_submission( $entry_fields, $form, $form_settings, $form_fields );
762 -
763 - $entry_fields = apply_filters( 'weforms_before_entry_submission', $entry_fields, $form, $form_settings, $form_fields );
764 -
765 - //check for entry_fields for a return error
766 - if ( is_wp_error( $entry_fields ) ) {
767 - wp_send_json( [
768 - 'success' => false,
769 - 'error' => $entry_fields->get_error_message(),
770 - ] );
771 - } else {
772 - $entry_id = 1;
773 - $global_settings = weforms_get_settings();
774 - if ( empty( $form_settings['after_submission'] ) ) {
775 - $entry_id = weforms_insert_entry( [
776 - 'form_id' => $form_id,
777 - ], $entry_fields );
778 - if ( is_wp_error( $entry_id ) ) {
779 - wp_send_json( [
780 - 'success' => false,
781 - 'error' => $entry_id->get_error_message(),
782 - ] );
783 - }
784 - // Fire a hook for integration
785 - do_action( 'weforms_entry_submission', $entry_id, $form_id, $page_id, $form_settings );
786 - $notification = new WeForms_Notification( [
787 - 'form_id' => $form_id,
788 - 'page_id' => $page_id,
789 - 'entry_id' => $entry_id,
790 - ] );
791 - $notification->send_notifications();
792 - }
793 - }
794 - // redirect URL
795 - $show_message = false;
796 - $redirect_to = false;
797 - if ( $form_settings['redirect_to'] == 'page' ) {
798 - $redirect_to = get_permalink( $form_settings['page_id'] );
799 - } elseif ( $form_settings['redirect_to'] == 'url' ) {
800 - $redirect_to = $form_settings['url'];
801 - } elseif ( $form_settings['redirect_to'] == 'same' ) {
802 - $show_message = true;
803 - } else {
804 - $show_message = true;
805 - }
806 - $field_search = $field_replace = [];
807 - foreach ( $form_fields as $r_field ) {
808 - $field_search[] = '{' . $r_field['name'] . '}';
809 - if ( $r_field['template'] == 'name_field' ) {
810 - $field_replace[] = implode( ' ', explode( '|', $entry_fields[ $r_field['name'] ] ) );
811 - } else if ( $r_field['template'] == 'address_field' ) {
812 - $field_replace[] = implode( ', ', $entry_fields[ $r_field['name'] ] );
813 - } else {
814 - $field_replace[] = isset( $entry_fields[ $r_field['name'] ] ) ? $entry_fields[ $r_field['name'] ] : '';
815 - }
816 - }
817 - $message = str_replace( $field_search, $field_replace, $form_settings['message'] );
818 - // send the response
819 - $response = apply_filters( 'weforms_entry_submission_response', [
820 - 'success' => true,
821 - 'redirect_to' => $redirect_to,
822 - 'show_message' => $show_message,
823 - 'message' => $message,
824 - 'data' => $_POST,
825 - 'form_id' => $form_id,
826 - 'entry_id' => $entry_id,
827 - 'entry_fields' =>$entry_fields,
828 - ] );
829 -
830 - weforms_clear_buffer();
831 - wp_send_json( $response );
832 - }
833 -
834 - function validate_reCaptchav3( $secret ) {
835 - check_ajax_referer( 'wpuf_form_add' );
836 -
837 - $post_data = wp_unslash($_POST);
838 - $token = $post_data['g-recaptcha-response'];
839 - $action = $post_data['g-action'];
840 - $google_captcha_url = esc_url( 'https://www.google.com/recaptcha/api/siteverify' );
841 -
842 - $response = wp_remote_post( $google_captcha_url,
843 - array(
844 - 'method' => 'POST',
845 - 'body' => array(
846 - 'secret' => $secret,
847 - 'response' => $token
848 - )
849 - )
850 - );
851 -
852 -
853 - if ( is_wp_error( $response ) ) {
854 - wp_send_json( [
855 - 'success' => false,
856 - 'error' => __( 'reCAPTCHA validation failed', 'weforms' ),
857 - ] );
858 - } else {
859 - $api_response = json_decode( wp_remote_retrieve_body( $response ), true );
860 - if( $api_response["success"] == '1' && $api_response["action"] == $action ) {
861 - return true;
862 - } else {
863 - wp_send_json( [
864 - 'success' => false,
865 - 'error' => __( 'reCAPTCHA validation failed', 'weforms' ),
866 - ] );
867 - }
868 - }
869 - }
870 - /**
871 - * reCaptcha Validation
872 - *
873 - * @return void
874 - */
875 - function validate_reCaptcha() {
876 - check_ajax_referer( 'wpuf_form_add' );
877 - if ( class_exists( 'WPUF_ReCaptcha' ) ) {
878 - $recaptcha_class = 'WPUF_ReCaptcha';
879 - } else {
880 - if ( !function_exists( 'recaptcha_get_html' ) ) {
881 - require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib.php';
882 - }
883 -
884 - require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib_noCaptcha.php';
885 - $recaptcha_class = 'Weforms_ReCaptcha';
886 - }
887 -
888 - $invisible = isset( $_POST['g-recaptcha-response'] ) ? false : true;
889 -
890 - $recaptcha_settings = weforms_get_settings( 'recaptcha' );
891 - $secret = isset( $recaptcha_settings->secret ) ? $recaptcha_settings->secret : '';
892 -
893 - if ( ! $invisible ) {
894 - $response = null;
895 - $reCaptcha = new $recaptcha_class( $secret );
896 - $remote_ADDR = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
897 - $recaptcha_response = isset( $_POST['g-recaptcha-response'] ) ? sanitize_text_field( wp_unslash( $_POST['g-recaptcha-response'] ) ) : '';
898 - $resp = $reCaptcha->verifyResponse(
899 - $remote_ADDR,
900 - $recaptcha_response
901 - );
902 -
903 - if ( !$resp->success ) {
904 - wp_send_json( [
905 - 'success' => false,
906 - 'error' => __( 'reCAPTCHA validation failed', 'weforms' ),
907 - ] );
908 - }
909 -
910 - } else {
911 -
912 - $recap_challenge = isset( $_POST['recaptcha_challenge_field'] ) ? sanitize_text_field( wp_unslash( $_POST['recaptcha_challenge_field'] ) ) : '';
913 - $recap_response = isset( $_POST['recaptcha_response_field'] ) ? sanitize_text_field( wp_unslash( $_POST['recaptcha_response_field'] ) ) : '';
914 - $resp = recaptcha_check_answer( $secret, $remote_ADDR, $recap_challenge, $recap_response );
915 -
916 - if ( !$resp->is_valid ) {
917 - ob_clean();
918 -
919 - wp_send_json( [
920 - 'success' => false,
921 - 'error' => __( 'reCAPTCHA validation failed', 'weforms' ),
922 - ] );
923 - }
924 - }
925 - }
926 -
927 - /**
928 - * Validate submission
929 - *
930 - * @param array $entry_fields
931 - * @param object $form
932 - * @param array $form_settings
933 - * @param array $form_fields
934 - *
935 - * @return bool|json
936 - */
937 - public function validate_submission( $entry_fields, $form, $form_settings, $form_fields ) {
938 - foreach ( $form_fields as $key => $field ) {
939 -
940 - //skip custom html field as it is not saved
941 - if ( 'custom_html' == $field['name'] ) {
942 - continue;
943 - }
944 -
945 - //skip recaptcha field as it is not saved
946 - if ( 'recaptcha' == $field['name'] ) {
947 - continue;
948 - }
949 -
950 - $value = $entry_fields[ $field['name'] ];
951 -
952 - if ( 'single_product' === $field['template'] ) {
953 - if ( !$value ) {
954 - $value = [];
955 - }
956 -
957 - $value['price'] = isset( $value['price'] ) ? floatval( $value['price'] ) : 0;
958 - $value['quantity'] = isset( $value['quantity'] ) ? floatval( $value['quantity'] ) : 0;
959 - $quantity = isset( $field['quantity'] ) ? $field['quantity'] : [];
960 - $price = isset( $field['price'] ) ? $field['price'] : [];
961 -
962 - if ( isset( $price['is_flexible'] ) && $price['is_flexible'] ) {
963 - $min = isset( $price['min'] ) ? floatval( $price['min'] ) : 0;
964 - $max = isset( $price['max'] ) ? floatval( $price['max'] ) : 0;
965 -
966 - if ( $value['price'] < $min ) {
967 - wp_send_json( [
968 - 'success' => false,
969 - 'error' => __( sprintf(
970 - '%s price must be equal or greater than %s',
971 - $field['weforms'],
972 - $min ),
973 - 'weforms' ),
974 - ] );
975 - }
976 -
977 - if ( $max && $value['price'] > $max ) {
978 - wp_send_json( [
979 - 'success' => false,
980 - 'error' => __( sprintf(
981 - '%s price must be equal or less than %s',
982 - $field['weforms'],
983 - $max ),
984 - 'weforms' ),
985 - ] );
986 - }
987 - }
988 -
989 - if ( isset( $quantity['status'] ) && $quantity['status'] ) {
990 - $min = isset( $quantity['min'] ) ? floatval( $quantity['min'] ) : 0;
991 - $max = isset( $quantity['max'] ) ? floatval( $quantity['max'] ) : 0;
992 -
993 - if ( $value['quantity'] < $min ) {
994 - wp_send_json( [
995 - 'success' => false,
996 - 'error' => __( sprintf(
997 - '%s quantity must be equal or greater than %s',
998 - $field['weforms'],
999 - $min ),
1000 - 'weforms' ),
1001 - ] );
1002 - }
1003 -
1004 - if ( $max && $value['quantity'] > $max ) {
1005 - wp_send_json( [
1006 - 'success' => false,
1007 - 'error' => __( sprintf(
1008 - '%s quantity must be equal or less than %s',
1009 - $field['weforms'],
1010 - $max ),
1011 - 'weforms' ),
1012 - ] );
1013 - }
1014 - }
1015 - }
1016 - }
1017 - }
1018 -
1019 - public static function prepare_meta_fields( $meta_vars ) {
1020 - check_ajax_referer( 'wpuf_form_add' );
1021 - // loop through custom fields
1022 - // skip files, put in a key => value paired array for later executation
1023 - // process repeatable fields separately
1024 - // if the input is array type, implode with separator in a field
1025 - $files = [];
1026 - $meta_key_value = [];
1027 - $multi_repeated = []; // multi repeated fields will in sotre duplicated meta key
1028 -
1029 - foreach ( $meta_vars as $key => $value ) {
1030 - switch ( $value['template'] ) {
1031 -
1032 - // put files in a separate array, we'll process it later
1033 - case 'file_upload':
1034 - case 'image_upload':
1035 - $files[] = [
1036 - 'name' => $value['name'],
1037 - 'value' => isset( $_POST['wpuf_files'][ $value['name'] ] ) ? sanitize_text_field( wp_unslash( $_POST['wpuf_files'][ $value['name'] ] ) ) : array(),
1038 - 'count' => $value['count']
1039 - ];
1040 - break;
1041 -
1042 - case 'repeat_field':
1043 - // if it is a multi column repeat field
1044 - if ( isset( $value['multiple'] ) && $value['multiple'] == 'true' ) {
1045 -
1046 - // if there's any items in the array, process it
1047 - if ( isset( $_POST[ $value['name'] ] ) ) {
1048 - $ref_arr = [];
1049 - $cols = count( $value['columns'] );
1050 - $first = array_shift( array_values( sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) ) ); // first element
1051 - $rows = count( $first );
1052 -
1053 - // loop through columns
1054 - for ( $i = 0; $i < $rows; $i++ ) {
1055 -
1056 - // loop through the rows and store in a temp array
1057 - $temp = [];
1058 - for ( $j = 0; $j < $cols; $j++ ) {
1059 - $temp[] = isset( $_POST[ $value['name'] ][ $j ][ $i ] ) ? sanitize_text_field( wp_unslash( $_POST[ $value['name'] ][ $j ][ $i ] ) ) : '';
1060 - }
1061 -
1062 - // store all fields in a row with WeForms::$field_separator separated
1063 - $ref_arr[] = implode( WeForms::$field_separator, $temp );
1064 - }
1065 -
1066 - // now, if we found anything in $ref_arr, store to $multi_repeated
1067 - if ( $ref_arr ) {
1068 - $multi_repeated[ $value['name'] ] = array_slice( $ref_arr, 0, $rows );
1069 - }
1070 - }
1071 - } else {
1072 - $meta_key_value[ $value['name'] ] = implode( WeForms::$field_separator, sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) );
1073 - }
1074 -
1075 - break;
1076 -
1077 - case 'address_field':
1078 - if ( isset( $_POST[ $value['name'] ] ) && is_array( $_POST[ $value['name'] ] ) ) {
1079 - $post_value = sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) );
1080 - foreach ( $post_value as $address_field => $field_value ) {
1081 - $meta_key_value[ $value['name'] ][ $address_field ] = sanitize_text_field( $field_value );
1082 - }
1083 - }
1084 -
1085 - break;
1086 -
1087 - case 'text_field':
1088 - case 'email_address':
1089 - case 'numeric_text_field':
1090 - case 'date_field':
1091 - $meta_key_value[ $value['name'] ] = isset( $_POST[ $value['name'] ] ) ? sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) : '';
1092 -
1093 - break;
1094 -
1095 - case 'textarea_field':
1096 - $meta_key_value[ $value['name'] ] = isset( $_POST[ $value['name'] ] ) ? sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) : '';
1097 -
1098 - break;
1099 -
1100 - case 'dropdown_field':
1101 - case 'radio_field':
1102 - $val = sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) );
1103 - $meta_key_value[ $value['name'] ] = isset( $value['options'][ $val ] ) ? $value['options'][ $val ] : '';
1104 - break;
1105 -
1106 - case 'multiple_select':
1107 - case 'checkbox_field':
1108 - $val = ( is_array( $_POST[ $value['name'] ] ) && sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) ) ? sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) : array();
1109 - $meta_key_value[ $value['name'] ] = $val;
1110 -
1111 - if ( $val ) {
1112 - $new_val = [];
1113 -
1114 - foreach ( $val as $option_key ) {
1115 - $new_val[] = isset( $value['options'][ $option_key ] ) ? $value['options'][ $option_key ] : '';
1116 - }
1117 -
1118 - $meta_key_value[ $value['name'] ] = implode( WeForms::$field_separator, $new_val );
1119 - }
1120 - break;
1121 -
1122 - default:
1123 - // if it's an array, implode with this->separator
1124 - if ( is_array( $_POST[ $value['name'] ] ) ) {
1125 - $meta_key_value[ $value['name'] ] = implode( WeForms::$field_separator, sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) );
1126 - } else {
1127 - $meta_key_value[ $value['name'] ] = isset( $_POST[ $value['name'] ] ) ? sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ): '';
1128 - }
1129 -
1130 - break;
1131 - }
1132 - } //end foreach
1133 -
1134 - return [ $meta_key_value, $multi_repeated, $files ];
1135 - }
1136 -
1137 - /**
1138 - * Import a form from a JSON file
1139 - *
1140 - * @return void
1141 - */
1142 - public function import_form() {
1143 - check_ajax_referer( 'weforms' );
1144 -
1145 - $this->check_admin();
1146 -
1147 - $the_file = isset( $_FILES['importFile'] ) ? $_FILES['importFile'] : false;
1148 -
1149 - if ( !$the_file ) {
1150 - wp_send_json_error( __( 'No file found to import.', 'weforms' ) );
1151 - }
1152 -
1153 - $file_ext = pathinfo( $the_file['name'], PATHINFO_EXTENSION );
1154 -
1155 - if ( !class_exists( 'WeForms_Admin_Tools' ) ) {
1156 - require_once __DIR__ . '/admin/class-admin-tools.php';
1157 - }
1158 -
1159 - if ( ( $file_ext == 'json' ) && ( $the_file['size'] < 500000 ) ) {
1160 - $status = WeForms_Admin_Tools::import_json_file( $the_file['tmp_name'] );
1161 -
1162 - if ( $status ) {
1163 - wp_send_json_success( __( 'The forms have been imported successfully!', 'weforms' ) );
1164 - } else {
1165 - wp_send_json_error( __( 'Something went wrong importing the file.', 'weforms' ) );
1166 - }
1167 - } else {
1168 - wp_send_json_error( __( 'Invalid file or file size too big.', 'weforms' ) );
1169 - }
1170 - }
1171 -
1172 - /**
1173 - * Read Log file
1174 - *
1175 - * @return json
1176 - **/
1177 - public function get_logs() {
1178 - check_ajax_referer( 'weforms' );
1179 -
1180 - $this->check_admin();
1181 -
1182 - $file = weforms_log_file_path();
1183 -
1184 - if ( !file_exists( $file ) ) {
1185 - return;
1186 - }
1187 -
1188 - $data = file_get_contents( $file );
1189 - $data = explode( "\n", $data );
1190 - $data = array_reverse( $data );
1191 - $data = array_filter( $data );
1192 -
1193 - if ( empty( $data ) ) {
1194 - return;
1195 - }
1196 -
1197 - $logs = [];
1198 -
1199 - foreach ( $data as $key => $row ) {
1200 - preg_match( '/\[(?<time>.+?)\]\[(?<type>.+?)\](?<message>.+)/im', $row, $log );
1201 -
1202 - if ( empty( $log['message'] ) ) {
1203 - $log = [];
1204 - $log['message'] = !empty( $log['message'] ) ? $log['message'] : $row;
1205 - }
1206 -
1207 - if ( !empty( $log['time'] ) ) {
1208 - $human_time = human_time_diff( strtotime( $log['time'] ), current_time( 'timestamp' ) );
1209 - $log['time'] = $human_time ? $human_time . ' ' . __( 'ago', 'weforms' ) : $log['time'];
1210 - }
1211 -
1212 - $logs[] = $log;
1213 - }
1214 -
1215 - wp_send_json_success( $logs );
1216 - }
1217 -
1218 - /**
1219 - * Read Log file
1220 - *
1221 - * @return json
1222 - **/
1223 - public function delete_logs() {
1224 - check_ajax_referer( 'weforms' );
1225 -
1226 - $this->check_admin();
1227 -
1228 - $file = weforms_log_file_path();
1229 -
1230 - if ( !file_exists( $file ) ) {
1231 - return;
1232 - }
1233 -
1234 - @unlink( $file );
1235 -
1236 - wp_send_json_success();
1237 - }
1238 -}
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 + } else {
114 + $settings = isset( $form_data['wpuf_settings'] ) ? $form_data['wpuf_settings'] : [];
115 + }
116 +
117 + if ( isset( $post_data['integrations'] ) ) {
118 + $integrations = (array) json_decode( $post_data['integrations'] );
119 + }
120 +
121 + // $form_fields = wp_unslash( $form_fields );
122 + // $notifications = wp_unslash( $notifications );
123 +
124 + $form_fields = json_decode( $form_fields, true );
125 + $notifications = json_decode( $notifications, true );
126 + $data = [
127 + 'form_id' => absint( $form_data['wpuf_form_id'] ),
128 + 'post_title' => $form_data['post_title'],
129 + 'form_fields' => $form_fields,
130 + 'form_settings' => $settings,
131 + 'form_settings_key' => isset( $form_data['form_settings_key'] ) ? $form_data['form_settings_key'] : '',
132 + 'notifications' => $notifications,
133 + 'integrations' => $integrations,
134 + ];
135 +
136 + $form_fields = weforms()->form->save( $data );
137 +
138 + wp_send_json_success( [ 'form_fields' => $form_fields ] );
139 + }
140 +
141 + /**
142 + * Get all contact forms
143 + *
144 + * @return void
145 + */
146 + public function get_contact_forms() {
147 + check_ajax_referer( 'weforms' );
148 +
149 + $this->check_admin();
150 +
151 + $args = [
152 + 'posts_per_page' => isset( $_POST['posts_per_page'] ) ? intval( $_POST['posts_per_page'] ) : 10,
153 + 'paged' => isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 1,
154 + 'order' => 'DESC',
155 + 'orderby' => 'post_date',
156 + ];
157 +
158 + $args = apply_filters( 'weforms_ajax_get_contact_forms_args', $args );
159 +
160 + $contact_forms = weforms()->form->get_forms( $args );
161 +
162 + array_map(
163 + function ( $form ) {
164 + $form->entries = $form->num_form_entries();
165 + $form->settings = $form->get_settings();
166 + $form->views = $form->num_form_views();
167 + $form->payments = $form->num_form_payments();
168 + $form->author = $form->get_form_author_details();
169 + }, $contact_forms['forms']
170 + );
171 +
172 + $contact_forms = $this->filter_contact_forms( $contact_forms );
173 + $contact_forms = apply_filters( 'weforms_ajax_get_contact_forms', $contact_forms );
174 +
175 + wp_send_json_success( $contact_forms );
176 + }
177 +
178 + /**
179 + * Get all users
180 + *
181 + * @return array
182 + */
183 + public function get_all_users() {
184 + check_ajax_referer( 'weforms' );
185 +
186 + $this->check_admin();
187 +
188 + $users_meta = [];
189 + $users = get_users( [ 'fields' => [ 'ID' ] ] );
190 +
191 + foreach ( $users as $user ) {
192 + $users_meta[] = [
193 + 'id' => $user->ID,
194 + 'data' => get_user_meta( $user->ID ),
195 + ];
196 + }
197 +
198 + wp_send_json_success( $users_meta );
199 + }
200 +
201 + /**
202 + * Filter
203 + *
204 + * @return void
205 + */
206 + public function filter_contact_forms( &$contact_forms ) {
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 = [];
232 +
233 + foreach ( $contact_forms['forms'] as $form ) {
234 + $response[] = [
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( wp_unslash( $_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( [
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'] ) : [];
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( wp_unslash( $_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( [
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 + $this->check_admin();
357 +
358 + $requires_wpuf_update = false;
359 + $wpuf_update_array = array();
360 + $settings = isset( $_POST['settings'] ) ? (array) json_decode( wp_unslash( $_POST['settings'] ) ) : [];
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 + $wpuf_update_array['recaptcha_type'] = $settings['recaptcha']->type;
374 + }
375 +
376 + if ( isset( $settings['no_conflict'] ) ) {
377 + $requires_wpuf_update = true;
378 + $wpuf_update_array['no_conflict'] = $settings['no_conflict'];
379 + }
380 +
381 + if ( isset( $settings['email_footer'] ) ) {
382 + $requires_wpuf_update = true;
383 + $wpuf_update_array['email_footer'] = $settings['email_footer'];
384 + }
385 +
386 + if ( $requires_wpuf_update ) {
387 + $wpuf_settings = get_option( 'wpuf_general', [] );
388 +
389 + $wpuf_settings = array_merge( $wpuf_settings, $wpuf_update_array );
390 + update_option( 'wpuf_general', $wpuf_settings );
391 + }
392 +
393 + do_action( 'weforms_save_settings', $settings );
394 +
395 + $settings = apply_filters( 'weforms_after_save_settings', $settings );
396 + wp_send_json_success( $settings );
397 + }
398 +
399 + /**
400 + * Get the weForms Settings
401 + *
402 + * @return void
403 + */
404 + public function get_settings() {
405 + check_ajax_referer( 'weforms' );
406 +
407 + $this->check_admin();
408 +
409 + $settings = weforms_get_settings();
410 + // checking to prevent js error, will be removed in future
411 + if ( !isset( $settings['credit'] ) ) {
412 + $settings['credit'] = false;
413 + }
414 +
415 + if ( !isset( $settings['permission'] ) ) {
416 + $settings['permission'] = 'manage_options';
417 + }
418 +
419 + wp_send_json_success( $settings );
420 + }
421 +
422 + /**
423 + * Get all entries
424 + *
425 + * @return void
426 + */
427 + public function get_entries() {
428 + check_ajax_referer( 'weforms' );
429 +
430 + $this->check_admin();
431 +
432 + $form_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
433 + $current_page = isset( $_REQUEST['page'] ) ? intval( $_REQUEST['page'] ) : 1;
434 + $status = isset( $_REQUEST['status'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['status'] ) ) : 'publish';
435 + $per_page = 20;
436 + $offset = ( $current_page - 1 ) * $per_page;
437 +
438 + if ( !$form_id ) {
439 + wp_send_json_error( __( 'No form id provided!', 'weforms' ) );
440 + }
441 +
442 + $entries = weforms_get_form_entries(
443 + $form_id, [
444 + 'number' => $per_page,
445 + 'offset' => $offset,
446 + 'status' => $status,
447 + ]
448 + );
449 +
450 + $columns = weforms_get_entry_columns( $form_id );
451 + $total_entries = weforms_count_form_entries( $form_id, $status );
452 +
453 + array_map(
454 + function ( $entry ) use ( $columns ) {
455 + $entry_id = $entry->id;
456 + $entry->fields = [];
457 +
458 + foreach ( $columns as $meta_key => $label ) {
459 + if ( empty( $meta_key ) ) {
460 + continue;
461 + }
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 = [
471 + 'columns' => $columns,
472 + 'entries' => $entries,
473 + 'form_title' => get_post_field( 'post_title', $form_id ),
474 + 'pagination' => [
475 + 'total' => $total_entries,
476 + 'per_page' => $per_page,
477 + 'pages' => ceil( $total_entries / $per_page ),
478 + 'current' => $current_page,
479 + ],
480 + 'meta' => [
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 + * Get an entry details
491 + *
492 + * @return void
493 + */
494 + public function get_entry_detail() {
495 + check_ajax_referer( 'weforms' );
496 +
497 + $this->check_admin();
498 +
499 + $form_id = isset( $_REQUEST['form_id'] ) ? intval( $_REQUEST['form_id'] ) : 0;
500 + $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0;
501 +
502 + $form = weforms()->form->get( $form_id );
503 + $form_settings = $form->get_settings();
504 + $entry = $form->entries()->get( $entry_id );
505 + $fields = $entry->get_fields();
506 + $metadata = $entry->get_metadata();
507 + $payment = $entry->get_payment_data();
508 +
509 + if ( isset( $payment->payment_data ) && is_serialized( $payment->payment_data ) ) {
510 + $payment->payment_data = unserialize( $payment->payment_data );
511 + }
512 +
513 + if ( false === $fields ) {
514 + wp_send_json_error( __( 'No form fields found!', 'weforms' ) );
515 + }
516 +
517 + if ( sizeof( $fields ) < 1 ) {
518 + $fields[] = [
519 + 'label' => __( 'No form fields found!', 'weforms' ),
520 + ];
521 + }
522 +
523 + $has_empty = false;
524 + $answers = [];
525 + $respondentPoints = isset( $form_settings['total_points'] ) ? floatval( $form_settings['total_points'] ) : 0;
526 +
527 + foreach ( $fields as $key => $field ) {
528 + if ( $form_settings['quiz_form'] == 'yes' ) {
529 + $selectedAnswers = isset( $field['selected_answers'] ) ? $field['selected_answers'] : '';
530 + $givenAnswer = isset( $field['value'] ) ? $field['value'] : '';
531 + $options = isset( $field['options'] ) ? $field['options'] : '';
532 + $template = $field['template'];
533 + $fieldPoints = isset( $field['points'] ) ? floatval( $field['points'] ) : 0;
534 +
535 + if ( $template == 'radio_field' || $template == 'dropdown_field' ) {
536 + $answers[$field['name']] = true;
537 +
538 + if ( empty( $givenAnswer ) ) {
539 + $answers[$field['name']] = false;
540 + $respondentPoints -= $fieldPoints;
541 + } else {
542 + foreach ( $options as $key => $value ) {
543 + if ( $givenAnswer == $value ) {
544 + if ( $key != $selectedAnswers ) {
545 + $answers[$field['name']] = false;
546 + $respondentPoints -= $fieldPoints;
547 + }
548 + }
549 + }
550 + }
551 + } elseif ( $template == 'checkbox_field' || $template == 'multiple_select' ) {
552 + $answers[$field['name']] = true;
553 + $userAnswer = [];
554 +
555 + foreach ( $options as $key => $value ) {
556 + foreach ( $givenAnswer as $answer ) {
557 + if ( $value == $answer ) {
558 + $userAnswer[] = $key;
559 + }
560 + }
561 + }
562 +
563 + $userAnswer = implode( '|', $userAnswer );
564 + $rightAnswers = implode( '|', $selectedAnswers );
565 +
566 + if ( $userAnswer != $rightAnswers || empty( $userAnswer ) ) {
567 + $answers[$field['name']] = false;
568 + $respondentPoints -= $fieldPoints;
569 + }
570 + }
571 + } elseif ( empty( $field['value'] ) ) {
572 + $has_empty = true;
573 + break;
574 + }
575 + }
576 +
577 + $response = [
578 + 'form_fields' => $fields,
579 + 'form_settings' => $form_settings,
580 + 'meta_data' => $metadata,
581 + 'payment_data' => $payment,
582 + 'has_empty' => $has_empty,
583 + 'respondent_points' => $respondentPoints,
584 + 'answers' => $answers,
585 + ];
586 +
587 + wp_send_json_success( $response );
588 + }
589 +
590 + /**
591 + * Trash an entry
592 + *
593 + * @return void
594 + */
595 + public function trash_entry() {
596 + check_ajax_referer( 'weforms' );
597 +
598 + $this->check_admin();
599 +
600 + $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0;
601 +
602 + weforms_change_entry_status( $entry_id, 'trash' );
603 + wp_send_json_success();
604 + }
605 +
606 + /**
607 + * Trash an entry
608 + *
609 + * @return void
610 + */
611 + public function delete_entry() {
612 + check_ajax_referer( 'weforms' );
613 +
614 + $this->check_admin();
615 +
616 + $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0;
617 +
618 + weforms_delete_entry( $entry_id );
619 +
620 + wp_send_json_success();
621 + }
622 +
623 + /**
624 + * Restore Entry
625 + *
626 + * @return void
627 + */
628 + public function restore_entry() {
629 + check_ajax_referer( 'weforms' );
630 +
631 + $this->check_admin();
632 +
633 + $entry_id = isset( $_REQUEST['entry_id'] ) ? intval( $_REQUEST['entry_id'] ) : 0;
634 +
635 + weforms_change_entry_status( $entry_id, 'publish' );
636 + wp_send_json_success();
637 + }
638 +
639 + /**
640 + * Bulk trash entries
641 + *
642 + * @return void
643 + */
644 + public function bulk_delete_entry() {
645 + check_ajax_referer( 'weforms' );
646 +
647 + $this->check_admin();
648 +
649 + $entry_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : array();
650 + $permanent = isset( $_POST['permanent'] ) && ( sanitize_text_field( wp_unslash ( $_POST['permanent'] ) ) ) ? true : false;
651 +
652 + if ( !$entry_ids ) {
653 + wp_send_json_error( __( 'No entry ids provided!', 'weforms' ) );
654 + }
655 +
656 + foreach ( $entry_ids as $entry_id ) {
657 + if ( $permanent ) {
658 + weforms_delete_entry( $entry_id );
659 + } else {
660 + weforms_change_entry_status( $entry_id, 'trash' );
661 + }
662 + }
663 +
664 + wp_send_json_success();
665 + }
666 +
667 + /**
668 + * Bulk trash entries
669 + *
670 + * @return void
671 + */
672 + public function bulk_restore_entry() {
673 + check_ajax_referer( 'weforms' );
674 +
675 + $this->check_admin();
676 +
677 + $entry_ids = isset( $_POST['ids'] ) ? array_map( 'absint', $_POST['ids'] ) : [];
678 +
679 + if ( !$entry_ids ) {
680 + wp_send_json_error( __( 'No entry ids provided!', 'weforms' ) );
681 + }
682 +
683 + foreach ( $entry_ids as $entry_id ) {
684 + weforms_change_entry_status( $entry_id, 'publish' );
685 + }
686 +
687 + wp_send_json_success();
688 + }
689 +
690 + /**
691 + * Handle the frontend submission
692 + *
693 + * @return void
694 + */
695 + public function handle_frontend_submission() {
696 + check_ajax_referer( 'wpuf_form_add' );
697 +
698 + $form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0;
699 + $page_id = isset( $_POST['page_id'] ) ? intval( $_POST['page_id'] ) : 0;
700 +
701 + $form = weforms()->form->get( $form_id );
702 + $form_settings = $form->get_settings();
703 + $form_fields = $form->get_fields();
704 + $entry_fields = $form->prepare_entries();
705 + $form_entries = weforms_get_form_entries( $form_id, [ 'number' => '', 'offset' => '' ] );
706 +
707 + if ( $form_fields && count( $form_entries ) && count( $entry_fields ) ) {
708 + foreach ( $entry_fields as $field_key => $field_value ) {
709 + $duplicate_check = false;
710 + $field_label = 'This';
711 +
712 + foreach ( $form_fields as $form_field ) {
713 + 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'] ) {
714 + $duplicate_check = true;
715 + $field_label = $form_field['label'];
716 + }
717 + }
718 +
719 + if ( $duplicate_check ) {
720 + foreach ( $form_entries as $entry ) {
721 + $existing = weforms_get_entry_meta( $entry->id, $field_key, true );
722 +
723 + if ( $existing && $field_value == $existing ) {
724 + wp_send_json( [
725 + 'success' => false,
726 + 'error' => sprintf( __( '"%s" field requires a unique entry and "%s" has already been used.', 'weforms' ), $field_label, $field_value ),
727 + ] );
728 + }
729 + }
730 + }
731 + }
732 + }
733 +
734 + if ( !$form_fields ) {
735 + wp_send_json( [
736 + 'success' => false,
737 + 'error' => __( 'No form field was found.', 'weforms' ),
738 + ] );
739 + }
740 +
741 + if ( $form->has_field( 'recaptcha' ) ) {
742 + $settings = weforms_get_settings( 'recaptcha' );
743 + $type = isset( $settings->type ) ? $settings->type : '';
744 + $secret = isset( $settings->secret ) ? $settings->secret : '';
745 + if( $type == 'v3' ) {
746 + $this->validate_reCaptchav3( $secret );
747 + } else {
748 + $this->validate_reCaptcha();
749 + }
750 + }
751 +
752 + // vaidate submission
753 + $this->validate_submission( $entry_fields, $form, $form_settings, $form_fields );
754 +
755 + $entry_fields = apply_filters( 'weforms_before_entry_submission', $entry_fields, $form, $form_settings, $form_fields );
756 +
757 + $entry_id = weforms_insert_entry( [
758 + 'form_id' => $form_id,
759 + ], $entry_fields );
760 +
761 + if ( is_wp_error( $entry_id ) ) {
762 + wp_send_json( [
763 + 'success' => false,
764 + 'error' => $entry_id->get_error_message(),
765 + ] );
766 + }
767 +
768 + // redirect URL
769 + $show_message = false;
770 + $redirect_to = false;
771 +
772 + if ( $form_settings['redirect_to'] == 'page' ) {
773 + $redirect_to = get_permalink( $form_settings['page_id'] );
774 + } elseif ( $form_settings['redirect_to'] == 'url' ) {
775 + $redirect_to = $form_settings['url'];
776 + } elseif ( $form_settings['redirect_to'] == 'same' ) {
777 + $show_message = true;
778 + } else {
779 + $redirect_to = get_permalink( $post_id );
780 + }
781 +
782 + // Fire a hook for integration
783 + do_action( 'weforms_entry_submission', $entry_id, $form_id, $page_id, $form_settings );
784 +
785 + $field_search = $field_replace = [];
786 +
787 + foreach ( $form_fields as $r_field ) {
788 + $field_search[] = '{' . $r_field['name'] . '}';
789 +
790 + if ( $r_field['template'] == 'name_field' ) {
791 + $field_replace[] = implode( ' ', explode( '|', $entry_fields[$r_field['name']] ) );
792 + } else {
793 + $field_replace[] = isset( $entry_fields[$r_field['name']] ) ? $entry_fields[$r_field['name']] : '';
794 + }
795 + }
796 + $message = str_replace( $field_search, $field_replace, $form_settings['message'] );
797 +
798 + // send the response
799 + $response = apply_filters( 'weforms_entry_submission_response', [
800 + 'success' => true,
801 + 'redirect_to' => $redirect_to,
802 + 'show_message' => $show_message,
803 + 'message' => $message,
804 + 'data' => $_POST,
805 + 'form_id' => $form_id,
806 + 'entry_id' => $entry_id,
807 + ] );
808 +
809 + $notification = new WeForms_Notification( [
810 + 'form_id' => $form_id,
811 + 'page_id' => $page_id,
812 + 'entry_id' => $entry_id,
813 + ] );
814 +
815 + $notification->send_notifications();
816 +
817 + weforms_clear_buffer();
818 + wp_send_json( $response );
819 + }
820 +
821 + function validate_reCaptchav3( $secret ) {
822 + check_ajax_referer( 'wpuf_form_add' );
823 +
824 + $post_data = wp_unslash($_POST);
825 + $token = $post_data['g-recaptcha-response'];
826 + $action = $post_data['g-action'];
827 + $google_captcha_url = esc_url( 'https://www.google.com/recaptcha/api/siteverify' );
828 +
829 + $response = wp_remote_post( $google_captcha_url,
830 + array(
831 + 'method' => 'POST',
832 + 'body' => array(
833 + 'secret' => $secret,
834 + 'response' => $token
835 + )
836 + )
837 + );
838 +
839 +
840 + if ( is_wp_error( $response ) ) {
841 + wp_send_json( [
842 + 'success' => false,
843 + 'error' => __( 'reCAPTCHA validation failed', 'weforms' ),
844 + ] );
845 + } else {
846 + $api_response = json_decode( wp_remote_retrieve_body( $response ), true );
847 + if( $api_response["success"] == '1' && $api_response["action"] == $action ) {
848 + return true;
849 + } else {
850 + wp_send_json( [
851 + 'success' => false,
852 + 'error' => __( 'reCAPTCHA validation failed', 'weforms' ),
853 + ] );
854 + }
855 + }
856 + }
857 + /**
858 + * reCaptcha Validation
859 + *
860 + * @return void
861 + */
862 + function validate_reCaptcha() {
863 + check_ajax_referer( 'wpuf_form_add' );
864 + if ( class_exists( 'WPUF_ReCaptcha' ) ) {
865 + $recaptcha_class = 'WPUF_ReCaptcha';
866 + } else {
867 + if ( !function_exists( 'recaptcha_get_html' ) ) {
868 + require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib.php';
869 + }
870 +
871 + require_once WEFORMS_INCLUDES . '/library/reCaptcha/recaptchalib_noCaptcha.php';
872 + $recaptcha_class = 'Weforms_ReCaptcha';
873 + }
874 +
875 + $invisible = isset( $_POST['g-recaptcha-response'] ) ? false : true;
876 +
877 + $recaptcha_settings = weforms_get_settings( 'recaptcha' );
878 + $secret = isset( $recaptcha_settings->secret ) ? $recaptcha_settings->secret : '';
879 +
880 + if ( ! $invisible ) {
881 + $response = null;
882 + $reCaptcha = new $recaptcha_class( $secret );
883 + $remote_ADDR = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
884 + $recaptcha_response = isset( $_POST['g-recaptcha-response'] ) ? sanitize_text_field( wp_unslash( $_POST['g-recaptcha-response'] ) ) : '';
885 + $resp = $reCaptcha->verifyResponse(
886 + $remote_ADDR,
887 + $recaptcha_response
888 + );
889 +
890 + if ( !$resp->success ) {
891 + wp_send_json( [
892 + 'success' => false,
893 + 'error' => __( 'reCAPTCHA validation failed', 'weforms' ),
894 + ] );
895 + }
896 +
897 + } else {
898 +
899 + $recap_challenge = isset( $_POST['recaptcha_challenge_field'] ) ? sanitize_text_field( wp_unslash( $_POST['recaptcha_challenge_field'] ) ) : '';
900 + $recap_response = isset( $_POST['recaptcha_response_field'] ) ? sanitize_text_field( wp_unslash( $_POST['recaptcha_response_field'] ) ) : '';
901 + $resp = recaptcha_check_answer( $secret, $remote_ADDR, $recap_challenge, $recap_response );
902 +
903 + if ( !$resp->is_valid ) {
904 + ob_clean();
905 +
906 + wp_send_json( [
907 + 'success' => false,
908 + 'error' => __( 'reCAPTCHA validation failed', 'weforms' ),
909 + ] );
910 + }
911 + }
912 + }
913 +
914 + /**
915 + * Validate submission
916 + *
917 + * @param array $entry_fields
918 + * @param object $form
919 + * @param array $form_settings
920 + * @param array $form_fields
921 + *
922 + * @return bool|json
923 + */
924 + public function validate_submission( $entry_fields, $form, $form_settings, $form_fields ) {
925 + foreach ( $form_fields as $key => $field ) {
926 +
927 + //skip custom html field as it is not saved
928 + if ( 'custom_html' == $field['name'] ) {
929 + continue;
930 + }
931 +
932 + //skip recaptcha field as it is not saved
933 + if ( 'recaptcha' == $field['name'] ) {
934 + continue;
935 + }
936 +
937 + $value = $entry_fields[ $field['name'] ];
938 +
939 + if ( 'single_product' === $field['template'] ) {
940 + if ( !$value ) {
941 + $value = [];
942 + }
943 +
944 + $value['price'] = isset( $value['price'] ) ? floatval( $value['price'] ) : 0;
945 + $value['quantity'] = isset( $value['quantity'] ) ? floatval( $value['quantity'] ) : 0;
946 + $quantity = isset( $field['quantity'] ) ? $field['quantity'] : [];
947 + $price = isset( $field['price'] ) ? $field['price'] : [];
948 +
949 + if ( isset( $price['is_flexible'] ) && $price['is_flexible'] ) {
950 + $min = isset( $price['min'] ) ? floatval( $price['min'] ) : 0;
951 + $max = isset( $price['max'] ) ? floatval( $price['max'] ) : 0;
952 +
953 + if ( $value['price'] < $min ) {
954 + wp_send_json( [
955 + 'success' => false,
956 + 'error' => __( sprintf(
957 + '%s price must be equal or greater than %s',
958 + $field['weforms'],
959 + $min ),
960 + 'weforms' ),
961 + ] );
962 + }
963 +
964 + if ( $max && $value['price'] > $max ) {
965 + wp_send_json( [
966 + 'success' => false,
967 + 'error' => __( sprintf(
968 + '%s price must be equal or less than %s',
969 + $field['weforms'],
970 + $max ),
971 + 'weforms' ),
972 + ] );
973 + }
974 + }
975 +
976 + if ( isset( $quantity['status'] ) && $quantity['status'] ) {
977 + $min = isset( $quantity['min'] ) ? floatval( $quantity['min'] ) : 0;
978 + $max = isset( $quantity['max'] ) ? floatval( $quantity['max'] ) : 0;
979 +
980 + if ( $value['quantity'] < $min ) {
981 + wp_send_json( [
982 + 'success' => false,
983 + 'error' => __( sprintf(
984 + '%s quantity must be equal or greater than %s',
985 + $field['weforms'],
986 + $min ),
987 + 'weforms' ),
988 + ] );
989 + }
990 +
991 + if ( $max && $value['quantity'] > $max ) {
992 + wp_send_json( [
993 + 'success' => false,
994 + 'error' => __( sprintf(
995 + '%s quantity must be equal or less than %s',
996 + $field['weforms'],
997 + $max ),
998 + 'weforms' ),
999 + ] );
1000 + }
1001 + }
1002 + }
1003 + }
1004 + }
1005 +
1006 + public static function prepare_meta_fields( $meta_vars ) {
1007 + check_ajax_referer( 'wpuf_form_add' );
1008 + // loop through custom fields
1009 + // skip files, put in a key => value paired array for later executation
1010 + // process repeatable fields separately
1011 + // if the input is array type, implode with separator in a field
1012 + $files = [];
1013 + $meta_key_value = [];
1014 + $multi_repeated = []; // multi repeated fields will in sotre duplicated meta key
1015 +
1016 + foreach ( $meta_vars as $key => $value ) {
1017 + switch ( $value['template'] ) {
1018 +
1019 + // put files in a separate array, we'll process it later
1020 + case 'file_upload':
1021 + case 'image_upload':
1022 + $files[] = [
1023 + 'name' => $value['name'],
1024 + 'value' => isset( $_POST['wpuf_files'][ $value['name'] ] ) ? sanitize_text_field( wp_unslash( $_POST['wpuf_files'][ $value['name'] ] ) ) : array(),
1025 + 'count' => $value['count']
1026 + ];
1027 + break;
1028 +
1029 + case 'repeat_field':
1030 + // if it is a multi column repeat field
1031 + if ( isset( $value['multiple'] ) && $value['multiple'] == 'true' ) {
1032 +
1033 + // if there's any items in the array, process it
1034 + if ( isset( $_POST[ $value['name'] ] ) ) {
1035 + $ref_arr = [];
1036 + $cols = count( $value['columns'] );
1037 + $first = array_shift( array_values( sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) ) ); // first element
1038 + $rows = count( $first );
1039 +
1040 + // loop through columns
1041 + for ( $i = 0; $i < $rows; $i++ ) {
1042 +
1043 + // loop through the rows and store in a temp array
1044 + $temp = [];
1045 + for ( $j = 0; $j < $cols; $j++ ) {
1046 + $temp[] = isset( $_POST[ $value['name'] ][ $j ][ $i ] ) ? sanitize_text_field( wp_unslash( $_POST[ $value['name'] ][ $j ][ $i ] ) ) : '';
1047 + }
1048 +
1049 + // store all fields in a row with WeForms::$field_separator separated
1050 + $ref_arr[] = implode( WeForms::$field_separator, $temp );
1051 + }
1052 +
1053 + // now, if we found anything in $ref_arr, store to $multi_repeated
1054 + if ( $ref_arr ) {
1055 + $multi_repeated[ $value['name'] ] = array_slice( $ref_arr, 0, $rows );
1056 + }
1057 + }
1058 + } else {
1059 + $meta_key_value[ $value['name'] ] = implode( WeForms::$field_separator, sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) );
1060 + }
1061 +
1062 + break;
1063 +
1064 + case 'address_field':
1065 + if ( isset( $_POST[ $value['name'] ] ) && is_array( $_POST[ $value['name'] ] ) ) {
1066 + $post_value = sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) );
1067 + foreach ( $post_value as $address_field => $field_value ) {
1068 + $meta_key_value[ $value['name'] ][ $address_field ] = sanitize_text_field( $field_value );
1069 + }
1070 + }
1071 +
1072 + break;
1073 +
1074 + case 'text_field':
1075 + case 'email_address':
1076 + case 'numeric_text_field':
1077 + case 'date_field':
1078 + $meta_key_value[ $value['name'] ] = isset( $_POST[ $value['name'] ] ) ? sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) : '';
1079 +
1080 + break;
1081 +
1082 + case 'textarea_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 'dropdown_field':
1088 + case 'radio_field':
1089 + $val = sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) );
1090 + $meta_key_value[ $value['name'] ] = isset( $value['options'][ $val ] ) ? $value['options'][ $val ] : '';
1091 + break;
1092 +
1093 + case 'multiple_select':
1094 + case 'checkbox_field':
1095 + $val = ( is_array( $_POST[ $value['name'] ] ) && sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) ) ? sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) : array();
1096 + $meta_key_value[ $value['name'] ] = $val;
1097 +
1098 + if ( $val ) {
1099 + $new_val = [];
1100 +
1101 + foreach ( $val as $option_key ) {
1102 + $new_val[] = isset( $value['options'][ $option_key ] ) ? $value['options'][ $option_key ] : '';
1103 + }
1104 +
1105 + $meta_key_value[ $value['name'] ] = implode( WeForms::$field_separator, $new_val );
1106 + }
1107 + break;
1108 +
1109 + default:
1110 + // if it's an array, implode with this->separator
1111 + if ( is_array( $_POST[ $value['name'] ] ) ) {
1112 + $meta_key_value[ $value['name'] ] = implode( WeForms::$field_separator, sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ) );
1113 + } else {
1114 + $meta_key_value[ $value['name'] ] = isset( $_POST[ $value['name'] ] ) ? sanitize_text_field( wp_unslash( $_POST[ $value['name'] ] ) ): '';
1115 + }
1116 +
1117 + break;
1118 + }
1119 + } //end foreach
1120 +
1121 + return [ $meta_key_value, $multi_repeated, $files ];
1122 + }
1123 +
1124 + /**
1125 + * Import a form from a JSON file
1126 + *
1127 + * @return void
1128 + */
1129 + public function import_form() {
1130 + check_ajax_referer( 'weforms' );
1131 +
1132 + $this->check_admin();
1133 +
1134 + $the_file = isset( $_FILES['importFile'] ) ? sanitize_text_field( wp_unslash( $_FILES['importFile'] ) ) : false;
1135 +
1136 + if ( !$the_file ) {
1137 + wp_send_json_error( __( 'No file found to import.', 'weforms' ) );
1138 + }
1139 +
1140 + $file_ext = pathinfo( $the_file['name'], PATHINFO_EXTENSION );
1141 +
1142 + if ( !class_exists( 'WeForms_Admin_Tools' ) ) {
1143 + require_once __DIR__ . '/admin/class-admin-tools.php';
1144 + }
1145 +
1146 + if ( ( $file_ext == 'json' ) && ( $the_file['size'] < 500000 ) ) {
1147 + $status = WeForms_Admin_Tools::import_json_file( $the_file['tmp_name'] );
1148 +
1149 + if ( $status ) {
1150 + wp_send_json_success( __( 'The forms have been imported successfully!', 'weforms' ) );
1151 + } else {
1152 + wp_send_json_error( __( 'Something went wrong importing the file.', 'weforms' ) );
1153 + }
1154 + } else {
1155 + wp_send_json_error( __( 'Invalid file or file size too big.', 'weforms' ) );
1156 + }
1157 + }
1158 +
1159 + /**
1160 + * Read Log file
1161 + *
1162 + * @return json
1163 + **/
1164 + public function get_logs() {
1165 + check_ajax_referer( 'weforms' );
1166 +
1167 + $this->check_admin();
1168 +
1169 + $file = weforms_log_file_path();
1170 +
1171 + if ( !file_exists( $file ) ) {
1172 + return;
1173 + }
1174 +
1175 + $data = file_get_contents( $file );
1176 + $data = explode( "\n", $data );
1177 + $data = array_reverse( $data );
1178 + $data = array_filter( $data );
1179 +
1180 + if ( empty( $data ) ) {
1181 + return;
1182 + }
1183 +
1184 + $logs = [];
1185 +
1186 + foreach ( $data as $key => $row ) {
1187 + preg_match( '/\[(?<time>.+?)\]\[(?<type>.+?)\](?<message>.+)/im', $row, $log );
1188 +
1189 + if ( empty( $log['message'] ) ) {
1190 + $log = [];
1191 + $log['message'] = !empty( $log['message'] ) ? $log['message'] : $row;
1192 + }
1193 +
1194 + if ( !empty( $log['time'] ) ) {
1195 + $human_time = human_time_diff( strtotime( $log['time'] ), current_time( 'timestamp' ) );
1196 + $log['time'] = $human_time ? $human_time . ' ' . __( 'ago', 'weforms' ) : $log['time'];
1197 + }
1198 +
1199 + $logs[] = $log;
1200 + }
1201 +
1202 + wp_send_json_success( $logs );
1203 + }
1204 +
1205 + /**
1206 + * Read Log file
1207 + *
1208 + * @return json
1209 + **/
1210 + public function delete_logs() {
1211 + check_ajax_referer( 'weforms' );
1212 +
1213 + $this->check_admin();
1214 +
1215 + $file = weforms_log_file_path();
1216 +
1217 + if ( !file_exists( $file ) ) {
1218 + return;
1219 + }
1220 +
1221 + @unlink( $file );
1222 +
1223 + wp_send_json_success();
1224 + }
1225 +}