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