PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 1.9.9
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v1.9.9
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 3 years ago admin 3 years ago elementor 4 years ago export 3 years ago fields 3 years ago interfaces 8 years ago libraries 7 years ago log-handlers 4 years ago shortcodes 3 years ago stats 3 years ago templates 5 years ago class-everest-forms.php 3 years ago class-evf-ajax.php 3 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 3 years ago class-evf-fields.php 6 years ago class-evf-form-block.php 4 years ago class-evf-form-handler.php 3 years ago class-evf-form-task.php 3 years ago class-evf-forms-features.php 3 years ago class-evf-frontend-scripts.php 4 years ago class-evf-install.php 3 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 3 years ago class-evf-template-loader.php 3 years ago class-evf-validation.php 6 years ago evf-conditional-functions.php 6 years ago evf-core-functions.php 3 years ago evf-deprecated-functions.php 6 years ago evf-entry-functions.php 4 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
821 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 'enabled_form' => false,
97 'import_form_action' => false,
98 'template_licence_check' => false,
99 'template_activate_addon' => false,
100 'ajax_form_submission' => true,
101 'send_test_email' => false,
102 );
103
104 foreach ( $ajax_events as $ajax_event => $nopriv ) {
105 add_action( 'wp_ajax_everest_forms_' . $ajax_event, array( __CLASS__, $ajax_event ) );
106
107 if ( $nopriv ) {
108 add_action( 'wp_ajax_nopriv_everest_forms_' . $ajax_event, array( __CLASS__, $ajax_event ) );
109
110 // EVF AJAX can be used for frontend ajax requests.
111 add_action( 'evf_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) );
112 }
113 }
114 }
115
116 /**
117 * Ajax handler to get next form ID.
118 */
119 public static function get_next_id() {
120 // Run a security check.
121 check_ajax_referer( 'everest_forms_get_next_id', 'security' );
122
123 $form_id = isset( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : 0;
124 if ( $form_id < 1 ) {
125 wp_send_json_error(
126 array(
127 'error' => esc_html__( 'Invalid form', 'everest-forms' ),
128 )
129 );
130 }
131
132 // Check permisssions.
133 if ( ! current_user_can( 'everest_forms_edit_form', $form_id ) ) {
134 wp_send_json_error();
135 }
136
137 if ( isset( $_POST['fields'] ) ) {
138 $fields_data = array();
139 for ( $i = 0; $i < $_POST['fields']; $i++ ) {
140 $field_key = evf()->form->field_unique_key( $form_id );
141 $field_id_array = explode( '-', $field_key );
142 $new_field_id = ( $field_id_array[ count( $field_id_array ) - 1 ] + 1 );
143 $fields_data [] = array(
144 'field_id' => $new_field_id,
145 'field_key' => $field_key,
146 );
147 }
148 wp_send_json_success(
149 $fields_data
150 );
151 } else {
152 $field_key = evf()->form->field_unique_key( $form_id );
153 $field_id_array = explode( '-', $field_key );
154 $new_field_id = ( $field_id_array[ count( $field_id_array ) - 1 ] + 1 );
155 wp_send_json_success(
156 array(
157 'field_id' => $new_field_id,
158 'field_key' => $field_key,
159 )
160 );
161 }
162 }
163
164 /**
165 * AJAX create new form.
166 */
167 public static function create_form() {
168 ob_start();
169
170 check_ajax_referer( 'everest_forms_create_form', 'security' );
171
172 // Check permissions.
173 if ( ! current_user_can( 'everest_forms_create_forms' ) ) {
174 wp_die( -1 );
175 }
176
177 $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : esc_html__( 'Blank Form', 'everest-forms' );
178 $template = isset( $_POST['template'] ) ? sanitize_text_field( wp_unslash( $_POST['template'] ) ) : 'blank';
179
180 $form_id = evf()->form->create( $title, $template );
181
182 if ( $form_id ) {
183 $data = array(
184 'id' => $form_id,
185 'redirect' => add_query_arg(
186 array(
187 'tab' => 'fields',
188 'form_id' => $form_id,
189 ),
190 admin_url( 'admin.php?page=evf-builder' )
191 ),
192 );
193
194 wp_send_json_success( $data );
195 }
196
197 wp_send_json_error(
198 array(
199 'error' => esc_html__( 'Something went wrong, please try again later', 'everest-forms' ),
200 )
201 );
202 }
203
204 /**
205 * AJAX Form save.
206 */
207 public static function save_form() {
208 check_ajax_referer( 'everest_forms_save_form', 'security' );
209
210 $logger = evf_get_logger();
211
212 // Check permissions.
213 $logger->info(
214 __( 'Checking permissions.', 'everest-forms' ),
215 array( 'source' => 'form-save' )
216 );
217 if ( ! current_user_can( 'everest_forms_edit_forms' ) ) {
218 $logger->critical(
219 __( 'You do not have permission.', 'everest-forms' ),
220 array( 'source' => 'form-save' )
221 );
222 die( esc_html__( 'You do not have permission.', 'everest-forms' ) );
223 }
224
225 // Check for form data.
226 $logger->info(
227 __( 'Checking for form data.', 'everest-forms' ),
228 array( 'source' => 'form-save' )
229 );
230 if ( empty( $_POST['form_data'] ) ) {
231 $logger->critical(
232 __( 'No data provided.', 'everest-forms' ),
233 array( 'source' => 'form-save' )
234 );
235 die( esc_html__( 'No data provided', 'everest-forms' ) );
236 }
237
238 $form_post = evf_sanitize_builder( json_decode( wp_unslash( $_POST['form_data'] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
239
240 $data = array();
241
242 if ( ! is_null( $form_post ) && $form_post ) {
243 foreach ( $form_post as $post_index => $post_input_data ) {
244 // For input names that are arrays (e.g. `menu-item-db-id[3][4][5]`),
245 // derive the array path keys via regex and set the value in $_POST.
246 preg_match( '#([^\[]*)(\[(.+)\])?#', $post_input_data->name, $matches );
247
248 $array_bits = array( $matches[1] );
249
250 if ( isset( $matches[3] ) ) {
251 $array_bits = array_merge( $array_bits, explode( '][', $matches[3] ) );
252 }
253
254 $new_post_data = array();
255
256 // Build the new array value from leaf to trunk.
257 for ( $i = count( $array_bits ) - 1; $i >= 0; $i -- ) {
258 if ( count( $array_bits ) - 1 === $i ) {
259 if ( '' === $array_bits[ $i ] ) {
260 $new_post_data [ $post_index ] = wp_slash( $post_input_data->value );
261 } else {
262 $new_post_data[ $array_bits[ $i ] ] = wp_slash( $post_input_data->value );
263 }
264 } else {
265 $new_post_data = array(
266 $array_bits[ $i ] => $new_post_data,
267 );
268 }
269 }
270 $data = array_replace_recursive( $data, $new_post_data );
271 }
272 }
273 // Check for empty meta key.
274 $logger->info(
275 __( 'Check for empty meta key.', 'everest-forms' ),
276 array( 'source' => 'form-save' )
277 );
278 $empty_meta_data = array();
279 if ( ! empty( $data['form_fields'] ) ) {
280 foreach ( $data['form_fields'] as $field_key => $field ) {
281 if ( ! empty( $field['label'] ) ) {
282 // Only allow specific html in label.
283 $data['form_fields'][ $field_key ]['label'] = wp_kses(
284 $field['label'],
285 array(
286 'a' => array(
287 'href' => array(),
288 'class' => array(),
289 ),
290 'span' => array(
291 'class' => array(),
292 ),
293 'em' => array(),
294 'small' => array(),
295 'strong' => array(),
296 )
297 );
298
299 // Register string for translation.
300 evf_string_translation( $data['id'], $field['id'], $field['label'] );
301 }
302
303 if ( empty( $field['meta-key'] ) && ! in_array( $field['type'], array( 'html', 'title', 'captcha', 'divider', 'reset' ), true ) ) {
304 $empty_meta_data[] = $field['label'];
305 }
306 }
307
308 if ( ! empty( $empty_meta_data ) ) {
309 $logger->error(
310 __( 'Meta Key missing.', 'everest-forms' ),
311 array( 'source' => 'form-save' )
312 );
313 wp_send_json_error(
314 array(
315 'errorTitle' => esc_html__( 'Meta Key missing', 'everest-forms' ),
316 /* translators: %s: empty meta data */
317 'errorMessage' => sprintf( esc_html__( 'Please add Meta key for fields: %s', 'everest-forms' ), '<strong>' . implode( ', ', $empty_meta_data ) . '</strong>' ),
318 )
319 );
320 }
321 }
322
323 // Fix for sorting field ordering.
324 $logger->info(
325 __( 'Fix for sorting field ordering.', 'everest-forms' ),
326 array( 'source' => 'form-save' )
327 );
328 if ( isset( $data['structure'], $data['form_fields'] ) ) {
329 $structure = evf_flatten_array( $data['structure'] );
330 $data['form_fields'] = array_merge( array_intersect_key( array_flip( $structure ), $data['form_fields'] ), $data['form_fields'] );
331 }
332
333 $form_id = evf()->form->update( $data['id'], $data );
334 $form_styles = get_option( 'everest_forms_styles', array() );
335 $logger->info(
336 __( 'Saving form.', 'everest-forms' ),
337 array( 'source' => 'form-save' )
338 );
339 do_action( 'everest_forms_save_form', $form_id, $data, array(), ! empty( $form_styles[ $form_id ] ) );
340
341 if ( ! $form_id ) {
342 $logger->error(
343 __( 'An error occurred while saving the form.', 'everest-forms' ),
344 array( 'source' => 'form-save' )
345 );
346 wp_send_json_error(
347 array(
348 'errorTitle' => esc_html__( 'Form not found', 'everest-forms' ),
349 'errorMessage' => esc_html__( 'An error occurred while saving the form.', 'everest-forms' ),
350 )
351 );
352 } else {
353 $logger->info(
354 __( 'Form Saved successfully.', 'everest-forms' ),
355 array( 'source' => 'form-save' )
356 );
357 wp_send_json_success(
358 apply_filters(
359 'everest_forms_save_form_data',
360 array(
361 'form_name' => esc_html( $data['settings']['form_title'] ),
362 'redirect_url' => admin_url( 'admin.php?page=evf-builder' ),
363 ),
364 $form_id,
365 $data
366 )
367 );
368 }
369 }
370
371 /**
372 * Ajax handler for form submission.
373 */
374 public static function ajax_form_submission() {
375 check_ajax_referer( 'everest_forms_ajax_form_submission', 'security' );
376
377 if ( ! empty( $_POST['everest_forms']['id'] ) ) {
378 $process = evf()->task->ajax_form_submission( evf_sanitize_entry( wp_unslash( $_POST['everest_forms'] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
379 if ( 'success' === $process['response'] ) {
380 wp_send_json_success( $process );
381 }
382
383 wp_send_json_error( $process );
384 }
385 }
386
387 /**
388 * Ajax handler for template required addon activation.
389 */
390 public static function template_activate_addon() {
391 check_ajax_referer( 'everest_forms_template_licence_check', 'security' );
392
393 if ( empty( $_POST['addon'] ) ) {
394 wp_send_json_error(
395 array(
396 'errorCode' => 'no_addon_specified',
397 'errorMessage' => esc_html__( 'No Addon specified.', 'everest-forms' ),
398 )
399 );
400 }
401
402 $activate = activate_plugin( sanitize_text_field( wp_unslash( $_POST['addon'] ) ) . '/' . sanitize_text_field( wp_unslash( $_POST['addon'] ) ) . '.php' );
403
404 if ( is_wp_error( $activate ) ) {
405 wp_send_json_error(
406 array(
407 'errorCode' => 'addon_not_active',
408 'errorMessage' => esc_html__( 'Addon can not be activate. Please try again.', 'everest-forms' ),
409 )
410 );
411 } else {
412 wp_send_json_success( 'Addon sucessfully activated.' );
413 }
414 }
415
416 /**
417 * Ajax handler for licence check.
418 *
419 * @global WP_Filesystem_Base $wp_filesystem Subclass
420 */
421 public static function template_licence_check() {
422 check_ajax_referer( 'everest_forms_template_licence_check', 'security' );
423
424 if ( empty( $_POST['plan'] ) ) {
425 wp_send_json_error(
426 array(
427 'plan' => '',
428 'errorCode' => 'no_plan_specified',
429 'errorMessage' => esc_html__( 'No Plan specified.', 'everest-forms' ),
430 )
431 );
432 }
433
434 $addons = array();
435 $template_data = EVF_Admin_Form_Templates::get_template_data();
436 $template_data = is_array( $template_data ) ? $template_data : array();
437 if ( ! empty( $template_data ) ) {
438 foreach ( $template_data as $template ) {
439 if ( isset( $_POST['slug'] ) && $template->slug === $_POST['slug'] && in_array( trim( $_POST['plan'] ), $template->plan, true ) ) {
440 $addons = $template->addons;
441 }
442 }
443 }
444
445 $output = '<div class="everest-forms-recommend-addons">';
446 $output .= '<p class="desc plugins-info">' . esc_html__( 'This form template requires the following addons.', 'everest-forms' ) . '</p>';
447 $output .= '<table class="plugins-list-table widefat striped">';
448 $output .= '<thead><tr><th scope="col" class="manage-column required-plugins" colspan="2">Required Addons</th></tr></thead><tbody id="the-list">';
449 $output .= '</div>';
450
451 $activated = true;
452 foreach ( $addons as $slug => $addon ) {
453 if ( is_plugin_active( $slug . '/' . $slug . '.php' ) ) {
454 $class = 'active';
455 $parent_class = '';
456 } elseif ( file_exists( WP_PLUGIN_DIR . '/' . $slug . '/' . $slug . '.php' ) ) {
457 $class = 'activate-now';
458 $parent_class = 'inactive';
459 $activated = false;
460 } else {
461 $class = 'install-now';
462 $parent_class = 'inactive';
463 $activated = false;
464 }
465 $output .= '<tr class="plugin-card-' . $slug . ' plugin ' . $parent_class . '" data-slug="' . $slug . '" data-plugin="' . $slug . '/' . $slug . '.php" data-name="' . $addon . '">';
466 $output .= '<td class="plugin-name">' . $addon . '</td>';
467 $output .= '<td class="plugin-status"><span class="' . esc_attr( $class ) . '"></span></td>';
468 $output .= '</tr>';
469 }
470 $output .= '</tbody></table></div>';
471
472 wp_send_json_success(
473 array(
474 'html' => $output,
475 'activate' => $activated,
476 )
477 );
478 }
479
480 /**
481 * Ajax handler for installing a extension.
482 *
483 * @since 1.2.0
484 *
485 * @see Plugin_Upgrader
486 *
487 * @global WP_Filesystem_Base $wp_filesystem Subclass
488 */
489 public static function install_extension() {
490 check_ajax_referer( 'updates' );
491
492 if ( empty( $_POST['slug'] ) ) {
493 wp_send_json_error(
494 array(
495 'slug' => '',
496 'errorCode' => 'no_plugin_specified',
497 'errorMessage' => esc_html__( 'No plugin specified.', 'everest-forms' ),
498 )
499 );
500 }
501
502 $slug = sanitize_key( wp_unslash( $_POST['slug'] ) );
503 $plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['slug'] . '/' . $_POST['slug'] . '.php' ) ) );
504 $status = array(
505 'install' => 'plugin',
506 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),
507 );
508
509 if ( ! current_user_can( 'install_plugins' ) ) {
510 $status['errorMessage'] = esc_html__( 'Sorry, you are not allowed to install plugins on this site.', 'everest-forms' );
511 wp_send_json_error( $status );
512 }
513
514 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
515 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
516
517 if ( file_exists( WP_PLUGIN_DIR . '/' . $slug ) ) {
518 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
519 $status['plugin'] = $plugin;
520 $status['pluginName'] = $plugin_data['Name'];
521
522 if ( current_user_can( 'activate_plugin', $plugin ) && is_plugin_inactive( $plugin ) ) {
523 $result = activate_plugin( $plugin );
524
525 if ( is_wp_error( $result ) ) {
526 $status['errorCode'] = $result->get_error_code();
527 $status['errorMessage'] = $result->get_error_message();
528 wp_send_json_error( $status );
529 }
530
531 wp_send_json_success( $status );
532 }
533 }
534
535 $api = json_decode(
536 EVF_Updater_Key_API::version(
537 array(
538 'license' => get_option( 'everest-forms-pro_license_key' ),
539 'item_name' => ! empty( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '',
540 )
541 )
542 );
543
544 if ( is_wp_error( $api ) ) {
545 $status['errorMessage'] = $api->get_error_message();
546 wp_send_json_error( $status );
547 }
548
549 $status['pluginName'] = $api->name;
550
551 $skin = new WP_Ajax_Upgrader_Skin();
552 $upgrader = new Plugin_Upgrader( $skin );
553 $result = $upgrader->install( $api->download_link );
554
555 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
556 $status['debug'] = $skin->get_upgrade_messages();
557 }
558
559 if ( is_wp_error( $result ) ) {
560 $status['errorCode'] = $result->get_error_code();
561 $status['errorMessage'] = $result->get_error_message();
562 wp_send_json_error( $status );
563 } elseif ( is_wp_error( $skin->result ) ) {
564 $status['errorCode'] = $skin->result->get_error_code();
565 $status['errorMessage'] = $skin->result->get_error_message();
566 wp_send_json_error( $status );
567 } elseif ( $skin->get_errors()->get_error_code() ) {
568 $status['errorMessage'] = $skin->get_error_messages();
569 wp_send_json_error( $status );
570 } elseif ( is_null( $result ) ) {
571 global $wp_filesystem;
572
573 $status['errorCode'] = 'unable_to_connect_to_filesystem';
574 $status['errorMessage'] = esc_html__( 'Unable to connect to the filesystem. Please confirm your credentials.', 'everest-forms' );
575
576 // Pass through the error from WP_Filesystem if one was raised.
577 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
578 $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() );
579 }
580
581 wp_send_json_error( $status );
582 }
583
584 $install_status = install_plugin_install_status( $api );
585
586 if ( current_user_can( 'activate_plugin', $install_status['file'] ) && is_plugin_inactive( $install_status['file'] ) ) {
587 if ( isset( $_POST['page'] ) && 'everest-forms_page_evf-builder' === $_POST['page'] ) {
588 activate_plugin( $install_status['file'] );
589 } else {
590 $status['activateUrl'] =
591 esc_url_raw(
592 add_query_arg(
593 array(
594 'action' => 'activate',
595 'plugin' => $install_status['file'],
596 '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $install_status['file'] ),
597 ),
598 admin_url( 'admin.php?page=evf-addons' )
599 )
600 );
601 }
602 }
603
604 wp_send_json_success( $status );
605 }
606
607 /**
608 * AJAX Integration connect.
609 */
610 public static function integration_connect() {
611 check_ajax_referer( 'process-ajax-nonce', 'security' );
612
613 // Check permissions.
614 if ( ! current_user_can( 'everest_forms_edit_forms' ) ) {
615 wp_die( -1 );
616 }
617
618 if ( empty( $_POST ) ) {
619 wp_send_json_error(
620 array(
621 'error' => esc_html__( 'Missing data', 'everest-forms' ),
622 )
623 );
624 }
625
626 do_action( 'everest_forms_integration_account_connect_' . ( isset( $_POST['source'] ) ? sanitize_text_field( wp_unslash( $_POST['source'] ) ) : '' ), $_POST );
627 }
628
629 /**
630 * AJAX Email Add.
631 */
632 public static function new_email_add() {
633 check_ajax_referer( 'process-ajax-nonce', 'security' );
634
635 // Check permissions.
636 if ( ! current_user_can( 'everest_forms_edit_forms' ) ) {
637 wp_die( -1 );
638 }
639
640 $connection_id = 'connection_' . uniqid();
641
642 wp_send_json_success(
643 array(
644 'connection_id' => $connection_id,
645 )
646 );
647 }
648
649
650 /**
651 * AJAX Integration disconnect.
652 */
653 public static function integration_disconnect() {
654 check_ajax_referer( 'process-ajax-nonce', 'security' );
655
656 // Check permissions.
657 if ( ! current_user_can( 'everest_forms_edit_forms' ) ) {
658 wp_die( -1 );
659 }
660
661 if ( empty( $_POST ) ) {
662 wp_send_json_error(
663 array(
664 'error' => esc_html__( 'Missing data', 'everest-forms' ),
665 )
666 );
667 }
668
669 do_action( 'everest_forms_integration_account_disconnect_' . ( isset( $_POST['source'] ) ? sanitize_text_field( wp_unslash( $_POST['source'] ) ) : '' ), $_POST );
670
671 $connected_accounts = get_option( 'everest_forms_integrations', false );
672
673 if ( ! empty( $connected_accounts[ $_POST['source'] ][ $_POST['key'] ] ) ) {
674 unset( $connected_accounts[ $_POST['source'] ][ $_POST['key'] ] );
675 update_option( 'everest_forms_integrations', $connected_accounts );
676 wp_send_json_success( array( 'remove' => true ) );
677 } else {
678 wp_send_json_error(
679 array(
680 'error' => esc_html__( 'Connection missing', 'everest-forms' ),
681 )
682 );
683 }
684 }
685
686 /**
687 * Triggered when clicking the rating footer.
688 */
689 public static function rated() {
690 if ( ! current_user_can( 'manage_everest_forms' ) ) {
691 wp_die( -1 );
692 }
693 update_option( 'everest_forms_admin_footer_text_rated', 1 );
694 wp_die();
695 }
696
697 /**
698 * Triggered when clicking the review notice button.
699 */
700 public static function review_dismiss() {
701 if ( ! current_user_can( 'manage_everest_forms' ) ) {
702 wp_die( -1 );
703 }
704 $review = get_option( 'everest_forms_review', array() );
705 $review['time'] = current_time( 'timestamp' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
706 $review['dismissed'] = true;
707 update_option( 'everest_forms_review', $review );
708 wp_die();
709 }
710
711 /**
712 * Triggered when clicking the survey notice button.
713 */
714 public static function survey_dismiss() {
715
716 if ( ! current_user_can( 'manage_everest_forms' ) ) {
717 wp_die( -1 );
718 }
719 $survey = get_option( 'everest_forms_survey', array() );
720 $survey['dismissed'] = true;
721 update_option( 'everest_forms_survey', $survey );
722 wp_die();
723 }
724
725 /**
726 * Triggered when clicking the allow usage notice allow or deny buttons.
727 */
728 public static function allow_usage_dismiss() {
729 check_ajax_referer( 'allow_usage_nonce', '_wpnonce' );
730
731 if ( ! current_user_can( 'manage_everest_forms' ) ) {
732 wp_die( -1 );
733 }
734
735 $allow_usage_tracking = isset( $_POST['allow_usage_tracking'] ) ? sanitize_text_field( wp_unslash( $_POST['allow_usage_tracking'] ) ) : false;
736
737 update_option( 'everest_forms_allow_usage_notice_shown', true );
738
739 if ( 'true' === $allow_usage_tracking ) {
740 update_option( 'everest_forms_allow_usage_tracking', 'yes' );
741 }
742
743 wp_die();
744 }
745
746 /**
747 * Triggered when clicking the form toggle.
748 */
749 public static function enabled_form() {
750 // Run a security check.
751 check_ajax_referer( 'everest_forms_enabled_form', 'security' );
752
753 $form_id = isset( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : 0;
754 $enabled = isset( $_POST['enabled'] ) ? absint( $_POST['enabled'] ) : 0;
755
756 if ( ! current_user_can( 'everest_forms_edit_form', $form_id ) ) {
757 wp_die( -1 );
758 }
759
760 $form_data = evf()->form->get( absint( $form_id ), array( 'content_only' => true ) );
761
762 $form_data['form_enabled'] = $enabled;
763
764 evf()->form->update( $form_id, $form_data );
765 }
766
767 /**
768 * Import Form ajax.
769 */
770 public static function import_form_action() {
771 try {
772 check_ajax_referer( 'process-import-ajax-nonce', 'security' );
773 EVF_Admin_Import_Export::import_forms();
774 } catch ( Exception $e ) {
775 wp_send_json_error(
776 array(
777 'message' => $e->getMessage(),
778 )
779 );
780 }
781 }
782
783 /**
784 * Send test email.
785 */
786 public static function send_test_email() {
787 try {
788 check_ajax_referer( 'process-ajax-nonce', 'security' );
789 $from = esc_attr( get_bloginfo( 'name', 'display' ) );
790 $email = sanitize_email( isset( $_POST['email'] ) ? wp_unslash( $_POST['email'] ) : '' );
791
792 /* translators: %s: from address */
793 $subject = 'Everest Form: ' . sprintf( esc_html__( 'Test email from %s', 'everest-forms' ), $from );
794 $header = "Reply-To: {{from}} \r\n";
795 $header .= 'Content-Type: text/html; charset=UTF-8';
796 $message = sprintf(
797 '%s <br /> %s <br /> %s <br /> %s <br /> %s',
798 __( 'Congratulations,', 'everest-forms' ),
799 __( 'Your test email has been received successfully.', 'everest-forms' ),
800 __( 'We thank you for trying out Everest Forms and joining our mission to make sure you get your emails delivered.', 'everest-forms' ),
801 __( 'Regards,', 'everest-forms' ),
802 __( 'Everest Forms Team', 'everest-forms' )
803 );
804 $status = wp_mail( $email, $subject, $message, $header );
805 if ( $status ) {
806 wp_send_json_success( array( 'message' => __( 'Test email was sent successfully! Please check your inbox to make sure it is delivered.', 'everest-forms' ) ) );
807 } else {
808 wp_send_json_error( array( 'message' => __( 'Test email was unsuccessful! Something went wrong.', 'everest-forms' ) ) );
809 }
810 } catch ( Exception $e ) {
811 wp_send_json_error(
812 array(
813 'message' => $e->getMessage(),
814 )
815 );
816 }
817 }
818 }
819
820 EVF_AJAX::init();
821