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