1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Give License handler |
4
|
|
|
* |
5
|
|
|
* @package Give |
6
|
|
|
* @subpackage Admin/License |
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
8
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
9
|
|
|
* @since 1.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
if ( ! class_exists( 'Give_License' ) ) : |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Give_License Class |
21
|
|
|
* |
22
|
|
|
* This class simplifies the process of adding license information |
23
|
|
|
* to new Give add-ons. |
24
|
|
|
* |
25
|
|
|
* @since 1.0 |
26
|
|
|
*/ |
27
|
|
|
class Give_License { |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* File |
31
|
|
|
* |
32
|
|
|
* @access private |
33
|
|
|
* @since 1.0 |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $file; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* License |
41
|
|
|
* |
42
|
|
|
* @access private |
43
|
|
|
* @since 1.0 |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $license; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Item name |
51
|
|
|
* |
52
|
|
|
* @access private |
53
|
|
|
* @since 1.0 |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $item_name; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* License Information object. |
61
|
|
|
* |
62
|
|
|
* @access private |
63
|
|
|
* @since 1.7 |
64
|
|
|
* |
65
|
|
|
* @var object |
66
|
|
|
*/ |
67
|
|
|
private $license_data; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Item shortname |
71
|
|
|
* |
72
|
|
|
* @access private |
73
|
|
|
* @since 1.0 |
74
|
|
|
* |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
private $item_shortname; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Version |
81
|
|
|
* |
82
|
|
|
* @access private |
83
|
|
|
* @since 1.0 |
84
|
|
|
* |
85
|
|
|
* @var string |
86
|
|
|
*/ |
87
|
|
|
private $version; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Author |
91
|
|
|
* |
92
|
|
|
* @access private |
93
|
|
|
* @since 1.0 |
94
|
|
|
* |
95
|
|
|
* @var string |
96
|
|
|
*/ |
97
|
|
|
private $author; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* API URL |
101
|
|
|
* |
102
|
|
|
* @access private |
103
|
|
|
* @since 1.0 |
104
|
|
|
* |
105
|
|
|
* @var string |
106
|
|
|
*/ |
107
|
|
|
private $api_url = 'https://givewp.com/edd-sl-api/'; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Account URL |
111
|
|
|
* |
112
|
|
|
* @access private |
113
|
|
|
* @since 1.7 |
114
|
|
|
* |
115
|
|
|
* @var null|string |
116
|
|
|
*/ |
117
|
|
|
private $account_url = 'https://givewp.com/my-account/'; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Checkout URL |
121
|
|
|
* |
122
|
|
|
* @access private |
123
|
|
|
* @since 1.7 |
124
|
|
|
* |
125
|
|
|
* @var null|string |
126
|
|
|
*/ |
127
|
|
|
private $checkout_url = 'https://givewp.com/checkout/'; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Class Constructor |
131
|
|
|
* |
132
|
|
|
* Set up the Give License Class. |
133
|
|
|
* |
134
|
|
|
* @access public |
135
|
|
|
* @since 1.0 |
136
|
|
|
* |
137
|
|
|
* @param string $_file |
138
|
|
|
* @param string $_item_name |
139
|
|
|
* @param string $_version |
140
|
|
|
* @param string $_author |
141
|
|
|
* @param string $_optname |
142
|
|
|
* @param string $_api_url |
143
|
|
|
* @param string $_checkout_url |
144
|
|
|
* @param string $_account_url |
145
|
|
|
*/ |
146
|
|
|
public function __construct( $_file, $_item_name, $_version, $_author, $_optname = null, $_api_url = null, $_checkout_url = null, $_account_url = null ) { |
|
|
|
|
147
|
|
|
|
148
|
|
|
$give_options = give_get_settings(); |
149
|
|
|
|
150
|
|
|
$this->file = $_file; |
151
|
|
|
$this->item_name = $_item_name; |
152
|
|
|
$this->item_shortname = 'give_' . preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->item_name ) ) ); |
153
|
|
|
$this->version = $_version; |
154
|
|
|
$this->license = isset( $give_options[ $this->item_shortname . '_license_key' ] ) ? trim( $give_options[ $this->item_shortname . '_license_key' ] ) : ''; |
155
|
|
|
$this->license_data = get_option( $this->item_shortname . '_license_active' ); |
156
|
|
|
$this->author = $_author; |
157
|
|
|
$this->api_url = is_null( $_api_url ) ? $this->api_url : $_api_url; |
158
|
|
|
$this->checkout_url = is_null( $_checkout_url ) ? $this->checkout_url : $_checkout_url; |
159
|
|
|
$this->account_url = is_null( $_account_url ) ? $this->account_url : $_account_url; |
160
|
|
|
$this->auto_updater_obj = null; |
161
|
|
|
|
162
|
|
|
// Add Setting for Give Add-on activation status. |
163
|
|
|
$is_addon_activated = get_option( 'give_is_addon_activated' ); |
164
|
|
|
if( ! $is_addon_activated && is_object( $this ) && sizeof( $this ) > 0 ) { |
|
|
|
|
165
|
|
|
update_option( 'give_is_addon_activated', true ); |
166
|
|
|
Give_Cache::set( 'give_cache_hide_license_notice_after_activation', true, DAY_IN_SECONDS ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// Setup hooks |
170
|
|
|
$this->includes(); |
171
|
|
|
$this->hooks(); |
172
|
|
|
$this->auto_updater(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Includes |
177
|
|
|
* |
178
|
|
|
* Include the updater class. |
179
|
|
|
* |
180
|
|
|
* @access private |
181
|
|
|
* @since 1.0 |
182
|
|
|
* |
183
|
|
|
* @return void |
184
|
|
|
*/ |
185
|
|
|
private function includes() { |
186
|
|
|
|
187
|
|
|
if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) { |
188
|
|
|
require_once 'admin/EDD_SL_Plugin_Updater.php'; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Hooks |
194
|
|
|
* |
195
|
|
|
* Setup license hooks. |
196
|
|
|
* |
197
|
|
|
* @access private |
198
|
|
|
* @since 1.0 |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
private function hooks() { |
203
|
|
|
|
204
|
|
|
// Register settings. |
205
|
|
|
add_filter( 'give_settings_licenses', array( $this, 'settings' ), 1 ); |
206
|
|
|
|
207
|
|
|
// Activate license key on settings save. |
208
|
|
|
add_action( 'admin_init', array( $this, 'activate_license' ) ); |
209
|
|
|
|
210
|
|
|
// Deactivate license key. |
211
|
|
|
add_action( 'admin_init', array( $this, 'deactivate_license' ) ); |
212
|
|
|
|
213
|
|
|
// Updater. |
214
|
|
|
add_action( 'admin_init', array( $this, 'auto_updater' ), 0 ); |
215
|
|
|
add_action( 'admin_notices', array( $this, 'notices' ) ); |
216
|
|
|
|
217
|
|
|
// Check license weekly. |
218
|
|
|
Give_Cron::add_weekly_event( array( $this, 'weekly_license_check' ) ); |
219
|
|
|
add_action( 'give_validate_license_when_site_migrated', array( $this, 'weekly_license_check' ) ); |
220
|
|
|
|
221
|
|
|
// Check subscription weekly. |
222
|
|
|
Give_Cron::add_weekly_event( array( $this, 'weekly_subscription_check' ) ); |
223
|
|
|
add_action( 'give_validate_license_when_site_migrated', array( $this, 'weekly_subscription_check' ) ); |
224
|
|
|
|
225
|
|
|
// Show addon notice on plugin page. |
226
|
|
|
$plugin_name = explode( 'plugins/', $this->file ); |
227
|
|
|
$plugin_name = end( $plugin_name ); |
228
|
|
|
add_action( "after_plugin_row_{$plugin_name}", array( $this, 'plugin_page_notices' ), 10, 3 ); |
229
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Auto Updater |
235
|
|
|
* |
236
|
|
|
* @access private |
237
|
|
|
* @since 1.0 |
238
|
|
|
* |
239
|
|
|
* @return void |
240
|
|
|
*/ |
241
|
|
|
public function auto_updater() { |
242
|
|
|
|
243
|
|
|
// Setup the updater. |
244
|
|
|
$this->auto_updater_obj = new EDD_SL_Plugin_Updater( |
245
|
|
|
$this->api_url, |
246
|
|
|
$this->file, |
247
|
|
|
array( |
248
|
|
|
'version' => $this->version, |
249
|
|
|
'license' => $this->license, |
250
|
|
|
'item_name' => $this->item_name, |
251
|
|
|
'author' => $this->author, |
252
|
|
|
) |
253
|
|
|
); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* License Settings |
258
|
|
|
* |
259
|
|
|
* Add license field to settings. |
260
|
|
|
* |
261
|
|
|
* @access public |
262
|
|
|
* @since 1.0 |
263
|
|
|
* |
264
|
|
|
* @param array $settings License settings. |
265
|
|
|
* |
266
|
|
|
* @return array License settings. |
267
|
|
|
*/ |
268
|
|
|
public function settings( $settings ) { |
269
|
|
|
|
270
|
|
|
$give_license_settings = array( |
271
|
|
|
array( |
272
|
|
|
'name' => $this->item_name, |
273
|
|
|
'id' => $this->item_shortname . '_license_key', |
274
|
|
|
'desc' => '', |
275
|
|
|
'type' => 'license_key', |
276
|
|
|
'options' => array( |
277
|
|
|
'license' => get_option( $this->item_shortname . '_license_active' ), |
278
|
|
|
'shortname' => $this->item_shortname, |
279
|
|
|
'item_name' => $this->item_name, |
280
|
|
|
'api_url' => $this->api_url, |
281
|
|
|
'checkout_url' => $this->checkout_url, |
282
|
|
|
'account_url' => $this->account_url, |
283
|
|
|
), |
284
|
|
|
'size' => 'regular', |
285
|
|
|
), |
286
|
|
|
); |
287
|
|
|
|
288
|
|
|
return array_merge( $settings, $give_license_settings ); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* License Settings Content |
293
|
|
|
* |
294
|
|
|
* Add Some Content to the Licensing Settings. |
295
|
|
|
* |
296
|
|
|
* @access public |
297
|
|
|
* @since 1.0 |
298
|
|
|
* |
299
|
|
|
* @param array $settings License settings content. |
300
|
|
|
* |
301
|
|
|
* @return array License settings content. |
302
|
|
|
*/ |
303
|
|
|
public function license_settings_content( $settings ) { |
304
|
|
|
|
305
|
|
|
$give_license_settings = array( |
306
|
|
|
array( |
307
|
|
|
'name' => __( 'Add-on Licenses', 'give' ), |
308
|
|
|
'desc' => '<hr>', |
309
|
|
|
'type' => 'give_title', |
310
|
|
|
'id' => 'give_title', |
311
|
|
|
), |
312
|
|
|
); |
313
|
|
|
|
314
|
|
|
return array_merge( $settings, $give_license_settings ); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Activate License |
319
|
|
|
* |
320
|
|
|
* Activate the license key. |
321
|
|
|
* |
322
|
|
|
* @access public |
323
|
|
|
* @since 1.0 |
324
|
|
|
* |
325
|
|
|
* @return void |
326
|
|
|
*/ |
327
|
|
|
public function activate_license() { |
328
|
|
|
// Bailout. |
329
|
|
|
if( ! $this->__is_user_can_edit_license() ) { |
|
|
|
|
330
|
|
|
return; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
// Allow third party addon developers to handle license activation. |
334
|
|
|
if ( $this->__is_third_party_addon() ) { |
335
|
|
|
do_action( 'give_activate_license', $this ); |
336
|
|
|
|
337
|
|
|
return; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
// Do not simultaneously activate add-ons if the user want to deactivate a specific add-on. |
341
|
|
|
foreach ( $_POST as $key => $value ) { |
|
|
|
|
342
|
|
|
if ( false !== strpos( $key, 'license_key_deactivate' ) ) { |
343
|
|
|
// Don't activate a key when deactivating a different key |
344
|
|
|
return; |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
// Delete previous license setting if a empty license key submitted. |
349
|
|
|
if ( empty( $_POST["{$this->item_shortname}_license_key"] ) ) { |
|
|
|
|
350
|
|
|
$this->unset_license(); |
351
|
|
|
|
352
|
|
|
return; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
// Check if plugin previously installed. |
356
|
|
|
if ( $this->is_valid_license() ) { |
357
|
|
|
return; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
// Get license key. |
361
|
|
|
$this->license = sanitize_text_field( $_POST[ $this->item_shortname . '_license_key' ] ); |
|
|
|
|
362
|
|
|
|
363
|
|
|
// Delete previous license key from subscription if previously added. |
364
|
|
|
$this->__remove_license_key_from_subscriptions(); |
365
|
|
|
|
366
|
|
|
// Make sure there are no api errors. |
367
|
|
|
if ( ! ( $license_data = $this->get_license_info( 'activate_license' ) ) ) { |
368
|
|
|
return; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
// Make sure license is valid. |
372
|
|
|
// return because admin will want to activate license again. |
373
|
|
|
if ( ! $this->is_license( $license_data ) ) { |
374
|
|
|
// Add license key. |
375
|
|
|
give_update_option( "{$this->item_shortname}_license_key", $this->license ); |
376
|
|
|
|
377
|
|
|
return; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
// Tell WordPress to look for updates. |
381
|
|
|
set_site_transient( 'update_plugins', null ); |
382
|
|
|
|
383
|
|
|
// Add license data. |
384
|
|
|
update_option( "{$this->item_shortname}_license_active", $license_data ); |
|
|
|
|
385
|
|
|
|
386
|
|
|
// Add license key. |
387
|
|
|
give_update_option( "{$this->item_shortname}_license_key", $this->license ); |
388
|
|
|
|
389
|
|
|
// Check subscription for license key and store this to db (if any). |
390
|
|
|
$this->__single_subscription_check(); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Deactivate License |
395
|
|
|
* |
396
|
|
|
* Deactivate the license key. |
397
|
|
|
* |
398
|
|
|
* @access public |
399
|
|
|
* @since 1.0 |
400
|
|
|
* |
401
|
|
|
* @return void |
402
|
|
|
*/ |
403
|
|
|
public function deactivate_license() { |
404
|
|
|
// Bailout. |
405
|
|
|
if( ! $this->__is_user_can_edit_license() ) { |
|
|
|
|
406
|
|
|
return; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
// Allow third party add-on developers to handle license deactivation. |
410
|
|
|
if ( $this->__is_third_party_addon() ) { |
411
|
|
|
do_action( 'give_deactivate_license', $this ); |
412
|
|
|
|
413
|
|
|
return; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
// Run on deactivate button press. |
417
|
|
|
if ( isset( $_POST[ $this->item_shortname . '_license_key_deactivate' ] ) ) { |
418
|
|
|
|
419
|
|
|
// Make sure there are no api errors. |
420
|
|
|
if ( ! ( $license_data = $this->get_license_info( 'deactivate_license' ) ) ) { |
421
|
|
|
return; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
// Ensure deactivated successfully. |
425
|
|
|
if ( isset( $license_data->success ) ) { |
426
|
|
|
$this->unset_license(); |
427
|
|
|
} |
428
|
|
|
}// End if(). |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Check if license key is valid once per week. |
433
|
|
|
* |
434
|
|
|
* @access public |
435
|
|
|
* @since 1.7 |
436
|
|
|
* |
437
|
|
|
* @return void |
438
|
|
|
*/ |
439
|
|
|
public function weekly_license_check() { |
440
|
|
|
|
441
|
|
|
if ( |
442
|
|
|
! empty( $_POST['give_settings'] ) || |
|
|
|
|
443
|
|
|
empty( $this->license ) |
444
|
|
|
) { |
445
|
|
|
return; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
// Allow third party add-on developers to handle their license check. |
449
|
|
|
if ( $this->__is_third_party_addon() ) { |
450
|
|
|
do_action( 'give_weekly_license_check', $this ); |
451
|
|
|
|
452
|
|
|
return; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
// Make sure there are no api errors. |
456
|
|
|
if ( ! ( $license_data = $this->get_license_info( 'check_license' ) ) ) { |
457
|
|
|
return; |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
// Bailout. |
461
|
|
|
if( ! $this->is_license( $license_data ) ) { |
|
|
|
|
462
|
|
|
return; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
update_option( $this->item_shortname . '_license_active', $license_data ); |
466
|
|
|
|
467
|
|
|
return; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Check subscription validation once per week |
472
|
|
|
* |
473
|
|
|
* @access public |
474
|
|
|
* @since 1.7 |
475
|
|
|
* |
476
|
|
|
* @return void |
477
|
|
|
*/ |
478
|
|
|
public function weekly_subscription_check() { |
479
|
|
|
// Bailout. |
480
|
|
|
if ( |
481
|
|
|
! empty( $_POST['give_settings'] ) || |
|
|
|
|
482
|
|
|
empty( $this->license ) |
483
|
|
|
) { |
484
|
|
|
return; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
// Remove old subscription data. |
488
|
|
|
if ( absint( get_option( '_give_subscriptions_edit_last', true ) ) < current_time( 'timestamp', 1 ) ) { |
489
|
|
|
delete_option( 'give_subscriptions' ); |
490
|
|
|
update_option( '_give_subscriptions_edit_last', strtotime( '+ 1 day', current_time( 'timestamp', 1 ) ) ); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
// Allow third party add-on developers to handle their subscription check. |
494
|
|
|
if ( $this->__is_third_party_addon() ) { |
495
|
|
|
do_action( 'give_weekly_subscription_check', $this ); |
496
|
|
|
|
497
|
|
|
return; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
$this->__single_subscription_check(); |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Check if license key is part of subscription or not |
505
|
|
|
* |
506
|
|
|
* @access private |
507
|
|
|
* @since 1.7 |
508
|
|
|
* |
509
|
|
|
* @return void |
510
|
|
|
*/ |
511
|
|
|
private function __single_subscription_check() { |
|
|
|
|
512
|
|
|
if ( empty( $this->license ) ) { |
513
|
|
|
return; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
// Make sure there are no api errors. |
517
|
|
|
// Do not get confused with edd_action check_subscription. |
518
|
|
|
// By default edd software licensing api does not have api to check subscription. |
519
|
|
|
// This is a custom feature to check subscriptions. |
520
|
|
|
if ( ! ( $subscription_data = $this->get_license_info( 'check_subscription', true ) ) ) { |
521
|
|
|
return; |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
// Bailout. |
525
|
|
|
if( ! $this->is_subscription( $subscription_data ) ) { |
|
|
|
|
526
|
|
|
return; |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
if ( ! empty( $subscription_data['success'] ) && absint( $subscription_data['success'] ) ) { |
530
|
|
|
$subscriptions = get_option( 'give_subscriptions', array() ); |
531
|
|
|
|
532
|
|
|
// Update subscription data only if subscription does not exist already. |
533
|
|
|
$subscriptions[ $subscription_data['id'] ] = $subscription_data; |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
536
|
|
|
// Initiate default set of license for subscription. |
537
|
|
|
if( ! isset( $subscriptions[ $subscription_data['id'] ]['licenses'] ) ) { |
|
|
|
|
538
|
|
|
$subscriptions[ $subscription_data['id']]['licenses'] = array(); |
|
|
|
|
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
// Store licenses for subscription. |
542
|
|
|
if ( ! in_array( $this->license, $subscriptions[ $subscription_data['id'] ]['licenses'] ) ) { |
543
|
|
|
$subscriptions[ $subscription_data['id']]['licenses'][] = $this->license; |
|
|
|
|
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
update_option( 'give_subscriptions', $subscriptions ); |
547
|
|
|
} |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* Admin notices for errors |
552
|
|
|
* |
553
|
|
|
* @access public |
554
|
|
|
* @since 1.0 |
555
|
|
|
* |
556
|
|
|
* @return void |
557
|
|
|
*/ |
558
|
|
|
public function notices() { |
559
|
|
|
|
560
|
|
|
if ( ! current_user_can( 'manage_give_settings' ) ) { |
561
|
|
|
return; |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
// Do not show licenses notices on license tab. |
565
|
|
|
if ( 'licenses' === give_get_current_setting_tab() ) { |
566
|
|
|
return; |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
static $showed_invalid_message; |
570
|
|
|
static $showed_subscriptions_message; |
571
|
|
|
static $addon_license_key_in_subscriptions; |
572
|
|
|
|
573
|
|
|
// Set default value. |
574
|
|
|
$addon_license_key_in_subscriptions = ! empty( $addon_license_key_in_subscriptions ) ? $addon_license_key_in_subscriptions : array(); |
575
|
|
|
$messages = array(); |
576
|
|
|
|
577
|
|
|
// Check whether admin has Give Add-on activated since 24 hours? |
578
|
|
|
$is_license_notice_hidden = Give_Cache::get( 'give_cache_hide_license_notice_after_activation' ); |
579
|
|
|
|
580
|
|
|
// Display Invalid License notice, if its more than 24 hours since first Give Add-on activation. |
581
|
|
|
if ( |
582
|
|
|
empty( $this->license ) |
583
|
|
|
&& empty( $showed_invalid_message ) |
584
|
|
|
&& ( false === $is_license_notice_hidden ) |
585
|
|
|
) { |
586
|
|
|
|
587
|
|
|
Give()->notices->register_notice( array( |
588
|
|
|
'id' => 'give-invalid-license', |
589
|
|
|
'type' => 'error', |
590
|
|
|
'description' => sprintf( |
591
|
|
|
__( 'You have invalid or expired license keys for one or more Give Add-ons. Please go to the <a href="%s">licenses page</a> to correct this issue.', 'give' ), |
592
|
|
|
admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' ) |
593
|
|
|
), |
594
|
|
|
'dismissible_type' => 'user', |
595
|
|
|
'dismiss_interval' => 'shortly', |
596
|
|
|
) ); |
597
|
|
|
|
598
|
|
|
$showed_invalid_message = true; |
599
|
|
|
|
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
// Get subscriptions. |
603
|
|
|
$subscriptions = get_option( 'give_subscriptions' ); |
604
|
|
|
|
605
|
|
|
// Show subscription messages. |
606
|
|
|
if ( ! empty( $subscriptions ) && ! $showed_subscriptions_message ) { |
607
|
|
|
|
608
|
|
|
foreach ( $subscriptions as $subscription ) { |
609
|
|
|
// Subscription expires timestamp. |
610
|
|
|
$subscription_expires = strtotime( $subscription['expires'] ); |
611
|
|
|
|
612
|
|
|
// Start showing subscriptions message before one week of renewal date. |
613
|
|
|
if ( strtotime( '- 7 days', $subscription_expires ) > current_time( 'timestamp', 1 ) ) { |
614
|
|
|
continue; |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
// Check if subscription message already exist in messages. |
618
|
|
|
if ( array_key_exists( $subscription['id'], $messages ) ) { |
619
|
|
|
continue; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
// Check if license already expired. |
623
|
|
|
if ( strtotime( $subscription['expires'] ) < current_time( 'timestamp', 1 ) ) { |
624
|
|
|
Give()->notices->register_notice( array( |
625
|
|
|
'id' => "give-expired-subscription-{$subscription['id']}", |
626
|
|
|
'type' => 'error', |
627
|
|
|
'description' => sprintf( |
628
|
|
|
__( 'Your Give add-on license expired for payment <a href="%1$s" target="_blank">#%2$d</a>. <a href="%3$s" target="_blank">Click to renew an existing license</a> or %4$s.', 'give' ), |
629
|
|
|
urldecode( $subscription['invoice_url'] ), |
630
|
|
|
$subscription['payment_id'], |
631
|
|
|
"{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired", |
632
|
|
|
Give()->notices->get_dismiss_link(array( |
633
|
|
|
'title' => __( 'Click here if already renewed', 'give' ), |
634
|
|
|
'dismissible_type' => 'user', |
635
|
|
|
'dismiss_interval' => 'permanent', |
636
|
|
|
)) |
637
|
|
|
), |
638
|
|
|
'dismissible_type' => 'user', |
639
|
|
|
'dismiss_interval' => 'shortly', |
640
|
|
|
) ); |
641
|
|
|
} else { |
642
|
|
|
Give()->notices->register_notice( array( |
643
|
|
|
'id' => "give-expires-subscription-{$subscription['id']}", |
644
|
|
|
'type' => 'error', |
645
|
|
|
'description' => sprintf( |
646
|
|
|
__( 'Your Give add-on license will expire in %1$s for payment <a href="%2$s" target="_blank">#%3$d</a>. <a href="%4$s" target="_blank">Click to renew an existing license</a> or %5$s.', 'give' ), |
647
|
|
|
human_time_diff( current_time( 'timestamp', 1 ), strtotime( $subscription['expires'] ) ), |
648
|
|
|
urldecode( $subscription['invoice_url'] ), |
649
|
|
|
$subscription['payment_id'], |
650
|
|
|
"{$this->checkout_url}?edd_license_key={$subscription['license_key']}&utm_campaign=admin&utm_source=licenses&utm_medium=expired", |
651
|
|
|
Give()->notices->get_dismiss_link(array( |
652
|
|
|
'title' => __( 'Click here if already renewed', 'give' ), |
653
|
|
|
'dismissible_type' => 'user', |
654
|
|
|
'dismiss_interval' => 'permanent', |
655
|
|
|
)) |
656
|
|
|
), |
657
|
|
|
'dismissible_type' => 'user', |
658
|
|
|
'dismiss_interval' => 'shortly', |
659
|
|
|
) ); |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
// Stop validation for these license keys. |
663
|
|
|
$addon_license_key_in_subscriptions = array_merge( $addon_license_key_in_subscriptions, $subscription['licenses'] ); |
664
|
|
|
}// End foreach(). |
665
|
|
|
$showed_subscriptions_message = true; |
666
|
|
|
}// End if(). |
667
|
|
|
|
668
|
|
|
// Show Non Subscription Give Add-on messages. |
669
|
|
|
if ( |
670
|
|
|
! in_array( $this->license, $addon_license_key_in_subscriptions ) |
671
|
|
|
&& ! empty( $this->license ) |
672
|
|
|
&& empty( $showed_invalid_message ) |
673
|
|
|
&& ! $this->is_valid_license() |
674
|
|
|
) { |
675
|
|
|
|
676
|
|
|
Give()->notices->register_notice( array( |
677
|
|
|
'id' => 'give-invalid-license', |
678
|
|
|
'type' => 'error', |
679
|
|
|
'description' => sprintf( |
680
|
|
|
__( 'You have invalid or expired license keys for one or more Give Add-ons. Please go to the <a href="%s">licenses page</a> to correct this issue.', 'give' ), |
681
|
|
|
admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' ) |
682
|
|
|
), |
683
|
|
|
'dismissible_type' => 'user', |
684
|
|
|
'dismiss_interval' => 'shortly', |
685
|
|
|
) ); |
686
|
|
|
|
687
|
|
|
$showed_invalid_message = true; |
688
|
|
|
|
689
|
|
|
} |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
/** |
693
|
|
|
* Check if license is valid or not. |
694
|
|
|
* |
695
|
|
|
* @since 1.7 |
696
|
|
|
* @access public |
697
|
|
|
* |
698
|
|
|
* @param null|object $licence_data |
699
|
|
|
* |
700
|
|
|
* @return bool |
701
|
|
|
*/ |
702
|
|
|
public function is_valid_license( $licence_data = null ) { |
703
|
|
|
$license_data = empty( $licence_data ) ? $this->license_data : $licence_data; |
704
|
|
|
|
705
|
|
|
if ( apply_filters( 'give_is_valid_license', ( $this->is_license( $license_data ) && 'valid' === $license_data->license ) ) ) { |
706
|
|
|
return true; |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
return false; |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
|
713
|
|
|
/** |
714
|
|
|
* Check if license is license object of no. |
715
|
|
|
* |
716
|
|
|
* @since 1.7 |
717
|
|
|
* @access public |
718
|
|
|
* |
719
|
|
|
* @param null|object $licence_data |
720
|
|
|
* |
721
|
|
|
* @return bool |
722
|
|
|
*/ |
723
|
|
View Code Duplication |
public function is_license( $licence_data = null ){ |
|
|
|
|
724
|
|
|
$license_data = empty( $licence_data ) ? $this->license_data : $licence_data; |
725
|
|
|
|
726
|
|
|
if ( apply_filters( 'give_is_license', ( is_object( $license_data ) && ! empty( $license_data ) && property_exists( $license_data, 'license' ) ) ) ) { |
727
|
|
|
return true; |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
return false; |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
/** |
734
|
|
|
* Check if subscription is subscription object of no. |
735
|
|
|
* |
736
|
|
|
* @since 1.7 |
737
|
|
|
* @access public |
738
|
|
|
* |
739
|
|
|
* @param null|object $subscription_data |
740
|
|
|
* |
741
|
|
|
* @return bool |
742
|
|
|
*/ |
743
|
|
View Code Duplication |
public function is_subscription( $subscription_data = null ){ |
|
|
|
|
744
|
|
|
$subscription_data = empty( $subscription_data ) ? $this->license_data : $subscription_data; |
745
|
|
|
|
746
|
|
|
if ( apply_filters( 'give_is_subscription', ( is_object( $subscription_data ) && ! empty( $subscription_data ) && property_exists( $subscription_data, 'license_key' ) ) ) ) { |
747
|
|
|
return true; |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
return false; |
751
|
|
|
} |
752
|
|
|
|
753
|
|
|
/** |
754
|
|
|
* Check if license is valid or not. |
755
|
|
|
* |
756
|
|
|
* @access private |
757
|
|
|
* @since 1.7 |
758
|
|
|
* |
759
|
|
|
* @return bool |
760
|
|
|
*/ |
761
|
|
|
private function __is_third_party_addon() { |
|
|
|
|
762
|
|
|
return ( false === strpos( $this->api_url, 'givewp.com/' ) ); |
763
|
|
|
} |
764
|
|
|
|
765
|
|
|
/** |
766
|
|
|
* Remove license key from subscription. |
767
|
|
|
* |
768
|
|
|
* This function mainly uses when admin user deactivate license key, |
769
|
|
|
* then we do not need subscription information for that license key. |
770
|
|
|
* |
771
|
|
|
* @access private |
772
|
|
|
* @since 1.7 |
773
|
|
|
* |
774
|
|
|
* @return bool |
775
|
|
|
*/ |
776
|
|
|
private function __remove_license_key_from_subscriptions() { |
|
|
|
|
777
|
|
|
$subscriptions = get_option( 'give_subscriptions', array() ); |
778
|
|
|
|
779
|
|
|
// Bailout. |
780
|
|
|
if ( empty( $this->license ) ) { |
781
|
|
|
return false; |
782
|
|
|
} |
783
|
|
|
|
784
|
|
|
if ( ! empty( $subscriptions ) ) { |
785
|
|
|
foreach ( $subscriptions as $subscription_id => $subscription ) { |
786
|
|
|
$license_index = array_search( $this->license, $subscription['licenses'] ); |
787
|
|
|
if ( false !== $license_index ) { |
788
|
|
|
// Remove license key. |
789
|
|
|
unset( $subscriptions[ $subscription_id ]['licenses'][ $license_index ] ); |
790
|
|
|
|
791
|
|
|
// Rearrange license keys. |
792
|
|
|
$subscriptions[ $subscription_id ]['licenses'] = array_values( $subscriptions[ $subscription_id ]['licenses'] ); |
793
|
|
|
|
794
|
|
|
// Update subscription information. |
795
|
|
|
update_option( 'give_subscriptions', $subscriptions ); |
796
|
|
|
break; |
797
|
|
|
} |
798
|
|
|
} |
799
|
|
|
} |
800
|
|
|
} |
801
|
|
|
|
802
|
|
|
/** |
803
|
|
|
* @param $plugin_file |
804
|
|
|
* @param $plugin_data |
805
|
|
|
* @param $status |
806
|
|
|
* |
807
|
|
|
* @return bool |
808
|
|
|
*/ |
809
|
|
|
public function plugin_page_notices( $plugin_file, $plugin_data, $status ) { |
|
|
|
|
810
|
|
|
// Bailout. |
811
|
|
|
if ( $this->is_valid_license() ) { |
812
|
|
|
return false; |
813
|
|
|
} |
814
|
|
|
|
815
|
|
|
$update_notice_wrap = '<tr class="give-addon-notice-tr active"><td colspan="3" class="colspanchange"><div class="notice inline notice-warning notice-alt give-invalid-license"><p><span class="dashicons dashicons-info"></span> %s</p></div></td></tr>'; |
816
|
|
|
$message = $this->license_state_message(); |
817
|
|
|
|
818
|
|
|
if ( ! empty( $message['message'] ) ) { |
819
|
|
|
echo sprintf( $update_notice_wrap, $message['message'] ); |
|
|
|
|
820
|
|
|
} |
821
|
|
|
} |
822
|
|
|
|
823
|
|
|
|
824
|
|
|
/** |
825
|
|
|
* Get message related to license state. |
826
|
|
|
* |
827
|
|
|
* @since 1.8.7 |
828
|
|
|
* @access public |
829
|
|
|
* @return array |
830
|
|
|
*/ |
831
|
|
|
public function license_state_message() { |
832
|
|
|
$message_data = array(); |
833
|
|
|
|
834
|
|
|
if ( ! $this->is_valid_license() ) { |
835
|
|
|
|
836
|
|
|
$message_data['message'] = sprintf( |
837
|
|
|
'Please <a href="%1$s">activate your license</a> to receive updates and support for the %2$s add-on.', |
838
|
|
|
esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=licenses' ) ), |
839
|
|
|
$this->item_name |
840
|
|
|
); |
841
|
|
|
} |
842
|
|
|
|
843
|
|
|
return $message_data; |
844
|
|
|
} |
845
|
|
|
|
846
|
|
|
|
847
|
|
|
/** |
848
|
|
|
* Check if admin can edit license or not, |
849
|
|
|
* |
850
|
|
|
* @since 1.8.9 |
851
|
|
|
* @access private |
852
|
|
|
*/ |
853
|
|
|
private function __is_user_can_edit_license() { |
|
|
|
|
854
|
|
|
// Bailout. |
855
|
|
|
if ( |
856
|
|
|
! Give_Admin_Settings::verify_nonce() || |
857
|
|
|
! current_user_can( 'manage_give_settings' ) || |
858
|
|
|
'licenses' !== give_get_current_setting_tab() |
859
|
|
|
) { |
860
|
|
|
return false; |
861
|
|
|
} |
862
|
|
|
|
863
|
|
|
// Security check. |
864
|
|
|
if ( |
865
|
|
|
isset( $_POST[ $this->item_shortname . '_license_key-nonce'] ) && |
|
|
|
|
866
|
|
|
! wp_verify_nonce( $_REQUEST[ $this->item_shortname . '_license_key-nonce' ], $this->item_shortname . '_license_key-nonce' ) |
|
|
|
|
867
|
|
|
) { |
868
|
|
|
wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) ); |
869
|
|
|
} |
870
|
|
|
|
871
|
|
|
return true; |
872
|
|
|
} |
873
|
|
|
|
874
|
|
|
|
875
|
|
|
/** |
876
|
|
|
* Get license information. |
877
|
|
|
* |
878
|
|
|
* @since 1.8.9 |
879
|
|
|
* @access public |
880
|
|
|
* |
881
|
|
|
* @param string $edd_action |
882
|
|
|
* @param bool $response_in_array |
883
|
|
|
* |
884
|
|
|
* @return mixed |
885
|
|
|
*/ |
886
|
|
|
public function get_license_info( $edd_action = '', $response_in_array = false ) { |
887
|
|
|
if( empty( $edd_action ) ) { |
|
|
|
|
888
|
|
|
return false; |
889
|
|
|
} |
890
|
|
|
|
891
|
|
|
// Data to send to the API. |
892
|
|
|
$api_params = array( |
893
|
|
|
'edd_action' => $edd_action, // never change from "edd_" to "give_"! |
894
|
|
|
'license' => $this->license, |
895
|
|
|
'item_name' => urlencode( $this->item_name ), |
896
|
|
|
'url' => home_url(), |
897
|
|
|
); |
898
|
|
|
|
899
|
|
|
// Call the API. |
900
|
|
|
$response = wp_remote_post( |
901
|
|
|
$this->api_url, |
902
|
|
|
array( |
903
|
|
|
'timeout' => 15, |
904
|
|
|
'sslverify' => false, |
905
|
|
|
'body' => $api_params, |
906
|
|
|
) |
907
|
|
|
); |
908
|
|
|
|
909
|
|
|
// Make sure there are no errors. |
910
|
|
|
if ( is_wp_error( $response ) ) { |
911
|
|
|
return false; |
912
|
|
|
} |
913
|
|
|
|
914
|
|
|
return json_decode( wp_remote_retrieve_body( $response ), $response_in_array ); |
915
|
|
|
} |
916
|
|
|
|
917
|
|
|
|
918
|
|
|
/** |
919
|
|
|
* Unset license |
920
|
|
|
* |
921
|
|
|
* @since 1.8.14 |
922
|
|
|
* @access private |
923
|
|
|
*/ |
924
|
|
|
private function unset_license() { |
925
|
|
|
// Remove license key from subscriptions if exist. |
926
|
|
|
$this->__remove_license_key_from_subscriptions(); |
927
|
|
|
|
928
|
|
|
// Remove license from database. |
929
|
|
|
delete_option( "{$this->item_shortname}_license_active" ); |
930
|
|
|
give_delete_option( "{$this->item_shortname}_license_key" ); |
931
|
|
|
|
932
|
|
|
// Unset license param. |
933
|
|
|
$this->license = ''; |
934
|
|
|
} |
935
|
|
|
} |
936
|
|
|
|
937
|
|
|
endif; // end class_exists check. |
938
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.