-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-checker.php
More file actions
388 lines (329 loc) · 11.3 KB
/
update-checker.php
File metadata and controls
388 lines (329 loc) · 11.3 KB
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
<?php
if (!class_exists('WC_SecurePayUpdateChecker')) {
class WC_SecurePayUpdateChecker
{
const API_ENDPOINT = 'https://plugins.b2bdev.com.au/api/plugins/info';
const API_LICENSE_ENDPOINT = 'https://plugins.b2bdev.com.au/api/licenses';
const API_TIMEOUT = 5;
const CACHE_TIME = HOUR_IN_SECONDS; // MINUTE_IN_SECONDS, HOUR_IN_SECONDS, DAY_IN_SECONDS;
private static $_instance = null;
/** @var array $plugin */
public $plugin;
/** @var string $plugin_file */
public $plugin_file;
/** @var string $plugin_slug */
public static $plugin_slug;
/** @var string $name */
public static $plugin_name;
/** @var string $version The current version of the plugin */
public $version;
/** @var string $cache_key */
public $cache_key;
/** @var string $option_license_key */
public $option_license_key;
/** @var object $license_data */
public $license_data = null;
/** @var string $current_license_key */
public $current_license_key = null;
/** @var bool $cache_allowed */
public $cache_allowed;
public function __construct($main_plugin_file)
{
global $pagenow;
if (!is_admin()) {
return false;
}
if (!file_exists($main_plugin_file)) {
return false;
}
if (!function_exists('get_plugin_data')) {
require_once(ABSPATH . 'wp-admin/includes/plugin.php');
}
$plugin = get_plugin_data($main_plugin_file);
$this->plugin = $plugin;
$this->plugin_file = plugin_basename($main_plugin_file);
self::$plugin_slug = $plugin['TextDomain'];
self::$plugin_name = $plugin['Name'];
$this->version = $plugin['Version'];
$this->cache_key = 'cache_'.self::$plugin_slug;
$this->cache_allowed = true;
add_filter('plugins_api', array($this, 'info'), 20, 3);
add_filter('site_transient_update_plugins', array($this, 'update'));
add_action('upgrader_process_complete', array($this, 'purge'), 10, 2);
$this->option_license_key = self::$plugin_slug.'_license_key';
$this->license_data = get_option($this->option_license_key, null);
$this->current_license_key = $this->license_data && isset($this->license_data->licenseKey) ? $this->license_data->licenseKey : null;
if(!$this->current_license_key && isset($pagenow) && $pagenow == 'plugins.php') {
add_action('admin_notices', array($this, 'add_plugins_page_notices'));
}
add_filter('wc_securepay_settings', array($this, 'add_license_key_setting'));
}
public static function get_instance($main_plugin_file = null) {
if (self::$_instance == null) {
self::$_instance = new self($main_plugin_file);
}
return self::$_instance;
}
public function prepareData() {
$user = wp_get_current_user();
$data = [
'plugin_slug' => self::$plugin_slug,
'current_version' => $this->version,
'wordpress_version' => get_bloginfo('version'),
'website_url' => home_url(),
'server_ip' => $_SERVER['SERVER_ADDR'],
'server_name' => $_SERVER['SERVER_NAME'],
'http_host' => $_SERVER['HTTP_HOST'],
'user_email' => $user ? $user->user_email : '',
'user_ip' => self::getUserIpAddr(),
'user_agent' => $_SERVER['HTTP_USER_AGENT'],
];
if (isset($_SERVER['HTTP_REFERER'])) {
$data['user_referrer'] = $_SERVER['HTTP_REFERER'];
}
if (defined('WOOCOMMERCE_VERSION')) {
// To avoid warning error
if (!defined('WOOCOMMERCE_VERSION')) define('WOOCOMMERCE_VERSION', get_option('woocommerce_version'));
$data['woocommerce_version'] = WOOCOMMERCE_VERSION;
}
if (class_exists('GFForms') && isset(GFForms::$version)) {
$data['gravityforms_version'] = GFForms::$version;
}
if ($this->current_license_key) {
$data['license_key'] = $this->current_license_key;
}
return $data;
}
public function getApiEndPointUrl() {
$data = $this->prepareData();
// Unset some data to avoid limit query string length
unset($data['user_agent']);
$query = http_build_query($data);
$url = self::API_ENDPOINT.'/'.self::$plugin_slug.'?'. $query;
return $url;
}
public function request()
{
$remote = get_transient($this->cache_key);
if (false === $remote || !$this->cache_allowed) {
$remote = wp_remote_get($this->getApiEndPointUrl(),
array(
'timeout' => self::API_TIMEOUT,
'headers' => array(
'Accept' => 'application/json'
)
)
);
if (
is_wp_error($remote)
|| 200 !== wp_remote_retrieve_response_code($remote)
|| empty(wp_remote_retrieve_body($remote))
) {
return false;
}
set_transient($this->cache_key, $remote, self::CACHE_TIME);
}
$remote = json_decode(wp_remote_retrieve_body($remote));
return $remote;
}
function info($res, $action, $args)
{
// do nothing if you're not getting plugin information right now
if ('plugin_information' !== $action) {
return $res;
}
// do nothing if it is not our plugin
if (self::$plugin_slug !== $args->slug) {
return $res;
}
// get updates
$remote = $this->request();
if (!$remote) {
return $res;
}
$res = new stdClass();
$res->name = $remote->name;
$res->slug = $remote->slug;
$res->version = $remote->version;
$res->tested = $remote->tested;
$res->requires = $remote->requires;
$res->author = $remote->author;
$res->author_profile = $remote->author_profile;
$res->download_link = $remote->download_url;
$res->trunk = $remote->download_url;
$res->requires_php = $remote->requires_php;
$res->last_updated = $remote->last_updated;
$res->sections = array(
'description' => $remote->sections->description,
'installation' => $remote->sections->installation,
'changelog' => $remote->sections->changelog
);
if (!empty($remote->banners)) {
$res->banners = array(
'low' => $remote->banners->low,
'high' => $remote->banners->high
);
}
return $res;
}
public function update($transient)
{
if (!$this->current_license_key) {
return $transient;
}
if (empty($transient->checked)) {
return $transient;
}
$remote = $this->request();
if ($remote) {
if (version_compare($this->version, $remote->version, '<')
&& version_compare($remote->requires, get_bloginfo('version'), '<=')
&& version_compare($remote->requires_php, PHP_VERSION, '<')
) {
$res = new stdClass();
$res->slug = self::$plugin_slug;
$res->plugin = $this->plugin_file;
$res->new_version = $remote->version;
$res->tested = $remote->tested;
if ($this->current_license_key) {
$res->package = $remote->download_url;
}
$transient->response[$res->plugin] = $res;
}
if (isset($remote->license, $remote->license->licenseKey) && $remote->license->licenseKey == $this->current_license_key) {
update_option($this->option_license_key, $remote->license);
}
}
return $transient;
}
public function purge($upgrader, $options)
{
if (
$this->cache_allowed
&& 'update' === $options['action']
&& 'plugin' === $options['type']
) {
// just clean the cache when new plugin version is installed
delete_transient($this->cache_key);
}
}
public function activate_license($license_key)
{
if ($this->current_license_key && $this->current_license_key === $license_key) {
return true;
}
$options = array(
'timeout' => 15000,
'body' => $this->prepareData()
);
$response = wp_remote_post(self::API_LICENSE_ENDPOINT.'/activate/'.$license_key, $options);
if (is_wp_error($response)) {
return false;
}
$license_data = json_decode(wp_remote_retrieve_body($response));
if (isset($license_data->status) && $license_data->status && isset($license_data->data)) {
if (!isset($license_data->data->pluginSlug) || $license_data->data->pluginSlug === self::$plugin_slug) {
update_option($this->option_license_key, $license_data->data);
$this->current_license_key = $license_key;
return true;
}
}
if ($this->current_license_key) {
update_option($this->option_license_key, null);
}
return false;
}
public function deactivate_license($license_key = '')
{
$license_key = $license_key ? $license_key : $this->current_license_key;
if ($this->current_license_key) {
update_option($this->option_license_key, null);
$options = array(
'timeout' => 15000,
'body' => $this->prepareData()
);
$response = wp_remote_post(self::API_LICENSE_ENDPOINT.'/deactivate/'.$license_key, $options);
$response_data = json_decode(wp_remote_retrieve_body($response));
$this->current_license_key = null;
}
return true;
}
public function is_license_activated()
{
return $this->current_license_key ? true : false;
}
public function is_license_expired()
{
if ($this->license_data && isset($this->license_data->expiresAt) && $this->license_data->expiresAt) {
$expires = strtotime($this->license_data->expiresAt);
if ($expires < time()) {
return true;
}
}
return false;
}
public function get_current_license_key()
{
return $this->current_license_key;
}
public function add_license_key_setting($settings)
{
$is_license_activated = $this->is_license_activated();
$is_license_expired = $this->is_license_expired();
$license_key_description = esc_html__('Please enter your license in order to keep your plugin up to date with important security patches and functionality', 'woocommerce' );
if ($is_license_expired) {
$license_key_description = esc_html__('Your license has expired! Please renew your license in order to keep your plugin up to date with important security patches and functionality', 'woocommerce' );
} else if ($is_license_activated) {
$license_key_description = esc_html__('The plugin license is activated', 'woocommerce' );
}
$settings = array_merge(array(
'license_key' => array(
'title' => 'License key',
'type' => 'text',
'description' => $license_key_description,
),
), $settings);
return $settings;
}
public static function getUserIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
//ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
//ip pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
/**
* Add plugin notices to the overview page of plugins
**/
public static function add_plugins_page_notices() {
$plugins = get_plugins();
foreach($plugins as $plugin_id => $plugin) {
$slug = dirname($plugin_id);
if(empty($slug) || $slug !== self::$plugin_slug) continue;
add_action('after_plugin_row_' . $plugin_id, array(self::class, 'show_purchase_notice'), 10, 3);
break;
}
}
/**
* Show message for activation benefits
**/
public static function show_purchase_notice($plugin_file, $plugin_data, $plugin_status) {
$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
?>
<tr class="plugin-update-tr active">
<td colspan="<?php echo $wp_list_table->get_column_count(); ?>" class="plugin-update colspanchange">
<div class="update-message notice inline notice-warning notice-alt">
<p><?php _e('There is a new version of '.self::$plugin_name.' available. To update directly <a href="'.admin_url('admin.php?page=wc-settings&tab=checkout§ion=securepay').'" target="_blank">register your license key now</a>.'); ?></p>
</div>
</td>
</tr>
<?php
}
}
}