-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEDD_Client_Init.php
387 lines (328 loc) · 16.8 KB
/
EDD_Client_Init.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
<?php
require_once 'EDD_Client_Updater.php';
if ( ! class_exists( 'EDD_Client_Init' ) ):
class EDD_Client_Init {
protected array $plugin = [];
/*
* Instantiate the EDD_Client_Init class
*/
public function __construct($plugin_path, $api_url)
{
$plugin_data = get_file_data($plugin_path, [
'name' => 'Plugin Name',
'version' => 'Version',
'author' => 'Author',
], 'plugin');
$this->plugin['name'] = $plugin_data['name'];
$this->plugin['version'] = $plugin_data['version'];
$this->plugin['author'] = $plugin_data['author'];
$this->plugin['machine_name'] = str_replace(' ', '-', strtolower($this->plugin['name']));
$this->plugin['path'] = $plugin_path;
$this->plugin['slug'] = plugin_basename($this->plugin['path']);
$this->plugin['api_url'] = $api_url;
$this->plugin['license'] = trim(get_option($this->plugin['machine_name'].'_license_key'));
$this->plugin['license_status'] = get_option($this->plugin['machine_name'].'_license_status');
add_action( 'admin_init', function (){
// Enqueue Scripts
add_action('admin_print_scripts-plugins.php', [ $this, 'enqueue_scripts' ]);
add_action('admin_print_styles-plugins.php', [ $this, 'enqueue_styles' ]);
// License GUI
if ($this->plugin['license_status'] !== 'valid') {
global $pagenow;
if ( $pagenow == 'plugins.php' ) {
add_action( 'admin_notices', [ $this, 'display_admin_notice' ] );
}
add_action('after_plugin_row_' . $this->plugin['slug'], [ $this, 'insert_license_row' ], 10, 3);
}
else{
add_filter('plugin_action_links_' . $this->plugin['slug'], [$this, 'insert_license_link'], 9, 2);
add_action('after_plugin_row_' . $this->plugin['slug'], [ $this, 'insert_license_operation_row' ], 10, 3);
}
// Add Ajax Actions
add_action( 'wp_ajax_'.$this->plugin['machine_name'].'-edd-client-operations', [ $this, 'edd_operations' ] );
// Trigger Plugin Update
$this->plugin_updater();
});
}
/*
* A helper function to return if plugin is premium
*/
public function is_premium(){
if ($this->plugin['license_status'] === 'valid'){
return true;
}
else{
return false;
}
}
/*
* Enqueue Scripts
*/
public function enqueue_scripts()
{
wp_register_script('edd-client', plugins_url('script.js', __FILE__), array('jquery'));
wp_enqueue_script('edd-client');
}
/*
* Enqueue Styles
*/
public function enqueue_styles()
{
wp_register_style('edd-client', plugins_url('style.css', __FILE__));
wp_enqueue_style('edd-client');
}
/*
* Add a License Link to Plugin
*/
public function insert_license_link($links)
{
$settings_link = '<a href="javascript:void(0);" class="edd-client-cred-link">License</a>';
array_push($links, $settings_link);
return $links;
}
/*
* Adds row on the plugin table. Provides GUI to enter License
*/
public function insert_license_row()
{
?>
<tr class="plugin-update-tr active">
<td colspan="3">
<div class="update-message notice inline notice-error notice-alt">
<p>Enter <a href="javascript:void(0);" class="edd-client-cred-link">License</a> key for <?php echo $this->plugin['name']?> to get security and feature updates to properly work on your site.</p>
<div id="<?php echo 'ec-'.$this->plugin['machine_name']?>" class="edd-client-row" style="display:none">
<input class="edd-client-license-key" type="text" placeholder="Enter Your License"/>
<button class="button edd-client-button" data-action=<?php echo $this->plugin['machine_name'].'-edd-client-operations'?> data-operation="activate_license" data-nonce="<?php echo wp_create_nonce( $this->plugin['machine_name'].'-edd-client-operations' ) ?>"> <span class="dashicons dashicons-update"></span> Activate License</button>
</div>
</div>
</td>
</tr>
<?php
}
/*
* Adds row on the plugin table. Provides GUI to deactivate, check Expiry of license etc.
*/
public function insert_license_operation_row()
{
?>
<tr class="edd-client-row notice-warning notice-alt" style="display: none">
<td colspan="3">
<div class="edd-client-row">
<input class="edd-client-license-key" type="text" style="margin-right:-14px; border-top-right-radius:0px; border-bottom-right-radius:0px; border-right:0px;" value="<?php echo $this->plugin['license'] ?>"/>
<button class="button edd-client-button" data-action=<?php echo $this->plugin['machine_name'].'-edd-client-operations'?> data-operation="change_license" data-nonce="<?php echo wp_create_nonce( $this->plugin['machine_name'].'-edd-client-operations' ) ?>" style="margin-left:-4px; border-top-left-radius:0px; border-bottom-left-radius:0px;"> <span class="dashicons dashicons-update"></span> Change License</button>
<button class="button edd-client-button" data-action=<?php echo $this->plugin['machine_name'].'-edd-client-operations'?> data-operation="check_expiry" data-nonce="<?php echo wp_create_nonce( $this->plugin['machine_name'].'-edd-client-operations' ) ?>"> <span class="dashicons dashicons-update"></span> Check Expiry Date</button>
<button class="button edd-client-button" data-action=<?php echo $this->plugin['machine_name'].'-edd-client-operations'?> data-operation="deactivate_license" data-nonce="<?php echo wp_create_nonce( $this->plugin['machine_name'].'-edd-client-operations' ) ?>"> <span class="dashicons dashicons-update"></span> Deactivate License</button>
</div>
</td>
</tr>
<?php
}
/*
* Display admin notice if plugin license key is not yet entered
*/
public function display_admin_notice(){
?>
<div class="notice notice-warning is-dismissible">
<p>Almost done - Activate license to make <strong><?php echo $this->plugin['name']?></strong> properly work on your site
<input class="edd-client-license-key" type="text" placeholder="Enter Your License"/>
<button class="button edd-client-button" data-action=<?php echo $this->plugin['machine_name'].'-edd-client-operations'?> data-operation="activate_license" data-nonce="<?php echo wp_create_nonce( $this->plugin['machine_name'].'-edd-client-operations' ) ?>"> <span class="dashicons dashicons-update"></span> Activate License</button>
</p>
</div>
<?php
}
/**
* Trigger Plugin Update
*/
public function plugin_updater()
{
new EDD_Client_Updater(
$this->plugin['api_url'],
$this->plugin['path'],
array(
'version' => $this->plugin['version'],
'license' => $this->plugin['license'],
'item_name' => $this->plugin['name'],
'author' => $this->plugin['author']
)
);
}
/*
* Different EDD Operations executed on Ajax call
*/
public function edd_operations()
{
if (empty( $operation = $_POST['operation'] ) || wp_verify_nonce($_POST['nonce'], $this->plugin['machine_name'].'-edd-client-operations') === false) {
wp_send_json_error('Something went wrong');
}
switch($operation){
case 'activate_license':
$license = !empty($_POST['license']) ? $_POST['license'] : wp_send_json_error('License field can not be empty');
$license = sanitize_text_field($license);
$license_data = $this->validate_license($license, $this->plugin['name'], $this->plugin['api_url']);
if( $license_data->license === 'valid' ) {
update_option($this->plugin['machine_name'].'_license_status', $license_data->license);
update_option($this->plugin['machine_name'].'_license_key', $license);
wp_send_json_success('License successfully activated');
}
break;
case 'deactivate_license':
$license_data = $this->invalidate_license($this->plugin['license'], $this->plugin['name'], $this->plugin['api_url']);
if( $license_data->license === 'deactivated' || $license_data->license === 'failed' ) {
delete_option($this->plugin['machine_name'].'_license_status');
delete_option($this->plugin['machine_name'].'_license_key');
wp_send_json_success('License deactivated for this site');
}
break;
case 'change_license':
$new_license = !empty($_POST['license']) ? $_POST['license'] : wp_send_json_error('License field can not be empty');
$new_license = sanitize_text_field($new_license);
$old_license = $this->plugin['license'];
if($new_license !== $old_license){
$license_data = $this->validate_license($new_license, $this->plugin['name'], $this->plugin['api_url']);
if ($license_data->license === 'valid'){
$license_data = $this->invalidate_license($old_license, $this->plugin['name'], $this->plugin['api_url']);
if($license_data->license === 'deactivated' || $license_data->license === 'failed'){
update_option($this->plugin['machine_name'].'_license_key', $new_license);
wp_send_json_success('License Successfully Changed.');
}
}
}
else{
wp_send_json_error('Enter a new license.');
}
break;
case 'check_expiry':
$license = $this->plugin['license'];
$this->check_expiry($license, $this->plugin['name'], $this->plugin['api_url']);
break;
default:
wp_send_json_error('Something went wrong');
}
}
/*
* Validate License
*/
public function validate_license($license, $plugin_name, $api_url){
// data to send in our API request
$api_params = array(
'edd_action' => 'activate_license',
'license' => $license,
'item_name' => urlencode($plugin_name),
'url' => home_url()
);
// Call the custom API.
$response = wp_remote_post($api_url, array('timeout' => 15, 'sslverify' => false, 'body' => $api_params));
// 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.');
}
wp_send_json_error($message);
}
else {
$license_data = json_decode(wp_remote_retrieve_body($response));
if (false === $license_data->success) {
switch ($license_data->error) {
case 'expired' :
$message = sprintf(
__('Your license key expired on %s.'),
date_i18n(get_option('date_format'), strtotime($license_data->expires, current_time('timestamp')))
);
break;
case 'disabled' :
case 'revoked' :
$message = __('Your license key has been disabled.');
break;
case 'missing' :
$message = __('Invalid license.');
break;
case 'invalid' :
case 'site_inactive' :
$message = __('Your license is not active for this URL.');
break;
case 'item_name_mismatch' :
$message = sprintf(__('This appears to be an invalid license key for %s.'), $plugin_name);
break;
case 'no_activations_left':
$message = __('Your license key has reached its activation limit.');
break;
default :
$message = __('An error occurred, please try again.');
break;
}
wp_send_json_error($message);
}
else{
return $license_data;
}
}
}
/*
* Invalidate License for current website. This will decrease the site count
*/
public function invalidate_license($license, $plugin_name, $api_url) {
// data to send in our API request
$api_params = array(
'edd_action' => 'deactivate_license',
'license' => $license,
'item_name' => urlencode($plugin_name), // the name of our product in EDD
'url' => home_url()
);
// Call the custom API.
$response = wp_remote_post( $api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
// 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.' );
}
wp_send_json_error($message);
}
// decode the license data
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
return $license_data;
}
/*
* Check License Expiry
*/
public function check_expiry($license, $plugin_name, $api_url) {
$api_params = array(
'edd_action' => 'check_license',
'license' => $license,
'item_name' => urlencode( $plugin_name ),
'url' => home_url()
);
// Call the custom API.
$response = wp_remote_post( $api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
// 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.' );
}
wp_send_json_error($message);
}
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
$expires = (bool) strtotime( $license_data->expires ) ? date( get_option( 'date_format' ), strtotime( $license_data->expires ) ) : $license_data->expires;
if( $license_data->license == 'valid' ) {
wp_send_json_success('License will expire on: '.sanitize_text_field($expires));
}
elseif ($license_data->license == 'expired'){
wp_send_json_success('License expired on: '.sanitize_text_field($expires));
}
elseif ($license_data->license == 'disabled'){
wp_send_json_success('Your license has been disabled by the seller');
}
elseif ($license_data->license == 'invalid'){
wp_send_json_success('Invalid license key');
}
else{
wp_send_json_error('Something went wrong');
}
}
}
endif;