class-license-manager.php
8.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
* Handle the add-on license and updates.
*
* @author Tijmen Smit
* @since 2.1.0
*/
if ( !defined( 'ABSPATH' ) ) exit;
class WPSL_License_Manager {
public $item_name;
public $item_shortname;
public $version;
public $author;
public $file;
public $api_url = 'https://wpstorelocator.co/';
/**
* Class constructor
*
* @param string $item_name
* @param string $version
* @param string $author
* @param string $file
*/
function __construct( $item_name, $version, $author, $file ) {
$this->item_name = $item_name;
$this->item_shortname = 'wpsl_' . preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->item_name ) ) );
$this->version = $version;
$this->author = $author;
$this->file = $file;
$this->includes();
add_action( 'admin_init', array( $this, 'auto_updater' ), 0 );
add_action( 'admin_init', array( $this, 'license_actions' ) );
add_filter( 'wpsl_license_settings', array( $this, 'add_license_field' ), 1 );
}
/**
* Include the updater class
*
* @since 2.1.0
* @access private
* @return void
*/
private function includes() {
if ( !class_exists( 'EDD_SL_Plugin_Updater' ) ) {
require_once 'EDD_SL_Plugin_Updater.php';
}
}
/**
* Handle the add-on updates.
*
* @since 2.1.0
* @return void
*/
public function auto_updater() {
if ( $this->get_license_option( 'status' ) !== 'valid' ) {
return;
}
$args = array(
'version' => $this->version,
'license' => $this->get_license_option( 'key' ),
'author' => $this->author,
'item_name' => $this->item_name
);
// Setup the updater.
$edd_updater = new EDD_SL_Plugin_Updater(
$this->api_url,
$this->file,
$args
);
}
/**
* Check which license actions to take.
*
* @since 2.1.0
* @return void
*/
public function license_actions() {
if ( !isset( $_POST['wpsl_licenses'] ) ) {
return;
}
if ( !isset( $_POST['wpsl_licenses'][ $this->item_shortname ] ) || empty( $_POST['wpsl_licenses'][ $this->item_shortname ] ) ) {
return;
}
if ( !check_admin_referer( $this->item_shortname . '_license-nonce', $this->item_shortname . '_license-nonce' ) ) {
return;
}
if ( !current_user_can( 'manage_wpsl_settings' ) ) {
return;
}
if ( isset( $_POST[ $this->item_shortname . '_license_key_deactivate' ] ) ) {
$this->deactivate_license();
} else {
$this->activate_license();
}
}
/**
* Try to activate the license key.
*
* @since 2.1.0
* @return void
*/
public function activate_license() {
// Stop if the current license is already active.
if ( $this->get_license_option( 'status' ) == 'valid' ) {
return;
}
$license = sanitize_text_field( $_POST['wpsl_licenses'][ $this->item_shortname ] );
// data to send in our API request.
$api_params = array(
'edd_action' => 'activate_license',
'license' => $license,
'item_name' => urlencode( $this->item_name ),
'url' => home_url()
);
// Get the license data from the API.
$license_data = $this->call_license_api( $api_params );
if ( $license_data ) {
update_option(
$this->item_shortname . '_license_data',
array(
'key' => $license,
'expiration' => $license_data->expires,
'status' => $license_data->license
)
);
if ( $license_data->success ) {
$this->set_license_notice( $this->item_name . ' license activated.', 'updated' );
} else if ( !empty( $license_data->error ) ) {
$this->handle_activation_errors( $license_data->error );
}
}
}
/**
* Deactivate the license key.
*
* @since 2.1.0
* @return void
*/
public function deactivate_license() {
// Data to send to the API
$api_params = array(
'edd_action' => 'deactivate_license',
'license' => $this->get_license_option( 'key' ),
'item_name' => urlencode( $this->item_name ),
'url' => home_url()
);
// Get the license data from the API.
$license_data = $this->call_license_api( $api_params );
if ( $license_data ) {
if ( $license_data->license == 'deactivated' ) {
delete_option( $this->item_shortname . '_license_data' );
$this->set_license_notice( $this->item_name . ' license deactivated.', 'updated' );
} else {
$message = sprintf (__( 'The %s license failed to deactivate, please try again later or contact support!', 'wpsl' ), $this->item_name );
$this->set_license_notice( $message, 'error' );
}
}
}
/**
* Access the license API.
*
* @since 2.1.0
* @params array $api_params The used API parameters
* @return void|array $license_data The returned license data on success
*/
public function call_license_api( $api_params ) {
$response = wp_remote_post(
$this->api_url,
array(
'timeout' => 15,
'sslverify' => false,
'body' => $api_params
)
);
// Make sure the response came back okay.
if ( is_wp_error( $response ) ) {
$message = $response->get_error_message() . '. ' . __( 'Please try again later!', 'wpsl' );
$this->set_license_notice( $message, 'error' );
} else {
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
return $license_data;
}
}
/**
* Get a single license option.
*
* @since 2.1.0
* @param string $option Name of the license option.
* @return void|string The value for the license option.
*/
public function get_license_option( $option ) {
$license_data = get_option( $this->item_shortname . '_license_data' );
if ( isset( $license_data[ $option ] ) ) {
return $license_data[ $option ];
}
}
/**
* Set a notice holding license information.
*
* @since 2.1.0
* @param string $message The license message to display.
* @param string $type Either updated or error.
* @return void
*/
public function set_license_notice( $message, $type ) {
add_settings_error( $this->item_shortname . '-license', 'license-notice', $message, $type );
}
/**
* Check the different license activation errors.
*
* @since 2.1.0
* @param string $activation_errors The activation errors returned by the license API.
* @return void
*/
public function handle_activation_errors( $activation_errors ) {
switch ( $activation_errors ) {
case 'item_name_mismatch':
$error_msg = sprintf( __( 'The %s license key does not belong to this add-on.', 'wpsl' ), $this->item_name );
break;
case 'no_activations_left':
$error_msg = sprintf( __( 'The %s license key does not have any activations left.', 'wpsl' ), $this->item_name );
break;
case 'expired':
$error_msg = sprintf( __( 'The %s license key is expired. Please renew it.', 'wpsl' ), $this->item_name );
break;
default:
$error_msg = sprintf( __( 'There was a problem activating the license key for the %s, please try again or contact support. Error code: %s', 'wpsl' ), $this->item_name, $activation_errors );
break;
}
$this->set_license_notice( $error_msg, 'error' );
}
/**
* Add license fields to the settings.
*
* @since 2.1.0
* @param array $settings The existing settings.
* @return array
*/
public function add_license_field( $settings ) {
$license_setting = array(
array(
'name' => $this->item_name,
'short_name' => $this->item_shortname,
'status' => $this->get_license_option( 'status' ),
'key' => $this->get_license_option( 'key' ),
'expiration' => $this->get_license_option( 'expiration' )
)
);
return array_merge( $settings, $license_setting );
}
}