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