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