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