License_Page.php
18.6 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
namespace Wpo\Pages;
use \Wpo\Core\Extensions_Helpers;
use \Wpo\Core\WordPress_Helpers;
use \Wpo\Core\Wpmu_Helpers;
use \Wpo\Services\Options_Service;
use \Wpo\Services\Log_Service;
// Prevent public access to this script
defined('ABSPATH') or die();
if (!class_exists('\Wpo\Pages\License_Page')) {
class License_Page
{
private static $extensions = array();
public function __construct()
{
/**
* Load custom updater.
*/
// Multisite frontend
if (is_multisite() && !is_network_admin()) {
return;
}
// Single site frontend
if (!is_multisite() && !is_admin()) {
return;
}
// Collect information about all activated extensions
self::$extensions = Extensions_Helpers::get_extensions();
// No extensions so no need to add the license page
if (empty(self::$extensions)) {
return;
}
/**
* Add admin page.
*/
add_action('admin_menu', '\Wpo\Pages\License_Page::license_menu');
add_action('network_admin_menu', '\Wpo\Pages\License_Page::license_menu');
/**
* Activate license.
*/
add_action('admin_init', '\Wpo\Pages\License_Page::activate_license');
/**
* Deactivate license.
*/
add_action('admin_init', '\Wpo\Pages\License_Page::deactivate_license');
/**
* Show activation result.
*/
add_action('admin_notices', '\Wpo\Pages\License_Page::activation_notice');
add_action('network_admin_notices', '\Wpo\Pages\License_Page::activation_notice');
}
/**
* Adds a "Licenses" submenu page to the main WPO365 admin menu.
*/
public static function license_menu()
{
add_submenu_page('wpo365-wizard', 'Licenses', 'Licenses', 'delete_users', 'wpo365-manage-licenses', '\Wpo\Pages\License_Page::license_page');
}
public static function activation_notice()
{
if (isset($_GET['sl_activation']) && !empty($_GET['message']) && isset($_GET['page']) && $_GET['page'] == 'wpo365-manage-licenses') {
$message = sanitize_text_field(urldecode($_GET['message']));
switch ($_GET['sl_activation']) {
case 'false': ?>
<div class="notice notice-error">
<p><?php echo esc_html($message); ?></p>
</div>
<?php
break;
case 'true':
default:
?>
<div class="notice notice-success"><?php echo esc_html($message) ?></div>
<?php
break;
}
}
}
public static function activate_license()
{
// listen for our activate button to be clicked
if (isset($_POST['activate_license']) && isset($_POST['store_item_id'])) {
foreach (self::$extensions as $slug => $data) {
if ($data['store_item_id'] === intval(trim($_POST['store_item_id']))) {
$extension = $data;
break;
}
}
if (empty($extension)) {
Log_Service::write_log('WARN', __METHOD__ . ' -> Could not find extension for store item id ' . WordPress_Helpers::trim(sanitize_text_field($_POST['store_item_id'])));
return;
}
// run a quick security check
if (!check_admin_referer('wpo365-manage-licenses', 'wpo365_license_nonce')) {
Log_Service::write_log('WARN', __METHOD__ . ' -> Could not successfully verify nonce [check admin referrer failed]');
return;
}
// retrieve the license from the POSTed data
$license_key_name = 'license_' . $extension['store_item_id'];
$posted_license_key = !empty($_POST[$license_key_name]) ? $_POST[$license_key_name] : '';
$license_key = sanitize_text_field(trim($posted_license_key));
// Call the custom API.
$url = is_multisite() ? network_home_url() : home_url();
$response = wp_remote_get(\sprintf("https://www.wpo365.com/?edd_action=activate_license&license=%s&item_id=%s&url=%s", $license_key, $extension['store_item_id'], $url), array('timeout' => 15, 'sslverify' => false));
// make sure the response came back okay
if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) {
if (is_wp_error($response)) {
$message = $response->get_error_message();
} else {
Log_Service::write_log('ERROR', __METHOD__ . ' -> Error occurred when activating license. Check the next line for the raw response message received.');
Log_Service::write_log('ERROR', $response);
$message = __('An error occurred, please try again.');
}
} else {
$license_data = json_decode(wp_remote_retrieve_body($response));
if ($license_data->license == 'invalid') {
switch ($license_data->error) {
case 'expired':
$message = sprintf(
__('Your license key for <strong>%s</strong> expired on %s.'),
$extension['store_item'],
date_i18n(get_option('date_format'), strtotime($license_data->expires, current_time('timestamp')))
);
break;
case 'disabled':
$message = sprintf(
__('Your license key for <strong>%s</strong> has been disabled / revoked.'),
$extension['store_item']
);
break;
case 'missing':
$message = sprintf(
__('The license <strong>%s</strong> you entered does not exist.'),
$license_key
);
break;
case 'missing_url':
$message = __('URL not provided.');
break;
case 'key_mismatch':
$message = sprintf(
__('The license <strong>%s</strong> appears to be an invalid license key for %s.'),
$license_key,
$extension['store_item']
);
break;
case 'item_name_mismatch':
$message = sprintf(
__('The license <strong>%s</strong> appears to be invalid for %s.'),
$license_key,
$extension['store_item']
);
break;
case 'invalid_item_id':
$message = sprintf(
__('The item ID <strong>%s</strong> appears to be invalid.'),
$extension['store_item_id']
);
break;
case 'no_activations_left':
$message = sprintf(
__('Your license key <strong>%s</strong> has reached its activation limit.'),
$license_key
);
break;
case 'license_not_activable':
$message = sprintf(
__('Cannot activate the parent license <strong>%s</strong> of a bundle.'),
$license_key
);
break;
default:
Log_Service::write_log('ERROR', __METHOD__ . ' -> Error occurred when activating license. Check the next line for the license data received.');
Log_Service::write_log('ERROR', $license_data);
$message = __('An error occurred, please try again.');
break;
}
}
}
$option_name = 'license_' . $extension['store_item_id'];
$base_url = is_multisite()
? network_admin_url('admin.php?page=wpo365-manage-licenses')
: admin_url('admin.php?page=wpo365-manage-licenses');
if (!empty($message)) {
Options_Service::add_update_option($option_name, ''); // WPMU > Will update site option because this page is only availabe in the network-admin
$redirect = add_query_arg(array('sl_activation' => 'false', 'message' => urlencode($message)), $base_url);
} else {
Options_Service::add_update_option($option_name, sprintf($license_key, $url)); // WPMU > Will update site option because this page is only availabe in the network-admin
$redirect = add_query_arg(array('sl_activation' => 'true', 'message' => urlencode('License for ' . $extension['store_item'] . ' has been successfully activated.')), $base_url);
}
\Wpo\Core\Plugin_Updater::check_licenses();
wp_redirect($redirect);
exit();
}
}
public static function deactivate_license()
{
if (isset($_POST['deactivate_license']) && isset($_POST['store_item_id'])) {
foreach (self::$extensions as $slug => $data) {
if ($data['store_item_id'] === intval(trim($_POST['store_item_id']))) {
$extension = $data;
break;
}
}
if (empty($extension)) {
Log_Service::write_log('WARN', __METHOD__ . ' -> Could not find extension for store item id ' . sanitize_text_field(trim($_POST['store_item_id'])));
return;
}
// run a quick security check
if (!check_admin_referer('wpo365-manage-licenses', 'wpo365_license_nonce')) {
Log_Service::write_log('WARN', __METHOD__ . ' -> Could not successfully verify nonce [check admin referrer failed]');
return;
}
// retrieve the license from the POSTed data
$license_key_name = 'license_' . $extension['store_item_id'];
$posted_license_key = !empty($_POST[$license_key_name]) ? $_POST[$license_key_name] : '';
$license_key = sanitize_text_field(trim($posted_license_key));
// Call the custom API.
$url = is_multisite() ? network_home_url() : home_url();
$response = wp_remote_get(\sprintf("https://www.wpo365.com/?edd_action=deactivate_license&license=%s&item_id=%s&url=%s", $license_key, $extension['store_item_id'], $url), array('timeout' => 15, 'sslverify' => false));
// make sure the response came back okay
if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) {
if (is_wp_error($response)) {
$message = $response->get_error_message();
} else {
$message = __('An error occurred, please try again.');
}
}
$option_name = 'license_' . $extension['store_item_id'];
$base_url = is_multisite()
? network_admin_url('admin.php?page=wpo365-manage-licenses')
: admin_url('admin.php?page=wpo365-manage-licenses');
if (!empty($message)) {
$redirect = add_query_arg(array('sl_activation' => 'false', 'message' => urlencode($message)), $base_url);
} else {
Options_Service::add_update_option($option_name, '');
$license_data = json_decode(wp_remote_retrieve_body($response));
if ($license_data->license == 'deactivated') {
$redirect = add_query_arg(array('sl_activation' => 'true', 'message' => urlencode('License for ' . $extension['store_item'] . ' has been successfully deactivated.')), $base_url);
} else {
$redirect = add_query_arg(array('sl_activation' => 'true', 'message' => urlencode('License for ' . $extension['store_item'] . ' could not be deactivated. Please try again.')), $base_url);
}
}
\Wpo\Core\Plugin_Updater::check_licenses();
wp_redirect($redirect);
exit();
}
}
public static function license_page()
{
?>
<style>
.wpo365-license-table {
background: #ffffff;
border: 1px solid #cccccc;
box-sizing: border-box;
float: left;
margin: 0 15px 15px 0;
max-width: 350px;
min-height: 220px;
padding: 14px;
position: relative;
position: relative;
width: 30.5%;
}
.wpo365-license-table TH {
background-color: #f9f9f9;
border-bottom: 1px solid #cccccc;
display: block;
margin: -14px -14px 20px;
padding: 14px;
width: 100%;
}
.wpo365-license-table TD {
display: block;
padding: 0;
}
.wpo365-license-table TD input[type=text] {
margin: 0 0 8px;
width: 100%;
}
.wpo365-license-table TD DIV {
background: #fafafa;
border-top: 1px solid #eeeeee;
bottom: 14px;
box-sizing: border-box;
margin: 20px -14px -14px;
min-height: 67px;
padding: 14px;
position: absolute;
width: 100%;
}
</style>
<div class="wrap">
<h2><?php _e('WPO365 | Licenses'); ?></h2>
<form method="post">
<input type="hidden" id="store_item_id" name="store_item_id">
<table class="form-table">
<tbody>
<?php foreach (self::$extensions as $slug => $data) :
$license_key_name = 'license_' . $data['store_item_id'];
$license_key = '';
$network_options = get_site_option('wpo365_options');
if (!empty($network_options[$license_key_name])) {
$license_option = $network_options[$license_key_name];
if (WordPress_Helpers::stripos($license_option, '|') > -1) {
$exploded = explode('|', $license_option);
$license_key = $exploded[0];
} else {
$license_key = $license_option;
}
}
?>
<tr valign="top" class="wpo365-license-table">
<th scope="row" valign="top">
<?php echo esc_html($data['store_item']) ?>
</th>
<td>
<?php echo wp_nonce_field('wpo365-manage-licenses', 'wpo365_license_nonce'); ?>
<input type="text" class="regular-text" id="<?php echo esc_attr($license_key_name) ?>" name="<?php echo esc_attr($license_key_name) ?>" value="<?php echo esc_attr($license_key) ?>">
<?php if (!empty($license_key)) : ?>
<input type="submit" class="button-secondary" name="deactivate_license" value="<?php _e('Deactivate License'); ?>" onclick="document.getElementById('store_item_id').value = <?php echo esc_attr($data['store_item_id']) ?>" />
<?php else : ?>
<input type="submit" class="button-secondary" name="activate_license" value="<?php _e('Activate License'); ?>" onclick="document.getElementById('store_item_id').value = <?php echo esc_attr($data['store_item_id']) ?>" />
<?php endif ?>
<div>
<p><a href="https://www.wpo365.com/your-account/" target="_blank">Manage Sites</a></p>
</div>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</form>
<?php
}
}
}