admin
3 months ago
pagebuilders
3 months ago
snippets
3 months ago
widgets
3 months ago
ajax-functions.php
3 months ago
extras.php
3 months ago
install.php
3 months ago
scripts.php
3 months ago
transient.php
3 months ago
ajax-functions.php
163 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * AJAX Functions |
| 5 | * |
| 6 | * Process AJAX actions. |
| 7 | * |
| 8 | * @copyright Copyright (c) 2016, Jeffrey Carandang |
| 9 | * @since 4.0 |
| 10 | */ |
| 11 | // Exit if accessed directly |
| 12 | if (!defined('ABSPATH')) exit; |
| 13 | |
| 14 | /** |
| 15 | * Save Options |
| 16 | * |
| 17 | * @since 1.0 |
| 18 | * @return void |
| 19 | */ |
| 20 | function widgetopts_ajax_save_settings() |
| 21 | { |
| 22 | $response = array('errors' => array()); |
| 23 | |
| 24 | if (!isset($_POST['method'])) return; |
| 25 | if (!isset($_POST['nonce'])) return; |
| 26 | |
| 27 | if (!wp_verify_nonce($_POST['nonce'], 'widgetopts-settings-nonce')) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | switch ($_POST['method']) { |
| 32 | case 'activate': |
| 33 | case 'deactivate': |
| 34 | if (!isset($_POST['module'])) return; |
| 35 | |
| 36 | //update options |
| 37 | update_option('widgetopts_tabmodule-' . sanitize_text_field($_POST['module']), sanitize_text_field($_POST['method'])); |
| 38 | |
| 39 | //update global variable |
| 40 | widgetopts_update_option(sanitize_text_field($_POST['module']), sanitize_text_field($_POST['method'])); |
| 41 | break; |
| 42 | |
| 43 | case 'save': |
| 44 | $response['messages'] = array(__('Settings saved successfully.', 'widget-options')); |
| 45 | if (!isset($_POST['data'])) return; |
| 46 | parse_str($_POST['data']['--widgetopts-form-serialized-data'], $params); |
| 47 | $sanitized = widgetopts_sanitize_array($params); |
| 48 | update_option('widgetopts_tabmodule-settings', maybe_serialize($sanitized)); |
| 49 | |
| 50 | //reset options |
| 51 | widgetopts_update_option('settings', $sanitized); |
| 52 | break; |
| 53 | |
| 54 | case 'deactivate_license': |
| 55 | global $extended_license; |
| 56 | $license = sanitize_text_field($_POST['data']['license-data']); |
| 57 | if (!empty($license)) { |
| 58 | |
| 59 | if (isset($_POST['data']['shortname'])) { |
| 60 | $item_shortname = sanitize_text_field($_POST['data']['shortname']); |
| 61 | } else { |
| 62 | $item_shortname = 'widgetopts_' . preg_replace('/[^a-zA-Z0-9_\s]/', '', str_replace(' ', '_', strtolower(WIDGETOPTS_PLUGIN_NAME))); |
| 63 | } |
| 64 | switch ($item_shortname) { |
| 65 | case 'widgetopts_sliding_widget_options': |
| 66 | global $widgetopts_sliding_license; |
| 67 | $data = $widgetopts_sliding_license->deactivate_license($license); |
| 68 | break; |
| 69 | |
| 70 | default: |
| 71 | $data = $extended_license->deactivate_license($license); |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | $response['button'] = sanitize_text_field($_POST['data']['button']); |
| 76 | if ($data == 'deactivated') { |
| 77 | |
| 78 | $optiondata = get_option('widgetopts_license_keys'); |
| 79 | $name = str_replace('widgetopts-license-btn-', '', sanitize_text_field($_POST['data']['button'])); |
| 80 | $optiondata[$name] = ''; |
| 81 | update_option('widgetopts_license_keys', $optiondata); |
| 82 | |
| 83 | //remove license key on option |
| 84 | delete_option($item_shortname . '_license_key'); |
| 85 | |
| 86 | $response['messages'] = array(__('Successfully Deactivated.', 'widget-options')); |
| 87 | } else { |
| 88 | $response['messages'] = array(__('Deactivation Failed.', 'widget-options')); |
| 89 | } |
| 90 | $response['success'] = $data; |
| 91 | } |
| 92 | |
| 93 | break; |
| 94 | case 'delete_widgetopts_update_transient': |
| 95 | update_option('widgetopts_upgrade', 0); |
| 96 | break; |
| 97 | |
| 98 | default: |
| 99 | # code... |
| 100 | break; |
| 101 | } |
| 102 | $response['source'] = 'WIDGETOPTS_Response'; |
| 103 | $response['response'] = 'success'; |
| 104 | $response['closeModal'] = true; |
| 105 | $response = (object) $response; |
| 106 | |
| 107 | //let devs do there action |
| 108 | do_action('widget_options_before_ajax_print', sanitize_text_field($_POST['method'])); |
| 109 | |
| 110 | echo json_encode($response); |
| 111 | die(); |
| 112 | } |
| 113 | add_action('wp_ajax_widgetopts_ajax_settings', 'widgetopts_ajax_save_settings'); |
| 114 | |
| 115 | /* Hide the rating div |
| 116 | * @return json string |
| 117 | * |
| 118 | */ |
| 119 | if (!function_exists('widgetopts_ajax_hide_rating')) : |
| 120 | function widgetopts_ajax_hide_rating() |
| 121 | { |
| 122 | if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'widgetopts_ajax_nonce')) { |
| 123 | wp_send_json_error('Invalid nonce or session.', 403); |
| 124 | exit; |
| 125 | } |
| 126 | |
| 127 | if (!current_user_can('update_plugins')) { |
| 128 | wp_send_json_error('You do not have permission to update plugins.', 403); |
| 129 | exit; |
| 130 | } |
| 131 | |
| 132 | $updated = update_option('widgetopts_RatingDiv', 'yes'); |
| 133 | |
| 134 | if ($updated) { |
| 135 | wp_send_json_success(array('message' => 'Rating visibility updated successfully.')); |
| 136 | } else { |
| 137 | wp_send_json_error('Failed to update the option.', 500); |
| 138 | } |
| 139 | exit; |
| 140 | } |
| 141 | add_action('wp_ajax_widgetopts_hideRating', 'widgetopts_ajax_hide_rating'); |
| 142 | endif; |
| 143 | |
| 144 | function widgetopts_ajax_validate_expression() |
| 145 | { |
| 146 | if (!wp_verify_nonce($_POST['nonce'], 'widgetopts-expression-nonce')) { |
| 147 | echo json_encode(['response' => 'failed', 'message' => 'Security check failed. Please refresh the page and try again.']); |
| 148 | die(); |
| 149 | } |
| 150 | |
| 151 | if (!isset($_POST['expression']) || empty(trim($_POST['expression']))) { |
| 152 | echo json_encode(['response' => 'success', 'message' => 'Expression is empty, but this will be considered as valid.', 'valid' => true]); |
| 153 | die(); |
| 154 | } |
| 155 | |
| 156 | $expression = sanitize_text_field($_POST['expression']); |
| 157 | $result = widgetopts_validate_expression($expression); |
| 158 | |
| 159 | echo json_encode(['response' => 'success', 'message' => $result['message'], 'valid' => $result['valid']]); |
| 160 | die(); |
| 161 | } |
| 162 | add_action('wp_ajax_widgetopts_ajax_validate_expression', 'widgetopts_ajax_validate_expression'); |
| 163 |