-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-skyverge-plugin-license-settings.php
513 lines (392 loc) · 14.6 KB
/
class-skyverge-plugin-license-settings.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
<?php
/**
* Skyverge Plugin License Settings
*
* 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.
*
* @package SkyVerge/WooCommerce/PluginUpdater
* @author SkyVerge
* @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_Settings' ) ) :
/**
* Provides a general license settings page for plugins to add license key inputs.
*
* @since 1.0.0
*/
class License_Settings {
/** @var string $plugin_url the URL for the plugin file */
protected $plugin_url;
/**
* \SkyVerge\WooCommerce\PluginUpdater\License_Settings constructor.
*
* @since 1.0.0
*
* @param string $plugin_url the plugin file URL
*/
public function __construct( $plugin_url ) {
$this->plugin_url = $plugin_url;
// add the license settings
add_action( 'current_screen', array( $this, 'current_screen' ) );
add_action( 'admin_init', array( $this, 'register_settings' ) );
}
/** Output settings ***************************************** */
/**
* Checks the current screen to output our tab and settings content.
*
* @since 1.0.0
*
* @param \WP_Screen $screen the current screen
*/
public function current_screen( $screen ) {
$wc_screen_id = sanitize_title( __( 'WooCommerce', 'woocommerce' ) );
if ( "{$wc_screen_id}_page_wc-addons" === $screen->id ) {
$this->add_settings_tab();
if ( isset( $_GET['section'] ) && 'skyverge-helper' === $_GET['section'] ) {
$this->output();
$this->move_output();
}
}
}
/**
* Outputs settings for all license sections.
*
* @since 1.0.0
*/
public function output() {
$this->change_active_tab();
add_filter( 'woocommerce_addons_sections', '__return_empty_array' );
$license_sections = $this->get_license_settings();
$output = '<div class="start skyverge-helper container">';
$output .= '<form method="post" id="mainform" action="options.php" enctype="multipart/form-data">';
$output .= '<div class="wrap wrap-licenses">';
$output .= '<table class="form-table">';
// shouldn't happen given if this class is loaded, a plugin is using it
if ( empty( $license_sections ) ) {
$message = esc_html__( 'There are no active SkyVerge plugins that require license keys.', 'skyverge-plugin-updater' );
$message .= '<a href="https://www.skyverge.com/products/woocommerce-extensions/" style="margin-left:10px;" class="button button-primary">' . esc_html__( 'View Plugins', 'skyverge-plugin-updater' ) . ' →</a>';
$output .= '<div class="notice-info notice inline"><p>' . wp_kses_post( $message ) . '</p></div>';
} else {
$output .= '<h2>' . __( 'SkyVerge Plugin License Keys', 'skyverge-plugin-updater' ) . '</h2>';
$output .= '<p>' . __( 'Enter your plugin license keys here to receive automatic updates.', 'skyverge-plugin-updater') . '</p>';
ob_start();
settings_fields( 'skyverge_plugin_license_settings' );
do_settings_sections( 'skyverge_plugin_license_settings_section' );
submit_button();
$output .= ob_get_clean();
}
$output .= '</table></div></form>';
$output .= '</div>'; // end wrapper container
echo $output;
}
/**
* Returns HTML for the Jilt banner if not active.
*
* @since 1.0.0
* @deprecated 1.1.3
*
* @return string
*/
protected function get_jilt_banner() {
_deprecated_function( __METHOD__, '1.1.3' );
return '';
}
/**
* Registers settings using the WP settings API.
*
* @since 1.0.0
*/
public function register_settings() {
$settings = $this->get_license_settings();
add_settings_section(
'skyverge_plugin_license_settings_section',
__return_null(),
'__return_false',
'skyverge_plugin_license_settings_section'
);
foreach ( $settings as $setting ) {
$setting = wp_parse_args( $setting, array(
'section' => 'wc-addons',
'id' => null,
'desc' => '',
'name' => '',
'size' => null,
'options' => '',
'std' => '',
'min' => null,
'max' => null,
'step' => null,
'chosen' => null,
'multiple' => null,
'placeholder' => null,
'allow_blank' => true,
'readonly' => false,
'faux' => false,
'tooltip_title' => false,
'tooltip_desc' => false,
'field_class' => '',
) );
add_settings_field(
$setting['id'],
str_replace( 'WooCommerce ', '' , $setting['name'] ),
array( $this, 'license_key_callback' ),
'skyverge_plugin_license_settings_section',
'skyverge_plugin_license_settings_section',
$setting
);
register_setting( 'skyverge_plugin_license_settings', $setting['id'], array( $this, 'sv_settings_sanitize' ) );
}
}
/**
* Registers the license field callback for Software Licensing.
*
* @since 1.0.0
*
* @param array $args arguments passed by the setting
*/
public function license_key_callback( $args ) {
$sv_option = get_option( $args['id'] );
$messages = array();
$license = get_option( $args['options']['is_valid_license_option'] );
if ( $sv_option ) {
$value = $sv_option;
} else {
$value = isset( $args['std'] ) ? $args['std'] : '';
}
if( ! empty( $license ) && is_object( $license ) ) {
// activate_license 'invalid' on anything other than valid, so if there was an error capture it
if ( false === $license->success ) {
switch( $license->error ) {
case 'expired' :
$class = 'expired';
$messages[] = sprintf(
__( 'Your license key expired on %1$s. Please %2$srenew your license key%3$s.', 'skyverge-plugin-updater' ),
date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) ),
'<a href="https://skyverge.com/checkout/?edd_license_key=' . $value . '&utm_campaign=admin&utm_source=licenses&utm_medium=expired" target="_blank">',
'</a>'
);
$license_status = 'license-' . $class . '-notice';
break;
case 'revoked' :
$class = 'error';
$messages[] = sprintf(
__( 'Your license key has been disabled. Please %1$scontact support%2$s for more information.', 'skyverge-plugin-updater' ),
'<a href="https://skyverge.com/contact?utm_campaign=admin&utm_source=licenses&utm_medium=revoked" target="_blank">',
'</a>'
);
$license_status = 'license-' . $class . '-notice';
break;
case 'missing' :
$class = 'error';
$messages[] = sprintf(
__( 'Invalid license. Please %1$svisit your account page%2$s and verify it.', 'skyverge-plugin-updater' ),
'<a href="https://skyverge.com/checkout/purchase-history/?utm_campaign=admin&utm_source=licenses&utm_medium=missing" target="_blank">',
'</a>'
);
$license_status = 'license-' . $class . '-notice';
break;
case 'invalid' :
case 'site_inactive' :
$class = 'error';
$messages[] = sprintf(
__( 'Your %1$s is not active for this URL. Please %2$svisit your account page%3$s to manage your license key URLs.', 'skyverge-plugin-updater' ),
$args['name'],
'<a href="https://skyverge.com/checkout/purchase-history/?utm_campaign=admin&utm_source=licenses&utm_medium=invalid" target="_blank">',
'</a>'
);
$license_status = 'license-' . $class . '-notice';
break;
case 'item_name_mismatch' :
$class = 'error';
$messages[] = sprintf( __( 'This appears to be an invalid license key for %s.', 'skyverge-plugin-updater' ), $args['name'] );
$license_status = 'license-' . $class . '-notice';
break;
case 'no_activations_left':
$class = 'error';
$messages[] = sprintf( __( 'Your license key has reached its activation limit. %1$sView possible upgrades%2$s now.', 'skyverge-plugin-updater' ), '<a href="https://skyverge.com/checkout/purchase-history/">', '</a>' );
$license_status = 'license-' . $class . '-notice';
break;
case 'license_not_activable':
$class = 'error';
$messages[] = __( 'The key you entered belongs to a bundle, please use the product specific license key.', 'skyverge-plugin-updater' );
$license_status = 'license-' . $class . '-notice';
break;
default :
$class = 'error';
$error = ! empty( $license->error ) ? $license->error : __( 'unknown_error', 'skyverge-plugin-updater' );
$messages[] = sprintf( __( 'There was an error with this license key: %1%s. Please %2$scontact our support team%3$s.', 'skyverge-plugin-updater' ), $error, '<a href="https://skyverge.com/contact/">', '</a>' );
$license_status = 'license-' . $class . '-notice';
break;
}
} else {
switch( $license->license ) {
case 'valid' :
default:
$class = 'valid';
$now = current_time( 'timestamp' );
$expiration = strtotime( $license->expires, current_time( 'timestamp' ) );
if ( 'lifetime' === $license->expires ) {
$messages[] = __( 'License key never expires.', 'skyverge-plugin-updater' );
$license_status = 'license-lifetime-notice';
} elseif( $expiration > $now && $expiration - $now < ( DAY_IN_SECONDS * 30 ) ) {
$messages[] = sprintf(
__( 'Your license key expires soon! It expires on %1$s. %2$sRenew your license key%3$s.', 'skyverge-plugin-updater' ),
date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) ),
'<a href="https://skyverge.com/checkout/?edd_license_key=' . $value . '&utm_campaign=admin&utm_source=licenses&utm_medium=renew" target="_blank">',
'</a>'
);
$license_status = 'license-expires-soon-notice';
} else {
$messages[] = sprintf(
__( 'Your license key expires on %s.', 'skyverge-plugin-updater' ),
date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) )
);
$license_status = 'license-expiration-date-notice';
}
break;
}
}
} else {
$class = 'empty';
$messages[] = sprintf( __( 'To receive updates, please enter your valid %s license key.', 'skyverge-plugin-updater' ), $args['name'] );
$license_status = null;
}
$class .= ' ' . sanitize_html_class( $args['field_class'] );
$size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
$html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text" id="' . sanitize_text_field( $args['id'] ) . '" name="' . sanitize_text_field( $args['id'] ) . '" value="' . esc_attr( $value ) . '"/>';
if ( ( is_object( $license ) && 'valid' == $license->license ) || 'valid' == $license ) {
$html .= '<input type="submit" class="button-secondary" name="' . $args['id'] . '_deactivate" value="' . __( 'Deactivate License', 'skyverge-plugin-updater' ) . '"/>';
}
$html .= '<label for="' . sanitize_text_field( $args['id'] ) . '"> ' . wp_kses_post( $args['desc'] ) . '</label>';
if ( ! empty( $messages ) ) {
foreach( $messages as $message ) {
$html .= '<div class="sv-license-data sv-license-' . $class . ' ' . $license_status . '">';
$html .= '<p>' . $message . '</p></div>';
}
}
wp_nonce_field( sanitize_text_field( $args['id'] ) . '-nonce', sanitize_text_field( $args['id'] ) . '-nonce' );
echo $html;
}
/**
* Adds a settings tab via javascript.
*
* @since 1.0.0
*/
public function add_settings_tab() {
$url = $this->get_license_settings_url();
wc_enqueue_js("
$( 'nav.nav-tab-wrapper' ).append('<a href=\"" . esc_url( $url ) . "\" class=\"nav-tab skyverge-helper\">" . esc_html__( 'SkyVerge Licenses', 'skyverge-plugin-updater' ) . " </a>' );
");
}
/**
* Changes the active tab to our own tab.
*
* @since 1.0.0
*/
protected function change_active_tab() {
// removes the sections WooCommerce tries to add
wc_enqueue_js("
$( 'nav.nav-tab-wrapper a' ).each( function() {
$( this ).removeClass( 'nav-tab-active' );
});
$( 'a.skyverge-helper' ).addClass( 'nav-tab-active' );
$( 'div.wrap.wc_addons_wrap p' ).first().remove();
$( 'div.wrap.wc_addons_wrap br.clear' ).first().remove();
");
}
/**
* Moves our licenses output in the DOM so it appears in the right place.
*
* @since 1.0.0
*/
protected function move_output() {
wc_enqueue_js("
$( 'div.wrap.wc_addons_wrap' ).append( $('div.skyverge-helper.container' ) );
");
}
/** Helper functions ***************************************** */
/**
* Settings sanitization.
*
* Adds a settings error (for the updated message).
*
* @since 1.0.0
*
* @param string $input the value inputted in the field
* @return string $input sanitized value
*/
public function settings_sanitize( $input = '' ) {
$setting_types = array(
// we may add more types in the future
'text',
);
foreach ( $setting_types as $type ) {
switch( $type ) {
case 'text':
$input = sanitize_text_field( $input );
break;
}
}
add_settings_error( 'skyverge-license-settings', '', __( 'Settings updated.', 'skyverge-plugin-updater' ), 'updated' );
return $input;
}
/**
* Gets license sections to output.
*
* @since 1.0.0
*
* @return string[] array of license settings
*/
protected function get_license_settings() {
// this will allow the license class to add new settings here as it's loaded
return apply_filters( 'skyverge_plugin_license_settings', array() );
}
/**
* 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' );
}
/**
* Checks if Jilt for WooCommerce is active.
*
* @since 1.0.0
* @deprecated 1.1.3
*
* @return bool
*/
protected function is_jilt_active() {
_deprecated_function( __METHOD__, '1.1.3' );
return false;
}
/**
* Returns a URL for Jilt upsells or banners.
*
* @since 1.0.0
* @deprecated 1.1.3
*
* @return string
*/
protected function get_jilt_url() {
_deprecated_function( __METHOD__, '1.1.3' );
return '';
}
}
endif;