RestApi
1 year ago
abstracts
1 year ago
admin
1 year ago
blocks
1 year ago
elementor
2 years ago
export
1 year ago
fields
1 year ago
interfaces
8 years ago
libraries
3 years ago
log-handlers
4 years ago
shortcodes
1 year ago
stats
3 years ago
templates
5 years ago
class-everest-forms.php
1 year ago
class-evf-ajax.php
1 year ago
class-evf-autoloader.php
8 years ago
class-evf-background-process-import-entries.php
2 years ago
class-evf-background-updater.php
8 years ago
class-evf-cache-helper.php
6 years ago
class-evf-cron.php
2 years ago
class-evf-deprecated-action-hooks.php
6 years ago
class-evf-deprecated-filter-hooks.php
5 years ago
class-evf-emails.php
1 year ago
class-evf-fields.php
2 years ago
class-evf-form-handler.php
2 years ago
class-evf-form-task.php
1 year ago
class-evf-forms-features.php
2 years ago
class-evf-frontend-scripts.php
1 year ago
class-evf-install.php
2 years ago
class-evf-integrations.php
2 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-report-cron.php
2 years ago
class-evf-reporting.php
2 years ago
class-evf-session-handler.php
7 years ago
class-evf-shortcodes.php
4 years ago
class-evf-smart-tags.php
1 year 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
1 year ago
evf-deprecated-functions.php
6 years ago
evf-entry-functions.php
2 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
8 years ago
evf-update-functions.php
5 years ago
class-evf-ajax.php
1752 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EverestForms EVF_AJAX. AJAX Event Handlers. |
| 4 | * |
| 5 | * @class EVF_AJAX |
| 6 | * @package EverestForms/Classes |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_AJAX class. |
| 13 | */ |
| 14 | class EVF_AJAX { |
| 15 | |
| 16 | /** |
| 17 | * Background update class. |
| 18 | * |
| 19 | * @var object |
| 20 | */ |
| 21 | private static $background_process; |
| 22 | |
| 23 | /** |
| 24 | * Hook in ajax handlers. |
| 25 | */ |
| 26 | public static function init() { |
| 27 | add_action( 'init', array( __CLASS__, 'define_ajax' ), 0 ); |
| 28 | add_action( 'template_redirect', array( __CLASS__, 'do_evf_ajax' ), 0 ); |
| 29 | self::add_ajax_events(); |
| 30 | add_action( 'init', array( __CLASS__, 'init_background_process' ), 5 ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Init background process. |
| 35 | */ |
| 36 | public static function init_background_process() { |
| 37 | include_once EVF_ABSPATH . 'includes/class-evf-background-process-import-entries.php'; |
| 38 | |
| 39 | self::$background_process = new \EVF_Background_Process_Import_Entries(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Set EVF AJAX constant and headers. |
| 44 | */ |
| 45 | public static function define_ajax() { |
| 46 | // @codingStandardsIgnoreStart |
| 47 | if ( ! empty( $_GET['evf-ajax'] ) ) { |
| 48 | evf_maybe_define_constant( 'DOING_AJAX', true ); |
| 49 | evf_maybe_define_constant( 'EVF_DOING_AJAX', true ); |
| 50 | if ( ! WP_DEBUG || ( WP_DEBUG && ! WP_DEBUG_DISPLAY ) ) { |
| 51 | @ini_set( 'display_errors', 0 ); // Turn off display_errors during AJAX events to prevent malformed JSON. |
| 52 | } |
| 53 | $GLOBALS['wpdb']->hide_errors(); |
| 54 | } |
| 55 | // @codingStandardsIgnoreEnd |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Send headers for EVF Ajax Requests. |
| 60 | * |
| 61 | * @since 1.0.0 |
| 62 | */ |
| 63 | private static function evf_ajax_headers() { |
| 64 | if ( ! headers_sent() ) { |
| 65 | send_origin_headers(); |
| 66 | send_nosniff_header(); |
| 67 | evf_nocache_headers(); |
| 68 | header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
| 69 | header( 'X-Robots-Tag: noindex' ); |
| 70 | status_header( 200 ); |
| 71 | } elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 72 | headers_sent( $file, $line ); |
| 73 | trigger_error( "evf_ajax_headers cannot set headers - headers already sent by {$file} on line {$line}", E_USER_NOTICE ); // @codingStandardsIgnoreLine |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Check for EVF Ajax request and fire action. |
| 79 | */ |
| 80 | public static function do_evf_ajax() { |
| 81 | global $wp_query; |
| 82 | |
| 83 | if ( ! empty( $_GET['evf-ajax'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 84 | $wp_query->set( 'evf-ajax', sanitize_text_field( wp_unslash( $_GET['evf-ajax'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 85 | } |
| 86 | |
| 87 | $action = $wp_query->get( 'evf-ajax' ); |
| 88 | |
| 89 | if ( $action ) { |
| 90 | self::evf_ajax_headers(); |
| 91 | $action = sanitize_text_field( $action ); |
| 92 | do_action( 'evf_ajax_' . $action ); |
| 93 | wp_die(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Hook in methods - uses WordPress ajax handlers (admin-ajax). |
| 99 | */ |
| 100 | public static function add_ajax_events() { |
| 101 | $ajax_events = array( |
| 102 | 'save_form' => false, |
| 103 | 'create_form' => false, |
| 104 | 'get_next_id' => false, |
| 105 | 'install_extension' => false, |
| 106 | 'integration_connect' => false, |
| 107 | 'new_email_add' => false, |
| 108 | 'integration_disconnect' => false, |
| 109 | 'rated' => false, |
| 110 | 'review_dismiss' => false, |
| 111 | 'survey_dismiss' => false, |
| 112 | 'allow_usage_dismiss' => false, |
| 113 | 'php_notice_dismiss' => false, |
| 114 | 'email_failed_notice_dismiss' => false, |
| 115 | 'enabled_form' => false, |
| 116 | 'import_form_action' => false, |
| 117 | 'template_licence_check' => false, |
| 118 | 'template_activate_addon' => false, |
| 119 | 'ajax_form_submission' => true, |
| 120 | 'send_test_email' => false, |
| 121 | 'locate_form_action' => false, |
| 122 | 'slot_booking' => true, |
| 123 | 'active_addons' => false, |
| 124 | 'get_local_font_url' => false, |
| 125 | 'form_migrator_forms_list' => false, |
| 126 | 'form_migrator' => false, |
| 127 | 'fm_dismiss_notice' => false, |
| 128 | 'email_duplicate' => false, |
| 129 | 'form_entry_migrator' => false, |
| 130 | 'embed_form' => false, |
| 131 | 'goto_edit_page' => false, |
| 132 | 'send_routine_report_test_email' => false, |
| 133 | 'map_csv' => false, |
| 134 | 'import_entries' => false, |
| 135 | 'generate_restapi_key' => false, |
| 136 | ); |
| 137 | |
| 138 | foreach ( $ajax_events as $ajax_event => $nopriv ) { |
| 139 | add_action( 'wp_ajax_everest_forms_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
| 140 | |
| 141 | if ( $nopriv ) { |
| 142 | add_action( 'wp_ajax_nopriv_everest_forms_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
| 143 | |
| 144 | // EVF AJAX can be used for frontend ajax requests. |
| 145 | add_action( 'evf_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Ajax handler to get next form ID. |
| 152 | */ |
| 153 | public static function get_next_id() { |
| 154 | // Run a security check. |
| 155 | check_ajax_referer( 'everest_forms_get_next_id', 'security' ); |
| 156 | |
| 157 | $form_id = isset( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : 0; |
| 158 | if ( $form_id < 1 ) { |
| 159 | wp_send_json_error( |
| 160 | array( |
| 161 | 'error' => esc_html__( 'Invalid form', 'everest-forms' ), |
| 162 | ) |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | // Check permisssions. |
| 167 | if ( ! current_user_can( 'everest_forms_edit_form', $form_id ) ) { |
| 168 | wp_send_json_error(); |
| 169 | } |
| 170 | |
| 171 | if ( isset( $_POST['fields'] ) ) { |
| 172 | $fields_data = array(); |
| 173 | for ( $i = 0; $i < $_POST['fields']; $i++ ) { |
| 174 | $field_key = evf()->form->field_unique_key( $form_id ); |
| 175 | $field_id_array = explode( '-', $field_key ); |
| 176 | $new_field_id = ( $field_id_array[ count( $field_id_array ) - 1 ] + 1 ); |
| 177 | $fields_data [] = array( |
| 178 | 'field_id' => $new_field_id, |
| 179 | 'field_key' => $field_key, |
| 180 | ); |
| 181 | } |
| 182 | wp_send_json_success( |
| 183 | $fields_data |
| 184 | ); |
| 185 | } else { |
| 186 | $field_key = evf()->form->field_unique_key( $form_id ); |
| 187 | $field_id_array = explode( '-', $field_key ); |
| 188 | $new_field_id = ( $field_id_array[ count( $field_id_array ) - 1 ] + 1 ); |
| 189 | wp_send_json_success( |
| 190 | array( |
| 191 | 'field_id' => $new_field_id, |
| 192 | 'field_key' => $field_key, |
| 193 | ) |
| 194 | ); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * AJAX create new form. |
| 200 | */ |
| 201 | public static function create_form() { |
| 202 | ob_start(); |
| 203 | |
| 204 | check_ajax_referer( 'everest_forms_create_form', 'security' ); |
| 205 | |
| 206 | // Check permissions. |
| 207 | if ( ! current_user_can( 'everest_forms_create_forms' ) ) { |
| 208 | wp_die( -1 ); |
| 209 | } |
| 210 | |
| 211 | $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : esc_html__( 'Blank Form', 'everest-forms' ); |
| 212 | $template = isset( $_POST['template'] ) ? sanitize_text_field( wp_unslash( $_POST['template'] ) ) : 'blank'; |
| 213 | |
| 214 | $form_id = evf()->form->create( $title, $template ); |
| 215 | |
| 216 | if ( $form_id ) { |
| 217 | $data = array( |
| 218 | 'id' => $form_id, |
| 219 | 'redirect' => add_query_arg( |
| 220 | array( |
| 221 | 'tab' => 'fields', |
| 222 | 'form_id' => $form_id, |
| 223 | ), |
| 224 | admin_url( 'admin.php?page=evf-builder' ) |
| 225 | ), |
| 226 | ); |
| 227 | |
| 228 | wp_send_json_success( $data ); |
| 229 | } |
| 230 | |
| 231 | wp_send_json_error( |
| 232 | array( |
| 233 | 'error' => esc_html__( 'Something went wrong, please try again later', 'everest-forms' ), |
| 234 | ) |
| 235 | ); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * AJAX Form save. |
| 240 | */ |
| 241 | public static function save_form() { |
| 242 | check_ajax_referer( 'everest_forms_save_form', 'security' ); |
| 243 | |
| 244 | $logger = evf_get_logger(); |
| 245 | |
| 246 | // Check permissions. |
| 247 | $logger->info( |
| 248 | __( 'Checking permissions.', 'everest-forms' ), |
| 249 | array( 'source' => 'form-save' ) |
| 250 | ); |
| 251 | if ( ! current_user_can( 'everest_forms_edit_forms' ) ) { |
| 252 | $logger->critical( |
| 253 | __( 'You do not have permission.', 'everest-forms' ), |
| 254 | array( 'source' => 'form-save' ) |
| 255 | ); |
| 256 | die( esc_html__( 'You do not have permission.', 'everest-forms' ) ); |
| 257 | } |
| 258 | |
| 259 | // Check for form data. |
| 260 | $logger->info( |
| 261 | __( 'Checking for form data.', 'everest-forms' ), |
| 262 | array( 'source' => 'form-save' ) |
| 263 | ); |
| 264 | if ( empty( $_POST['form_data'] ) ) { |
| 265 | $logger->critical( |
| 266 | __( 'No data provided.', 'everest-forms' ), |
| 267 | array( 'source' => 'form-save' ) |
| 268 | ); |
| 269 | die( esc_html__( 'No data provided', 'everest-forms' ) ); |
| 270 | } |
| 271 | |
| 272 | $form_post = evf_sanitize_builder( json_decode( wp_unslash( $_POST['form_data'] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 273 | |
| 274 | $data = array(); |
| 275 | |
| 276 | if ( ! is_null( $form_post ) && $form_post ) { |
| 277 | foreach ( $form_post as $post_index => $post_input_data ) { |
| 278 | // For input names that are arrays (e.g. `menu-item-db-id[3][4][5]`), |
| 279 | // derive the array path keys via regex and set the value in $_POST. |
| 280 | preg_match( '#([^\[]*)(\[(.+)\])?#', $post_input_data->name, $matches ); |
| 281 | |
| 282 | $array_bits = array( $matches[1] ); |
| 283 | |
| 284 | if ( isset( $matches[3] ) ) { |
| 285 | $array_bits = array_merge( $array_bits, explode( '][', $matches[3] ) ); |
| 286 | } |
| 287 | |
| 288 | $new_post_data = array(); |
| 289 | |
| 290 | // Build the new array value from leaf to trunk. |
| 291 | for ( $i = count( $array_bits ) - 1; $i >= 0; $i-- ) { |
| 292 | if ( count( $array_bits ) - 1 === $i ) { |
| 293 | if ( '' === $array_bits[ $i ] ) { |
| 294 | $new_post_data [ $post_index ] = wp_slash( $post_input_data->value ); |
| 295 | } else { |
| 296 | $new_post_data[ $array_bits[ $i ] ] = wp_slash( $post_input_data->value ); |
| 297 | } |
| 298 | } else { |
| 299 | $new_post_data = array( |
| 300 | $array_bits[ $i ] => $new_post_data, |
| 301 | ); |
| 302 | } |
| 303 | } |
| 304 | $data = array_replace_recursive( $data, $new_post_data ); |
| 305 | } |
| 306 | } |
| 307 | // Check for empty meta key. |
| 308 | $logger->info( |
| 309 | __( 'Check for empty meta key.', 'everest-forms' ), |
| 310 | array( 'source' => 'form-save' ) |
| 311 | ); |
| 312 | $empty_meta_data = array(); |
| 313 | |
| 314 | // Calculation backward compatibility. |
| 315 | $old_calculation_format = 0; |
| 316 | $new_calculation_format = 0; |
| 317 | $not_supported_operator = 0; |
| 318 | |
| 319 | if ( ! empty( $data['form_fields'] ) ) { |
| 320 | foreach ( $data['form_fields'] as $field_key => $field ) { |
| 321 | if ( ! empty( $field['label'] ) ) { |
| 322 | // Only allow specific html in label. |
| 323 | $data['form_fields'][ $field_key ]['label'] = wp_kses( |
| 324 | $field['label'], |
| 325 | array( |
| 326 | 'a' => array( |
| 327 | 'href' => array(), |
| 328 | 'class' => array(), |
| 329 | ), |
| 330 | 'span' => array( |
| 331 | 'class' => array(), |
| 332 | ), |
| 333 | 'em' => array(), |
| 334 | 'small' => array(), |
| 335 | 'strong' => array(), |
| 336 | ) |
| 337 | ); |
| 338 | |
| 339 | // Register string for translation. |
| 340 | evf_string_translation( $data['id'], $field['id'], $field['label'] ); |
| 341 | } |
| 342 | |
| 343 | if ( empty( $field['meta-key'] ) && ! in_array( $field['type'], array( 'html', 'title', 'captcha', 'divider', 'reset', 'recaptcha', 'hcaptcha', 'turnstile' ), true ) ) { |
| 344 | $empty_meta_data[] = $field['label']; |
| 345 | } |
| 346 | |
| 347 | if ( isset( $field['enable_calculation'] ) && ! empty( $field['enable_calculation'] ) ) { |
| 348 | if ( isset( $field['calculation_field'] ) && ! empty( $field['calculation_field'] ) ) { |
| 349 | $formula = stripslashes( $field['calculation_field'] ); |
| 350 | $old_formula_pattern = '/\{field_id="([^"]+)"\}/'; |
| 351 | preg_match_all( $old_formula_pattern, $formula, $matches ); |
| 352 | |
| 353 | if ( ! empty( $matches[0] ) ) { |
| 354 | ++$old_calculation_format; |
| 355 | } |
| 356 | |
| 357 | preg_match_all( '/\^/', $formula, $operator ); |
| 358 | |
| 359 | if ( ! empty( $operator[0] ) ) { |
| 360 | ++$not_supported_operator; |
| 361 | } |
| 362 | |
| 363 | $new_formula_pattern = '/\$FIELD_(\d+)/'; |
| 364 | preg_match_all( $new_formula_pattern, $formula, $new_matches ); |
| 365 | if ( ! empty( $new_matches[0] ) ) { |
| 366 | ++$new_calculation_format; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if ( ! empty( $empty_meta_data ) ) { |
| 373 | $logger->error( |
| 374 | __( 'Meta Key missing.', 'everest-forms' ), |
| 375 | array( 'source' => 'form-save' ) |
| 376 | ); |
| 377 | wp_send_json_error( |
| 378 | array( |
| 379 | 'errorTitle' => esc_html__( 'Meta Key missing', 'everest-forms' ), |
| 380 | /* translators: %s: empty meta data */ |
| 381 | 'errorMessage' => sprintf( esc_html__( 'Please add Meta key for fields: %s', 'everest-forms' ), '<strong>' . implode( ', ', $empty_meta_data ) . '</strong>' ), |
| 382 | ) |
| 383 | ); |
| 384 | } |
| 385 | |
| 386 | if ( ! empty( $old_calculation_format ) && ! empty( $new_calculation_format ) ) { |
| 387 | $logger->error( |
| 388 | __( 'Formula update error.', 'everest-forms' ), |
| 389 | array( 'source' => 'form-save' ) |
| 390 | ); |
| 391 | wp_send_json_error( |
| 392 | array( |
| 393 | 'errorTitle' => esc_html__( 'Heads Up!', 'everest-forms' ), |
| 394 | /* translators: %s: empty meta data */ |
| 395 | 'errorMessage' => sprintf( esc_html__( 'Seems like your formula is not up to date. We suggest you update your formula.', 'everest-forms' ) ), |
| 396 | ) |
| 397 | ); |
| 398 | } |
| 399 | |
| 400 | if ( ! empty( $not_supported_operator ) && ! empty( $new_calculation_format ) ) { |
| 401 | $logger->error( |
| 402 | __( 'Not supported operator.', 'everest-forms' ), |
| 403 | array( 'source' => 'form-save' ) |
| 404 | ); |
| 405 | wp_send_json_error( |
| 406 | array( |
| 407 | 'errorTitle' => esc_html__( 'Heads Up!', 'everest-forms' ), |
| 408 | /* translators: %s: empty meta data */ |
| 409 | 'errorMessage' => sprintf( esc_html__( 'The ^ sign is now replaced with pow(). Please update accordingly. Tip: pow(a,b) = a^b', 'everest-forms' ) ), |
| 410 | ) |
| 411 | ); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | // Fix for sorting field ordering. |
| 416 | $logger->info( |
| 417 | __( 'Fix for sorting field ordering.', 'everest-forms' ), |
| 418 | array( 'source' => 'form-save' ) |
| 419 | ); |
| 420 | if ( isset( $data['structure'], $data['form_fields'] ) ) { |
| 421 | $structure = evf_flatten_array( $data['structure'] ); |
| 422 | $data['form_fields'] = array_merge( array_intersect_key( array_flip( $structure ), $data['form_fields'] ), $data['form_fields'] ); |
| 423 | } |
| 424 | |
| 425 | $form_id = evf()->form->update( $data['id'], $data ); |
| 426 | $form_styles = get_option( 'everest_forms_styles', array() ); |
| 427 | $logger->info( |
| 428 | __( 'Saving form.', 'everest-forms' ), |
| 429 | array( 'source' => 'form-save' ) |
| 430 | ); |
| 431 | do_action( 'everest_forms_save_form', $form_id, $data, array(), ! empty( $form_styles[ $form_id ] ) ); |
| 432 | |
| 433 | if ( ! $form_id ) { |
| 434 | $logger->error( |
| 435 | __( 'An error occurred while saving the form.', 'everest-forms' ), |
| 436 | array( 'source' => 'form-save' ) |
| 437 | ); |
| 438 | wp_send_json_error( |
| 439 | array( |
| 440 | 'errorTitle' => esc_html__( 'Form not found', 'everest-forms' ), |
| 441 | 'errorMessage' => esc_html__( 'An error occurred while saving the form.', 'everest-forms' ), |
| 442 | ) |
| 443 | ); |
| 444 | } else { |
| 445 | $logger->info( |
| 446 | __( 'Form Saved successfully.', 'everest-forms' ), |
| 447 | array( 'source' => 'form-save' ) |
| 448 | ); |
| 449 | wp_send_json_success( |
| 450 | apply_filters( |
| 451 | 'everest_forms_save_form_data', |
| 452 | array( |
| 453 | 'form_name' => esc_html( $data['settings']['form_title'] ), |
| 454 | 'redirect_url' => admin_url( 'admin.php?page=evf-builder' ), |
| 455 | ), |
| 456 | $form_id, |
| 457 | $data |
| 458 | ) |
| 459 | ); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Ajax handler for form submission. |
| 465 | */ |
| 466 | public static function ajax_form_submission() { |
| 467 | check_ajax_referer( 'everest_forms_ajax_form_submission', 'security' ); |
| 468 | |
| 469 | if ( ! empty( $_POST['everest_forms']['id'] ) ) { |
| 470 | $process = evf()->task->ajax_form_submission( evf_sanitize_entry( wp_unslash( $_POST['everest_forms'] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 471 | if ( isset( $process['response'] ) && 'success' === $process['response'] ) { |
| 472 | wp_send_json_success( $process ); |
| 473 | } |
| 474 | |
| 475 | wp_send_json_error( $process ); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Ajax handler for template required addon activation. |
| 481 | */ |
| 482 | public static function template_activate_addon() { |
| 483 | check_ajax_referer( 'everest_forms_template_licence_check', 'security' ); |
| 484 | |
| 485 | if ( empty( $_POST['addon'] ) ) { |
| 486 | wp_send_json_error( |
| 487 | array( |
| 488 | 'errorCode' => 'no_addon_specified', |
| 489 | 'errorMessage' => esc_html__( 'No Addon specified.', 'everest-forms' ), |
| 490 | ) |
| 491 | ); |
| 492 | } |
| 493 | |
| 494 | $activate = activate_plugin( sanitize_text_field( wp_unslash( $_POST['addon'] ) ) . '/' . sanitize_text_field( wp_unslash( $_POST['addon'] ) ) . '.php' ); |
| 495 | |
| 496 | if ( is_wp_error( $activate ) ) { |
| 497 | wp_send_json_error( |
| 498 | array( |
| 499 | 'errorCode' => 'addon_not_active', |
| 500 | 'errorMessage' => esc_html__( 'Addon can not be activate. Please try again.', 'everest-forms' ), |
| 501 | ) |
| 502 | ); |
| 503 | } else { |
| 504 | wp_send_json_success( 'Addon sucessfully activated.' ); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Ajax handler for licence check. |
| 510 | * |
| 511 | * @global WP_Filesystem_Base $wp_filesystem Subclass |
| 512 | */ |
| 513 | public static function template_licence_check() { |
| 514 | check_ajax_referer( 'everest_forms_template_licence_check', 'security' ); |
| 515 | |
| 516 | if ( empty( $_POST['plan'] ) ) { |
| 517 | wp_send_json_error( |
| 518 | array( |
| 519 | 'plan' => '', |
| 520 | 'errorCode' => 'no_plan_specified', |
| 521 | 'errorMessage' => esc_html__( 'No Plan specified.', 'everest-forms' ), |
| 522 | ) |
| 523 | ); |
| 524 | } |
| 525 | |
| 526 | $addons = array(); |
| 527 | $template_data = EVF_Admin_Form_Templates::get_template_data(); |
| 528 | $template_data = is_array( $template_data ) ? $template_data : array(); |
| 529 | if ( ! empty( $template_data ) ) { |
| 530 | foreach ( $template_data as $template ) { |
| 531 | if ( isset( $_POST['slug'] ) && $template->slug === $_POST['slug'] && in_array( trim( $_POST['plan'] ), $template->plan, true ) ) { |
| 532 | $addons = $template->addons; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | $output = '<div class="everest-forms-recommend-addons">'; |
| 538 | $output .= '<p class="desc plugins-info">' . esc_html__( 'This form template requires the following addons.', 'everest-forms' ) . '</p>'; |
| 539 | $output .= '<table class="plugins-list-table widefat striped">'; |
| 540 | $output .= '<thead><tr><th scope="col" class="manage-column required-plugins" colspan="2">Required Addons</th></tr></thead><tbody id="the-list">'; |
| 541 | $output .= '</div>'; |
| 542 | |
| 543 | $activated = true; |
| 544 | foreach ( $addons as $slug => $addon ) { |
| 545 | if ( is_plugin_active( $slug . '/' . $slug . '.php' ) ) { |
| 546 | $class = 'active'; |
| 547 | $parent_class = ''; |
| 548 | } elseif ( file_exists( WP_PLUGIN_DIR . '/' . $slug . '/' . $slug . '.php' ) ) { |
| 549 | $class = 'activate-now'; |
| 550 | $parent_class = 'inactive'; |
| 551 | $activated = false; |
| 552 | } else { |
| 553 | $class = 'install-now'; |
| 554 | $parent_class = 'inactive'; |
| 555 | $activated = false; |
| 556 | } |
| 557 | $output .= '<tr class="plugin-card-' . $slug . ' plugin ' . $parent_class . '" data-slug="' . $slug . '" data-plugin="' . $slug . '/' . $slug . '.php" data-name="' . $addon . '">'; |
| 558 | $output .= '<td class="plugin-name">' . $addon . '</td>'; |
| 559 | $output .= '<td class="plugin-status"><span class="' . esc_attr( $class ) . '"></span></td>'; |
| 560 | $output .= '</tr>'; |
| 561 | } |
| 562 | $output .= '</tbody></table></div>'; |
| 563 | |
| 564 | wp_send_json_success( |
| 565 | array( |
| 566 | 'html' => $output, |
| 567 | 'activate' => $activated, |
| 568 | ) |
| 569 | ); |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * Ajax handler for installing a extension. |
| 574 | * |
| 575 | * @since 1.2.0 |
| 576 | * |
| 577 | * @see Plugin_Upgrader |
| 578 | * |
| 579 | * @global WP_Filesystem_Base $wp_filesystem Subclass |
| 580 | */ |
| 581 | public static function install_extension() { |
| 582 | check_ajax_referer( 'updates' ); |
| 583 | |
| 584 | if ( empty( $_POST['slug'] ) ) { |
| 585 | wp_send_json_error( |
| 586 | array( |
| 587 | 'slug' => '', |
| 588 | 'errorCode' => 'no_plugin_specified', |
| 589 | 'errorMessage' => esc_html__( 'No plugin specified.', 'everest-forms' ), |
| 590 | ) |
| 591 | ); |
| 592 | } |
| 593 | |
| 594 | $slug = sanitize_key( wp_unslash( $_POST['slug'] ) ); |
| 595 | $plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['slug'] . '/' . $_POST['slug'] . '.php' ) ) ); |
| 596 | $status = array( |
| 597 | 'install' => 'plugin', |
| 598 | 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ), |
| 599 | ); |
| 600 | |
| 601 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 602 | $status['errorMessage'] = esc_html__( 'Sorry, you are not allowed to install plugins on this site.', 'everest-forms' ); |
| 603 | wp_send_json_error( $status ); |
| 604 | } |
| 605 | |
| 606 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 607 | include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 608 | |
| 609 | if ( file_exists( WP_PLUGIN_DIR . '/' . $slug ) ) { |
| 610 | $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); |
| 611 | $status['plugin'] = $plugin; |
| 612 | $status['pluginName'] = $plugin_data['Name']; |
| 613 | |
| 614 | if ( current_user_can( 'activate_plugin', $plugin ) && is_plugin_inactive( $plugin ) ) { |
| 615 | $result = activate_plugin( $plugin ); |
| 616 | |
| 617 | if ( is_wp_error( $result ) ) { |
| 618 | $status['errorCode'] = $result->get_error_code(); |
| 619 | $status['errorMessage'] = $result->get_error_message(); |
| 620 | wp_send_json_error( $status ); |
| 621 | } |
| 622 | |
| 623 | wp_send_json_success( $status ); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | $api = json_decode( |
| 628 | EVF_Updater_Key_API::version( |
| 629 | array( |
| 630 | 'license' => get_option( 'everest-forms-pro_license_key' ), |
| 631 | 'item_name' => ! empty( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '', |
| 632 | ) |
| 633 | ) |
| 634 | ); |
| 635 | |
| 636 | if ( is_wp_error( $api ) ) { |
| 637 | $status['errorMessage'] = $api->get_error_message(); |
| 638 | wp_send_json_error( $status ); |
| 639 | } |
| 640 | |
| 641 | $status['pluginName'] = $api->name; |
| 642 | |
| 643 | $skin = new WP_Ajax_Upgrader_Skin(); |
| 644 | $upgrader = new Plugin_Upgrader( $skin ); |
| 645 | $result = $upgrader->install( $api->download_link ); |
| 646 | |
| 647 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 648 | $status['debug'] = $skin->get_upgrade_messages(); |
| 649 | } |
| 650 | |
| 651 | if ( is_wp_error( $result ) ) { |
| 652 | $status['errorCode'] = $result->get_error_code(); |
| 653 | $status['errorMessage'] = $result->get_error_message(); |
| 654 | wp_send_json_error( $status ); |
| 655 | } elseif ( is_wp_error( $skin->result ) ) { |
| 656 | $status['errorCode'] = $skin->result->get_error_code(); |
| 657 | $status['errorMessage'] = $skin->result->get_error_message(); |
| 658 | wp_send_json_error( $status ); |
| 659 | } elseif ( $skin->get_errors()->get_error_code() ) { |
| 660 | $status['errorMessage'] = $skin->get_error_messages(); |
| 661 | wp_send_json_error( $status ); |
| 662 | } elseif ( is_null( $result ) ) { |
| 663 | global $wp_filesystem; |
| 664 | |
| 665 | $status['errorCode'] = 'unable_to_connect_to_filesystem'; |
| 666 | $status['errorMessage'] = esc_html__( 'Unable to connect to the filesystem. Please confirm your credentials.', 'everest-forms' ); |
| 667 | |
| 668 | // Pass through the error from WP_Filesystem if one was raised. |
| 669 | if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { |
| 670 | $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); |
| 671 | } |
| 672 | |
| 673 | wp_send_json_error( $status ); |
| 674 | } |
| 675 | |
| 676 | $install_status = install_plugin_install_status( $api ); |
| 677 | |
| 678 | if ( current_user_can( 'activate_plugin', $install_status['file'] ) && is_plugin_inactive( $install_status['file'] ) ) { |
| 679 | if ( isset( $_POST['page'] ) && 'everest-forms_page_evf-builder' === $_POST['page'] ) { |
| 680 | activate_plugin( $install_status['file'] ); |
| 681 | } else { |
| 682 | $status['activateUrl'] = |
| 683 | esc_url_raw( |
| 684 | add_query_arg( |
| 685 | array( |
| 686 | 'action' => 'activate', |
| 687 | 'plugin' => $install_status['file'], |
| 688 | '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $install_status['file'] ), |
| 689 | ), |
| 690 | admin_url( 'admin.php?page=evf-addons' ) |
| 691 | ) |
| 692 | ); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | wp_send_json_success( $status ); |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * AJAX Integration connect. |
| 701 | */ |
| 702 | public static function integration_connect() { |
| 703 | check_ajax_referer( 'process-ajax-nonce', 'security' ); |
| 704 | |
| 705 | // Check permissions. |
| 706 | if ( ! current_user_can( 'everest_forms_edit_forms' ) ) { |
| 707 | wp_die( -1 ); |
| 708 | } |
| 709 | |
| 710 | if ( empty( $_POST ) ) { |
| 711 | wp_send_json_error( |
| 712 | array( |
| 713 | 'error' => esc_html__( 'Missing data', 'everest-forms' ), |
| 714 | ) |
| 715 | ); |
| 716 | } |
| 717 | |
| 718 | do_action( 'everest_forms_integration_account_connect_' . ( isset( $_POST['source'] ) ? sanitize_text_field( wp_unslash( $_POST['source'] ) ) : '' ), $_POST ); |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * AJAX Email Add. |
| 723 | */ |
| 724 | public static function new_email_add() { |
| 725 | check_ajax_referer( 'process-ajax-nonce', 'security' ); |
| 726 | |
| 727 | // Check permissions. |
| 728 | if ( ! current_user_can( 'everest_forms_edit_forms' ) ) { |
| 729 | wp_die( -1 ); |
| 730 | } |
| 731 | |
| 732 | $connection_id = 'connection_' . uniqid(); |
| 733 | $preview_url = add_query_arg( |
| 734 | array( |
| 735 | 'evf_email_preview' => $connection_id, |
| 736 | 'form_id' => isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0, |
| 737 | ), |
| 738 | home_url() |
| 739 | ); |
| 740 | wp_send_json_success( |
| 741 | array( |
| 742 | 'connection_id' => $connection_id, |
| 743 | 'preview_url' => $preview_url, |
| 744 | ) |
| 745 | ); |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * AJAX Email Duplicate. |
| 750 | */ |
| 751 | public static function email_duplicate() { |
| 752 | check_ajax_referer( 'process-ajax-nonce', 'security' ); |
| 753 | |
| 754 | // Check permissions. |
| 755 | if ( ! current_user_can( 'everest_forms_edit_forms' ) ) { |
| 756 | wp_die( -1 ); |
| 757 | } |
| 758 | |
| 759 | $connection_id = 'connection_' . uniqid(); |
| 760 | $preview_url = ''; |
| 761 | $preview_url = add_query_arg( |
| 762 | array( |
| 763 | 'evf_email_preview' => $connection_id, |
| 764 | 'form_id' => isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0, |
| 765 | 'preview_url' => $preview_url, |
| 766 | ), |
| 767 | home_url() |
| 768 | ); |
| 769 | |
| 770 | wp_send_json_success( |
| 771 | array( |
| 772 | 'connection_id' => $connection_id, |
| 773 | 'prev_connection_id' => isset( $_POST['prev_connection_id'] ) ? sanitize_text_field( wp_unslash( $_POST['prev_connection_id'] ) ) : '', |
| 774 | 'preview_url' => $preview_url, |
| 775 | ) |
| 776 | ); |
| 777 | } |
| 778 | |
| 779 | |
| 780 | /** |
| 781 | * AJAX Integration disconnect. |
| 782 | */ |
| 783 | public static function integration_disconnect() { |
| 784 | check_ajax_referer( 'process-ajax-nonce', 'security' ); |
| 785 | |
| 786 | // Check permissions. |
| 787 | if ( ! current_user_can( 'everest_forms_edit_forms' ) ) { |
| 788 | wp_die( -1 ); |
| 789 | } |
| 790 | |
| 791 | if ( empty( $_POST ) ) { |
| 792 | wp_send_json_error( |
| 793 | array( |
| 794 | 'error' => esc_html__( 'Missing data', 'everest-forms' ), |
| 795 | ) |
| 796 | ); |
| 797 | } |
| 798 | |
| 799 | do_action( 'everest_forms_integration_account_disconnect_' . ( isset( $_POST['source'] ) ? sanitize_text_field( wp_unslash( $_POST['source'] ) ) : '' ), $_POST ); |
| 800 | |
| 801 | $connected_accounts = get_option( 'everest_forms_integrations', false ); |
| 802 | |
| 803 | if ( ! empty( $connected_accounts[ $_POST['source'] ][ $_POST['key'] ] ) ) { |
| 804 | unset( $connected_accounts[ $_POST['source'] ][ $_POST['key'] ] ); |
| 805 | update_option( 'everest_forms_integrations', $connected_accounts ); |
| 806 | wp_send_json_success( array( 'remove' => true ) ); |
| 807 | } else { |
| 808 | wp_send_json_error( |
| 809 | array( |
| 810 | 'error' => esc_html__( 'Connection missing', 'everest-forms' ), |
| 811 | ) |
| 812 | ); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Triggered when clicking the rating footer. |
| 818 | */ |
| 819 | public static function rated() { |
| 820 | if ( ! current_user_can( 'manage_everest_forms' ) ) { |
| 821 | wp_die( -1 ); |
| 822 | } |
| 823 | update_option( 'everest_forms_admin_footer_text_rated', 1 ); |
| 824 | wp_die(); |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * Triggered when clicking the review notice button. |
| 829 | */ |
| 830 | public static function review_dismiss() { |
| 831 | if ( ! current_user_can( 'manage_everest_forms' ) ) { |
| 832 | wp_die( -1 ); |
| 833 | } |
| 834 | $review = get_option( 'everest_forms_review', array() ); |
| 835 | $review['time'] = current_time( 'timestamp' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested |
| 836 | $review['dismissed'] = true; |
| 837 | update_option( 'everest_forms_review', $review ); |
| 838 | wp_die(); |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * Triggered when clicking the survey notice button. |
| 843 | */ |
| 844 | public static function survey_dismiss() { |
| 845 | |
| 846 | if ( ! current_user_can( 'manage_everest_forms' ) ) { |
| 847 | wp_die( -1 ); |
| 848 | } |
| 849 | $survey = get_option( 'everest_forms_survey', array() ); |
| 850 | $survey['dismissed'] = true; |
| 851 | update_option( 'everest_forms_survey', $survey ); |
| 852 | wp_die(); |
| 853 | } |
| 854 | |
| 855 | /** |
| 856 | * Triggered when clicking the allow usage notice allow or deny buttons. |
| 857 | */ |
| 858 | public static function allow_usage_dismiss() { |
| 859 | check_ajax_referer( 'allow_usage_nonce', '_wpnonce' ); |
| 860 | |
| 861 | if ( ! current_user_can( 'manage_everest_forms' ) ) { |
| 862 | wp_die( -1 ); |
| 863 | } |
| 864 | |
| 865 | $allow_usage_tracking = isset( $_POST['allow_usage_tracking'] ) ? sanitize_text_field( wp_unslash( $_POST['allow_usage_tracking'] ) ) : false; |
| 866 | |
| 867 | update_option( 'everest_forms_allow_usage_notice_shown', true ); |
| 868 | |
| 869 | if ( 'true' === $allow_usage_tracking ) { |
| 870 | update_option( 'everest_forms_allow_usage_tracking', 'yes' ); |
| 871 | } |
| 872 | |
| 873 | wp_die(); |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * Triggered when clicking the PHP deprecation notice. |
| 878 | */ |
| 879 | public static function php_notice_dismiss() { |
| 880 | check_ajax_referer( 'php_notice_nonce', '_wpnonce' ); |
| 881 | |
| 882 | if ( ! current_user_can( 'manage_everest_forms' ) ) { |
| 883 | wp_die( -1 ); |
| 884 | } |
| 885 | $current_date = gmdate( 'Y-m-d' ); |
| 886 | $prompt_count = get_option( 'everest_forms_php_deprecated_notice_prompt_count', 0 ); |
| 887 | |
| 888 | update_option( 'everest_forms_php_deprecated_notice_last_prompt_date', $current_date ); |
| 889 | update_option( 'everest_forms_php_deprecated_notice_prompt_count', ++$prompt_count ); |
| 890 | wp_die(); |
| 891 | } |
| 892 | |
| 893 | |
| 894 | /** |
| 895 | * Triggered when clicking the email failed notice. |
| 896 | */ |
| 897 | public static function email_failed_notice_dismiss() { |
| 898 | check_ajax_referer( 'email_failed_nonce', '_wpnonce' ); |
| 899 | |
| 900 | if ( ! current_user_can( 'manage_everest_forms' ) ) { |
| 901 | wp_die( -1 ); |
| 902 | } |
| 903 | update_option( 'everest_forms_email_send_notice_dismiss', true ); |
| 904 | wp_die(); |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * Triggered when clicking the form toggle. |
| 909 | */ |
| 910 | public static function enabled_form() { |
| 911 | // Run a security check. |
| 912 | check_ajax_referer( 'everest_forms_enabled_form', 'security' ); |
| 913 | |
| 914 | $form_id = isset( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : 0; |
| 915 | $enabled = isset( $_POST['enabled'] ) ? absint( $_POST['enabled'] ) : 0; |
| 916 | |
| 917 | if ( ! current_user_can( 'everest_forms_edit_form', $form_id ) ) { |
| 918 | wp_die( -1 ); |
| 919 | } |
| 920 | |
| 921 | $form_data = evf()->form->get( absint( $form_id ), array( 'content_only' => true ) ); |
| 922 | |
| 923 | $form_data['form_enabled'] = $enabled; |
| 924 | |
| 925 | evf()->form->update( $form_id, $form_data ); |
| 926 | } |
| 927 | |
| 928 | /** |
| 929 | * Import Form ajax. |
| 930 | */ |
| 931 | public static function import_form_action() { |
| 932 | try { |
| 933 | check_ajax_referer( 'process-import-ajax-nonce', 'security' ); |
| 934 | EVF_Admin_Import_Export::import_forms(); |
| 935 | } catch ( Exception $e ) { |
| 936 | wp_send_json_error( |
| 937 | array( |
| 938 | 'message' => $e->getMessage(), |
| 939 | ) |
| 940 | ); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | /** |
| 945 | * Send test email. |
| 946 | */ |
| 947 | public static function send_test_email() { |
| 948 | try { |
| 949 | check_ajax_referer( 'process-ajax-nonce', 'security' ); |
| 950 | $from = esc_attr( get_bloginfo( 'name', 'display' ) ); |
| 951 | $email = sanitize_email( isset( $_POST['email'] ) ? wp_unslash( $_POST['email'] ) : '' ); |
| 952 | |
| 953 | /* translators: %s: from address */ |
| 954 | $subject = 'Everest Form: ' . sprintf( esc_html__( 'Test email from %s', 'everest-forms' ), $from ); |
| 955 | $header = "Reply-To: {{from}} \r\n"; |
| 956 | $header .= 'Content-Type: text/html; charset=UTF-8'; |
| 957 | $message = sprintf( |
| 958 | '%s <br /> %s <br /> %s <br /> %s <br /> %s', |
| 959 | __( 'Congratulations,', 'everest-forms' ), |
| 960 | __( 'Your test email has been received successfully.', 'everest-forms' ), |
| 961 | __( 'We thank you for trying out Everest Forms and joining our mission to make sure you get your emails delivered.', 'everest-forms' ), |
| 962 | __( 'Regards,', 'everest-forms' ), |
| 963 | __( 'Everest Forms Team', 'everest-forms' ) |
| 964 | ); |
| 965 | $status = wp_mail( $email, $subject, $message, $header ); |
| 966 | if ( $status ) { |
| 967 | wp_send_json_success( array( 'message' => __( 'Test email was sent successfully! Please check your inbox to make sure it is delivered.', 'everest-forms' ) ) ); |
| 968 | } else { |
| 969 | wp_send_json_error( array( 'message' => __( 'Test email was unsuccessful! Something went wrong.', 'everest-forms' ) ) ); |
| 970 | } |
| 971 | } catch ( Exception $e ) { |
| 972 | wp_send_json_error( |
| 973 | array( |
| 974 | 'message' => $e->getMessage(), |
| 975 | ) |
| 976 | ); |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | /** |
| 981 | * Send stat routine test email. |
| 982 | * |
| 983 | * @since 2.0.9 |
| 984 | */ |
| 985 | public static function send_routine_report_test_email() { |
| 986 | try { |
| 987 | check_ajax_referer( 'process-ajax-nonce', 'security' ); |
| 988 | $from = esc_attr( get_bloginfo( 'name', 'display' ) ); |
| 989 | $email = esc_attr( get_bloginfo( 'admin_email' ) ); |
| 990 | $evf_routine_report_frequency = get_option( 'everest_forms_entries_reporting_frequency' ); |
| 991 | $evf_routine_report_day = get_option( 'everest_forms_entries_reporting_day' ); |
| 992 | $evf_routine_entries_reporting_email = get_option( 'everest_forms_entries_reporting_email' ); |
| 993 | $subject = get_option( 'everest_forms_entries_reporting_subject', 'Test email from ' . $from ); |
| 994 | $evf_routine_reporting_send_to = get_option( 'everest_forms_email_send_to' ); |
| 995 | $evf_routine_reporting_forms = get_option( 'everest_forms_reporting_form_lists' ); |
| 996 | $evf_routine_reporting_test_email = get_option( 'everest_forms_routine_report_send_email_test_to' ); |
| 997 | |
| 998 | switch ( $evf_routine_report_frequency ) { |
| 999 | case 'Daily': |
| 1000 | $evf_summary_duration = esc_html__( 'in the past week', 'everest-forms' ); |
| 1001 | break; |
| 1002 | |
| 1003 | case 'Weekly': |
| 1004 | $evf_summary_duration = esc_html__( 'yesterday', 'everest-forms' ); |
| 1005 | break; |
| 1006 | |
| 1007 | case 'Monthly': |
| 1008 | $evf_summary_duration = esc_html__( 'in the past month', 'everest-forms' ); |
| 1009 | break; |
| 1010 | } |
| 1011 | /* translators: %s: from address */ |
| 1012 | $subject = 'Everest Form: ' . sprintf( esc_html( $subject ) ); |
| 1013 | $header = "Reply-To: {{from}} \r\n"; |
| 1014 | $header .= 'Content-Type: text/html; charset=UTF-8'; |
| 1015 | $message = '<div class="everest-forms-message-text">'; |
| 1016 | $message .= '<h3 style="text-align:center; color: #ffc107;">' . esc_html( 'PS. This is just the sample data' ) . '</h3>'; |
| 1017 | $message .= '<p><strong>' . esc_html__( 'Hi there!', 'everest-forms' ) . ' 👋</strong></p>'; |
| 1018 | $message .= '<p>' . esc_html__( 'Let\'s see how your forms performed ' . $evf_summary_duration . '.', 'everest-forms' ) . '</p>'; |
| 1019 | $message .= '<br/>'; |
| 1020 | $message .= '<p><strong>' . esc_html__( 'Forms Stats', 'everest-forms' ) . '</strong></p>'; |
| 1021 | $message .= '<table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" style="solid #dddddd; display:block;min-width: 100%;border-collapse: collapse;width:100%; display:table; padding-bottom:2rem" class="evf_entries_summary_table">'; |
| 1022 | $message .= '<thead style="display:block; background:#7e3bd0; color:#fff; padding:1rem;">'; |
| 1023 | $message .= '<tr style="display:flex; justify-content:space-between; paddiing:1rem">'; |
| 1024 | $message .= '<th>' . esc_html__( 'Form Name', 'everest-forms' ) . '</th>'; |
| 1025 | $message .= '<th>' . esc_html__( 'Entries', 'everest-forms' ) . '</th>'; |
| 1026 | $message .= '</tr>'; |
| 1027 | $message .= '</thead>'; |
| 1028 | $message .= '<tbody style="display:block;">'; |
| 1029 | $message .= '<tr style="display:flex; justify-content:space-between; color:#000; padding:1rem">'; |
| 1030 | $message .= '<td>' . esc_html( 'Sample Contact Form' ) . '</td>'; |
| 1031 | $message .= '<td>' . esc_html( '10' ) . '</td>'; |
| 1032 | $message .= '</tr>'; |
| 1033 | $message .= '</tbody>'; |
| 1034 | $message .= '</table>'; |
| 1035 | $message .= '</div>'; |
| 1036 | |
| 1037 | $status = wp_mail( $email, $subject, $message, $header ); |
| 1038 | |
| 1039 | if ( $status ) { |
| 1040 | wp_send_json_success( array( 'message' => __( 'Test email was sent successfully! Please check your inbox to make sure it is delivered.', 'everest-forms' ) ) ); |
| 1041 | } else { |
| 1042 | wp_send_json_error( array( 'message' => __( 'Test email was unsuccessful! Something went wrong.', 'everest-forms' ) ) ); |
| 1043 | } |
| 1044 | } catch ( Exception $e ) { |
| 1045 | wp_send_json_error( |
| 1046 | array( |
| 1047 | 'message' => $e->getMessage(), |
| 1048 | ) |
| 1049 | ); |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | /** |
| 1054 | * Locate form. |
| 1055 | */ |
| 1056 | public static function locate_form_action() { |
| 1057 | global $wpdb; |
| 1058 | try { |
| 1059 | check_ajax_referer( 'process-locate-ajax-nonce', 'security' ); |
| 1060 | $id = isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : ''; |
| 1061 | $everest_form_shortcode = '%[everest_form id="' . $id . '"%'; |
| 1062 | $form_id_shortcode = '%{"formId":"' . $id . '"%'; |
| 1063 | $pages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}posts WHERE post_content LIKE %s OR post_content LIKE %s", $everest_form_shortcode, $form_id_shortcode ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 1064 | $page_list = array(); |
| 1065 | foreach ( $pages as $page ) { |
| 1066 | if ( 'page' === $page->post_type || 'post' === $page->post_type ) { |
| 1067 | $page_title = $page->post_title; |
| 1068 | $page_guid = get_permalink( $page->ID ); |
| 1069 | $page_list[ $page_title ] = $page_guid; |
| 1070 | } |
| 1071 | } |
| 1072 | wp_send_json_success( $page_list ); |
| 1073 | } catch ( Exception $e ) { |
| 1074 | wp_send_json_error( |
| 1075 | array( |
| 1076 | 'message' => $e->getMessage(), |
| 1077 | ) |
| 1078 | ); |
| 1079 | } |
| 1080 | } |
| 1081 | /** |
| 1082 | * Slot booking. |
| 1083 | */ |
| 1084 | public static function slot_booking() { |
| 1085 | try { |
| 1086 | check_ajax_referer( 'everest_forms_slot_booking_nonce', 'security' ); |
| 1087 | $datetime_value = isset( $_POST['data-time-value'] ) ? sanitize_text_field( wp_unslash( $_POST['data-time-value'] ) ) : ''; |
| 1088 | $datetime_format = isset( $_POST['data-time-format'] ) ? sanitize_text_field( wp_unslash( $_POST['data-time-format'] ) ) : ''; |
| 1089 | $date_format = isset( $_POST['data-format'] ) ? sanitize_text_field( wp_unslash( $_POST['data-format'] ) ) : ''; |
| 1090 | $mode = isset( $_POST['mode'] ) ? sanitize_text_field( wp_unslash( $_POST['mode'] ) ) : ''; |
| 1091 | $form_id = isset( $_POST['form-id'] ) ? sanitize_text_field( wp_unslash( $_POST['form-id'] ) ) : ''; |
| 1092 | $time_interval = isset( $_POST['time-interval'] ) ? sanitize_text_field( wp_unslash( $_POST['time-interval'] ) ) : ''; |
| 1093 | $datetime_arr = parse_datetime_values( $datetime_value, $datetime_format, $date_format, $mode, $time_interval ); |
| 1094 | |
| 1095 | if ( empty( $datetime_arr ) ) { |
| 1096 | wp_send_json_error( |
| 1097 | array( |
| 1098 | 'message' => __( 'Please select at least one date time.', 'everest-forms' ), |
| 1099 | ) |
| 1100 | ); |
| 1101 | } |
| 1102 | $booked_slot = maybe_unserialize( get_option( 'evf_booked_slot', '' ) ); |
| 1103 | $is_booked = false; |
| 1104 | if ( ! empty( $booked_slot ) && array_key_exists( $form_id, $booked_slot ) ) { |
| 1105 | foreach ( $datetime_arr as $arr ) { |
| 1106 | |
| 1107 | foreach ( $booked_slot[ $form_id ] as $key => $slot ) { |
| 1108 | if ( $arr[0] >= $slot[0] && $arr[1] <= $slot[1] ) { |
| 1109 | $is_booked = true; |
| 1110 | break; |
| 1111 | } elseif ( $arr[0] >= $slot[0] && $arr[0] < $slot[1] && $arr[1] >= $slot[1] ) { |
| 1112 | $is_booked = true; |
| 1113 | break; |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | if ( $is_booked ) { |
| 1119 | wp_send_json_success( |
| 1120 | array( |
| 1121 | 'message' => __( 'This slot is already booked. Please choose other slot', 'everest-forms' ), |
| 1122 | ) |
| 1123 | ); |
| 1124 | } |
| 1125 | wp_send_json_error( |
| 1126 | array( |
| 1127 | 'message' => __( 'This slot is not booked.', 'everest-forms' ), |
| 1128 | ) |
| 1129 | ); |
| 1130 | |
| 1131 | } catch ( Exception $e ) { |
| 1132 | wp_send_json_error( |
| 1133 | array( |
| 1134 | 'message' => __( 'Something went wrong.', 'everest-forms' ), |
| 1135 | ) |
| 1136 | ); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | /** |
| 1141 | * Activate addons from builder. |
| 1142 | */ |
| 1143 | public static function active_addons() { |
| 1144 | try { |
| 1145 | check_ajax_referer( 'evf_active_nonce', 'security' ); |
| 1146 | $plugin = isset( $_POST['plugin_file'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_file'] ) ) : ''; |
| 1147 | $activate = activate_plugin( $plugin ); |
| 1148 | if ( is_wp_error( $activate ) ) { |
| 1149 | $activation_error = $activate->get_error_message(); |
| 1150 | wp_send_json_error( |
| 1151 | array( |
| 1152 | 'message' => $activation_error, |
| 1153 | ) |
| 1154 | ); |
| 1155 | } else { |
| 1156 | wp_send_json_success( |
| 1157 | array( |
| 1158 | 'message' => __( 'Activated successfully', 'everest-forms' ), |
| 1159 | ) |
| 1160 | ); |
| 1161 | } |
| 1162 | } catch ( Exception $e ) { |
| 1163 | wp_send_json_error( |
| 1164 | array( |
| 1165 | 'message' => $e->getMessage(), |
| 1166 | ) |
| 1167 | ); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | /** |
| 1172 | * Download the provided font and return the url for font file. |
| 1173 | * |
| 1174 | * @since 2.0.8 |
| 1175 | */ |
| 1176 | public static function get_local_font_url() { |
| 1177 | $font_url = isset( $_POST['font_url'] ) ? sanitize_text_field( wp_unslash( $_POST['font_url'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification |
| 1178 | |
| 1179 | $allowed_urls = array( |
| 1180 | 'https://fonts.googleapis.com', |
| 1181 | ); |
| 1182 | |
| 1183 | if ( ! in_array( $font_url, $allowed_urls ) ) { |
| 1184 | return; |
| 1185 | } |
| 1186 | |
| 1187 | if ( str_contains( $font_url, 'https://fonts.googleapis.com' ) ) { |
| 1188 | $font_url = evf_maybe_get_local_font_url( $font_url ); |
| 1189 | } |
| 1190 | |
| 1191 | return wp_send_json_success( $font_url ); |
| 1192 | } |
| 1193 | |
| 1194 | /** |
| 1195 | * Forms list for form migrator. |
| 1196 | * |
| 1197 | * @since 2.0.8 |
| 1198 | * |
| 1199 | * @throws Exception If there is an error. |
| 1200 | */ |
| 1201 | public static function form_migrator_forms_list() { |
| 1202 | try { |
| 1203 | check_ajax_referer( 'evf_form_migrator_forms_list_nonce', 'security' ); |
| 1204 | |
| 1205 | $form_slug = isset( $_POST['form_slug'] ) ? sanitize_text_field( wp_unslash( $_POST['form_slug'] ) ) : ''; |
| 1206 | if ( '' === $form_slug ) { |
| 1207 | wp_send_json_error( |
| 1208 | array( |
| 1209 | 'message' => __( 'Missing form slug !', 'everest-forms' ), |
| 1210 | ) |
| 1211 | ); |
| 1212 | } |
| 1213 | |
| 1214 | // Creating the form instance and getting the form list. |
| 1215 | $class_name = 'EVF_Fm_' . ucfirst( trim( str_replace( '-', '', $form_slug ) ) ); |
| 1216 | |
| 1217 | if ( ! class_exists( $class_name ) ) { |
| 1218 | $except_message = sprintf( '<b><i>%s</i></b> %s', $class_name, esc_html__( 'does not exist.', 'everest-forms' ) ); |
| 1219 | throw new Exception( $except_message ); |
| 1220 | } |
| 1221 | |
| 1222 | $form_instance = new $class_name(); |
| 1223 | $forms_list = $form_instance->get_forms(); |
| 1224 | |
| 1225 | if ( empty( $forms_list ) ) { |
| 1226 | wp_send_json_error( |
| 1227 | array( |
| 1228 | 'message' => esc_html( 'No forms are currently available in the list !!!', 'everest-forms' ), |
| 1229 | ) |
| 1230 | ); |
| 1231 | } |
| 1232 | $row = 0; |
| 1233 | $form_per_page = 5; |
| 1234 | $ceil = ceil( count( $forms_list ) / $form_per_page ); |
| 1235 | $forms_list_table = '<div class="evf-fm-forms-table-wrapper">'; |
| 1236 | $forms_list_table .= '<h4>' . sprintf( '%s %s', esc_html__( 'Import', 'everest-forms' ), $form_instance->name ) . '</h4>'; |
| 1237 | $forms_list_table .= '<table class="evf-fm-forms-table" data-form-slug="' . esc_attr( $form_slug ) . '">'; |
| 1238 | $forms_list_table .= '<tr class="evf-th-title"><th><input id="evf-fm-select-all" type="checkbox" name="fm_select_all_form" /></th><th>' . esc_html__( 'Form Name', 'everest-forms' ) . '</th><th>' . esc_html__( 'Imported', 'everest-forms' ) . '</th><th>' . esc_html__( 'Action', 'everest-forms' ) . '</th></tr>'; |
| 1239 | $hidden = ''; |
| 1240 | $imported = get_option( 'evf_fm_' . $form_slug . '_imported_form_list', array() ); |
| 1241 | foreach ( $forms_list as $form_id => $form_name ) { |
| 1242 | ++$row; |
| 1243 | if ( in_array( $form_id, $imported ) ) { |
| 1244 | $is_imported = true; |
| 1245 | $imported_text = esc_html__( 'Yes', 'everest-forms' ); |
| 1246 | } else { |
| 1247 | $is_imported = false; |
| 1248 | $imported_text = esc_html__( 'No', 'everest-forms' ); |
| 1249 | } |
| 1250 | $forms_list_table .= '<tr id="evf-fm-row-' . esc_attr( $row ) . '" class="evf-fm-row ' . esc_attr( $hidden ) . '"><td><input class="evf-fm-select-single" type="checkbox" name="fm_select_single_form_' . esc_attr( $form_id ) . '" data-form-id="' . esc_attr( $form_id ) . '" /></td><td>' . esc_html__( $form_name, 'everest-forms' ) . '</td><td><p class="evf-fm-imported" data-form-id="' . esc_attr( $form_id ) . '">' . esc_attr( $imported_text ) . '<p></td>'; |
| 1251 | $forms_list_table .= '<td>'; |
| 1252 | $forms_list_table .= '<div class="evf-fm-import-actions"><button class="evf-fm-import-single" data-form-id="' . esc_attr( $form_id ) . '">' . esc_html( 'Import Form' ) . '</button>'; |
| 1253 | if ( 'contact-form-7' !== $form_slug ) { |
| 1254 | $disabled = $is_imported ? '' : 'disabled'; |
| 1255 | $forms_list_table .= '<button class="evf-fm-import-entry" data-form-id="' . esc_attr( $form_id ) . '"' . esc_attr( $disabled ) . '>' . esc_html( 'Import Entry' ) . '</button>'; |
| 1256 | } |
| 1257 | $forms_list_table .= '</div></td></tr>'; |
| 1258 | if ( $row === $form_per_page ) { |
| 1259 | $hidden = 'evf-fm-hide-row'; |
| 1260 | } |
| 1261 | } |
| 1262 | $forms_list_table .= '</table>'; |
| 1263 | $forms_list_table .= '<div class="evf-fm-import-selected-wrapper"><button class="evf-fm-import-selected-btn">' . esc_html( 'Import Selected Forms' ) . '</button>'; |
| 1264 | $forms_list_table .= '<div data-total-page="' . esc_attr( count( $forms_list ) ) . '" data-fm-ceil="' . esc_attr( $ceil ) . '" data-form-per-page="' . esc_attr( $form_per_page ) . '" class="evf-fm-pagination">'; |
| 1265 | |
| 1266 | for ( $page = 1; $page <= $ceil; $page++ ) { |
| 1267 | $active = ''; |
| 1268 | if ( 1 === $page ) { |
| 1269 | $active = 'evf-fm-btn-active'; |
| 1270 | } |
| 1271 | $forms_list_table .= '<button class="evf-fm-page ' . esc_attr( $active ) . '" data-page="' . esc_attr( $page ) . '">' . esc_attr( $page ) . '</button>'; |
| 1272 | } |
| 1273 | $forms_list_table .= '</div></div>'; |
| 1274 | $forms_list_table .= '</div>'; |
| 1275 | wp_send_json_success( |
| 1276 | array( |
| 1277 | 'message' => esc_html__( 'All Forms List', 'everest-forms' ), |
| 1278 | 'forms_list_table' => $forms_list_table, |
| 1279 | ) |
| 1280 | ); |
| 1281 | |
| 1282 | } catch ( Exception $e ) { |
| 1283 | wp_send_json_error( |
| 1284 | array( |
| 1285 | 'message' => $e->getMessage(), |
| 1286 | ) |
| 1287 | ); |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | /** |
| 1292 | * Form migrator. |
| 1293 | * |
| 1294 | * @since 2.0.8 |
| 1295 | * |
| 1296 | * @throws Exception If there is an error. |
| 1297 | */ |
| 1298 | public static function form_migrator() { |
| 1299 | try { |
| 1300 | check_ajax_referer( 'evf_form_migrator_nonce', 'security' ); |
| 1301 | $form_slug = isset( $_POST['form_slug'] ) ? sanitize_text_field( wp_unslash( $_POST['form_slug'] ) ) : ''; |
| 1302 | $form_ids = isset( $_POST['form_ids'] ) ? wp_unslash( $_POST['form_ids'] ) : ''; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 1303 | if ( '' === $form_ids ) { |
| 1304 | wp_send_json_error( |
| 1305 | array( |
| 1306 | 'message' => __( 'Missing Form ID !!!', 'everest-forms' ), |
| 1307 | ) |
| 1308 | ); |
| 1309 | } |
| 1310 | |
| 1311 | $class_name = 'EVF_Fm_' . ucfirst( trim( str_replace( '-', '', $form_slug ) ) ); |
| 1312 | |
| 1313 | if ( ! class_exists( $class_name ) ) { |
| 1314 | $except_message = sprintf( '<b><i>%s</i></b> %s', $class_name, esc_html__( 'does not exist.', 'everest-forms' ) ); |
| 1315 | throw new Exception( $except_message ); |
| 1316 | } |
| 1317 | // Create the instance of class. |
| 1318 | $form_instance = new $class_name(); |
| 1319 | $forms_data = $form_instance->get_fm_mapped_form_data( $form_ids ); |
| 1320 | |
| 1321 | if ( 1 === count( $forms_data ) ) { |
| 1322 | wp_send_json_success( |
| 1323 | array( |
| 1324 | 'message' => sprintf( '%s <a href="%s" target="_blank">%s</a>', __( 'Imported Successfully.', 'everest-forms' ), esc_url( $forms_data[ $form_ids[0] ]['edit'] ), __( 'View Form', 'everest-forms' ) ), |
| 1325 | 'form_data' => $forms_data, |
| 1326 | ) |
| 1327 | ); |
| 1328 | } else { |
| 1329 | wp_send_json_success( |
| 1330 | array( |
| 1331 | 'message' => __( 'Imported Successfully', 'everest-forms' ), |
| 1332 | 'form_data' => $forms_data, |
| 1333 | ) |
| 1334 | ); |
| 1335 | } |
| 1336 | } catch ( Exception $e ) { |
| 1337 | wp_send_json_error( |
| 1338 | array( |
| 1339 | 'message' => $e->getMessage(), |
| 1340 | ) |
| 1341 | ); |
| 1342 | } |
| 1343 | } |
| 1344 | /** |
| 1345 | * Dismiss Form migrator notice. |
| 1346 | * |
| 1347 | * @since 2.0.8 |
| 1348 | */ |
| 1349 | public static function fm_dismiss_notice() { |
| 1350 | try { |
| 1351 | check_ajax_referer( 'evf_fm_dismiss_notice_nonce', 'security' ); |
| 1352 | $option_id = isset( $_POST['option_id'] ) ? sanitize_text_field( $_POST['option_id'] ) : ''; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 1353 | update_option( $option_id, true ); |
| 1354 | |
| 1355 | wp_send_json_success( |
| 1356 | array( |
| 1357 | 'message' => __( 'Updated !', 'everest-forms' ), |
| 1358 | ) |
| 1359 | ); |
| 1360 | } catch ( Exception $e ) { |
| 1361 | wp_send_json_error( |
| 1362 | array( |
| 1363 | 'message' => $e->getMessage(), |
| 1364 | ) |
| 1365 | ); |
| 1366 | } |
| 1367 | } |
| 1368 | /** |
| 1369 | * Form entry migrator. |
| 1370 | * |
| 1371 | * @since 2.0.8 |
| 1372 | * |
| 1373 | * @throws Exception If there is an error. |
| 1374 | */ |
| 1375 | public static function form_entry_migrator() { |
| 1376 | try { |
| 1377 | check_ajax_referer( 'evf_form_entry_migrator_nonce', 'security' ); |
| 1378 | if ( ! wpforms()->is_pro() ) { |
| 1379 | wp_send_json_error( |
| 1380 | array( |
| 1381 | 'message' => esc_html__( 'Entries not available in WPForms Lite.', 'everest-forms' ), |
| 1382 | ) |
| 1383 | ); |
| 1384 | } |
| 1385 | $form_id = isset( $_POST['form_id'] ) ? sanitize_text_field( wp_unslash( $_POST['form_id'] ) ) : ''; |
| 1386 | $form_slug = isset( $_POST['form_slug'] ) ? sanitize_text_field( wp_unslash( $_POST['form_slug'] ) ) : ''; |
| 1387 | |
| 1388 | if ( empty( $form_id ) || empty( $form_slug ) ) { |
| 1389 | |
| 1390 | wp_send_json_error( |
| 1391 | array( |
| 1392 | 'message' => __( 'Invalid Request !!', 'everest-forms' ), |
| 1393 | ) |
| 1394 | ); |
| 1395 | } |
| 1396 | |
| 1397 | $migrated_form_list = get_option( 'evf_fm_' . $form_slug . '_imported_form_list', array() ); |
| 1398 | $evf_form_id = array_search( $form_id, $migrated_form_list ); |
| 1399 | |
| 1400 | if ( ! $evf_form_id ) { |
| 1401 | wp_send_json_error( |
| 1402 | array( |
| 1403 | 'message' => esc_html__( 'Please migrate the form before importing the entry', 'everest-forms' ), |
| 1404 | ) |
| 1405 | ); |
| 1406 | } |
| 1407 | // Creating the form instance and getting the form list. |
| 1408 | $class_name = 'EVF_Fm_' . ucfirst( trim( str_replace( '-', '', $form_slug ) ) ); |
| 1409 | |
| 1410 | if ( ! class_exists( $class_name ) ) { |
| 1411 | $except_message = sprintf( '<b><i>%s</i></b> %s', $class_name, esc_html__( 'does not exist.', 'everest-forms' ) ); |
| 1412 | throw new Exception( $except_message ); |
| 1413 | } |
| 1414 | // Create the instance of class. |
| 1415 | $form_instance = new $class_name(); |
| 1416 | $evf_entries = $form_instance->migrate_entry( $evf_form_id, $form_id ); |
| 1417 | |
| 1418 | $success = array(); |
| 1419 | $unsuccess = array(); |
| 1420 | foreach ( $evf_entries as $key => $entry ) { |
| 1421 | if ( ! $entry ) { |
| 1422 | $unsuccess[] = $key; |
| 1423 | continue; |
| 1424 | } |
| 1425 | $success[] = $key; |
| 1426 | } |
| 1427 | if ( count( $unsuccess ) === 0 ) { |
| 1428 | $response = array( |
| 1429 | 'message' => esc_html__( 'All entries are migrated successfully!!', 'everest-forms' ), |
| 1430 | 'success' => $success, |
| 1431 | 'unsuccess' => $unsuccess, |
| 1432 | ); |
| 1433 | } elseif ( count( $unsuccess ) > 0 && count( $success ) === 0 ) { |
| 1434 | $response = array( |
| 1435 | 'message' => esc_html__( 'Entry migration failed!!', 'everest-forms' ), |
| 1436 | 'success' => $success, |
| 1437 | 'unsuccess' => $unsuccess, |
| 1438 | ); |
| 1439 | } elseif ( count( $unsuccess ) > 0 && count( $success ) > 0 ) { |
| 1440 | $response = array( |
| 1441 | 'message' => esc_html__( 'Only some entries are migrated successfully!!', 'everest-forms' ), |
| 1442 | 'success' => $success, |
| 1443 | 'unsuccess' => $unsuccess, |
| 1444 | ); |
| 1445 | } |
| 1446 | wp_send_json_success( |
| 1447 | $response |
| 1448 | ); |
| 1449 | |
| 1450 | } catch ( Exception $e ) { |
| 1451 | wp_send_json_error( |
| 1452 | array( |
| 1453 | 'message' => $e->getMessage(), |
| 1454 | ) |
| 1455 | ); |
| 1456 | } |
| 1457 | } |
| 1458 | /** |
| 1459 | * Everest Forms - Embed Form. |
| 1460 | * |
| 1461 | * @since 2.0.8 |
| 1462 | */ |
| 1463 | public static function embed_form() { |
| 1464 | check_ajax_referer( 'everest_forms_embed_form', 'security' ); |
| 1465 | $args = array( |
| 1466 | 'post_status' => 'publish', |
| 1467 | 'post_type' => 'page', |
| 1468 | ); |
| 1469 | $pages = get_pages( $args ); |
| 1470 | |
| 1471 | wp_send_json_success( $pages ); |
| 1472 | } |
| 1473 | |
| 1474 | /** |
| 1475 | * Get page edit link |
| 1476 | * |
| 1477 | * @since 2.0.8 |
| 1478 | */ |
| 1479 | public static function goto_edit_page() { |
| 1480 | check_ajax_referer( 'everest_forms_goto_edit_page', 'security' ); |
| 1481 | |
| 1482 | $page_id = empty( $_POST['page_id'] ) ? 0 : sanitize_text_field( absint( $_POST['page_id'] ) ); |
| 1483 | |
| 1484 | if ( empty( $page_id ) ) { |
| 1485 | $url = add_query_arg( 'post_type', 'page', admin_url( 'post-new.php' ) ); |
| 1486 | $meta = array( |
| 1487 | 'embed_page' => 0, |
| 1488 | 'embed_page_title' => ! empty( $_POST['page_title'] ) ? sanitize_text_field( wp_unslash( $_POST['page_title'] ) ) : '', |
| 1489 | ); |
| 1490 | } else { |
| 1491 | $url = get_edit_post_link( $page_id, '' ); |
| 1492 | $meta = array( |
| 1493 | 'embed_page' => $page_id, |
| 1494 | ); |
| 1495 | } |
| 1496 | $page_url = add_query_arg( |
| 1497 | array( |
| 1498 | 'form' => 'everest-forms', |
| 1499 | ), |
| 1500 | esc_url_raw( $url ) |
| 1501 | ); |
| 1502 | $meta['form_id'] = ! empty( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : 0; |
| 1503 | EVF_Admin_Embed_Wizard::set_meta( $meta ); |
| 1504 | |
| 1505 | wp_send_json_success( $page_url ); |
| 1506 | } |
| 1507 | |
| 1508 | /** |
| 1509 | * Map csv fields with form fields. |
| 1510 | * |
| 1511 | * @since 3.0.0 |
| 1512 | */ |
| 1513 | public static function map_csv() { |
| 1514 | check_ajax_referer( 'evf-import-entries', 'security' ); |
| 1515 | |
| 1516 | if ( empty( $_FILES ) ) { |
| 1517 | wp_send_json_error( |
| 1518 | array( |
| 1519 | 'message' => 'Please upload csv file.', |
| 1520 | ) |
| 1521 | ); |
| 1522 | } |
| 1523 | |
| 1524 | $form_id = isset( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : 0; //phpcs:ignore |
| 1525 | $form_fields = evf_get_form_fields( $form_id ); |
| 1526 | |
| 1527 | $csv_header = self::get_csv_header( $_FILES ); |
| 1528 | |
| 1529 | $output = ''; |
| 1530 | $output .= '<div class="evf-map-entries-to-form">'; |
| 1531 | $output .= '<form id="evf-import-entries-form">'; |
| 1532 | $output .= '<p>' . esc_html__( 'Map CSV fields to Form fields.', 'everest-forms' ) . '</p>'; |
| 1533 | $output .= '<div class ="evf-form-fields-and-csv-fields" style="display: flex;">'; |
| 1534 | $output .= '<div class="evf-form-fields" style="width: 50%;">'; |
| 1535 | $output .= '<h4>' . esc_html__( 'Form Fields', 'everest-forms' ) . '</h4>'; |
| 1536 | $output .= '</div>'; |
| 1537 | $output .= '<div class="evf-csv-fields" style="width: 50%;">'; |
| 1538 | $output .= '<h4>' . esc_html__( 'CSV Fields', 'everest-forms' ) . '</h4>'; |
| 1539 | $output .= '</div>'; |
| 1540 | $output .= '</div>'; |
| 1541 | $output .= '<div class = "evf-map-entries-to-form-wrapper" style="display: flex;">'; |
| 1542 | $output .= '<div class="evf-form-fields" style="width: 50%;">'; |
| 1543 | $output .= '<select name="map-entries-to-form" class="evf-form-fields-csv" style="min-width: 90%;">'; |
| 1544 | |
| 1545 | if ( ! empty( $form_fields ) ) { |
| 1546 | foreach ( $form_fields as $key => $value ) { |
| 1547 | $output .= '<option value="' . esc_attr( $key ) . '">' . esc_html( $value['label'] ) . '</option>'; |
| 1548 | } |
| 1549 | } else { |
| 1550 | $output .= '<option value="">' . esc_html__( 'No form fields', 'everest-forms' ) . '</option>'; |
| 1551 | } |
| 1552 | |
| 1553 | $output .= '</select>'; |
| 1554 | $output .= '</div>'; |
| 1555 | $output .= '<div class="evf-csv-fields" style="width: 50%;">'; |
| 1556 | $output .= '<select name="map-entries-to-csv" style="min-width: 90%;">'; |
| 1557 | |
| 1558 | if ( ! empty( $csv_header ) ) { |
| 1559 | foreach ( $csv_header as $value ) { |
| 1560 | $output .= '<option value="' . esc_attr( $value ) . '">' . esc_html( str_replace( '"', '', $value ) ) . '</option>'; |
| 1561 | } |
| 1562 | } else { |
| 1563 | $output .= '<option value="">' . esc_html__( 'No csv fields', 'everest-forms' ) . '</option>'; |
| 1564 | } |
| 1565 | |
| 1566 | $output .= '</select>'; |
| 1567 | $output .= '</div>'; |
| 1568 | $output .= '<span class="actions" style="display: flex; align-items: center; justify-content: space-between;"><a class="evf-add-clone" href="#" style="text-decoration: none;"><i class="dashicons dashicons-plus"></i></a><a class="evf-remove-clone everest-forms-hidden" href="#" style="text-decoration: none;"><i class="dashicons dashicons-minus"></i></a></span>'; |
| 1569 | $output .= '</div>'; |
| 1570 | $output .= '<input type="hidden" name="form_id" value="' . esc_attr( $form_id ) . '">'; |
| 1571 | $output .= '<span class="evf_import_entries_btn"><input type="submit" class="everest-forms-btn everest-forms-btn-primary evf-import-entries-btn" value="' . esc_html__( 'Import Entries', 'everest-forms' ) . '"></span>'; |
| 1572 | $output .= '</form>'; |
| 1573 | $output .= '</div>'; |
| 1574 | |
| 1575 | wp_send_json_success( |
| 1576 | array( |
| 1577 | 'html' => $output, |
| 1578 | ) |
| 1579 | ); |
| 1580 | } |
| 1581 | |
| 1582 | /** |
| 1583 | * Retrieves the header of a CSV file. |
| 1584 | * |
| 1585 | * The function reads the contents of the CSV file, splits it into an array of lines, and retrieves |
| 1586 | * the first line, which represents the header of the CSV file. It then sends a JSON success |
| 1587 | * response with the header as an array of values. |
| 1588 | * |
| 1589 | * @since 3.0.0 |
| 1590 | * |
| 1591 | * @param array $csv_data The CSV data containing the file to be processed. |
| 1592 | */ |
| 1593 | public static function get_csv_header( $csv_data ) { |
| 1594 | |
| 1595 | if ( ! isset( $csv_data['csvfile'] ) ) { |
| 1596 | wp_send_json_error( |
| 1597 | array( |
| 1598 | 'message' => 'Please upload csv file.', |
| 1599 | ) |
| 1600 | ); |
| 1601 | } |
| 1602 | |
| 1603 | $file_extension = strtolower( pathinfo( $csv_data['csvfile']['name'], PATHINFO_EXTENSION ) ); |
| 1604 | |
| 1605 | if ( 'csv' != $file_extension ) { |
| 1606 | wp_send_json_error( |
| 1607 | array( |
| 1608 | 'message' => 'File must be a CSV file.', |
| 1609 | ) |
| 1610 | ); |
| 1611 | } |
| 1612 | |
| 1613 | if ( ! function_exists( 'wp_handle_upload' ) ) { |
| 1614 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 1615 | } |
| 1616 | |
| 1617 | $upload_dir = wp_upload_dir(); |
| 1618 | $filename = 'import_entries_data.csv'; |
| 1619 | $csv_url = $upload_dir['basedir'] . $upload_dir['subdir'] . '/' . $filename; |
| 1620 | |
| 1621 | if ( ! empty( $csv_url ) && file_exists( $csv_url ) ) { |
| 1622 | @unlink( $csv_url ); |
| 1623 | } |
| 1624 | |
| 1625 | $csv_file = $csv_data['csvfile']['tmp_name']; |
| 1626 | $data = file_get_contents( $csv_file ); |
| 1627 | $data_array = explode( "\n", $data ); |
| 1628 | |
| 1629 | $upload_file = move_uploaded_file( $csv_data['csvfile']['tmp_name'], $csv_url ); |
| 1630 | |
| 1631 | if ( empty( $data_array[0] ) ) { |
| 1632 | wp_send_json_error( |
| 1633 | array( |
| 1634 | 'message' => 'CSV file doesn\'t contain any data.', |
| 1635 | ) |
| 1636 | ); |
| 1637 | } |
| 1638 | |
| 1639 | return explode( ',', $data_array[0] ); |
| 1640 | } |
| 1641 | |
| 1642 | /** |
| 1643 | * Import entries from the CSV file and process them in the background. |
| 1644 | * |
| 1645 | * This function checks the AJAX referer and retrieves the mapping fields array from the POST data. |
| 1646 | * It then reads the CSV file and processes each row, updating the options and pushing the data to the background process queue. |
| 1647 | * Finally, it sends a JSON success response with a message and a link to the imported entries. |
| 1648 | * |
| 1649 | * @since 3.0.0 |
| 1650 | * |
| 1651 | * @throws Exception If an error occurs during the import process. |
| 1652 | */ |
| 1653 | public static function import_entries() { |
| 1654 | try { |
| 1655 | check_ajax_referer( 'evf-import-entries', 'security' ); |
| 1656 | |
| 1657 | $map_fields_array = array(); |
| 1658 | |
| 1659 | if ( ! isset( $_POST['data'] ) ) { |
| 1660 | wp_send_json_error( |
| 1661 | array( |
| 1662 | 'message' => 'Something went wrong. Please try again.', |
| 1663 | ) |
| 1664 | ); |
| 1665 | } |
| 1666 | |
| 1667 | $data = ! empty( $_POST['data'] ) ? $_POST['data'] : array(); //phpcs:ignore |
| 1668 | |
| 1669 | if ( empty( $data ) ) { |
| 1670 | wp_send_json_error( |
| 1671 | array( |
| 1672 | 'message' => 'Something went wrong. Please try again.', |
| 1673 | ) |
| 1674 | ); |
| 1675 | } |
| 1676 | |
| 1677 | foreach ( $data as $key => $map_fields ) { |
| 1678 | if ( count( $data ) - 1 === $key ) { |
| 1679 | $map_fields_array['form_id'] = sanitize_text_field( wp_unslash( $map_fields['value'] ) ); //phpcs:ignore |
| 1680 | continue; |
| 1681 | } |
| 1682 | |
| 1683 | if ( 0 != $key % 2 ) { |
| 1684 | continue; |
| 1685 | } |
| 1686 | |
| 1687 | $map_fields_array[ $key ] = array( |
| 1688 | 'field_id' => sanitize_text_field( wp_unslash( $map_fields['value'] ) ), //phpcs:ignore |
| 1689 | 'map_csv_column' => sanitize_text_field( wp_unslash( $data[ ++$key ]['value'] ) ), //phpcs:ignore |
| 1690 | ); |
| 1691 | } |
| 1692 | |
| 1693 | $upload_dir = wp_upload_dir(); |
| 1694 | $filename = 'import_entries_data.csv'; |
| 1695 | $csv_url = $upload_dir['basedir'] . $upload_dir['subdir'] . '/' . $filename; |
| 1696 | |
| 1697 | if ( ! empty( $csv_url ) && file_exists( $csv_url ) ) { |
| 1698 | $csv_file = fopen( $csv_url, 'r' ); |
| 1699 | $row = 0; |
| 1700 | update_option( 'everest_forms_mapping_fields_array', $map_fields_array ); |
| 1701 | while ( ( $row_data = fgetcsv( $csv_file, 0, ',' ) ) !== false ) { |
| 1702 | if ( strlen( implode( $row_data ) ) != 0 ) { |
| 1703 | if ( 0 === $row ) { |
| 1704 | update_option( 'everest_forms_csv_titles', $row_data ); |
| 1705 | } else { |
| 1706 | self::$background_process->push_to_queue( $row_data ); |
| 1707 | } |
| 1708 | $row++; |
| 1709 | } |
| 1710 | } |
| 1711 | fclose( $csv_file ); |
| 1712 | unlink( $csv_url ); |
| 1713 | $test = self::$background_process->save()->dispatch(); |
| 1714 | } |
| 1715 | |
| 1716 | wp_send_json_success( |
| 1717 | array( |
| 1718 | 'message' => 'Your data is currently being imported in the background. Please check the imported entries shortly.', |
| 1719 | 'entry_link' => admin_url( 'admin.php?page=evf-entries&form_id=' . $map_fields_array['form_id'] ), |
| 1720 | 'button_text' => 'View Entries', |
| 1721 | ) |
| 1722 | ); |
| 1723 | } catch ( Exception $e ) { |
| 1724 | wp_send_json_error( |
| 1725 | array( |
| 1726 | 'message' => $e->getMessage(), |
| 1727 | ) |
| 1728 | ); |
| 1729 | } |
| 1730 | } |
| 1731 | /** |
| 1732 | * Generate the restapi key |
| 1733 | * |
| 1734 | * @since 3.0.5 |
| 1735 | */ |
| 1736 | public static function generate_restapi_key() { |
| 1737 | try { |
| 1738 | check_ajax_referer( 'process-restapi-api-ajax-nonce', 'security' ); |
| 1739 | $key = generate_api_key(); |
| 1740 | wp_send_json_success( $key ); |
| 1741 | } catch ( Exception $e ) { |
| 1742 | wp_send_json_error( |
| 1743 | array( |
| 1744 | 'message' => $e->getMessage(), |
| 1745 | ) |
| 1746 | ); |
| 1747 | } |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | EVF_AJAX::init(); |
| 1752 |