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