-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-skyverge-plugin-license.php
520 lines (401 loc) · 13.9 KB
/
class-skyverge-plugin-license.php
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<?php
/**
* Skyverge Plugin License class
*
* This source file is subject to the GNU General Public License v3.0
* that is bundled with this package in the file license.txt.
* It is also available through the world-wide-web at this URL:
* http://www.gnu.org/licenses/gpl-3.0.html
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade to newer
* versions in the future. If you wish to customize this plugin for your
* needs please refer to http://skyverge.com/product/tba/ for more information.
*
* This file is based on the `EDD_License` class from the Easy Digital Downloads team.
*
* @package SkyVerge/WooCommerce/PluginUpdater
* @author SkyVerge
* @copyright Copyright (c) 2012-2017, Easy Digital Downloads
* @copyright Copyright (c) 2017-2020, SkyVerge, Inc.
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*/
namespace SkyVerge\WooCommerce\PluginUpdater;
defined( 'ABSPATH' ) or exit;
if ( ! class_exists( '\\SkyVerge\\WooCommerce\\PluginUpdater\\License' ) ) :
/**
* Provides a general license settings page for plugins to add license key inputs.
*
* @since 1.0.0
*/
class License {
/** @var string $api_url plugin update URL */
protected $api_url;
/** @var string $file plugin file */
protected $file;
/** @var string $plugin_url plugin url */
protected $plugin_url;
/** @var string $item_name plugin name on our site */
protected $item_name;
/** @var int $item_id plugin post ID on our site */
protected $item_id;
/** @var string $version plugin version */
protected $version;
/** @var string $author plugin author */
protected $author;
/** @var string $item_shortname slugified plugin name */
private $item_shortname;
/** @var string $license license key */
private $license;
/** @var string $updater_url the URL for plugin updates */
protected $updater_url = 'https://skyverge.com/';
/** @var \SkyVerge\WooCommerce\PluginUpdater\License_Settings $settings the settings instance */
protected $settings;
/**
* SkyVerge\WooCommerce\PluginUpdater\License constructor.
*
* @since 1.0.0
*
* @param string $_file
* @param string $_path
* @param string $_plugin_url
* @param string $_item_name
* @param string $_version
* @param int $_item_id
* @param string $_author
*/
public function __construct( $_file, $_path, $_plugin_url, $_item_name, $_version, $_item_id = null, $_author = 'SkyVerge' ) {
if ( is_numeric( $_item_id ) ) {
$this->item_id = absint( $_item_id );
}
$this->api_url = 'https://skyverge.com/';
$this->file = $_file;
$this->path = $_path;
$this->plugin_url = $_plugin_url;
$this->item_name = $_item_name;
$this->version = $_version;
$this->author = $_author;
$this->item_shortname = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( 'woocommerce', 'wc', str_replace( ' ', '_', strtolower( $this->item_name ) ) ) );
$this->license = trim( get_option( "{$this->item_shortname}_license_key", '' ) );
$this->includes();
$this->add_hooks();
}
/**
* Includes required files.
*
* @since 1.0.0
*/
public function includes() {
// load settings if not available already
if ( ! class_exists( '\\SkyVerge\\WooCommerce\\PluginUpdater\\License_Settings' ) ) {
require_once( plugin_dir_path( __FILE__ ) . 'class-skyverge-plugin-license-settings.php' );
$this->settings = new License_Settings( $this->plugin_url );
}
if ( ! class_exists( '\\SkyVerge\\WooCommerce\\PluginUpdater\\Updater' ) ) {
require_once( plugin_dir_path( __FILE__ ) . 'class-skyverge-plugin-updater.php' );
}
}
/**
* Setup plugin hooks.
*
* @since 1.0.0
*/
private function add_hooks() {
// load plugin updater
add_action( 'admin_init', array( $this, 'auto_updater' ), 0 );
// add scheduled event for license update check
add_filter( 'cron_schedules', array( $this, 'add_cron_schedule' ) );
add_action( 'wp', array( $this, 'schedule_events' ) );
// register settings
add_filter( 'skyverge_plugin_license_settings', array( $this, 'add_settings' ), 1 );
// add styles for settings
add_action( 'admin_enqueue_scripts', array( $this, 'add_styles' ) );
// activate license key on settings save
add_action( 'admin_init', array( $this, 'activate_license' ) );
// deactivate license key
add_action( 'admin_init', array( $this, 'deactivate_license' ) );
// check that license is valid once per week
add_action( 'skyverge_weekly_scheduled_events', array( $this, 'weekly_license_check' ) );
// for testing license notices, uncomment this line to force checks on every page load
//add_action( 'admin_init', array( $this, 'weekly_license_check' ) );
// Display notices to admins
add_action( 'admin_notices', array( $this, 'notices' ) );
add_action( 'in_plugin_update_message-' . plugin_basename( $this->file ), array( $this, 'plugin_row_license_missing' ), 10, 2 );
}
/**
* Load the auto updater.
*
* @since 1.0.0
*/
public function auto_updater() {
$data = [
'version' => $this->version,
'license' => $this->license,
'author' => $this->author,
];
if ( ! empty( $this->item_id ) ) {
$data['item_id'] = $this->item_id;
} else {
$data['item_name'] = $this->item_name;
}
// Setup the updater
$plugin_updater = new Updater( $this->file, $data );
}
/**
* Registers new cron schedule.
*
* @since 1.0.0
*
* @param string[] $schedules existing schedules
* @return string[] update schedules
*/
public function add_cron_schedule( $schedules = [] ) {
// adds a weekly schedule to available cron schedules
$schedules['weekly'] = array(
'interval' => 604800,
'display' => __( 'Once per week', 'skyverge-plugin-updater' ),
);
return $schedules;
}
/**
* Schedule weekly events.
*
* @since 1.0.0
*/
public function schedule_events() {
if ( ! wp_next_scheduled( 'skyverge_weekly_scheduled_events' ) ) {
wp_schedule_event( current_time( 'timestamp', true ), 'weekly', 'skyverge_weekly_scheduled_events' );
}
}
/**
* Get license settings page URL.
*
* @since 1.0.0
*
* @param string[] $settings license settings
* @return string[] updated settings
*/
public function add_settings( $settings ) {
$plugin_license_settings = array(
array(
'id' => "{$this->item_shortname}_license_key",
'name' => sprintf( __( '%1$s', 'skyverge-plugin-updater' ), $this->item_name ),
'desc' => '',
'type' => 'license_key',
'options' => array( 'is_valid_license_option' => "{$this->item_shortname}_license_active" ),
'size' => 'regular',
)
);
return array_merge( $settings, $plugin_license_settings );
}
/**
* Adds updater page stylesheet.
*
* @since 1.0.0
*/
public function add_styles() {
if ( isset( $_GET['section'] ) && 'skyverge-helper' === $_GET['section'] ) {
wp_enqueue_style( 'skyverge-plugin-license-settings', plugin_dir_url( __FILE__ ) . 'assets/css/skyverge-updater-styles.css', [], $this->version );
}
}
/**
* Activate the license key
*
* @since 1.0.0
*/
public function activate_license() {
if ( ! isset( $_REQUEST["{$this->item_shortname}_license_key-nonce"] ) || ! wp_verify_nonce( $_REQUEST["{$this->item_shortname}_license_key-nonce"], "{$this->item_shortname}_license_key-nonce" ) || ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
if ( empty( $_POST["{$this->item_shortname}_license_key"] ) ) {
delete_option( "{$this->item_shortname}_license_active" );
return;
}
// don't activate a key when deactivating a different key
foreach ( $_POST as $key => $value ) {
if ( false !== strpos( $key, 'license_key_deactivate' ) ) {
return;
}
}
$details = get_option( "{$this->item_shortname}_license_active" );
if ( is_object( $details ) && 'valid' === $details->license ) {
return;
}
$license = sanitize_text_field( $_POST["{$this->item_shortname}_license_key"] );
if ( empty( $license ) ) {
return;
}
// data to send to the API
$api_params = array(
'edd_action' => 'activate_license',
'license' => $license,
'item_name' => urlencode( $this->item_name ),
'url' => home_url(),
);
$response = wp_remote_post(
$this->api_url,
array(
'timeout' => 15,
'body' => $api_params,
)
);
// make sure there are no errors
if ( is_wp_error( $response ) ) {
return;
}
// tell WP to look for updates
set_site_transient( 'update_plugins', null );
// decode license data
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
update_option( "{$this->item_shortname}_license_active", $license_data );
}
/**
* Deactivate the license key
*
* @since 1.0.0
*/
public function deactivate_license() {
if ( ! isset( $_POST["{$this->item_shortname}_license_key"] ) || ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
if ( ! wp_verify_nonce( $_REQUEST["{$this->item_shortname}_license_key-nonce"], "{$this->item_shortname}_license_key-nonce" ) ) {
wp_die( __( 'Nonce verification failed', 'skyverge-plugin-updater' ), __( 'Error', 'skyverge-plugin-updater' ), array( 'response' => 403 ) );
}
// run on deactivate button press
if ( isset( $_POST["{$this->item_shortname}_license_key_deactivate"] ) ) {
// data to send to the API
$api_params = array(
'edd_action' => 'deactivate_license',
'license' => $this->license,
'item_name' => urlencode( $this->item_name ),
'url' => home_url(),
);
$response = wp_remote_post(
$this->api_url,
array(
'timeout' => 15,
'body' => $api_params,
)
);
// Make sure there are no errors
if ( is_wp_error( $response ) ) {
return;
}
// Decode the license data
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
delete_option( "{$this->item_shortname}_license_active" );
}
}
/**
* Check for a valid license on this plugin.
*
* @since 1.0.0
*
* @return bool true if valid
*/
public function is_license_valid() {
$details = get_option( "{$this->item_shortname}_license_active" );
return is_object( $details ) && 'valid' === $details->license;
}
/**
* Check if license key is valid once per week
*
* @since 1.0.0
*
* @return bool
*/
public function weekly_license_check() {
if ( ! empty( $_POST["{$this->item_shortname}_license_key"] ) ) {
return false; // don't fire when saving settings
}
if ( empty( $this->license ) ) {
return false;
}
// data to send in our API request
$api_params = array(
'edd_action' => 'check_license',
'license' => $this->license,
'item_name' => urlencode( $this->item_name ),
'url' => home_url(),
);
$response = wp_remote_post(
$this->api_url,
array(
'timeout' => 15,
'body' => $api_params,
)
);
// make sure the response came back okay
if ( is_wp_error( $response ) ) {
return false;
}
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
update_option( "{$this->item_shortname}_license_active", $license_data );
}
/**
* Add admin notices to WooCommerce pages for errors.
*
* @since 1.0.0
*/
public function notices() {
global $current_screen;
static $showed_invalid_message = false;
$prefix = sanitize_title( __( 'WooCommerce', 'woocommerce' ) );
$screens = array( "{$prefix}_page_wc-addons", "{$prefix}_page_wc-settings", 'plugins' );
$key = trim( get_option( "{$this->item_shortname}_license_key", '' ) );
if ( empty( $key ) || ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
$messages = [];
$license = get_option( "{$this->item_shortname}_license_active" );
if ( in_array( $current_screen->id, $screens, true ) && is_object( $license ) && 'valid' !== $license->license && ! $showed_invalid_message ) {
// only show this notice on settings / Extensions screens
if ( ! isset( $_GET['section'] ) || 'skyverge-helper' !== $_GET['section'] ) {
$messages[] = sprintf(
/* translators: Placeholder: %1$s - <a>, %2$s - </a> */
__( 'You have invalid or expired license keys for SkyVerge Plugins. Please go to the %1$sLicenses page%2$s to correct this issue.', 'skyverge-plugin-updater' ),
'<a href="' . $this->get_license_settings_url() . '">',
'</a>'
);
$showed_invalid_message = true;
}
}
if ( ! empty( $messages ) ) {
foreach( $messages as $message ) {
echo '<div class="error"><p>' . $message . '</p></div>';
}
}
}
/**
* Displays message inline on plugin row that the license key is missing
*
* @since 1.0.0
*/
public function plugin_row_license_missing( $plugin_data, $version_info ) {
static $showed_missing_key_message = [];
$license = get_option( "{$this->item_shortname}_license_active" );
if ( ( ! is_object( $license ) || 'valid' !== $license->license ) && empty( $showed_missing_key_message[ $this->item_shortname ] ) ) {
echo ' <strong><a href="' . esc_url( $this->get_license_settings_url() ) . '">' . __( 'Enter valid license key for automatic updates.', 'skyverge-plugin-updater' ) . '</a></strong>';
$showed_missing_key_message[ $this->item_shortname ] = true;
}
}
/**
* Get license settings page URL.
*
* @since 1.0.0
*/
public function get_license_settings_url() {
return admin_url( 'admin.php?page=wc-addons§ion=skyverge-helper' );
}
/**
* Gets the license settings instance.
*/
public function get_license_settings_instance() {
return $this->settings;
}
}
endif;