|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Admin Ajax |
|
4
|
|
|
* |
|
5
|
|
|
* @package SimpleCalendar\Admin |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace SimpleCalendar\Admin; |
|
8
|
|
|
|
|
9
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
10
|
|
|
exit; |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Admin ajax. |
|
15
|
|
|
* |
|
16
|
|
|
* @since 3.0.0 |
|
17
|
|
|
*/ |
|
18
|
|
|
class Ajax { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Set up ajax hooks. |
|
22
|
|
|
* |
|
23
|
|
|
* @since 3.0.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct() { |
|
26
|
|
|
|
|
27
|
|
|
// Set an option if the user rated the plugin. |
|
28
|
|
|
add_action( 'wp_ajax_simcal_rated', array( $this, 'rate_plugin' ) ); |
|
29
|
|
|
|
|
30
|
|
|
// Set an option if the user rated the plugin. |
|
31
|
|
|
add_action( 'wp_ajax_simcal_clear_cache', array( $this, 'clear_cache' ) ); |
|
32
|
|
|
|
|
33
|
|
|
// Convert a datetime format. |
|
34
|
|
|
add_action( 'wp_ajax_simcal_date_i18n_input_preview', array( $this, 'date_i18n' ) ); |
|
35
|
|
|
|
|
36
|
|
|
// Manage an add-on license activation or deactivation. |
|
37
|
|
|
add_action( 'wp_ajax_simcal_manage_add_on_license', array( $this, 'manage_add_on_license' ) ); |
|
38
|
|
|
|
|
39
|
|
|
// Reset add-ons licenses. |
|
40
|
|
|
add_action( 'wp_ajax_simcal_reset_add_ons_licenses', array( $this, 'reset_licenses' ) ); |
|
41
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Clear transients. |
|
46
|
|
|
* |
|
47
|
|
|
* @since 3.0.0 |
|
48
|
|
|
*/ |
|
49
|
|
|
public function clear_cache() { |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
$id = isset( $_POST['id'] ) ? ( is_array( $_POST['id'] ) ? array_map( 'intval', $_POST['id'] ) : intval( $_POST['id'] ) ) : ''; |
|
52
|
|
|
|
|
53
|
|
|
if ( ! empty( $id ) ) { |
|
54
|
|
|
simcal_delete_feed_transients( $id ); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
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() { |
|
64
|
|
|
update_option( 'simple-calendar_admin_footer_text_rated', date( 'Y-m-d', time() ) ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Ajax callback to return a formatted datetime string. |
|
69
|
|
|
* |
|
70
|
|
|
* @since 3.0.0 |
|
71
|
|
|
*/ |
|
72
|
|
|
public function date_i18n() { |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
$value = isset( $_POST['value'] ) ? esc_attr( $_POST['value'] ) : ' '; |
|
75
|
|
|
$timestamp = isset( $_POST['timestamp'] ) ? absint( $_POST['timestamp'] ) : time(); |
|
76
|
|
|
|
|
77
|
|
|
wp_send_json_success( date_i18n( $value, $timestamp ) ); |
|
78
|
|
|
} |
|
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() { |
|
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
$nonce = isset( $_POST['nonce'] ) ? esc_attr( $_POST['nonce'] ) : ''; |
|
165
|
|
|
|
|
166
|
|
|
// Verify this request comes from the add-ons licenses activation settings page. |
|
167
|
|
|
if ( empty ( $nonce ) || ! wp_verify_nonce( $nonce, 'simcal_license_manager' ) ) { |
|
168
|
|
|
wp_send_json_error( sprintf( __( 'An error occurred: %s', 'google-calendar-events' ), 'Nonce verification failed.' ) ); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
delete_option( 'simple-calendar_settings_licenses' ); |
|
172
|
|
|
delete_option( 'simple-calendar_licenses_status' ); |
|
173
|
|
|
|
|
174
|
|
|
wp_send_json_success( 'success' ); |
|
175
|
|
|
} |
|
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: