-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin-license-manager.php
340 lines (261 loc) · 9.88 KB
/
plugin-license-manager.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
<?php
/**
* Plugin Updater.
*
* Allow activation and auto updates for plugins hosted with License Manager for WooCommerce.
*/
if ( ! class_exists( 'Fluidweb_PluginLicenseManager' ) ) {
class Fluidweb_PluginLicenseManager {
private $slug;
private $plugin_data;
private $api_update_called = false;
private $plugin_file;
private $product_id;
private $api_url;
private $customer_key;
private $customer_secret;
private $license_key;
private $activate_option;
/**
* Construct a new instance of plugin updater
*
* @param string $plugin_file Relative path to plugin file.
* @param string $product_id ID of the product on the license manager website.
* @param string $customer_key License Manager API consumer key.
* @param string $customer_secret License Manager API consumer secret.
* @param string $activate_option License Manager API consumer secret.
* @param string $license_key License key.
*/
function __construct( $plugin_file, $product_id, $api_url, $customer_key, $customer_secret, $activate_option, $license_key ) {
// Set variables
$this->plugin_file = $plugin_file;
$this->product_id = $product_id;
$this->api_url = $api_url;
$this->customer_key = $customer_key;
$this->customer_secret = $customer_secret;
$this->activate_option = $activate_option;
$this->license_key = $license_key;
}
/**
* Initialize hooks.
*/
public function init_plugin_update_hooks() {
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'set_transient' ) );
add_filter( 'plugins_api', array( $this, 'set_plugin_info' ), 10, 3 );
add_filter( 'upgrader_post_install', array( $this, 'post_install' ), 10, 3 );
}
/**
* Get information regarding the current plugin version
*/
private function init_plugin_data() {
$this->slug = plugin_basename( $this->plugin_file );
$this->plugin_data = get_plugin_data( $this->plugin_file );
}
/**
* Call API for data
*/
private function call_api( $url ) {
$process = curl_init( $url );
curl_setopt( $process, CURLOPT_USERPWD, sprintf( '%s:%s', $this->customer_key, $this->customer_secret ) );
curl_setopt( $process, CURLOPT_RETURNTRANSFER, TRUE );
$response = curl_exec( $process );
curl_close( $process );
return $response;
}
/**
* Get information regarding plugin releases from repository
*/
public function get_release_info( $force_check = false ) {
$transient_name = $this->slug . '_plugin_info';
// Check transient but allow for $force_check to override
if( ! $force_check ) {
$transient = get_transient( $transient_name );
if( $transient !== false ) {
return $transient;
}
}
$url = untrailingslashit( $this->api_url ) . '/wp-json/lmfwc/v2/products/update/' . $this->product_id;
$response = $this->call_api( $url );
$response = json_decode( $response );
// Set flag for API called
$this->api_update_called = true;
// Update transient
set_transient( $transient_name, $response, DAY_IN_SECONDS );
return $response;
}
/**
* Get the plugin license information from the license manager server.
*/
public function get_info( $license_key = null ) {
// Defaults to the instance license key.
if ( ! $license_key ) {
$license_key = $this->license_key;
}
$url = untrailingslashit( $this->api_url ) . '/wp-json/lmfwc/v2/licenses/' . $license_key;
$response = $this->call_api( $url );
if ( $response ) {
$data = json_decode( $response );
return $data;
}
$error_response = new \stdClass();
$error_response->code = 'fwplm_rest_connection_error';
$error_response->message = sprintf( 'Couldn\'t connect to the license server (%s). Try again later.', $this->api_url );
return $error_response;
}
/**
* Validate the plugin license key against the license manager server.
*/
public function validate( $license_key = null ) {
// Defaults to the instance license key.
if ( ! $license_key ) {
$license_key = $this->license_key;
}
$url = untrailingslashit( $this->api_url ) . '/wp-json/lmfwc/v2/licenses/validate/' . $license_key;
$response = $this->call_api( $url );
if ( $response ) {
$data = json_decode( $response );
return $data;
}
$error_response = new \stdClass();
$error_response->code = 'fwplm_rest_connection_error';
$error_response->message = sprintf( 'Couldn\'t connect to the license server (%s). Try again later.', $this->api_url );
return $error_response;
}
/**
* Activate the plugin license, also validate against the license manager server.
*/
public function activate( $license_key = null ) {
// Defaults to the instance license key.
if ( ! $license_key ) {
$license_key = $this->license_key;
}
if ( ! $license_key ) {
$error_response = new \stdClass();
$error_response->code = 'fwplm_missing_license_key';
$error_response->message = 'Missing the license key. Please provide a valid license key and try again.';
return $error_response;
}
$url = untrailingslashit( $this->api_url ) . '/wp-json/lmfwc/v2/licenses/activate/' . $license_key;
$response = $this->call_api( $url );
if ( $response ) {
$data = json_decode( $response );
if ( $data && isset( $data->success ) && $data->success ) {
update_option( $this->activate_option, 'yes' );
return $data;
}
else if ( $data && isset( $data->message ) && $data->message ) {
$error_response = new \stdClass();
$error_response->code = isset( $data->code ) ? $data->code : 'fwplm_generic_error';
$error_response->message = $data->message;
return $error_response;
}
}
$error_response = new \stdClass();
$error_response->code = 'fwplm_rest_connection_error';
$error_response->message = sprintf( 'Couldn\'t connect to the license server (%s). Try again later.', $this->api_url );
return $error_response;
}
/**
* Push in plugin version information to get the update notification
*/
public function set_transient( $transient ) {
// Bail if no response (error)
if( ! isset( $transient->response ) ) {
return $transient;
}
// Get flag for `force-check` (force check only once)
$force_check = ( ! $this->api_update_called ) ? ! empty( $_GET['force-check'] ) : false;
// Get plugin & latest release information
$this->init_plugin_data();
$release_info = $this->get_release_info( $force_check );
// Nothing found.
if ( ! $release_info || ! property_exists( $release_info, 'success' ) || ! $release_info->success || ! isset( $release_info->data ) ) { return $transient; }
// Check the versions if we need to do an update ( $repo_version > current version )
$doUpdate = version_compare( $release_info->data->version, $this->plugin_data["Version"] );
// Update the transient to include our updated plugin data
if ( $doUpdate == 1 ) {
$obj = new \stdClass();
$obj->slug = $this->slug;
$obj->new_version = $release_info->data->version;
$obj->tested = $release_info->data->tested;
$obj->url = $this->plugin_data["PluginURI"];
$obj->package = str_replace( '{license_key}', $this->license_key, $release_info->data->package );
// Copy icons from api_result
if ( $release_info->data->icons ) {
$obj->icons = array();
foreach ( $release_info->data->icons as $key => $value ) {
$obj->icons[ $key ] = $value;
}
}
$transient->response[ $this->slug ] = $obj;
}
return $transient;
}
/**
* Push in plugin version information to display in the details lightbox
*/
public function set_plugin_info( $res, $action, $args ) {
// Only for 'plugin_information' action
if( 'plugin_information' !== $action ) { return $res; }
// Get plugin data
$this->init_plugin_data();
// Only for 'plugin_information' action
if( ! $this->plugin_data || ! is_array( $this->plugin_data ) ) { return $res; }
// Get latest release information
$release_info = $this->get_release_info();
// Bail if new plugin info is not available
if ( ! $release_info || ! property_exists( $release_info, 'success' ) || ! $release_info->success || ! isset( $release_info->data ) ) { return $res; }
if ( $args->slug == $this->slug ) {
$res = new \stdClass();
$res->slug = $this->slug;
$res->name = $this->plugin_data['Name'];
$res->author = $this->plugin_data['Author'];
$res->homepage = $this->plugin_data['PluginURI'];
// Copy values from release info
foreach ( $release_info->data as $key => $value ) {
// Skip sections
if ( 'sections' == $key ) { continue; }
$res->$key = $value;
}
// Copy icons from release info
if ( $release_info->data->icons ) {
$res->icons = array();
foreach ( $release_info->data->icons as $key => $value ) {
$res->icons[ $key ] = $value;
}
}
// Copy banners from release info
if ( $release_info->data->banners ) {
$res->banners = array();
foreach ( $release_info->data->banners as $key => $value ) {
$res->banners[ $key ] = $value;
}
}
// Copy sections from release info
if ( $release_info->data->sections ) {
$res->sections = array();
foreach ( $release_info->data->sections as $key => $value ) {
$res->sections[ $key ] = $value;
}
}
}
return $res;
}
/**
* Perform additional actions to successfully install our plugin
*/
public function post_install( $true, $hook_extra, $result ) {
// Get plugin information
$this->init_plugin_data();
// Remember if our plugin was previously activated
$wasActivated = is_plugin_active( $this->slug );
global $wp_filesystem;
$pluginFolder = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . dirname( $this->slug );
$wp_filesystem->move( $result['destination'], $pluginFolder );
$result['destination'] = $pluginFolder;
// Re-activate plugin if needed
if ( $wasActivated ) { $activate = activate_plugin( $this->slug ); }
return $result;
}
}
}