PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 2.0.4
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v2.0.4
3.5.2 3.5.1 3.5.0 3.4.8 3.4.7 3.4.6 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5.1 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.10 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.6.1 1.6.7 1.7.0 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.2 1.7.2.1 1.7.2.2 1.7.3 1.7.4 1.7.5 1.7.5.1 1.7.5.2 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.8.0 1.8.0.1 1.8.1 1.8.2 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.0.1 1.9.1 1.9.2 1.9.3 1.9.4 1.9.4.1 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.0.1 2.0.1 2.0.2 2.0.3 2.0.3.1 2.0.4 2.0.4.1 2.0.5 2.0.6 2.0.7 2.0.8 2.0.8.1 2.0.9 3.0.0 3.0.0.1 3.0.1 3.0.2 3.0.3 3.0.3.1 3.0.4 3.0.4.1 3.0.4.2 3.0.5 3.0.5.1 3.0.5.2 3.0.6 3.0.6.1 3.0.7.1 3.0.8 3.0.8.1 3.0.9 3.0.9.1 3.0.9.2 3.0.9.3 3.0.9.4 3.0.9.5 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.3.0 3.4.0 3.4.1 3.4.2 3.4.2.1 3.4.3 3.4.4 3.4.5 trunk 1.0 1.0.1 1.0.2 1.0.3
everest-forms / includes / class-evf-form-task.php
everest-forms / includes Last commit date
abstracts 2 years ago admin 2 years ago elementor 4 years ago export 3 years ago fields 2 years ago interfaces 8 years ago libraries 2 years ago log-handlers 4 years ago shortcodes 2 years ago stats 3 years ago templates 5 years ago class-everest-forms.php 2 years ago class-evf-ajax.php 2 years ago class-evf-autoloader.php 7 years ago class-evf-background-updater.php 7 years ago class-evf-cache-helper.php 6 years ago class-evf-cron.php 3 years ago class-evf-deprecated-action-hooks.php 6 years ago class-evf-deprecated-filter-hooks.php 5 years ago class-evf-emails.php 2 years ago class-evf-fields.php 2 years ago class-evf-form-block.php 4 years ago class-evf-form-handler.php 3 years ago class-evf-form-task.php 2 years ago class-evf-forms-features.php 2 years ago class-evf-frontend-scripts.php 2 years ago class-evf-install.php 2 years ago class-evf-integrations.php 7 years ago class-evf-log-levels.php 8 years ago class-evf-logger.php 5 years ago class-evf-post-types.php 5 years ago class-evf-privacy.php 6 years ago class-evf-session-handler.php 7 years ago class-evf-shortcodes.php 4 years ago class-evf-smart-tags.php 2 years ago class-evf-template-loader.php 2 years ago class-evf-validation.php 6 years ago evf-conditional-functions.php 6 years ago evf-core-functions.php 2 years ago evf-deprecated-functions.php 6 years ago evf-entry-functions.php 3 years ago evf-formatting-functions.php 4 years ago evf-notice-functions.php 4 years ago evf-template-functions.php 4 years ago evf-template-hooks.php 7 years ago evf-update-functions.php 5 years ago
class-evf-form-task.php
1159 lines
1 <?php
2 /**
3 * Process form data
4 *
5 * @package EverestForms
6 * @since 1.0.0
7 */
8
9 defined( 'ABSPATH' ) || exit;
10
11 /**
12 * EVF_Form_Task class.
13 */
14 class EVF_Form_Task {
15
16 /**
17 * Holds errors.
18 *
19 * @since 1.0.0
20 * @var array
21 */
22 public $errors;
23
24 /**
25 * Holds formatted fields.
26 *
27 * @since 1.0.0
28 * @var array
29 */
30 public $form_fields;
31
32 /**
33 * Holds the ID of a successful entry.
34 *
35 * @since 1.0.0
36 * @var int
37 */
38 public $entry_id = 0;
39
40 /**
41 * Form data and settings.
42 *
43 * @since 1.5.0
44 *
45 * @var array
46 */
47 public $form_data = array();
48
49 /**
50 * Is hash validation?
51 *
52 * @var 1.7.4
53 */
54 public $is_valid_hash = false;
55
56 /**
57 * Primary class constructor.
58 *
59 * @since 1.0.0
60 */
61 public function __construct() {
62 add_action( 'wp', array( $this, 'listen_task' ) );
63 add_filter( 'everest_forms_field_properties', array( $this, 'load_previous_field_value' ), 99, 3 );
64 add_action( 'everest_forms_complete_entry_save', array( $this, 'update_slot_booking_value' ), 10, 5 );
65 }
66
67 /**
68 * Listen to see if this is a return callback or a posted form entry.
69 *
70 * @since 1.0.0
71 */
72 public function listen_task() {
73 if ( ! empty( $_GET['everest_forms_return'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
74 $this->entry_confirmation_redirect( '', sanitize_text_field( wp_unslash( $_GET['everest_forms_return'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification
75 }
76
77 $form_id = ! empty( $_POST['everest_forms']['id'] ) ? absint( $_POST['everest_forms']['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification
78
79 if ( ! $form_id ) {
80 return;
81 }
82
83 if ( ! empty( $_POST['everest_forms']['id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
84 $this->do_task( evf_sanitize_entry( wp_unslash( $_POST['everest_forms'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
85 }
86
87 if ( ! evf_is_amp() ) {
88 return;
89 }
90
91 $settings = $this->form_data['settings'];
92 $success_message = isset( $settings['successful_form_submission_message'] ) ? $settings['successful_form_submission_message'] : __( 'Thanks for contacting us! We will be in touch with you shortly.', 'everest-forms' );
93 // Send 400 Bad Request when there are errors.
94 if ( empty( $this->errors[ $form_id ] ) ) {
95 wp_send_json(
96 array(
97 'message' => $success_message,
98 ),
99 200
100 );
101
102 return;
103 }
104 $message = $this->errors[ $form_id ]['header'];
105
106 if ( ! empty( $this->errors[ $form_id ]['footer'] ) ) {
107 $message .= ' ' . $this->errors[ $form_id ]['footer'];
108 }
109
110 wp_send_json(
111 array(
112 'message' => $message,
113 ),
114 400
115 );
116 }
117
118 /**
119 * Do task of form entry
120 *
121 * @since 1.0.0
122 * @param array $entry $_POST object.
123 */
124 public function do_task( $entry ) {
125 $logger = evf_get_logger();
126 try {
127 $this->errors = array();
128 $this->form_fields = array();
129 $form_id = absint( $entry['id'] );
130 $form = evf()->form->get( $form_id );
131 $honeypot = false;
132 $response_data = array();
133 $this->ajax_err = array();
134 $this->evf_notice_print = false;
135 $logger = evf_get_logger();
136
137 // Check nonce for form submission.
138 if ( empty( $_POST[ '_wpnonce' . $form_id ] ) || ! wp_verify_nonce( wp_unslash( sanitize_key( $_POST[ '_wpnonce' . $form_id ] ) ), 'everest-forms_process_submit' ) ) { // phpcs:ignore WordPress.Security.NonceVerification
139 $this->errors[ $form_id ]['header'] = esc_html__( 'We were unable to process your form, please try again.', 'everest-forms' );
140 $logger->error(
141 $this->errors[ $form_id ]['header'],
142 array( 'source' => 'form-submission' )
143 );
144 return $this->errors;
145 }
146
147 // Validate form is real and active (published).
148 if ( ! $form || 'publish' !== $form->post_status ) {
149 $this->errors[ $form_id ]['header'] = esc_html__( 'Invalid form. Please check again.', 'everest-forms' );
150 $logger->error(
151 $this->errors[ $form_id ]['header'],
152 array( 'source' => 'form-submission' )
153 );
154 return $this->errors;
155 }
156
157 // Check if the form is enabled or not.
158 $form_enabled = evf_decode( $form->post_content );
159 if ( isset( $form_enabled['form_enabled'] ) && ! $form_enabled['form_enabled'] ) {
160 $this->errors[ $form_id ]['header'] = esc_html__( 'This form is disabled.', 'everest-forms' );
161 $logger->error(
162 $this->errors[ $form_id ]['header'],
163 array( 'source' => 'form-submission' )
164 );
165 return $this->errors;
166 }
167
168 // Formatted form data for hooks.
169 $this->form_data = apply_filters( 'everest_forms_process_before_form_data', evf_decode( $form->post_content ), $entry );
170
171 // Pre-process/validate hooks and filter. Data is not validated or cleaned yet so use with caution.
172 $entry = apply_filters( 'everest_forms_process_before_filter', $entry, $this->form_data );
173 $this->form_data['page_id'] = array_key_exists( 'post_id', $entry ) ? $entry['post_id'] : $form_id;
174
175 $logger->info(
176 __( 'Everest Forms Process Before.', 'everest-forms' ),
177 array( 'source' => 'form-submission' )
178 );
179 do_action( 'everest_forms_process_before', $entry, $this->form_data );
180 $logger->info(
181 __( 'Everest Forms Process Before Form ID.', 'everest-forms' ),
182 array( 'source' => 'form-submission' )
183 );
184 do_action( "everest_forms_process_before_{$form_id}", $entry, $this->form_data );
185
186 $ajax_form_submission = isset( $this->form_data['settings']['ajax_form_submission'] ) ? $this->form_data['settings']['ajax_form_submission'] : 0;
187 if ( isset( $this->form_data['payments']['stripe']['enable_stripe'] ) && '1' === $this->form_data['payments']['stripe']['enable_stripe'] ) {
188 $ajax_form_submission = '1';
189 }
190 if ( '1' === $ajax_form_submission ) {
191 // For the sake of validation we completely remove the validator option.
192 update_option( 'evf_validation_error', '' );
193
194 // Prepare fields for entry_save.
195 foreach ( $this->form_data['form_fields'] as $field ) {
196 if ( '' === isset( $this->form_data['form_fields']['meta-key'] ) ) {
197 continue;
198 }
199
200 $field_id = $field['id'];
201 $field_type = $field['type'];
202 $field_submit = isset( $entry['form_fields'][ $field_id ] ) ? $entry['form_fields'][ $field_id ] : '';
203
204 if ( 'signature' === $field_type ) {
205 $field_submit = isset( $field_submit['signature_image'] ) ? $field_submit['signature_image'] : '';
206 }
207
208 $exclude = array( 'title', 'html', 'captcha', 'image-upload', 'file-upload', 'divider', 'reset' );
209
210 if ( ! in_array( $field_type, $exclude, true ) ) {
211
212 $this->form_fields[ $field_id ] = array(
213 'id' => $field_id,
214 'name' => sanitize_text_field( $field['label'] ),
215 'meta_key' => $this->form_data['form_fields'][ $field_id ]['meta-key'],
216 'type' => $field_type,
217 'value' => evf_sanitize_textarea_field( $field_submit ),
218 );
219 }
220 }
221 }
222
223 $this->form_data['entry'] = $entry;
224
225 // Validate fields.
226 foreach ( $this->form_data['form_fields'] as $field ) {
227 $field_id = $field['id'];
228 $field_type = $field['type'];
229 $repeater_fields = array_key_exists( 'repeater-fields', $field ) ? $field['repeater-fields'] : 'no';
230
231 $field_submit = isset( $entry['form_fields'][ $field_id ] ) ? $entry['form_fields'][ $field_id ] : '';
232
233 if ( 'no' === $repeater_fields || 'repeater-fields' === $field_type ) {
234 $logger->info(
235 "Everest Forms Process Before validate {$field_type}.",
236 array( 'source' => 'form-submission' )
237 );
238 do_action( "everest_forms_process_validate_{$field_type}", $field_id, $field_submit, $this->form_data, $field_type );
239 }
240
241 if ( 'credit-card' === $field_type && isset( $_POST['everest_form_stripe_payment_intent_id'] ) ) {
242 $this->evf_notice_print = true;
243 }
244
245 if ( 'yes' === get_option( 'evf_validation_error' ) && $ajax_form_submission ) {
246 if ( count( $this->errors ) ) {
247 foreach ( $this->errors as $_error ) {
248 $this->ajax_err [] = $_error;
249 }
250 }
251 update_option( 'evf_validation_error', '' );
252 }
253 }
254
255 // If validation issues occur, send the results accordingly.
256 if ( $ajax_form_submission && count( $this->ajax_err ) ) {
257 $response_data['error'] = $this->ajax_err;
258 $response_data['message'] = __( 'Form has not been submitted, please see the errors below.', 'everest-forms' );
259 $response_data['response'] = 'error';
260 $logger->error(
261 __( 'Form has not been submitted.', 'everest-forms' ),
262 array( 'source' => 'form-submission' )
263 );
264 return $response_data;
265 }
266
267 // reCAPTCHA check.
268 if ( ! apply_filters( 'everest_forms_recaptcha_disabled', false ) ) {
269 $recaptcha_type = get_option( 'everest_forms_recaptcha_type', 'v2' );
270 $invisible_recaptcha = get_option( 'everest_forms_recaptcha_v2_invisible', 'no' );
271
272 if ( 'v2' === $recaptcha_type && 'no' === $invisible_recaptcha ) {
273 $site_key = get_option( 'everest_forms_recaptcha_v2_site_key' );
274 $secret_key = get_option( 'everest_forms_recaptcha_v2_secret_key' );
275 } elseif ( 'v2' === $recaptcha_type && 'yes' === $invisible_recaptcha ) {
276 $site_key = get_option( 'everest_forms_recaptcha_v2_invisible_site_key' );
277 $secret_key = get_option( 'everest_forms_recaptcha_v2_invisible_secret_key' );
278 } elseif ( 'v3' === $recaptcha_type ) {
279 $site_key = get_option( 'everest_forms_recaptcha_v3_site_key' );
280 $secret_key = get_option( 'everest_forms_recaptcha_v3_secret_key' );
281 } elseif ( 'hcaptcha' === $recaptcha_type ) {
282 $site_key = get_option( 'everest_forms_recaptcha_hcaptcha_site_key' );
283 $secret_key = get_option( 'everest_forms_recaptcha_hcaptcha_secret_key' );
284 } elseif ( 'turnstile' === $recaptcha_type ) {
285 $site_key = get_option( 'everest_forms_recaptcha_turnstile_site_key' );
286 $secret_key = get_option( 'everest_forms_recaptcha_turnstile_secret_key' );
287 $theme_mode = get_option( 'everest_forms_recaptcha_turnstile_theme' );
288 }
289
290 if ( ! empty( $site_key ) && ! empty( $secret_key ) && isset( $this->form_data['settings']['recaptcha_support'] ) && '1' === $this->form_data['settings']['recaptcha_support'] &&
291 ! isset( $_POST['__amp_form_verify'] ) && ( 'v3' === $recaptcha_type || ! evf_is_amp() ) ) {
292 if ( 'hcaptcha' === $recaptcha_type ) {
293 $error = esc_html__( 'hCaptcha verification failed, please try again later.', 'everest-forms' );
294 } elseif ( 'turnstile' === $recaptcha_type ) {
295 $error = esc_html__( 'Cloudflare Turnstile verification failed, please try again later.', 'everest-forms' );
296 } else {
297 $error = esc_html__( 'Google reCAPTCHA verification failed, please try again later.', 'everest-forms' );
298 }
299
300 $logger->error(
301 $error,
302 array( 'source' => 'Google reCAPTCHA' )
303 );
304
305 $token = ! empty( $_POST['g-recaptcha-response'] ) ? evf_clean( wp_unslash( $_POST['g-recaptcha-response'] ) ) : false;
306
307 if ( 'v3' === $recaptcha_type ) {
308 $token = ! empty( $_POST['everest_forms']['recaptcha'] ) ? evf_clean( wp_unslash( $_POST['everest_forms']['recaptcha'] ) ) : false;
309 }
310 if ( 'hcaptcha' === $recaptcha_type ) {
311 $token = ! empty( $_POST['h-captcha-response'] ) ? evf_clean( wp_unslash( $_POST['h-captcha-response'] ) ) : false;
312 $raw_response = wp_safe_remote_get( 'https://hcaptcha.com/siteverify?secret=' . $secret_key . '&response=' . $token );
313 } elseif ( 'turnstile' === $recaptcha_type ) {
314 $token = ! empty( $_POST['cf-turnstile-response'] ) ? evf_clean( wp_unslash( $_POST['cf-turnstile-response'] ) ) : false;
315 $url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
316 $params = array(
317 'method' => 'POST',
318 'body' => array(
319 'secret' => $secret_key,
320 'response' => $token,
321 ),
322 );
323 $raw_response = wp_safe_remote_post( $url, $params );
324 } else {
325 $raw_response = wp_safe_remote_get( 'https://www.google.com/recaptcha/api/siteverify?secret=' . $secret_key . '&response=' . $token );
326 }
327
328 if ( ! is_wp_error( $raw_response ) ) {
329 $response = json_decode( wp_remote_retrieve_body( $raw_response ) );
330 // Check reCAPTCHA response.
331 if ( empty( $response->success ) || ( 'v3' === $recaptcha_type && $response->score <= get_option( 'everest_forms_recaptcha_v3_threshold_score', apply_filters( 'everest_forms_recaptcha_v3_threshold', '0.5' ) ) ) ) {
332 if ( 'v3' === $recaptcha_type ) {
333 if ( isset( $response->score ) ) {
334 $error .= ' (' . esc_html( $response->score ) . ')';
335 }
336 }
337 $this->errors[ $form_id ]['header'] = $error;
338 $logger->error(
339 $error,
340 array( 'source' => 'Google reCAPTCHA' )
341 );
342 return $this->errors;
343 }
344 }
345 }
346 }
347 // Initial error check.
348 $errors = apply_filters( 'everest_forms_process_initial_errors', $this->errors, $this->form_data );
349 if ( isset( $_POST['__amp_form_verify'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
350 if ( empty( $errors[ $form_id ] ) ) {
351 wp_send_json( array(), 200 );
352 } else {
353 $verify_errors = array();
354
355 foreach ( $errors[ $form_id ] as $field_id => $error_fields ) {
356 $field = $this->form_data['fields'][ $field_id ];
357 $field_properties = EVF_Shortcode_Form::get_field_properties( $field, $this->form_data );
358
359 if ( is_string( $error_fields ) ) {
360
361 if ( 'checkbox' === $field['type'] || 'radio' === $field['type'] || 'select' === $field['type'] ) {
362 $first = current( $field_properties['inputs'] );
363 $name = $first['attr']['name'];
364 } elseif ( isset( $field_properties['inputs']['primary']['attr']['name'] ) ) {
365 $name = $field_properties['inputs']['primary']['attr']['name'];
366 }
367
368 $verify_errors[] = array(
369 'name' => $name,
370 'message' => $error_fields,
371 );
372 } else {
373 foreach ( $error_fields as $error_field => $error_message ) {
374
375 if ( isset( $field_properties['inputs'][ $error_field ]['attr']['name'] ) ) {
376 $name = $field_properties['inputs'][ $error_field ]['attr']['name'];
377 }
378
379 $verify_errors[] = array(
380 'name' => $name,
381 'message' => $error_message,
382 );
383 }
384 }
385 }
386
387 wp_send_json(
388 array(
389 'verifyErrors' => $verify_errors,
390 ),
391 400
392 );
393 }
394 return;
395 }
396 if ( ! empty( $errors[ $form_id ] ) ) {
397 if ( empty( $errors[ $form_id ]['header'] ) ) {
398 $errors[ $form_id ]['header'] = __( 'Form has not been submitted, please see the errors below.', 'everest-forms' );
399 $logger->error(
400 $errors[ $form_id ]['header'],
401 array( 'source' => 'form-submission' )
402 );
403 }
404 $this->errors = $errors;
405 return $this->errors;
406 }
407
408 // Early honeypot validation - before actual processing.
409 if ( isset( $this->form_data['settings']['honeypot'] ) && '1' === $this->form_data['settings']['honeypot'] && ! empty( $entry['hp'] ) ) {
410 $honeypot = esc_html__( 'Everest Forms honeypot field triggered.', 'everest-forms' );
411 }
412
413 $honeypot = apply_filters( 'everest_forms_process_honeypot', $honeypot, $this->form_fields, $entry, $this->form_data );
414
415 // If spam - return early.
416 if ( $honeypot ) {
417 $logger = evf_get_logger();
418 $logger->notice( sprintf( 'Spam entry for Form ID %d Response: %s', absint( $this->form_data['id'] ), evf_print_r( $entry, true ) ), array( 'source' => 'honeypot' ) );
419 return $this->errors;
420 }
421
422 // Pass the form created date into the form data.
423 $this->form_data['created'] = $form->post_date;
424
425 // Format and Sanitize inputs.
426 foreach ( (array) $this->form_data['form_fields'] as $field ) {
427 $field_id = $field['id'];
428 $field_key = isset( $field['meta-key'] ) ? $field['meta-key'] : '';
429 $field_type = $field['type'];
430 $field_submit = isset( $entry['form_fields'][ $field_id ] ) ? $entry['form_fields'][ $field_id ] : '';
431 $repeater_fields = array_key_exists( 'repeater-fields', $field ) ? $field['repeater-fields'] : 'no';
432
433 if ( 'no' === $repeater_fields || 'repeater-fields' === $field_type ) {
434 $logger->info(
435 sprintf( 'Everest Forms Process Format %s.', $field_type ),
436 array( 'source' => 'form-submission' )
437 );
438 do_action( "everest_forms_process_format_{$field_type}", $field_id, $field_submit, $this->form_data, $field_key );
439 }
440 }
441
442 // This hook is for internal purposes and should not be leveraged.
443 $logger->info(
444 'Everest Forms Process Format After.',
445 array( 'source' => 'form-submission' )
446 );
447 do_action( 'everest_forms_process_format_after', $this->form_data );
448
449 // Process hooks/filter - this is where most addons should hook
450 // because at this point we have completed all field validation and
451 // formatted the data.
452 $this->form_fields = apply_filters( 'everest_forms_process_filter', $this->form_fields, $entry, $this->form_data );
453 $logger->notice( sprintf( 'Everest Form Process: %s', evf_print_r( $this->form_fields, true ) ) );
454
455 $logger->info(
456 'Everest Forms Process.',
457 array( 'source' => 'form-submission' )
458 );
459 do_action( 'everest_forms_process', $this->form_fields, $entry, $this->form_data );
460 $logger->info(
461 "Everest Forms Process {$form_id}.",
462 array( 'source' => 'form-submission' )
463 );
464 do_action( "everest_forms_process_{$form_id}", $this->form_fields, $entry, $this->form_data );
465
466 $this->form_fields = apply_filters( 'everest_forms_process_after_filter', $this->form_fields, $entry, $this->form_data );
467 $logger->notice( sprintf( 'Everest Form Process After: %s', evf_print_r( $this->form_fields, true ) ) );
468
469 // One last error check - don't proceed if there are any errors.
470 if ( ! empty( $this->errors[ $form_id ] ) ) {
471 if ( empty( $this->errors[ $form_id ]['header'] ) ) {
472 $this->errors[ $form_id ]['header'] = esc_html__( 'Form has not been submitted, please see the errors below.', 'everest-forms' );
473 }
474 $logger->error(
475 __( 'Form has not been submitted', 'everest-forms' ),
476 array( 'source' => 'form-submission' )
477 );
478 return $this->errors;
479 }
480
481 $logger->notice( sprintf( 'Entry is Saving to DataBase' ) );
482 // Success - add entry to database.
483 $logger->info(
484 __( 'Entry Added to Database.', 'everest-forms' ),
485 array( 'source' => 'form-submission' )
486 );
487 $entry_id = $this->entry_save( $this->form_fields, $entry, $this->form_data['id'], $this->form_data );
488 $logger->notice( sprintf( 'Entry is Saved to DataBase' ) );
489
490 $logger->notice( sprintf( 'Sending Email' ) );
491 // Success - send email notification.
492 $logger->info(
493 __( 'Sent Email Notification.', 'everest-forms' ),
494 array( 'source' => 'form-submission' )
495 );
496 $this->entry_email( $this->form_fields, $entry, $this->form_data, $entry_id, 'entry' );
497 $logger->notice( sprintf( 'Successfully Send the email' ) );
498
499 // @todo remove this way of printing notices.
500 add_filter( 'everest_forms_success', array( $this, 'check_success_message' ), 10, 2 );
501
502 // Pass completed and formatted fields in POST.
503 $_POST['everest-forms']['complete'] = $this->form_fields;
504
505 // Pass entry ID in POST.
506 $_POST['everest-forms']['entry_id'] = $entry_id;
507
508 // Post-process hooks.
509 $logger->info(
510 __( 'Everest Forms Process Completed.', 'everest-forms' ),
511 array( 'source' => 'form-submission' )
512 );
513 do_action( 'everest_forms_process_complete', $this->form_fields, $entry, $this->form_data, $entry_id );
514 $logger->info(
515 "Everest Forms Process Completed {$form_id}.",
516 array( 'source' => 'form-submission' )
517 );
518 do_action( "everest_forms_process_complete_{$form_id}", $this->form_fields, $entry, $this->form_data, $entry_id );
519 } catch ( Exception $e ) {
520 evf_add_notice( $e->getMessage(), 'error' );
521 $logger->error(
522 $e->getMessage(),
523 array( 'source' => 'form-submission' )
524 );
525 if ( '1' === $ajax_form_submission ) {
526 $this->errors[] = $e->getMessage();
527 $response_data['message'] = $this->errors;
528 $response_data['response'] = 'error';
529 return $response_data;
530 }
531 }
532
533 $settings = $this->form_data['settings'];
534 $message = isset( $settings['successful_form_submission_message'] ) ? $settings['successful_form_submission_message'] : __( 'Thanks for contacting us! We will be in touch with you shortly.', 'everest-forms' );
535 $pdf_submission = isset( $settings['pdf_submission']['enable_pdf_submission'] ) && 0 !== $settings['pdf_submission']['enable_pdf_submission'] ? $settings['pdf_submission'] : '';
536
537 if ( defined( 'EVF_PDF_SUBMISSION_VERSION' ) && ( 'yes' === get_option( 'everest_forms_pdf_download_after_submit', 'no' ) || ( isset( $pdf_submission['everest_forms_pdf_download_after_submit'] ) && 'yes' === $pdf_submission['everest_forms_pdf_download_after_submit'] ) ) ) {
538 global $__everest_form_id;
539 global $__everest_form_entry_id;
540 $__everest_form_id = $form_id;
541 $__everest_form_entry_id = $entry_id;
542 }
543
544 // Check Conditional Logic and get the redirection URL.
545 $submission_redirection_process = apply_filters( 'everest_forms_submission_redirection_process', array(), $this->form_fields, $this->form_data );
546
547 // Backward compatibility for evf form templates.
548 $this->form_data['settings']['redirect_to'] = '0' === $this->form_data['settings']['redirect_to'] ? 'same' : $this->form_data['settings']['redirect_to'];
549
550 if ( '1' === $ajax_form_submission ) {
551 $response_data['message'] = $message;
552 $response_data['response'] = 'success';
553 $response_data['form_id'] = $form_id;
554 $response_data['entry_id'] = $entry_id;
555
556 if ( defined( 'EVF_PDF_SUBMISSION_VERSION' ) && ( 'yes' === get_option( 'everest_forms_pdf_download_after_submit', 'no' ) || ( isset( $pdf_submission['everest_forms_pdf_download_after_submit'] ) && 'yes' === $pdf_submission['everest_forms_pdf_download_after_submit'] ) ) ) {
557 $response_data['pdf_download'] = true;
558 $pdf_download_message = get_option( 'everest_forms_pdf_custom_download_text', '' );
559
560 if ( isset( $pdf_submission['everest_forms_pdf_custom_download_text'] ) ) {
561 $pdf_download_message = $pdf_submission['everest_forms_pdf_custom_download_text'];
562 }
563
564 if ( empty( $pdf_download_message ) ) {
565 $pdf_download_message = __( 'Download your form submission in PDF format', 'everest-forms' );
566 }
567 $response_data['pdf_download_message'] = $pdf_download_message;
568 }
569
570 // Backward Compatibility Check.
571 switch ( $settings['redirect_to'] ) {
572 case '0':
573 $settings['redirect_to'] = 'same';
574 break;
575
576 case '1':
577 $settings['redirect_to'] = 'custom_page';
578 break;
579
580 case '2':
581 $settings['redirect_to'] = 'external_url';
582 break;
583 }
584
585 // Check for Submission Redirection in Ajax Submission.
586 if ( empty( $submission_redirection_process ) ) {
587 if ( isset( $settings['redirect_to'] ) && 'external_url' === $settings['redirect_to'] ) {
588 $response_data['redirect_url'] = isset( $settings['external_url'] ) ? esc_url( $settings['external_url'] ) : 'undefined';
589 } elseif ( isset( $settings['redirect_to'] ) && 'custom_page' === $settings['redirect_to'] ) {
590 $response_data['redirect_url'] = isset( $settings['custom_page'] ) ? get_page_link( absint( $settings['custom_page'] ) ) : 'undefined';
591 }
592 } else {
593 $response_data['redirect_url'] = $submission_redirection_process['external_url'];
594 }
595
596 // Add notice only if credit card is populated in form fields.
597 if ( isset( $this->evf_notice_print ) && $this->evf_notice_print ) {
598 evf_add_notice( $message, 'success' );
599 }
600 // $this->entry_confirmation_redirect( $this->form_data );
601 $response_data = apply_filters( 'everest_forms_after_success_ajax_message', $response_data, $this->form_data, $entry );
602 return $response_data;
603 } elseif ( ( 'same' === $this->form_data['settings']['redirect_to'] && empty( $submission_redirection_process ) ) || ( ! empty( $submission_redirection_process ) && 'same_page' == $submission_redirection_process['redirect_to'] ) ) {
604 evf_add_notice( $message, 'success' );
605 }
606 $logger->info(
607 'Everest Forms After success Message.',
608 array( 'source' => 'form-submission' )
609 );
610
611 do_action( 'everest_forms_after_success_message', $this->form_data, $entry );
612 $this->entry_confirmation_redirect( $this->form_data );
613 }
614
615 /**
616 * Process AJAX form submission.
617 *
618 * @since 1.6.0
619 *
620 * @param mixed $posted_data Posted data.
621 */
622 public function ajax_form_submission( $posted_data ) {
623 add_filter( 'wp_redirect', array( $this, 'ajax_process_redirect' ), 999 );
624 $process = $this->do_task( $posted_data );
625 return $process;
626 }
627
628 /**
629 * Process AJAX redirect.
630 *
631 * @since 1.6.0
632 *
633 * @param string $url Redirect URL.
634 */
635 public function ajax_process_redirect( $url ) {
636 $form_id = isset( $_POST['everest_forms']['id'] ) ? absint( $_POST['everest_forms']['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification
637
638 if ( empty( $form_id ) ) {
639 wp_send_json_error();
640 }
641
642 $response = array(
643 'form_id' => $form_id,
644 'redirect_url' => $url,
645 );
646
647 $response = apply_filters( 'everest_forms_ajax_submit_redirect', $response, $form_id, $url );
648
649 do_action( 'everest_forms_ajax_submit_completed', $form_id, $response );
650 wp_send_json_success( $response );
651 }
652
653 /**
654 * Check the sucessful message.
655 *
656 * @param bool $status Message status.
657 * @param int $form_id Form ID.
658 */
659 public function check_success_message( $status, $form_id ) {
660 if ( isset( $this->form_data['id'] ) && absint( $this->form_data['id'] ) === $form_id ) {
661 return true;
662 }
663 return false;
664 }
665
666 /**
667 * Validate the form return hash.
668 *
669 * @since 1.0.0
670 *
671 * @param string $hash Base64-encoded hash of form and entry IDs.
672 * @return array|false False for invalid or form id.
673 */
674 public function validate_return_hash( $hash = '' ) {
675 $query_args = base64_decode( $hash );
676
677 parse_str( $query_args, $output );
678
679 // Verify hash matches.
680 if ( wp_hash( $output['form_id'] . ',' . $output['entry_id'] ) !== $output['hash'] ) {
681 return false;
682 }
683
684 // Get lead and verify it is attached to the form we received with it.
685 $entry = evf_get_entry( $output['entry_id'] );
686
687 if ( empty( $entry->form_id ) ) {
688 return false;
689 }
690
691 if ( $output['form_id'] !== $entry->form_id ) {
692 return false;
693 }
694
695 return array(
696 'form_id' => absint( $output['form_id'] ),
697 'entry_id' => absint( $output['form_id'] ),
698 'fields' => null !== $entry && isset( $entry->fields ) ? $entry->fields : array(),
699 );
700 }
701
702 /**
703 * Redirects user to a page or URL specified in the form confirmation settings.
704 *
705 * @since 1.0.0
706 *
707 * @param array $form_data Form data and settings.
708 * @param string $hash Base64-encoded hash of form and entry IDs.
709 */
710 public function entry_confirmation_redirect( $form_data = '', $hash = '' ) {
711 $_POST = array(); // Clear fields after successful form submission.
712
713 // Process return hash.
714 if ( ! empty( $hash ) ) {
715 $hash_data = $this->validate_return_hash( $hash );
716
717 if ( ! $hash_data || ! is_array( $hash_data ) ) {
718 return;
719 }
720
721 $this->is_valid_hash = true;
722 $this->entry_id = absint( $hash_data['entry_id'] );
723 $this->form_fields = json_decode( $hash_data['fields'], true );
724 $this->form_data = evf()->form->get(
725 absint( $hash_data['form_id'] ),
726 array(
727 'content_only' => true,
728 )
729 );
730 } else {
731 $this->form_data = $form_data;
732 }
733
734 $settings = $this->form_data['settings'];
735
736 // Backward Compatibility Check.
737 switch ( $settings['redirect_to'] ) {
738 case '0':
739 $settings['redirect_to'] = 'same';
740 break;
741
742 case '1':
743 $settings['redirect_to'] = 'custom_page';
744 break;
745
746 case '2':
747 $settings['redirect_to'] = 'external_url';
748 break;
749 }
750
751 $submission_redirect_process = apply_filters( 'everest_forms_submission_redirection_process', array(), $this->form_fields, $this->form_data );
752
753 if ( ! empty( $submission_redirect_process ) ) {
754 $settings['redirect_to'] = $submission_redirect_process['redirect_to'];
755 $settings['external_url'] = $submission_redirect_process['external_url'];
756 $settings['custom_page'] = $submission_redirect_process['custom_page'];
757 }
758
759 if ( isset( $settings['redirect_to'] ) && 'custom_page' === $settings['redirect_to'] ) {
760 if ( isset( $settings['enable_redirect_query_string'] ) && '1' === $settings['enable_redirect_query_string'] ) {
761 parse_str( $settings['query_string'], $output );
762 $query_redirect_url = array();
763 foreach ( $output as $key => $value ) {
764 $query_redirect_url[ $key ] = apply_filters( 'everest_forms_process_smart_tags', $value, $this->form_data, $this->form_fields );
765 }
766 $redirect_url = add_query_arg( $query_redirect_url, esc_url( get_page_link( $settings['custom_page'] ) ) );
767 } else {
768 $redirect_url = get_page_link( $settings['custom_page'] );
769 }
770
771 ?>
772 <script>
773 var redirect = '<?php echo esc_url_raw( $redirect_url ); ?>';
774 window.setTimeout( function () {
775 window.location.href = redirect;
776 })
777 </script>
778 <?php
779 } elseif ( isset( $settings['redirect_to'] ) && 'external_url' === $settings['redirect_to'] ) {
780 ?>
781 <script>
782 window.setTimeout( function () {
783 window.location.href = '<?php echo esc_url( $settings['external_url'] ); ?>';
784 })
785 </script>
786 <?php
787 }
788
789 // Redirect if needed, to either a page or URL, after form processing.
790 if ( ! empty( $this->form_data['settings']['confirmation_type'] ) && 'message' !== $this->form_data['settings']['confirmation_type'] ) {
791 if ( 'redirect' === $this->form_data['settings']['confirmation_type'] ) {
792 $url = apply_filters( 'everest_forms_process_smart_tags', $this->form_data['settings']['confirmation_redirect'], $this->form_data, $this->form_fields, $this->entry_id );
793 }
794
795 if ( 'page' === $this->form_data['settings']['confirmation_type'] ) {
796 $url = get_permalink( (int) $this->form_data['settings']['confirmation_page'] );
797 }
798 }
799
800 if ( ! empty( $this->form_data['id'] ) ) {
801 $form_id = $this->form_data['id'];
802 } else {
803 return;
804 }
805 if ( isset( $settings['submission_message_scroll'] ) && $settings['submission_message_scroll'] ) {
806 add_filter( 'everest_forms_success_notice_class', array( $this, 'add_scroll_notice_class' ) );
807 }
808
809 if ( ! empty( $url ) ) {
810 $url = apply_filters( 'everest_forms_process_redirect_url', $url, $form_id, $this->form_fields );
811 wp_safe_redirect( esc_url_raw( $url ) );
812 do_action( 'everest_forms_process_redirect', $form_id );
813 do_action( "everest_forms_process_redirect_{$form_id}", $form_id );
814 exit;
815 }
816 }
817
818 /**
819 * Add scroll notice class.
820 *
821 * @param array $classes Notice Classes.
822 * @return array of notice classes.
823 */
824 public function add_scroll_notice_class( $classes ) {
825 $classes[] = 'everest-forms-submission-scroll';
826
827 return $classes;
828 }
829
830 /**
831 * Sends entry email notifications.
832 *
833 * @param array $fields List of fields.
834 * @param array $entry Submitted form entry.
835 * @param array $form_data Form data and settings.
836 * @param int $entry_id Saved entry id.
837 * @param string $context In which context this email is sent.
838 */
839 public function entry_email( $fields, $entry, $form_data, $entry_id, $context = '' ) {
840 // Provide the opportunity to override via a filter.
841 if ( ! apply_filters( 'everest_forms_entry_email', true, $fields, $entry, $form_data ) ) {
842 return;
843 }
844
845 // Make sure we have an entry id.
846 if ( empty( $this->entry_id ) ) {
847 $this->entry_id = (int) $entry_id;
848 }
849
850 $fields = apply_filters( 'everest_forms_entry_email_data', $fields, $entry, $form_data );
851
852 if ( ! isset( $form_data['settings']['email']['connection_1'] ) ) {
853 $old_email_data = $form_data['settings']['email'];
854 $form_data['settings']['email'] = array();
855 $form_data['settings']['email']['connection_1'] = array( 'connection_name' => __( 'Admin Notification', 'everest-forms' ) );
856
857 $email_settings = array( 'evf_to_email', 'evf_from_name', 'evf_from_email', 'evf_reply_to', 'evf_email_subject', 'enable-ai-email-prompt', 'evf_email_message_prompt', 'evf_email_message', 'attach_pdf_to_admin_email', 'show_header_in_attachment_pdf_file', 'conditional_logic_status', 'conditional_option', 'conditionals' );
858 foreach ( $email_settings as $email_setting ) {
859 $form_data['settings']['email']['connection_1'][ $email_setting ] = isset( $old_email_data[ $email_setting ] ) ? $old_email_data[ $email_setting ] : '';
860 }
861 }
862
863 $notifications = isset( $form_data['settings']['email'] ) ? $form_data['settings']['email'] : array();
864
865 foreach ( $notifications as $connection_id => $notification ) :
866
867 // Don't proceed if email notification is not enabled.
868 if ( isset( $notification['enable_email_notification'] ) && '1' !== $notification['enable_email_notification'] ) {
869 continue;
870 }
871
872 $process_email = apply_filters( 'everest_forms_entry_email_process', true, $fields, $form_data, $context, $connection_id );
873
874 if ( ! $process_email ) {
875 continue;
876 }
877
878 $email = array();
879 $evf_to_email = isset( $notification['evf_to_email'] ) ? $notification['evf_to_email'] : '';
880
881 // Setup email properties.
882 /* translators: %s - form name. */
883 $email['subject'] = ! empty( $notification['evf_email_subject'] ) ? $notification['evf_email_subject'] : sprintf( esc_html__( 'New %s Entry', 'everest-forms' ), $form_data['settings']['form_title'] );
884 $email['address'] = explode( ',', apply_filters( 'everest_forms_process_smart_tags', $evf_to_email, $form_data, $fields, $this->entry_id ) );
885 $email['address'] = array_map( 'sanitize_email', $email['address'] );
886 $email['sender_name'] = ! empty( $notification['evf_from_name'] ) ? $notification['evf_from_name'] : get_bloginfo( 'name' );
887 $email['sender_address'] = ! empty( $notification['evf_from_email'] ) ? $notification['evf_from_email'] : get_option( 'admin_email' );
888 $email['reply_to'] = ! empty( $notification['evf_reply_to'] ) ? $notification['evf_reply_to'] : $email['sender_address'];
889 if ( ! empty( get_option( 'everest_forms_ai_api_key' ) ) ) {
890 $email['message_ai_prompt'] = ! empty( $notification['evf_email_message_prompt'] ) ? $notification['evf_email_message_prompt'] : '';
891 $email['enable_ai_prompt'] = ! empty( $notification['enable_ai_email_prompt'] ) ? $notification['enable_ai_email_prompt'] : 0;
892 }
893 $email['message'] = ! empty( $notification['evf_email_message'] ) ? evf_string_translation( $form_data['id'], 'evf_email_message', $notification['evf_email_message'] ) : '{all_fields}';
894 $email = apply_filters( 'everest_forms_entry_email_atts', $email, $fields, $entry, $form_data );
895 $attachment = '';
896
897 // Create new email.
898 $emails = new EVF_Emails();
899 $emails->__set( 'form_data', $form_data );
900 $emails->__set( 'fields', $fields );
901 $emails->__set( 'entry_id', $entry_id );
902 $emails->__set( 'from_name', $email['sender_name'] );
903 $emails->__set( 'from_address', $email['sender_address'] );
904 $emails->__set( 'reply_to', $email['reply_to'] );
905
906 /**
907 * This filter relies on consistent data being passed for the resultant filters to function.
908 * The third param passed for the filter, $fields, is derived from validation routine, not the DB.
909 */
910 $emails->__set( 'attachments', apply_filters( 'everest_forms_email_file_attachments', $attachment, $fields, $form_data, 'entry-email', $connection_id, $entry_id ) );
911
912 // Maybe include Cc and Bcc email addresses.
913 if ( 'yes' === get_option( 'everest_forms_enable_email_copies' ) ) {
914 if ( ! empty( $notification['evf_carboncopy'] ) ) {
915 $emails->__set( 'cc', $notification['evf_carboncopy'] );
916 }
917 if ( ! empty( $notification['evf_blindcarboncopy'] ) ) {
918 $emails->__set( 'bcc', $notification['evf_blindcarboncopy'] );
919 }
920 }
921
922 $emails = apply_filters( 'everest_forms_entry_email_before_send', $emails );
923
924 // Send entry email.
925 foreach ( $email['address'] as $address ) {
926 $emails->send( trim( $address ), $email['subject'], $email['message'], '', $connection_id );
927 }
928
929 endforeach;
930 if ( isset( $attachment ) ) {
931 do_action( 'everest_forms_remove_attachments_after_send_email', $attachment, $fields, $form_data, 'entry-email', $connection_id, $entry_id );
932 }
933 }
934
935 /**
936 * Saves entry to database.
937 *
938 * @param array $fields List of form fields.
939 * @param array $entry User submitted data.
940 * @param int $form_id Form ID.
941 * @param array $form_data Prepared form settings.
942 * @return int
943 */
944 public function entry_save( $fields, $entry, $form_id, $form_data = array() ) {
945 global $wpdb;
946
947 // Check if form has entries disabled.
948 if ( isset( $form_data['settings']['disabled_entries'] ) && '1' === $form_data['settings']['disabled_entries'] ) {
949 return;
950 }
951
952 // Provide the opportunity to override via a filter.
953 if ( ! apply_filters( 'everest_forms_entry_save', true, $fields, $entry, $form_data ) ) {
954 return;
955 }
956
957 do_action( 'everest_forms_process_entry_save', $fields, $entry, $form_id, $form_data );
958
959 $fields = apply_filters( 'everest_forms_entry_save_data', $fields, $entry, $form_data );
960 $browser = evf_get_browser();
961 $user_ip = evf_get_ip_address();
962 $user_device = evf_get_user_device();
963 $user_agent = $browser['name'] . '/' . $browser['platform'] . '/' . $user_device;
964 $referer = ! empty( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : '';
965 $entry_id = false;
966
967 // GDPR enhancements - If user details are disabled globally discard the IP and UA.
968 if ( 'yes' === get_option( 'everest_forms_disable_user_details' ) ) {
969 $user_agent = '';
970 $user_ip = '';
971 }
972
973 $entry_data = apply_filters(
974 'everest_forms_entry_data',
975 array(
976 'form_id' => $form_id,
977 'user_id' => get_current_user_id(),
978 'user_device' => sanitize_text_field( $user_agent ),
979 'user_ip_address' => sanitize_text_field( $user_ip ),
980 'status' => 'publish',
981 'referer' => $referer,
982 'fields' => wp_json_encode( $fields ),
983 'date_created' => current_time( 'mysql', true ),
984 ),
985 $entry
986 );
987
988 if ( ! $entry_data['form_id'] ) {
989 return new WP_Error( 'no-form-id', __( 'No form ID was found.', 'everest-forms' ) );
990 }
991
992 // Create entry.
993 $success = $wpdb->insert( $wpdb->prefix . 'evf_entries', $entry_data );
994
995 if ( is_wp_error( $success ) || ! $success ) {
996 return new WP_Error( 'could-not-create', __( 'Could not create an entry', 'everest-forms' ) );
997 }
998
999 $entry_id = $wpdb->insert_id;
1000
1001 // Create meta data.
1002 if ( $entry_id ) {
1003 foreach ( $fields as $field ) {
1004 $field = apply_filters( 'everest_forms_entry_save_fields', $field, $form_data, $entry_id );
1005 // Add only whitelisted fields to entry meta.
1006 if ( in_array( $field['type'], array( 'html', 'title' ), true ) ) {
1007 continue;
1008 }
1009
1010 // If empty file is supplied, don't store their data nor send email.
1011 if ( in_array( $field['type'], array( 'image-upload', 'file-upload' ), true ) ) {
1012
1013 // BW compatibility for previous file uploader.
1014 if ( isset( $field['value']['file_url'] ) && '' === $field['value']['file_url'] ) {
1015 continue;
1016 }
1017 }
1018
1019 // If empty label is provided for choice field, don't store their data nor send email.
1020 if ( in_array( $field['type'], array( 'radio', 'payment-multiple' ), true ) ) {
1021 if ( isset( $field['value']['label'] ) && '' === $field['value']['label'] ) {
1022 continue;
1023 }
1024 } elseif ( in_array( $field['type'], array( 'checkbox', 'payment-checkbox' ), true ) ) {
1025 if ( isset( $field['value']['label'] ) && ( empty( $field['value']['label'] ) || '' === current( $field['value']['label'] ) ) ) {
1026 continue;
1027 }
1028 }
1029
1030 if ( isset( $field['meta_key'], $field['value'] ) && '' !== $field['value'] ) {
1031 $entry_metadata = array(
1032 'entry_id' => $entry_id,
1033 'meta_key' => sanitize_key( $field['meta_key'] ),
1034 'meta_value' => maybe_serialize( $field['value'] ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
1035 );
1036
1037 // Insert entry meta.
1038 $wpdb->insert( $wpdb->prefix . 'evf_entrymeta', $entry_metadata );
1039 }
1040 }
1041 }
1042
1043 $this->entry_id = $entry_id;
1044
1045 // Removing Entries Cache.
1046 wp_cache_delete( $entry_id, 'evf-entry' );
1047 wp_cache_delete( $entry_id, 'evf-entrymeta' );
1048 wp_cache_delete( $form_id, 'evf-entries-ids' );
1049 wp_cache_delete( $form_id, 'evf-last-entries-count' );
1050 wp_cache_delete( $form_id, 'evf-search-entries' );
1051 wp_cache_delete( EVF_Cache_Helper::get_cache_prefix( 'entries' ) . '_unread_count', 'entries' );
1052
1053 do_action( 'everest_forms_complete_entry_save', $entry_id, $fields, $entry, $form_id, $form_data );
1054
1055 return $this->entry_id;
1056 }
1057
1058 /**
1059 * Insert or update the slot booking data.
1060 *
1061 * @param int $entry_id Entry id.
1062 * @param array $fields List of form fields.
1063 * @param array $entry User submitted data.
1064 * @param int $form_id Form ID.
1065 * @param array $form_data Prepared form settings.
1066 */
1067 public function update_slot_booking_value( $entry_id, $fields, $entry, $form_id, $form_data ) {
1068 $new_slot_booking_field_meta_key_list = array();
1069 $time_interval = 0;
1070 foreach ( $form_data['form_fields'] as $field ) {
1071 if ( ( 'date-time' === $field['type'] ) && isset( $field['slot_booking_advanced'] ) && evf_string_to_bool( $field['slot_booking_advanced'] ) ) {
1072 $new_slot_booking_field_meta_key_list[ $field['meta-key'] ] = array(
1073 $field['datetime_format'],
1074 $field['date_format'],
1075 $field['date_mode'],
1076 );
1077 $time_interval = $field['time_interval'];
1078 }
1079 }
1080
1081 foreach ( $fields as $key => $value ) {
1082 if ( array_key_exists( $value['meta_key'], $new_slot_booking_field_meta_key_list ) ) {
1083 $new_value = $value['value'];
1084 $datetime_format = $new_slot_booking_field_meta_key_list[ $value['meta_key'] ][0];
1085 $date_format = $new_slot_booking_field_meta_key_list[ $value['meta_key'] ][1];
1086 $mode = $new_slot_booking_field_meta_key_list[ $value['meta_key'] ][2];
1087 $datetime_arr = parse_datetime_values( $new_value, $datetime_format, $date_format, $mode, $time_interval, $entry_id );
1088 }
1089 }
1090 if ( ! empty( $datetime_arr ) ) {
1091 $get_booked_slot = get_option( 'evf_booked_slot', array() );
1092 $new_booked_slot = array( $form_id => $datetime_arr );
1093
1094 if ( empty( $get_booked_slot ) ) {
1095 $all_booked_slot = maybe_serialize( $new_booked_slot );
1096 } else {
1097 $unserialized_booked_slot = maybe_unserialize( $get_booked_slot );
1098
1099 if ( array_key_exists( $form_id, $unserialized_booked_slot ) ) {
1100 $booked_slot = $unserialized_booked_slot[ $form_id ];
1101 $booked_slot = (array) $booked_slot + $datetime_arr;
1102 $new_booked_slot = array( $form_id => $booked_slot );
1103 }
1104
1105 $all_booked_slot = maybe_serialize( array_replace( $unserialized_booked_slot, $new_booked_slot ) );
1106 }
1107
1108 update_option( 'evf_booked_slot', $all_booked_slot );
1109 }
1110 }
1111
1112 /**
1113 * Load Previous Field Value.
1114 *
1115 * @param string $properties Value.
1116 * @param mixed $field Field.
1117 * @param mixed $form_data Form Data.
1118 * @return $properties Properties.
1119 */
1120 public function load_previous_field_value( $properties, $field, $form_data ) {
1121
1122 if ( ! isset( $_POST['everest_forms'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
1123 return $properties;
1124 }
1125 $data = ! empty( $_POST['everest_forms']['form_fields'][ $field['id'] ] ) ? wp_unslash( $_POST['everest_forms']['form_fields'][ $field['id'] ] ) : array(); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1126
1127 if ( 'checkbox' === $field['type'] ) {
1128 foreach ( $field['choices'] as $key => $option_value ) {
1129 $selected = ! empty( $option_value['default'] ) ? $option_value['default'] : '';
1130 foreach ( $data as $value ) {
1131 if ( $value === $option_value['label'] ) {
1132 $selected = 1;
1133 $properties['inputs'][ $key ]['default'] = $selected;
1134 }
1135 }
1136 }
1137 } elseif ( 'radio' === $field['type'] || 'select' === $field['type'] ) {
1138 foreach ( $field['choices'] as $key => $option_value ) {
1139 if ( $data === $option_value['label'] ) { // phpcs:ignore WordPress.Security.NonceVerification
1140 $selected = 1;
1141 $properties['inputs'][ $key ]['default'] = $selected;
1142 }
1143 }
1144 } elseif ( 'likert' === $field['type'] ) {
1145 if ( count( $data ) ) {
1146 foreach ( $data as $row => $col ) {
1147 foreach ( (array) $col as $col_selected ) {
1148 $index = sprintf( 'rows%d_columns%d', (int) $row, (int) $col_selected );
1149 $properties['inputs'][ $index ]['attr']['checked'] = true;
1150 }
1151 }
1152 }
1153 } elseif ( ! is_array( $data ) ) {
1154 $properties['inputs']['primary']['attr']['value'] = esc_attr( $data );
1155 }
1156 return $properties;
1157 }
1158 }
1159