abstracts
6 years ago
admin
6 years ago
export
6 years ago
fields
6 years ago
interfaces
8 years ago
libraries
7 years ago
log-handlers
6 years ago
shortcodes
6 years ago
templates
6 years ago
class-everest-forms.php
6 years ago
class-evf-ajax.php
6 years ago
class-evf-autoloader.php
8 years ago
class-evf-background-updater.php
8 years ago
class-evf-cache-helper.php
6 years ago
class-evf-deprecated-action-hooks.php
6 years ago
class-evf-deprecated-filter-hooks.php
7 years ago
class-evf-emails.php
6 years ago
class-evf-fields.php
6 years ago
class-evf-form-block.php
6 years ago
class-evf-form-handler.php
6 years ago
class-evf-form-task.php
6 years ago
class-evf-forms-features.php
6 years ago
class-evf-frontend-scripts.php
6 years ago
class-evf-install.php
6 years ago
class-evf-integrations.php
7 years ago
class-evf-log-levels.php
8 years ago
class-evf-logger.php
6 years ago
class-evf-post-types.php
6 years ago
class-evf-privacy.php
6 years ago
class-evf-session-handler.php
7 years ago
class-evf-shortcodes.php
7 years ago
class-evf-smart-tags.php
6 years ago
class-evf-template-loader.php
6 years ago
class-evf-validation.php
6 years ago
evf-conditional-functions.php
6 years ago
evf-core-functions.php
6 years ago
evf-deprecated-functions.php
6 years ago
evf-entry-functions.php
6 years ago
evf-formatting-functions.php
6 years ago
evf-notice-functions.php
6 years ago
evf-template-functions.php
6 years ago
evf-template-hooks.php
8 years ago
evf-update-functions.php
6 years ago
class-evf-ajax.php
695 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 | 'deactivation_notice' => false, |
| 93 | 'rated' => false, |
| 94 | 'review_dismiss' => false, |
| 95 | 'enabled_form' => false, |
| 96 | 'import_form_action' => false, |
| 97 | 'template_licence_check' => false, |
| 98 | 'template_activate_addon' => false, |
| 99 | 'ajax_form_submission' => true, |
| 100 | ); |
| 101 | |
| 102 | foreach ( $ajax_events as $ajax_event => $nopriv ) { |
| 103 | add_action( 'wp_ajax_everest_forms_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
| 104 | |
| 105 | if ( $nopriv ) { |
| 106 | add_action( 'wp_ajax_nopriv_everest_forms_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
| 107 | |
| 108 | // EVF AJAX can be used for frontend ajax requests. |
| 109 | add_action( 'evf_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Ajax handler to get next form ID. |
| 116 | */ |
| 117 | public static function get_next_id() { |
| 118 | // Run a security check. |
| 119 | check_ajax_referer( 'everest_forms_get_next_id', 'security' ); |
| 120 | |
| 121 | $form_id = isset( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : 0; |
| 122 | if ( $form_id < 1 ) { |
| 123 | wp_send_json_error( |
| 124 | array( |
| 125 | 'error' => esc_html__( 'Invalid form', 'everest-forms' ), |
| 126 | ) |
| 127 | ); |
| 128 | } |
| 129 | if ( ! current_user_can( apply_filters( 'everest_forms_manage_cap', 'manage_options' ) ) ) { |
| 130 | wp_send_json_error(); |
| 131 | } |
| 132 | $field_key = evf()->form->field_unique_key( $form_id ); |
| 133 | $field_id_array = explode( '-', $field_key ); |
| 134 | $new_field_id = ( $field_id_array[ count( $field_id_array ) - 1 ] + 1 ); |
| 135 | wp_send_json_success( |
| 136 | array( |
| 137 | 'field_id' => $new_field_id, |
| 138 | 'field_key' => $field_key, |
| 139 | ) |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * AJAX create new form. |
| 145 | */ |
| 146 | public static function create_form() { |
| 147 | ob_start(); |
| 148 | |
| 149 | check_ajax_referer( 'everest_forms_create_form', 'security' ); |
| 150 | |
| 151 | if ( ! current_user_can( 'edit_everest_forms' ) ) { |
| 152 | wp_die( -1 ); |
| 153 | } |
| 154 | |
| 155 | $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : esc_html__( 'Blank Form', 'everest-forms' ); |
| 156 | $template = isset( $_POST['template'] ) ? sanitize_text_field( wp_unslash( $_POST['template'] ) ) : 'blank'; |
| 157 | |
| 158 | $form_id = evf()->form->create( $title, $template ); |
| 159 | |
| 160 | if ( $form_id ) { |
| 161 | $data = array( |
| 162 | 'id' => $form_id, |
| 163 | 'redirect' => add_query_arg( |
| 164 | array( |
| 165 | 'tab' => 'fields', |
| 166 | 'form_id' => $form_id, |
| 167 | ), |
| 168 | admin_url( 'admin.php?page=evf-builder' ) |
| 169 | ), |
| 170 | ); |
| 171 | |
| 172 | wp_send_json_success( $data ); |
| 173 | } |
| 174 | |
| 175 | wp_send_json_error( |
| 176 | array( |
| 177 | 'error' => esc_html__( 'Something went wrong, please try again later', 'everest-forms' ), |
| 178 | ) |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * AJAX Form save. |
| 184 | */ |
| 185 | public static function save_form() { |
| 186 | check_ajax_referer( 'everest_forms_save_form', 'security' ); |
| 187 | |
| 188 | // Check for permissions. |
| 189 | if ( ! current_user_can( apply_filters( 'everest_forms_manage_cap', 'manage_options' ) ) ) { |
| 190 | die( esc_html__( 'You do not have permission.', 'everest-forms' ) ); |
| 191 | } |
| 192 | |
| 193 | // Check for form data. |
| 194 | if ( empty( $_POST['form_data'] ) ) { |
| 195 | die( esc_html__( 'No data provided', 'everest-forms' ) ); |
| 196 | } |
| 197 | |
| 198 | $form_post = json_decode( stripslashes( $_POST['form_data'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 199 | |
| 200 | $data = array(); |
| 201 | |
| 202 | if ( ! is_null( $form_post ) && $form_post ) { |
| 203 | foreach ( $form_post as $post_input_data ) { |
| 204 | // For input names that are arrays (e.g. `menu-item-db-id[3][4][5]`), |
| 205 | // derive the array path keys via regex and set the value in $_POST. |
| 206 | preg_match( '#([^\[]*)(\[(.+)\])?#', $post_input_data->name, $matches ); |
| 207 | |
| 208 | $array_bits = array( $matches[1] ); |
| 209 | |
| 210 | if ( isset( $matches[3] ) ) { |
| 211 | $array_bits = array_merge( $array_bits, explode( '][', $matches[3] ) ); |
| 212 | } |
| 213 | |
| 214 | $new_post_data = array(); |
| 215 | |
| 216 | // Build the new array value from leaf to trunk. |
| 217 | for ( $i = count( $array_bits ) - 1; $i >= 0; $i -- ) { |
| 218 | if ( count( $array_bits ) - 1 === $i ) { |
| 219 | $new_post_data[ $array_bits[ $i ] ] = wp_slash( $post_input_data->value ); |
| 220 | } else { |
| 221 | $new_post_data = array( |
| 222 | $array_bits[ $i ] => $new_post_data, |
| 223 | ); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | $data = array_replace_recursive( $data, $new_post_data ); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // Check for empty meta key. |
| 232 | $empty_meta_data = array(); |
| 233 | if ( ! empty( $data['form_fields'] ) ) { |
| 234 | foreach ( $data['form_fields'] as $field ) { |
| 235 | // Register string for translation. |
| 236 | if ( isset( $field['label'] ) ) { |
| 237 | evf_string_translation( $data['id'], $field['id'], $field['label'] ); |
| 238 | } |
| 239 | |
| 240 | if ( empty( $field['meta-key'] ) && ! in_array( $field['type'], array( 'html', 'title', 'captcha' ), true ) ) { |
| 241 | $empty_meta_data[] = $field['label']; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | if ( ! empty( $empty_meta_data ) ) { |
| 246 | wp_send_json_error( |
| 247 | array( |
| 248 | 'errorTitle' => esc_html__( 'Meta Key missing', 'everest-forms' ), |
| 249 | /* translators: %s: empty meta data */ |
| 250 | 'errorMessage' => sprintf( esc_html__( 'Please add Meta key for fields: %s', 'everest-forms' ), '<strong>' . implode( ', ', $empty_meta_data ) . '</strong>' ), |
| 251 | ) |
| 252 | ); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // Fix for sorting field ordering. |
| 257 | if ( isset( $data['structure'], $data['form_fields'] ) ) { |
| 258 | $structure = evf_flatten_array( $data['structure'] ); |
| 259 | $data['form_fields'] = array_merge( array_intersect_key( array_flip( $structure ), $data['form_fields'] ), $data['form_fields'] ); |
| 260 | } |
| 261 | |
| 262 | $form_id = evf()->form->update( $data['id'], $data ); |
| 263 | |
| 264 | do_action( 'everest_forms_save_form', $form_id, $data ); |
| 265 | |
| 266 | if ( ! $form_id ) { |
| 267 | wp_send_json_error( |
| 268 | array( |
| 269 | 'errorTitle' => esc_html__( 'Form not found', 'everest-forms' ), |
| 270 | 'errorMessage' => esc_html__( 'An error occurred while saving the form.', 'everest-forms' ), |
| 271 | ) |
| 272 | ); |
| 273 | } else { |
| 274 | wp_send_json_success( |
| 275 | array( |
| 276 | 'form_name' => esc_html( $data['settings']['form_title'] ), |
| 277 | 'redirect_url' => admin_url( 'admin.php?page=evf-builder' ), |
| 278 | ) |
| 279 | ); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Ajax handler for form submission. |
| 285 | */ |
| 286 | public static function ajax_form_submission() { |
| 287 | check_ajax_referer( 'everest_forms_ajax_form_submission', 'security' ); |
| 288 | |
| 289 | if ( ! empty( $_POST['everest_forms']['id'] ) ) { |
| 290 | $process = evf()->task->ajax_form_submission( stripslashes_deep( $_POST['everest_forms'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 291 | if ( 'success' === $process['response'] ) { |
| 292 | wp_send_json_success( $process ); |
| 293 | } |
| 294 | |
| 295 | wp_send_json_error( $process ); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Ajax handler for template required addon activation. |
| 301 | */ |
| 302 | public static function template_activate_addon() { |
| 303 | check_ajax_referer( 'everest_forms_template_licence_check', 'security' ); |
| 304 | |
| 305 | if ( empty( $_POST['addon'] ) ) { |
| 306 | wp_send_json_error( |
| 307 | array( |
| 308 | 'errorCode' => 'no_addon_specified', |
| 309 | 'errorMessage' => esc_html__( 'No Addon specified.', 'everest-forms' ), |
| 310 | ) |
| 311 | ); |
| 312 | } |
| 313 | |
| 314 | $activate = activate_plugin( sanitize_text_field( wp_unslash( $_POST['addon'] ) ) . '/' . sanitize_text_field( wp_unslash( $_POST['addon'] ) ) . '.php' ); |
| 315 | |
| 316 | if ( is_wp_error( $activate ) ) { |
| 317 | wp_send_json_error( |
| 318 | array( |
| 319 | 'errorCode' => 'addon_not_active', |
| 320 | 'errorMessage' => esc_html__( 'Addon can not be activate. Please try again.', 'everest-forms' ), |
| 321 | ) |
| 322 | ); |
| 323 | } else { |
| 324 | wp_send_json_success( 'Addon sucessfully activated.' ); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Ajax handler for licence check. |
| 330 | * |
| 331 | * @global WP_Filesystem_Base $wp_filesystem Subclass |
| 332 | */ |
| 333 | public static function template_licence_check() { |
| 334 | check_ajax_referer( 'everest_forms_template_licence_check', 'security' ); |
| 335 | |
| 336 | if ( empty( $_POST['plan'] ) ) { |
| 337 | wp_send_json_error( |
| 338 | array( |
| 339 | 'plan' => '', |
| 340 | 'errorCode' => 'no_plan_specified', |
| 341 | 'errorMessage' => esc_html__( 'No Plan specified.', 'everest-forms' ), |
| 342 | ) |
| 343 | ); |
| 344 | } |
| 345 | |
| 346 | $addons = array(); |
| 347 | $raw_templates = wp_safe_remote_get( 'https://raw.githubusercontent.com/wpeverest/extensions-json/master/everest-forms/templates/all_templates.json' ); |
| 348 | |
| 349 | if ( ! is_wp_error( $raw_templates ) ) { |
| 350 | $template_data = json_decode( wp_remote_retrieve_body( $raw_templates ) ); |
| 351 | |
| 352 | if ( ! empty( $template_data->templates ) ) { |
| 353 | foreach ( $template_data->templates as $template ) { |
| 354 | if ( isset( $_POST['slug'] ) && $template->slug === $_POST['slug'] && in_array( $_POST['plan'], $template->plan, true ) ) { |
| 355 | $addons = $template->addons; |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | $output = '<div class="everest-forms-recommend-addons">'; |
| 362 | $output .= '<p class="desc plugins-info">' . esc_html__( 'This form template requires the following addons.', 'everest-forms' ) . '</p>'; |
| 363 | $output .= '<table class="plugins-list-table widefat striped">'; |
| 364 | $output .= '<thead><tr><th scope="col" class="manage-column required-plugins" colspan="2">Required Addons</th></tr></thead><tbody id="the-list">'; |
| 365 | $output .= '</div>'; |
| 366 | |
| 367 | $activated = true; |
| 368 | foreach ( $addons as $slug => $addon ) { |
| 369 | if ( is_plugin_active( $slug . '/' . $slug . '.php' ) ) { |
| 370 | $class = 'active'; |
| 371 | $parent_class = ''; |
| 372 | } elseif ( file_exists( WP_PLUGIN_DIR . '/' . $slug . '/' . $slug . '.php' ) ) { |
| 373 | $class = 'activate-now'; |
| 374 | $parent_class = 'inactive'; |
| 375 | $activated = false; |
| 376 | } else { |
| 377 | $class = 'install-now'; |
| 378 | $parent_class = 'inactive'; |
| 379 | $activated = false; |
| 380 | } |
| 381 | $output .= '<tr class="plugin-card-' . $slug . ' plugin ' . $parent_class . '" data-slug="' . $slug . '" data-plugin="' . $slug . '/' . $slug . '.php" data-name="' . $addon . '">'; |
| 382 | $output .= '<td class="plugin-name">' . $addon . '</td>'; |
| 383 | $output .= '<td class="plugin-status"><span class="' . esc_attr( $class ) . '"></span></td>'; |
| 384 | $output .= '</tr>'; |
| 385 | } |
| 386 | $output .= '</tbody></table></div>'; |
| 387 | |
| 388 | wp_send_json_success( |
| 389 | array( |
| 390 | 'html' => $output, |
| 391 | 'activate' => $activated, |
| 392 | ) |
| 393 | ); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Ajax handler for installing a extension. |
| 398 | * |
| 399 | * @since 1.2.0 |
| 400 | * |
| 401 | * @see Plugin_Upgrader |
| 402 | * |
| 403 | * @global WP_Filesystem_Base $wp_filesystem Subclass |
| 404 | */ |
| 405 | public static function install_extension() { |
| 406 | check_ajax_referer( 'updates' ); |
| 407 | |
| 408 | if ( empty( $_POST['slug'] ) ) { |
| 409 | wp_send_json_error( |
| 410 | array( |
| 411 | 'slug' => '', |
| 412 | 'errorCode' => 'no_plugin_specified', |
| 413 | 'errorMessage' => esc_html__( 'No plugin specified.', 'everest-forms' ), |
| 414 | ) |
| 415 | ); |
| 416 | } |
| 417 | |
| 418 | $slug = sanitize_key( wp_unslash( $_POST['slug'] ) ); |
| 419 | $plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['slug'] . '/' . $_POST['slug'] . '.php' ) ) ); |
| 420 | $status = array( |
| 421 | 'install' => 'plugin', |
| 422 | 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ), |
| 423 | ); |
| 424 | |
| 425 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 426 | $status['errorMessage'] = esc_html__( 'Sorry, you are not allowed to install plugins on this site.', 'everest-forms' ); |
| 427 | wp_send_json_error( $status ); |
| 428 | } |
| 429 | |
| 430 | include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 431 | include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 432 | |
| 433 | if ( file_exists( WP_PLUGIN_DIR . '/' . $slug ) ) { |
| 434 | $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); |
| 435 | $status['plugin'] = $plugin; |
| 436 | $status['pluginName'] = $plugin_data['Name']; |
| 437 | |
| 438 | if ( current_user_can( 'activate_plugin', $plugin ) && is_plugin_inactive( $plugin ) ) { |
| 439 | $result = activate_plugin( $plugin ); |
| 440 | |
| 441 | if ( is_wp_error( $result ) ) { |
| 442 | $status['errorCode'] = $result->get_error_code(); |
| 443 | $status['errorMessage'] = $result->get_error_message(); |
| 444 | wp_send_json_error( $status ); |
| 445 | } |
| 446 | |
| 447 | wp_send_json_success( $status ); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | $api = json_decode( |
| 452 | EVF_Updater_Key_API::version( |
| 453 | array( |
| 454 | 'license' => get_option( 'everest-forms-pro_license_key' ), |
| 455 | 'item_name' => sanitize_text_field( wp_unslash( $_POST['name'] ) ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated |
| 456 | ) |
| 457 | ) |
| 458 | ); |
| 459 | |
| 460 | if ( is_wp_error( $api ) ) { |
| 461 | $status['errorMessage'] = $api->get_error_message(); |
| 462 | wp_send_json_error( $status ); |
| 463 | } |
| 464 | |
| 465 | $status['pluginName'] = $api->name; |
| 466 | |
| 467 | $skin = new WP_Ajax_Upgrader_Skin(); |
| 468 | $upgrader = new Plugin_Upgrader( $skin ); |
| 469 | $result = $upgrader->install( $api->download_link ); |
| 470 | |
| 471 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 472 | $status['debug'] = $skin->get_upgrade_messages(); |
| 473 | } |
| 474 | |
| 475 | if ( is_wp_error( $result ) ) { |
| 476 | $status['errorCode'] = $result->get_error_code(); |
| 477 | $status['errorMessage'] = $result->get_error_message(); |
| 478 | wp_send_json_error( $status ); |
| 479 | } elseif ( is_wp_error( $skin->result ) ) { |
| 480 | $status['errorCode'] = $skin->result->get_error_code(); |
| 481 | $status['errorMessage'] = $skin->result->get_error_message(); |
| 482 | wp_send_json_error( $status ); |
| 483 | } elseif ( $skin->get_errors()->get_error_code() ) { |
| 484 | $status['errorMessage'] = $skin->get_error_messages(); |
| 485 | wp_send_json_error( $status ); |
| 486 | } elseif ( is_null( $result ) ) { |
| 487 | global $wp_filesystem; |
| 488 | |
| 489 | $status['errorCode'] = 'unable_to_connect_to_filesystem'; |
| 490 | $status['errorMessage'] = esc_html__( 'Unable to connect to the filesystem. Please confirm your credentials.', 'everest-forms' ); |
| 491 | |
| 492 | // Pass through the error from WP_Filesystem if one was raised. |
| 493 | if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { |
| 494 | $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); |
| 495 | } |
| 496 | |
| 497 | wp_send_json_error( $status ); |
| 498 | } |
| 499 | |
| 500 | $install_status = install_plugin_install_status( $api ); |
| 501 | |
| 502 | if ( current_user_can( 'activate_plugin', $install_status['file'] ) && is_plugin_inactive( $install_status['file'] ) ) { |
| 503 | if ( isset( $_POST['page'] ) && 'everest-forms_page_evf-builder' === $_POST['page'] ) { |
| 504 | activate_plugin( $install_status['file'] ); |
| 505 | } else { |
| 506 | $status['activateUrl'] = add_query_arg( |
| 507 | array( |
| 508 | 'action' => 'activate', |
| 509 | 'plugin' => $install_status['file'], |
| 510 | '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $install_status['file'] ), |
| 511 | ), |
| 512 | admin_url( 'admin.php?page=evf-addons' ) |
| 513 | ); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | wp_send_json_success( $status ); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * AJAX Integration connect. |
| 522 | */ |
| 523 | public static function integration_connect() { |
| 524 | check_ajax_referer( 'process-ajax-nonce', 'security' ); |
| 525 | |
| 526 | // Checking permission. |
| 527 | if ( ! current_user_can( 'manage_everest_forms' ) ) { |
| 528 | wp_die( -1 ); |
| 529 | } |
| 530 | |
| 531 | if ( empty( $_POST ) ) { |
| 532 | wp_send_json_error( |
| 533 | array( |
| 534 | 'error' => esc_html__( 'Missing data', 'everest-forms' ), |
| 535 | ) |
| 536 | ); |
| 537 | } |
| 538 | |
| 539 | do_action( 'everest_forms_integration_account_connect_' . sanitize_text_field( wp_unslash( $_POST['source'] ) ), $_POST ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * AJAX Email Add. |
| 544 | */ |
| 545 | public static function new_email_add() { |
| 546 | check_ajax_referer( 'process-ajax-nonce', 'security' ); |
| 547 | |
| 548 | if ( ! current_user_can( 'manage_everest_forms' ) ) { |
| 549 | wp_die( -1 ); |
| 550 | } |
| 551 | $connection_id = 'connection_' . uniqid(); |
| 552 | |
| 553 | wp_send_json_success( |
| 554 | array( |
| 555 | 'connection_id' => $connection_id, |
| 556 | ) |
| 557 | ); |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * AJAX Integration disconnect. |
| 562 | */ |
| 563 | public static function integration_disconnect() { |
| 564 | check_ajax_referer( 'process-ajax-nonce', 'security' ); |
| 565 | |
| 566 | // Checking permission. |
| 567 | if ( ! current_user_can( 'manage_everest_forms' ) ) { |
| 568 | wp_die( -1 ); |
| 569 | } |
| 570 | |
| 571 | if ( empty( $_POST ) ) { |
| 572 | wp_send_json_error( |
| 573 | array( |
| 574 | 'error' => esc_html__( 'Missing data', 'everest-forms' ), |
| 575 | ) |
| 576 | ); |
| 577 | } |
| 578 | |
| 579 | $connected_accounts = get_option( 'everest_forms_integrations', false ); |
| 580 | |
| 581 | if ( ! empty( $connected_accounts[ $_POST['source'] ][ $_POST['key'] ] ) ) { |
| 582 | unset( $connected_accounts[ $_POST['source'] ][ $_POST['key'] ] ); |
| 583 | update_option( 'everest_forms_integrations', $connected_accounts ); |
| 584 | wp_send_json_success(); |
| 585 | } else { |
| 586 | wp_send_json_error( |
| 587 | array( |
| 588 | 'error' => esc_html__( 'Connection missing', 'everest-forms' ), |
| 589 | ) |
| 590 | ); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * AJAX plugin deactivation notice. |
| 596 | */ |
| 597 | public static function deactivation_notice() { |
| 598 | global $status, $page, $s; |
| 599 | |
| 600 | check_ajax_referer( 'deactivation-notice', 'security' ); |
| 601 | |
| 602 | $deactivate_url = wp_nonce_url( |
| 603 | add_query_arg( |
| 604 | array( |
| 605 | 'action' => 'deactivate', |
| 606 | 'plugin' => EVF_PLUGIN_BASENAME, |
| 607 | 'plugin_status' => $status, |
| 608 | 'paged' => $page, |
| 609 | 's' => $s, |
| 610 | ), |
| 611 | admin_url( 'plugins.php' ) |
| 612 | ), |
| 613 | 'deactivate-plugin_' . EVF_PLUGIN_BASENAME |
| 614 | ); |
| 615 | |
| 616 | /* translators: %1$s - deactivation reason page; %2$d - deactivation url. */ |
| 617 | $deactivation_notice = sprintf( __( 'Before we deactivate Everest Forms, would you care to <a href="%1$s" target="_blank">let us know why</a> so we can improve it for you? <a href="%2$s">No, deactivate now</a>.', 'everest-forms' ), 'https://wpeverest.com/deactivation/everest-forms/', $deactivate_url ); |
| 618 | |
| 619 | wp_send_json( |
| 620 | array( |
| 621 | 'fragments' => apply_filters( |
| 622 | 'everest_forms_deactivation_notice_fragments', |
| 623 | array( |
| 624 | 'deactivation_notice' => '<tr class="plugin-update-tr active updated" data-slug="everest-forms" data-plugin="everest-forms/everest-forms.php"><td colspan ="3" class="plugin-update colspanchange"><div class="notice inline notice-warning notice-alt"><p>' . $deactivation_notice . '</p></div></td></tr>', |
| 625 | ) |
| 626 | ), |
| 627 | ) |
| 628 | ); |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * Triggered when clicking the rating footer. |
| 633 | */ |
| 634 | public static function rated() { |
| 635 | if ( ! current_user_can( 'manage_everest_forms' ) ) { |
| 636 | wp_die( -1 ); |
| 637 | } |
| 638 | update_option( 'everest_forms_admin_footer_text_rated', 1 ); |
| 639 | wp_die(); |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Triggered when clicking the review notice button. |
| 644 | */ |
| 645 | public static function review_dismiss() { |
| 646 | if ( ! current_user_can( 'manage_everest_forms' ) ) { |
| 647 | wp_die( -1 ); |
| 648 | } |
| 649 | $review = get_option( 'everest_forms_review', array() ); |
| 650 | $review['time'] = current_time( 'timestamp' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested |
| 651 | $review['dismissed'] = true; |
| 652 | update_option( 'everest_forms_review', $review ); |
| 653 | wp_die(); |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Triggered when clicking the form toggle. |
| 658 | */ |
| 659 | public static function enabled_form() { |
| 660 | // Run a security check. |
| 661 | check_ajax_referer( 'everest_forms_enabled_form', 'security' ); |
| 662 | |
| 663 | if ( ! current_user_can( 'manage_everest_forms' ) ) { |
| 664 | wp_die( -1 ); |
| 665 | } |
| 666 | |
| 667 | $form_id = isset( $_POST['form_id'] ) ? absint( $_POST['form_id'] ) : 0; |
| 668 | $enabled = isset( $_POST['enabled'] ) ? absint( $_POST['enabled'] ) : 0; |
| 669 | |
| 670 | $form_data = evf()->form->get( absint( $form_id ), array( 'content_only' => true ) ); |
| 671 | |
| 672 | $form_data['form_enabled'] = $enabled; |
| 673 | |
| 674 | evf()->form->update( $form_id, $form_data ); |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Import Form ajax. |
| 679 | */ |
| 680 | public static function import_form_action() { |
| 681 | try { |
| 682 | check_ajax_referer( 'process-import-ajax-nonce', 'security' ); |
| 683 | EVF_Admin_Import_Export::import_form(); |
| 684 | } catch ( Exception $e ) { |
| 685 | wp_send_json_error( |
| 686 | array( |
| 687 | 'message' => $e->getMessage(), |
| 688 | ) |
| 689 | ); |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | EVF_AJAX::init(); |
| 695 |