1 | <?php |
||
18 | class Ajax { |
||
19 | |||
20 | /** |
||
21 | * Set up ajax hooks. |
||
22 | * |
||
23 | * @since 3.0.0 |
||
24 | */ |
||
25 | public function __construct() { |
||
43 | |||
44 | /** |
||
45 | * Clear transients. |
||
46 | * |
||
47 | * @since 3.0.0 |
||
48 | */ |
||
49 | public function clear_cache() { |
||
57 | |||
58 | /** |
||
59 | * Ajax callback when a user clicks on the rate plugin link. |
||
60 | * |
||
61 | * @since 3.0.0 |
||
62 | */ |
||
63 | public function rate_plugin() { |
||
66 | |||
67 | /** |
||
68 | * Ajax callback to return a formatted datetime string. |
||
69 | * |
||
70 | * @since 3.0.0 |
||
71 | */ |
||
72 | public function date_i18n() { |
||
79 | |||
80 | /** |
||
81 | * Activate add-on license. |
||
82 | * |
||
83 | * This code is run only when an add-on requiring a license is installed and active. |
||
84 | * |
||
85 | * @since 3.0.0 |
||
86 | */ |
||
87 | public function manage_add_on_license() { |
||
88 | |||
89 | $addon = isset( $_POST['add_on'] ) ? sanitize_key( $_POST['add_on'] ) : false; |
||
90 | $action = isset( $_POST['license_action'] ) ? esc_attr( $_POST['license_action'] ) : false; |
||
91 | $key = isset( $_POST['license_key'] ) ? esc_attr( $_POST['license_key'] ) : ''; |
||
92 | $nonce = isset( $_POST['nonce'] ) ? esc_attr( $_POST['nonce'] ) : ''; |
||
93 | |||
94 | // Verify that there are valid variables to process. |
||
95 | if ( false === $addon || ! in_array( $action, array( 'activate_license', 'deactivate_license' ) ) ) { |
||
96 | wp_send_json_error( __( 'Add-on unspecified or invalid action.', 'google-calendar-events' ) ); |
||
97 | } |
||
98 | |||
99 | // Verify this request comes from the add-ons licenses activation settings page. |
||
100 | if ( ! wp_verify_nonce( $nonce, 'simcal_license_manager' ) ) { |
||
101 | wp_send_json_error( sprintf( __( 'An error occurred: %s', 'google-calendar-events' ), 'Nonce verification failed.' ) ); |
||
102 | } |
||
103 | |||
104 | // Removes the prefix and converts simcal_{id_no} to {id_no}. |
||
105 | $id = intval( substr( $addon, 7 ) ); |
||
106 | |||
107 | // Data to send in API request. |
||
108 | $api_request = array( |
||
109 | 'edd_action' => $action, |
||
110 | 'license' => $key, |
||
111 | 'item_id' => urlencode( $id ), |
||
112 | 'url' => home_url() |
||
113 | ); |
||
114 | |||
115 | // Call the custom API. |
||
116 | $response = wp_remote_post( |
||
117 | defined( 'SIMPLE_CALENDAR_STORE_URL' ) ? SIMPLE_CALENDAR_STORE_URL : simcal_get_url( 'home' ), |
||
118 | array( |
||
119 | 'timeout' => 15, |
||
120 | 'sslverify' => false, |
||
121 | 'body' => $api_request |
||
122 | ) |
||
123 | ); |
||
124 | |||
125 | // Update license in db. |
||
126 | $keys = get_option( 'simple-calendar_settings_licenses', array() ); |
||
127 | $keys['keys'][ $addon ] = $key; |
||
128 | update_option( 'simple-calendar_settings_licenses', $keys ); |
||
129 | |||
130 | // Make sure there is a response. |
||
131 | if ( is_wp_error( $response ) ) { |
||
132 | wp_send_json_error( sprintf( __( 'There was an error processing your request: %s', 'google-calendar-events' ), $response->get_error_message() ) ); |
||
133 | } |
||
134 | |||
135 | // Decode the license data and save. |
||
136 | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
||
137 | $status = simcal_get_license_status(); |
||
138 | |||
139 | if ( ! empty( $license_data ) ) { |
||
140 | if ('deactivated' == $license_data->license) { |
||
141 | unset($status[$addon]); |
||
142 | update_option('simple-calendar_licenses_status', $status); |
||
143 | wp_send_json_success($license_data->license); |
||
144 | } elseif (in_array($license_data->license, array('valid', 'invalid'))) { |
||
145 | $status[$addon] = $license_data->license; |
||
146 | update_option('simple-calendar_licenses_status', $status); |
||
147 | $message = 'valid' == $license_data->license ? 'valid' : __('License key is invalid.', 'google-calendar-events'); |
||
148 | wp_send_json_success($message); |
||
149 | } else { |
||
150 | wp_send_json_error( '' ); |
||
151 | } |
||
152 | } else { |
||
153 | wp_send_json_error( __( 'An error has occurred, please try again.', 'google-calendar-events' ) ); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Reset licenses. |
||
159 | * |
||
160 | * @since 3.0.0 |
||
161 | */ |
||
162 | public function reset_licenses() { |
||
176 | |||
177 | } |
||
178 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: