PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 2.0.3
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v2.0.3
3.5.2 3.5.1 3.5.0 3.4.8 3.4.7 3.4.6 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5.1 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.10 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.6.1 1.6.7 1.7.0 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.2 1.7.2.1 1.7.2.2 1.7.3 1.7.4 1.7.5 1.7.5.1 1.7.5.2 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.8.0 1.8.0.1 1.8.1 1.8.2 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.0.1 1.9.1 1.9.2 1.9.3 1.9.4 1.9.4.1 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.0.1 2.0.1 2.0.2 2.0.3 2.0.3.1 2.0.4 2.0.4.1 2.0.5 2.0.6 2.0.7 2.0.8 2.0.8.1 2.0.9 3.0.0 3.0.0.1 3.0.1 3.0.2 3.0.3 3.0.3.1 3.0.4 3.0.4.1 3.0.4.2 3.0.5 3.0.5.1 3.0.5.2 3.0.6 3.0.6.1 3.0.7.1 3.0.8 3.0.8.1 3.0.9 3.0.9.1 3.0.9.2 3.0.9.3 3.0.9.4 3.0.9.5 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.3.0 3.4.0 3.4.1 3.4.2 3.4.2.1 3.4.3 3.4.4 3.4.5 trunk 1.0 1.0.1 1.0.2 1.0.3
everest-forms / includes / class-evf-ajax.php
everest-forms / includes Last commit date
abstracts 2 years ago admin 2 years ago elementor 4 years ago export 3 years ago fields 2 years ago interfaces 8 years ago libraries 2 years ago log-handlers 4 years ago shortcodes 2 years ago stats 3 years ago templates 5 years ago class-everest-forms.php 2 years ago class-evf-ajax.php 2 years ago class-evf-autoloader.php 7 years ago class-evf-background-updater.php 7 years ago class-evf-cache-helper.php 6 years ago class-evf-cron.php 3 years ago class-evf-deprecated-action-hooks.php 6 years ago class-evf-deprecated-filter-hooks.php 5 years ago class-evf-emails.php 2 years ago class-evf-fields.php 2 years ago class-evf-form-block.php 4 years ago class-evf-form-handler.php 3 years ago class-evf-form-task.php 2 years ago class-evf-forms-features.php 2 years ago class-evf-frontend-scripts.php 2 years ago class-evf-install.php 2 years ago class-evf-integrations.php 7 years ago class-evf-log-levels.php 8 years ago class-evf-logger.php 5 years ago class-evf-post-types.php 5 years ago class-evf-privacy.php 6 years ago class-evf-session-handler.php 7 years ago class-evf-shortcodes.php 4 years ago class-evf-smart-tags.php 2 years ago class-evf-template-loader.php 2 years ago class-evf-validation.php 6 years ago evf-conditional-functions.php 6 years ago evf-core-functions.php 2 years ago evf-deprecated-functions.php 6 years ago evf-entry-functions.php 3 years ago evf-formatting-functions.php 4 years ago evf-notice-functions.php 4 years ago evf-template-functions.php 4 years ago evf-template-hooks.php 7 years ago evf-update-functions.php 5 years ago
class-evf-ajax.php
977 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 * Hook in ajax handlers.
18 */
19 public static function init() {
20 add_action( 'init', array( __CLASS__, 'define_ajax' ), 0 );
21 add_action( 'template_redirect', array( __CLASS__, 'do_evf_ajax' ), 0 );
22 self::add_ajax_events();
23 }
24
25 /**
26 * Set EVF AJAX constant and headers.
27 */
28 public static function define_ajax() {
29 // @codingStandardsIgnoreStart
30 if ( ! empty( $_GET['evf-ajax'] ) ) {
31 evf_maybe_define_constant( 'DOING_AJAX', true );
32 evf_maybe_define_constant( 'EVF_DOING_AJAX', true );
33 if ( ! WP_DEBUG || ( WP_DEBUG && ! WP_DEBUG_DISPLAY ) ) {
34 @ini_set( 'display_errors', 0 ); // Turn off display_errors during AJAX events to prevent malformed JSON.
35 }
36 $GLOBALS['wpdb']->hide_errors();
37 }
38 // @codingStandardsIgnoreEnd
39 }
40
41 /**
42 * Send headers for EVF Ajax Requests.
43 *
44 * @since 1.0.0
45 */
46 private static function evf_ajax_headers() {
47 if ( ! headers_sent() ) {
48 send_origin_headers();
49 send_nosniff_header();
50 evf_nocache_headers();
51 header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
52 header( 'X-Robots-Tag: noindex' );
53 status_header( 200 );
54 } elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
55 headers_sent( $file, $line );
56 trigger_error( "evf_ajax_headers cannot set headers - headers already sent by {$file} on line {$line}", E_USER_NOTICE ); // @codingStandardsIgnoreLine
57 }
58 }
59
60 /**
61 * Check for EVF Ajax request and fire action.
62 */
63 public static function do_evf_ajax() {
64 global $wp_query;
65
66 if ( ! empty( $_GET['evf-ajax'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
67 $wp_query->set( 'evf-ajax', sanitize_text_field( wp_unslash( $_GET['evf-ajax'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification
68 }
69
70 $action = $wp_query->get( 'evf-ajax' );
71
72 if ( $action ) {
73 self::evf_ajax_headers();
74 $action = sanitize_text_field( $action );
75 do_action( 'evf_ajax_' . $action );
76 wp_die();
77 }
78 }
79
80 /**
81 * Hook in methods - uses WordPress ajax handlers (admin-ajax).
82 */
83 public static function add_ajax_events() {
84 $ajax_events = array(
85 'save_form' => false,
86 'create_form' => false,
87 'get_next_id' => false,
88 'install_extension' => false,
89 'integration_connect' => false,
90 'new_email_add' => false,
91 'integration_disconnect' => false,
92 'rated' => false,
93 'review_dismiss' => false,
94 'survey_dismiss' => false,
95 'allow_usage_dismiss' => false,
96 'php_notice_dismiss' => false,
97 'enabled_form' => false,
98 'import_form_action' => false,
99 'template_licence_check' => false,
100 'template_activate_addon' => false,
101 'ajax_form_submission' => true,
102 'send_test_email' => false,
103 'locate_form_action' => false,
104 'slot_booking' => true,
105 'active_addons' => false,
106 'get_local_font_url' => true,
107 );
108
109 foreach ( $ajax_events as $ajax_event => $nopriv ) {
110 add_action( 'wp_ajax_everest_forms_' . $ajax_event, array( __CLASS__, $ajax_event ) );
111
112 if ( $nopriv ) {
113 add_action( 'wp_ajax_nopriv_everest_forms_' . $ajax_event, array( __CLASS__, $ajax_event ) );
114
115 // EVF AJAX can be used for frontend ajax requests.
116 add_action( 'evf_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) );
117 }
118 }
119 }
120
121 /**
122 * Ajax handler to get next form ID.
123 */
124 public static function get_next_id() {
125 // Run a security check.
126 check_ajax_referer( 'everest_forms_get_next_id', 'security' );
127
128 $form_id = isset( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : 0;
129 if ( $form_id < 1 ) {
130 wp_send_json_error(
131 array(
132 'error' => esc_html__( 'Invalid form', 'everest-forms' ),
133 )
134 );
135 }
136
137 // Check permisssions.
138 if ( ! current_user_can( 'everest_forms_edit_form', $form_id ) ) {
139 wp_send_json_error();
140 }
141
142 if ( isset( $_POST['fields'] ) ) {
143 $fields_data = array();
144 for ( $i = 0; $i < $_POST['fields']; $i++ ) {
145 $field_key = evf()->form->field_unique_key( $form_id );
146 $field_id_array = explode( '-', $field_key );
147 $new_field_id = ( $field_id_array[ count( $field_id_array ) - 1 ] + 1 );
148 $fields_data [] = array(
149 'field_id' => $new_field_id,
150 'field_key' => $field_key,
151 );
152 }
153 wp_send_json_success(
154 $fields_data
155 );
156 } else {
157 $field_key = evf()->form->field_unique_key( $form_id );
158 $field_id_array = explode( '-', $field_key );
159 $new_field_id = ( $field_id_array[ count( $field_id_array ) - 1 ] + 1 );
160 wp_send_json_success(
161 array(
162 'field_id' => $new_field_id,
163 'field_key' => $field_key,
164 )
165 );
166 }
167 }
168
169 /**
170 * AJAX create new form.
171 */
172 public static function create_form() {
173 ob_start();
174
175 check_ajax_referer( 'everest_forms_create_form', 'security' );
176
177 // Check permissions.
178 if ( ! current_user_can( 'everest_forms_create_forms' ) ) {
179 wp_die( -1 );
180 }
181
182 $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : esc_html__( 'Blank Form', 'everest-forms' );
183 $template = isset( $_POST['template'] ) ? sanitize_text_field( wp_unslash( $_POST['template'] ) ) : 'blank';
184
185 $form_id = evf()->form->create( $title, $template );
186
187 if ( $form_id ) {
188 $data = array(
189 'id' => $form_id,
190 'redirect' => add_query_arg(
191 array(
192 'tab' => 'fields',
193 'form_id' => $form_id,
194 ),
195 admin_url( 'admin.php?page=evf-builder' )
196 ),
197 );
198
199 wp_send_json_success( $data );
200 }
201
202 wp_send_json_error(
203 array(
204 'error' => esc_html__( 'Something went wrong, please try again later', 'everest-forms' ),
205 )
206 );
207 }
208
209 /**
210 * AJAX Form save.
211 */
212 public static function save_form() {
213 check_ajax_referer( 'everest_forms_save_form', 'security' );
214
215 $logger = evf_get_logger();
216
217 // Check permissions.
218 $logger->info(
219 __( 'Checking permissions.', 'everest-forms' ),
220 array( 'source' => 'form-save' )
221 );
222 if ( ! current_user_can( 'everest_forms_edit_forms' ) ) {
223 $logger->critical(
224 __( 'You do not have permission.', 'everest-forms' ),
225 array( 'source' => 'form-save' )
226 );
227 die( esc_html__( 'You do not have permission.', 'everest-forms' ) );
228 }
229
230 // Check for form data.
231 $logger->info(
232 __( 'Checking for form data.', 'everest-forms' ),
233 array( 'source' => 'form-save' )
234 );
235 if ( empty( $_POST['form_data'] ) ) {
236 $logger->critical(
237 __( 'No data provided.', 'everest-forms' ),
238 array( 'source' => 'form-save' )
239 );
240 die( esc_html__( 'No data provided', 'everest-forms' ) );
241 }
242
243 $form_post = evf_sanitize_builder( json_decode( wp_unslash( $_POST['form_data'] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
244
245 $data = array();
246
247 if ( ! is_null( $form_post ) && $form_post ) {
248 foreach ( $form_post as $post_index => $post_input_data ) {
249 // For input names that are arrays (e.g. `menu-item-db-id[3][4][5]`),
250 // derive the array path keys via regex and set the value in $_POST.
251 preg_match( '#([^\[]*)(\[(.+)\])?#', $post_input_data->name, $matches );
252
253 $array_bits = array( $matches[1] );
254
255 if ( isset( $matches[3] ) ) {
256 $array_bits = array_merge( $array_bits, explode( '][', $matches[3] ) );
257 }
258
259 $new_post_data = array();
260
261 // Build the new array value from leaf to trunk.
262 for ( $i = count( $array_bits ) - 1; $i >= 0; $i-- ) {
263 if ( count( $array_bits ) - 1 === $i ) {
264 if ( '' === $array_bits[ $i ] ) {
265 $new_post_data [ $post_index ] = wp_slash( $post_input_data->value );
266 } else {
267 $new_post_data[ $array_bits[ $i ] ] = wp_slash( $post_input_data->value );
268 }
269 } else {
270 $new_post_data = array(
271 $array_bits[ $i ] => $new_post_data,
272 );
273 }
274 }
275 $data = array_replace_recursive( $data, $new_post_data );
276 }
277 }
278 // Check for empty meta key.
279 $logger->info(
280 __( 'Check for empty meta key.', 'everest-forms' ),
281 array( 'source' => 'form-save' )
282 );
283 $empty_meta_data = array();
284 if ( ! empty( $data['form_fields'] ) ) {
285 foreach ( $data['form_fields'] as $field_key => $field ) {
286 if ( ! empty( $field['label'] ) ) {
287 // Only allow specific html in label.
288 $data['form_fields'][ $field_key ]['label'] = wp_kses(
289 $field['label'],
290 array(
291 'a' => array(
292 'href' => array(),
293 'class' => array(),
294 ),
295 'span' => array(
296 'class' => array(),
297 ),
298 'em' => array(),
299 'small' => array(),
300 'strong' => array(),
301 )
302 );
303
304 // Register string for translation.
305 evf_string_translation( $data['id'], $field['id'], $field['label'] );
306 }
307
308 if ( empty( $field['meta-key'] ) && ! in_array( $field['type'], array( 'html', 'title', 'captcha', 'divider', 'reset' ), true ) ) {
309 $empty_meta_data[] = $field['label'];
310 }
311 }
312
313 if ( ! empty( $empty_meta_data ) ) {
314 $logger->error(
315 __( 'Meta Key missing.', 'everest-forms' ),
316 array( 'source' => 'form-save' )
317 );
318 wp_send_json_error(
319 array(
320 'errorTitle' => esc_html__( 'Meta Key missing', 'everest-forms' ),
321 /* translators: %s: empty meta data */
322 'errorMessage' => sprintf( esc_html__( 'Please add Meta key for fields: %s', 'everest-forms' ), '<strong>' . implode( ', ', $empty_meta_data ) . '</strong>' ),
323 )
324 );
325 }
326 }
327
328 // Fix for sorting field ordering.
329 $logger->info(
330 __( 'Fix for sorting field ordering.', 'everest-forms' ),
331 array( 'source' => 'form-save' )
332 );
333 if ( isset( $data['structure'], $data['form_fields'] ) ) {
334 $structure = evf_flatten_array( $data['structure'] );
335 $data['form_fields'] = array_merge( array_intersect_key( array_flip( $structure ), $data['form_fields'] ), $data['form_fields'] );
336 }
337
338 $form_id = evf()->form->update( $data['id'], $data );
339 $form_styles = get_option( 'everest_forms_styles', array() );
340 $logger->info(
341 __( 'Saving form.', 'everest-forms' ),
342 array( 'source' => 'form-save' )
343 );
344 do_action( 'everest_forms_save_form', $form_id, $data, array(), ! empty( $form_styles[ $form_id ] ) );
345
346 if ( ! $form_id ) {
347 $logger->error(
348 __( 'An error occurred while saving the form.', 'everest-forms' ),
349 array( 'source' => 'form-save' )
350 );
351 wp_send_json_error(
352 array(
353 'errorTitle' => esc_html__( 'Form not found', 'everest-forms' ),
354 'errorMessage' => esc_html__( 'An error occurred while saving the form.', 'everest-forms' ),
355 )
356 );
357 } else {
358 $logger->info(
359 __( 'Form Saved successfully.', 'everest-forms' ),
360 array( 'source' => 'form-save' )
361 );
362 wp_send_json_success(
363 apply_filters(
364 'everest_forms_save_form_data',
365 array(
366 'form_name' => esc_html( $data['settings']['form_title'] ),
367 'redirect_url' => admin_url( 'admin.php?page=evf-builder' ),
368 ),
369 $form_id,
370 $data
371 )
372 );
373 }
374 }
375
376 /**
377 * Ajax handler for form submission.
378 */
379 public static function ajax_form_submission() {
380 check_ajax_referer( 'everest_forms_ajax_form_submission', 'security' );
381
382 if ( ! empty( $_POST['everest_forms']['id'] ) ) {
383 $process = evf()->task->ajax_form_submission( evf_sanitize_entry( wp_unslash( $_POST['everest_forms'] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
384 if ( 'success' === $process['response'] ) {
385 wp_send_json_success( $process );
386 }
387
388 wp_send_json_error( $process );
389 }
390 }
391
392 /**
393 * Ajax handler for template required addon activation.
394 */
395 public static function template_activate_addon() {
396 check_ajax_referer( 'everest_forms_template_licence_check', 'security' );
397
398 if ( empty( $_POST['addon'] ) ) {
399 wp_send_json_error(
400 array(
401 'errorCode' => 'no_addon_specified',
402 'errorMessage' => esc_html__( 'No Addon specified.', 'everest-forms' ),
403 )
404 );
405 }
406
407 $activate = activate_plugin( sanitize_text_field( wp_unslash( $_POST['addon'] ) ) . '/' . sanitize_text_field( wp_unslash( $_POST['addon'] ) ) . '.php' );
408
409 if ( is_wp_error( $activate ) ) {
410 wp_send_json_error(
411 array(
412 'errorCode' => 'addon_not_active',
413 'errorMessage' => esc_html__( 'Addon can not be activate. Please try again.', 'everest-forms' ),
414 )
415 );
416 } else {
417 wp_send_json_success( 'Addon sucessfully activated.' );
418 }
419 }
420
421 /**
422 * Ajax handler for licence check.
423 *
424 * @global WP_Filesystem_Base $wp_filesystem Subclass
425 */
426 public static function template_licence_check() {
427 check_ajax_referer( 'everest_forms_template_licence_check', 'security' );
428
429 if ( empty( $_POST['plan'] ) ) {
430 wp_send_json_error(
431 array(
432 'plan' => '',
433 'errorCode' => 'no_plan_specified',
434 'errorMessage' => esc_html__( 'No Plan specified.', 'everest-forms' ),
435 )
436 );
437 }
438
439 $addons = array();
440 $template_data = EVF_Admin_Form_Templates::get_template_data();
441 $template_data = is_array( $template_data ) ? $template_data : array();
442 if ( ! empty( $template_data ) ) {
443 foreach ( $template_data as $template ) {
444 if ( isset( $_POST['slug'] ) && $template->slug === $_POST['slug'] && in_array( trim( $_POST['plan'] ), $template->plan, true ) ) {
445 $addons = $template->addons;
446 }
447 }
448 }
449
450 $output = '<div class="everest-forms-recommend-addons">';
451 $output .= '<p class="desc plugins-info">' . esc_html__( 'This form template requires the following addons.', 'everest-forms' ) . '</p>';
452 $output .= '<table class="plugins-list-table widefat striped">';
453 $output .= '<thead><tr><th scope="col" class="manage-column required-plugins" colspan="2">Required Addons</th></tr></thead><tbody id="the-list">';
454 $output .= '</div>';
455
456 $activated = true;
457 foreach ( $addons as $slug => $addon ) {
458 if ( is_plugin_active( $slug . '/' . $slug . '.php' ) ) {
459 $class = 'active';
460 $parent_class = '';
461 } elseif ( file_exists( WP_PLUGIN_DIR . '/' . $slug . '/' . $slug . '.php' ) ) {
462 $class = 'activate-now';
463 $parent_class = 'inactive';
464 $activated = false;
465 } else {
466 $class = 'install-now';
467 $parent_class = 'inactive';
468 $activated = false;
469 }
470 $output .= '<tr class="plugin-card-' . $slug . ' plugin ' . $parent_class . '" data-slug="' . $slug . '" data-plugin="' . $slug . '/' . $slug . '.php" data-name="' . $addon . '">';
471 $output .= '<td class="plugin-name">' . $addon . '</td>';
472 $output .= '<td class="plugin-status"><span class="' . esc_attr( $class ) . '"></span></td>';
473 $output .= '</tr>';
474 }
475 $output .= '</tbody></table></div>';
476
477 wp_send_json_success(
478 array(
479 'html' => $output,
480 'activate' => $activated,
481 )
482 );
483 }
484
485 /**
486 * Ajax handler for installing a extension.
487 *
488 * @since 1.2.0
489 *
490 * @see Plugin_Upgrader
491 *
492 * @global WP_Filesystem_Base $wp_filesystem Subclass
493 */
494 public static function install_extension() {
495 check_ajax_referer( 'updates' );
496
497 if ( empty( $_POST['slug'] ) ) {
498 wp_send_json_error(
499 array(
500 'slug' => '',
501 'errorCode' => 'no_plugin_specified',
502 'errorMessage' => esc_html__( 'No plugin specified.', 'everest-forms' ),
503 )
504 );
505 }
506
507 $slug = sanitize_key( wp_unslash( $_POST['slug'] ) );
508 $plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['slug'] . '/' . $_POST['slug'] . '.php' ) ) );
509 $status = array(
510 'install' => 'plugin',
511 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),
512 );
513
514 if ( ! current_user_can( 'install_plugins' ) ) {
515 $status['errorMessage'] = esc_html__( 'Sorry, you are not allowed to install plugins on this site.', 'everest-forms' );
516 wp_send_json_error( $status );
517 }
518
519 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
520 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
521
522 if ( file_exists( WP_PLUGIN_DIR . '/' . $slug ) ) {
523 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
524 $status['plugin'] = $plugin;
525 $status['pluginName'] = $plugin_data['Name'];
526
527 if ( current_user_can( 'activate_plugin', $plugin ) && is_plugin_inactive( $plugin ) ) {
528 $result = activate_plugin( $plugin );
529
530 if ( is_wp_error( $result ) ) {
531 $status['errorCode'] = $result->get_error_code();
532 $status['errorMessage'] = $result->get_error_message();
533 wp_send_json_error( $status );
534 }
535
536 wp_send_json_success( $status );
537 }
538 }
539
540 $api = json_decode(
541 EVF_Updater_Key_API::version(
542 array(
543 'license' => get_option( 'everest-forms-pro_license_key' ),
544 'item_name' => ! empty( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '',
545 )
546 )
547 );
548
549 if ( is_wp_error( $api ) ) {
550 $status['errorMessage'] = $api->get_error_message();
551 wp_send_json_error( $status );
552 }
553
554 $status['pluginName'] = $api->name;
555
556 $skin = new WP_Ajax_Upgrader_Skin();
557 $upgrader = new Plugin_Upgrader( $skin );
558 $result = $upgrader->install( $api->download_link );
559
560 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
561 $status['debug'] = $skin->get_upgrade_messages();
562 }
563
564 if ( is_wp_error( $result ) ) {
565 $status['errorCode'] = $result->get_error_code();
566 $status['errorMessage'] = $result->get_error_message();
567 wp_send_json_error( $status );
568 } elseif ( is_wp_error( $skin->result ) ) {
569 $status['errorCode'] = $skin->result->get_error_code();
570 $status['errorMessage'] = $skin->result->get_error_message();
571 wp_send_json_error( $status );
572 } elseif ( $skin->get_errors()->get_error_code() ) {
573 $status['errorMessage'] = $skin->get_error_messages();
574 wp_send_json_error( $status );
575 } elseif ( is_null( $result ) ) {
576 global $wp_filesystem;
577
578 $status['errorCode'] = 'unable_to_connect_to_filesystem';
579 $status['errorMessage'] = esc_html__( 'Unable to connect to the filesystem. Please confirm your credentials.', 'everest-forms' );
580
581 // Pass through the error from WP_Filesystem if one was raised.
582 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
583 $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
584 }
585
586 wp_send_json_error( $status );
587 }
588
589 $install_status = install_plugin_install_status( $api );
590
591 if ( current_user_can( 'activate_plugin', $install_status['file'] ) && is_plugin_inactive( $install_status['file'] ) ) {
592 if ( isset( $_POST['page'] ) && 'everest-forms_page_evf-builder' === $_POST['page'] ) {
593 activate_plugin( $install_status['file'] );
594 } else {
595 $status['activateUrl'] =
596 esc_url_raw(
597 add_query_arg(
598 array(
599 'action' => 'activate',
600 'plugin' => $install_status['file'],
601 '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $install_status['file'] ),
602 ),
603 admin_url( 'admin.php?page=evf-addons' )
604 )
605 );
606 }
607 }
608
609 wp_send_json_success( $status );
610 }
611
612 /**
613 * AJAX Integration connect.
614 */
615 public static function integration_connect() {
616 check_ajax_referer( 'process-ajax-nonce', 'security' );
617
618 // Check permissions.
619 if ( ! current_user_can( 'everest_forms_edit_forms' ) ) {
620 wp_die( -1 );
621 }
622
623 if ( empty( $_POST ) ) {
624 wp_send_json_error(
625 array(
626 'error' => esc_html__( 'Missing data', 'everest-forms' ),
627 )
628 );
629 }
630
631 do_action( 'everest_forms_integration_account_connect_' . ( isset( $_POST['source'] ) ? sanitize_text_field( wp_unslash( $_POST['source'] ) ) : '' ), $_POST );
632 }
633
634 /**
635 * AJAX Email Add.
636 */
637 public static function new_email_add() {
638 check_ajax_referer( 'process-ajax-nonce', 'security' );
639
640 // Check permissions.
641 if ( ! current_user_can( 'everest_forms_edit_forms' ) ) {
642 wp_die( -1 );
643 }
644
645 $connection_id = 'connection_' . uniqid();
646
647 wp_send_json_success(
648 array(
649 'connection_id' => $connection_id,
650 )
651 );
652 }
653
654
655 /**
656 * AJAX Integration disconnect.
657 */
658 public static function integration_disconnect() {
659 check_ajax_referer( 'process-ajax-nonce', 'security' );
660
661 // Check permissions.
662 if ( ! current_user_can( 'everest_forms_edit_forms' ) ) {
663 wp_die( -1 );
664 }
665
666 if ( empty( $_POST ) ) {
667 wp_send_json_error(
668 array(
669 'error' => esc_html__( 'Missing data', 'everest-forms' ),
670 )
671 );
672 }
673
674 do_action( 'everest_forms_integration_account_disconnect_' . ( isset( $_POST['source'] ) ? sanitize_text_field( wp_unslash( $_POST['source'] ) ) : '' ), $_POST );
675
676 $connected_accounts = get_option( 'everest_forms_integrations', false );
677
678 if ( ! empty( $connected_accounts[ $_POST['source'] ][ $_POST['key'] ] ) ) {
679 unset( $connected_accounts[ $_POST['source'] ][ $_POST['key'] ] );
680 update_option( 'everest_forms_integrations', $connected_accounts );
681 wp_send_json_success( array( 'remove' => true ) );
682 } else {
683 wp_send_json_error(
684 array(
685 'error' => esc_html__( 'Connection missing', 'everest-forms' ),
686 )
687 );
688 }
689 }
690
691 /**
692 * Triggered when clicking the rating footer.
693 */
694 public static function rated() {
695 if ( ! current_user_can( 'manage_everest_forms' ) ) {
696 wp_die( -1 );
697 }
698 update_option( 'everest_forms_admin_footer_text_rated', 1 );
699 wp_die();
700 }
701
702 /**
703 * Triggered when clicking the review notice button.
704 */
705 public static function review_dismiss() {
706 if ( ! current_user_can( 'manage_everest_forms' ) ) {
707 wp_die( -1 );
708 }
709 $review = get_option( 'everest_forms_review', array() );
710 $review['time'] = current_time( 'timestamp' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
711 $review['dismissed'] = true;
712 update_option( 'everest_forms_review', $review );
713 wp_die();
714 }
715
716 /**
717 * Triggered when clicking the survey notice button.
718 */
719 public static function survey_dismiss() {
720
721 if ( ! current_user_can( 'manage_everest_forms' ) ) {
722 wp_die( -1 );
723 }
724 $survey = get_option( 'everest_forms_survey', array() );
725 $survey['dismissed'] = true;
726 update_option( 'everest_forms_survey', $survey );
727 wp_die();
728 }
729
730 /**
731 * Triggered when clicking the allow usage notice allow or deny buttons.
732 */
733 public static function allow_usage_dismiss() {
734 check_ajax_referer( 'allow_usage_nonce', '_wpnonce' );
735
736 if ( ! current_user_can( 'manage_everest_forms' ) ) {
737 wp_die( -1 );
738 }
739
740 $allow_usage_tracking = isset( $_POST['allow_usage_tracking'] ) ? sanitize_text_field( wp_unslash( $_POST['allow_usage_tracking'] ) ) : false;
741
742 update_option( 'everest_forms_allow_usage_notice_shown', true );
743
744 if ( 'true' === $allow_usage_tracking ) {
745 update_option( 'everest_forms_allow_usage_tracking', 'yes' );
746 }
747
748 wp_die();
749 }
750
751 /**
752 * Triggered when clicking the PHP deprecation notice.
753 */
754 public static function php_notice_dismiss() {
755 check_ajax_referer( 'php_notice_nonce', '_wpnonce' );
756
757 if ( ! current_user_can( 'manage_everest_forms' ) ) {
758 wp_die( -1 );
759 }
760 $current_date = gmdate( 'Y-m-d' );
761 $prompt_count = get_option( 'everest_forms_php_deprecated_notice_prompt_count', 0 );
762
763 update_option( 'everest_forms_php_deprecated_notice_last_prompt_date', $current_date );
764 update_option( 'everest_forms_php_deprecated_notice_prompt_count', ++$prompt_count );
765 wp_die();
766 }
767
768 /**
769 * Triggered when clicking the form toggle.
770 */
771 public static function enabled_form() {
772 // Run a security check.
773 check_ajax_referer( 'everest_forms_enabled_form', 'security' );
774
775 $form_id = isset( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : 0;
776 $enabled = isset( $_POST['enabled'] ) ? absint( $_POST['enabled'] ) : 0;
777
778 if ( ! current_user_can( 'everest_forms_edit_form', $form_id ) ) {
779 wp_die( -1 );
780 }
781
782 $form_data = evf()->form->get( absint( $form_id ), array( 'content_only' => true ) );
783
784 $form_data['form_enabled'] = $enabled;
785
786 evf()->form->update( $form_id, $form_data );
787 }
788
789 /**
790 * Import Form ajax.
791 */
792 public static function import_form_action() {
793 try {
794 check_ajax_referer( 'process-import-ajax-nonce', 'security' );
795 EVF_Admin_Import_Export::import_forms();
796 } catch ( Exception $e ) {
797 wp_send_json_error(
798 array(
799 'message' => $e->getMessage(),
800 )
801 );
802 }
803 }
804
805 /**
806 * Send test email.
807 */
808 public static function send_test_email() {
809 try {
810 check_ajax_referer( 'process-ajax-nonce', 'security' );
811 $from = esc_attr( get_bloginfo( 'name', 'display' ) );
812 $email = sanitize_email( isset( $_POST['email'] ) ? wp_unslash( $_POST['email'] ) : '' );
813
814 /* translators: %s: from address */
815 $subject = 'Everest Form: ' . sprintf( esc_html__( 'Test email from %s', 'everest-forms' ), $from );
816 $header = "Reply-To: {{from}} \r\n";
817 $header .= 'Content-Type: text/html; charset=UTF-8';
818 $message = sprintf(
819 '%s <br /> %s <br /> %s <br /> %s <br /> %s',
820 __( 'Congratulations,', 'everest-forms' ),
821 __( 'Your test email has been received successfully.', 'everest-forms' ),
822 __( 'We thank you for trying out Everest Forms and joining our mission to make sure you get your emails delivered.', 'everest-forms' ),
823 __( 'Regards,', 'everest-forms' ),
824 __( 'Everest Forms Team', 'everest-forms' )
825 );
826 $status = wp_mail( $email, $subject, $message, $header );
827 if ( $status ) {
828 wp_send_json_success( array( 'message' => __( 'Test email was sent successfully! Please check your inbox to make sure it is delivered.', 'everest-forms' ) ) );
829 } else {
830 wp_send_json_error( array( 'message' => __( 'Test email was unsuccessful! Something went wrong.', 'everest-forms' ) ) );
831 }
832 } catch ( Exception $e ) {
833 wp_send_json_error(
834 array(
835 'message' => $e->getMessage(),
836 )
837 );
838 }
839 }
840
841 /**
842 * Locate form.
843 */
844 public static function locate_form_action() {
845 global $wpdb;
846 try {
847 check_ajax_referer( 'process-locate-ajax-nonce', 'security' );
848 $id = isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : '';
849 $everest_form_shortcode = '%[everest_form id="' . $id . '"%';
850 $form_id_shortcode = '%{"formId":"' . $id . '"%';
851 $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
852 $page_list = array();
853 foreach ( $pages as $page ) {
854 if ( '0' === $page->post_parent ) {
855 $page_title = $page->post_title;
856 $page_guid = $page->guid;
857 $page_list[ $page_title ] = $page_guid;
858 }
859 }
860 wp_send_json_success( $page_list );
861 } catch ( Exception $e ) {
862 wp_send_json_error(
863 array(
864 'message' => $e->getMessage(),
865 )
866 );
867 }
868 }
869 /**
870 * Slot booking.
871 */
872 /**
873 * Slot booking.
874 */
875 public static function slot_booking() {
876 try {
877 check_ajax_referer( 'everest_forms_slot_booking_nonce', 'security' );
878 $datetime_value = isset( $_POST['data-time-value'] ) ? sanitize_text_field( wp_unslash( $_POST['data-time-value'] ) ) : '';
879 $datetime_format = isset( $_POST['data-time-format'] ) ? sanitize_text_field( wp_unslash( $_POST['data-time-format'] ) ) : '';
880 $date_format = isset( $_POST['data-format'] ) ? sanitize_text_field( wp_unslash( $_POST['data-format'] ) ) : '';
881 $mode = isset( $_POST['mode'] ) ? sanitize_text_field( wp_unslash( $_POST['mode'] ) ) : '';
882 $form_id = isset( $_POST['form-id'] ) ? sanitize_text_field( wp_unslash( $_POST['form-id'] ) ) : '';
883 $time_interval = isset( $_POST['time-interval'] ) ? sanitize_text_field( wp_unslash( $_POST['time-interval'] ) ) : '';
884 $datetime_arr = parse_datetime_values( $datetime_value, $datetime_format, $date_format, $mode, $time_interval );
885
886 if ( empty( $datetime_arr ) ) {
887 wp_send_json_error(
888 array(
889 'message' => __( 'Please select at least one date time.', 'everest-forms' ),
890 )
891 );
892 }
893 $booked_slot = maybe_unserialize( get_option( 'evf_booked_slot', '' ) );
894 $is_booked = false;
895 if ( ! empty( $booked_slot ) && array_key_exists( $form_id, $booked_slot ) ) {
896 foreach ( $datetime_arr as $arr ) {
897
898 foreach ( $booked_slot[ $form_id ] as $slot ) {
899 if ( $arr[0] >= $slot[0] && $arr[1] <= $slot[1] ) {
900 $is_booked = true;
901 break;
902 } elseif ( $arr[0] >= $slot[0] && $arr[0] < $slot[1] && $arr[1] >= $slot[1] ) {
903 $is_booked = true;
904 break;
905 }
906 }
907 }
908 }
909 if ( $is_booked ) {
910 wp_send_json_success(
911 array(
912 'message' => __( 'This slot is already booked. Please choose other slot', 'everest-forms' ),
913 )
914 );
915 }
916 wp_send_json_error(
917 array(
918 'message' => __( 'This slot is not booked.', 'everest-forms' ),
919 )
920 );
921
922 } catch ( Exception $e ) {
923 wp_send_json_error(
924 array(
925 'message' => __( 'Something went wrong.', 'everest-forms' ),
926 )
927 );
928 }
929 }
930
931 /**
932 * Activate addons from builder.
933 */
934 public static function active_addons() {
935 try {
936 check_ajax_referer( 'evf_active_nonce', 'security' );
937 $plugin = isset( $_POST['plugin_file'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_file'] ) ) : '';
938 $activate = activate_plugin( $plugin );
939 if ( is_wp_error( $activate ) ) {
940 $activation_error = $activate->get_error_message();
941 wp_send_json_error(
942 array(
943 'message' => $activation_error,
944 )
945 );
946 } else {
947 wp_send_json_success(
948 array(
949 'message' => __( 'Activated successfully', 'everest-forms' ),
950 )
951 );
952 }
953 } catch ( Exception $e ) {
954 wp_send_json_error(
955 array(
956 'message' => $e->getMessage(),
957 )
958 );
959 }
960 }
961
962 /**
963 * Download the provided font and return the url for font file.
964 */
965 public static function get_local_font_url() {
966 $font_url = isset( $_POST['font_url'] ) ? sanitize_text_field( wp_unslash( $_POST['font_url'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification
967
968 if ( str_contains( $font_url, 'https://fonts.googleapis.com' ) ) {
969 $font_url = evf_maybe_get_local_font_url( $font_url );
970 }
971
972 return wp_send_json_success( $font_url );
973 }
974 }
975
976 EVF_AJAX::init();
977