class-fl-updater.php
10.7 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
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
<?php
/**
* Manages remote updates for all Beaver Builder products.
*
* @since 1.0
*/
final class FLUpdater {
/**
* The API URL for the Beaver Builder update server.
*
* @since 1.0
* @access private
* @var string $_updates_api_url
*/
static private $_updates_api_url = 'http://updates.wpbeaverbuilder.com/';
/**
* An internal array of data for each product.
*
* @since 1.0
* @access private
* @var array $_products
*/
static private $_products = array();
/**
* An internal array of remote responses with
* update data for each product.
*
* @since 1.8.4
* @access private
* @var array $_responses
*/
static private $_responses = array();
/**
* An internal array of settings for the updater instance.
*
* @since 1.0
* @access private
* @var array $settings
*/
private $settings = array();
/**
* Updater constructor method.
*
* @since 1.0
* @param array $settings An array of settings for this instance.
* @return void
*/
public function __construct( $settings = array() )
{
$this->settings = $settings;
if ( 'plugin' == $settings['type'] ) {
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'update_check' ) );
add_filter( 'plugins_api', array( $this, 'plugin_info' ), 10, 3 );
add_action( 'in_plugin_update_message-' . self::get_plugin_file( $settings['slug'] ), array( $this, 'update_message' ), 1, 2 );
}
else if ( $settings['type'] == 'theme' ) {
add_filter( 'pre_set_site_transient_update_themes', array( $this, 'update_check' ) );
}
}
/**
* Get the update data response from the API.
*
* @since 1.7.7
* @return object
*/
public function get_response()
{
$slug = $this->settings['slug'];
if ( isset( FLUpdater::$_responses[ $slug ] ) ) {
return FLUpdater::$_responses[ $slug ];
}
FLUpdater::$_responses[ $slug ] = FLUpdater::api_request( FLUpdater::$_updates_api_url, array(
'fl-api-method' => 'update_info',
'license' => FLUpdater::get_subscription_license(),
'domain' => network_home_url(),
'product' => $this->settings['name'],
'slug' => $this->settings['slug'],
'version' => $this->settings['version']
) );
return FLUpdater::$_responses[ $slug ];
}
/**
* Checks to see if an update is available for the current product.
*
* @since 1.0
* @param object $transient A WordPress transient object with update data.
* @return object
*/
public function update_check( $transient )
{
global $pagenow;
if( 'plugins.php' == $pagenow && is_multisite() ) {
return $transient;
}
if ( ! is_object( $transient ) ) {
$transient = new stdClass();
}
if ( ! isset( $transient->checked ) ) {
$transient->checked = array();
}
$response = $this->get_response();
if( ! isset( $response->error ) ) {
$transient->last_checked = time();
$transient->checked[ $this->settings['slug'] ] = $this->settings['version'];
if($this->settings['type'] == 'plugin') {
$plugin = self::get_plugin_file($this->settings['slug']);
if ( version_compare( $response->new_version, $this->settings['version'], '>' ) ) {
$transient->response[ $plugin ] = new stdClass();
$transient->response[ $plugin ]->slug = $response->slug;
$transient->response[ $plugin ]->new_version = $response->new_version;
$transient->response[ $plugin ]->url = $response->homepage;
$transient->response[ $plugin ]->package = $response->package;
if ( empty( $response->package ) ) {
$transient->response[ $plugin ]->upgrade_notice = FLUpdater::get_update_error_message();
}
}
}
else if($this->settings['type'] == 'theme') {
if(version_compare($response->new_version, $this->settings['version'], '>')) {
$transient->response[$this->settings['slug']] = array(
'new_version' => $response->new_version,
'url' => $response->homepage,
'package' => $response->package
);
}
}
}
return $transient;
}
/**
* Retrives the data for the plugin info lightbox.
*
* @since 1.0
* @param bool $false
* @param string $action
* @param object $args
* @return object|bool
*/
public function plugin_info($false, $action, $args)
{
if ( 'plugin_information' != $action ) {
return $false;
}
if(!isset($args->slug) || $args->slug != $this->settings['slug']) {
return $false;
}
$response = $this->get_response();
if( ! isset( $response->error ) ) {
$info = new stdClass();
$info->name = $this->settings['name'];
$info->version = $response->new_version;
$info->slug = $response->slug;
$info->plugin_name = $response->plugin_name;
$info->author = $response->author;
$info->homepage = $response->homepage;
$info->requires = $response->requires;
$info->tested = $response->tested;
$info->last_updated = $response->last_updated;
$info->download_link = $response->package;
$info->sections = (array)$response->sections;
return $info;
}
return $false;
}
/**
* Shows an update message on the plugins page if an update
* is available but there is no active subscription.
*
* @since 1.0
* @param array $plugin_data An array of data for this plugin.
* @param object $response An object with update data for this plugin.
* @return void
*/
public function update_message( $plugin_data, $response )
{
if ( empty( $response->package ) ) {
echo FLUpdater::get_update_error_message( $plugin_data );
}
}
/**
* Static method for initializing an instance of the updater
* for each active product.
*
* @since 1.0
* @return void
*/
static public function init()
{
include FL_UPDATER_DIR . 'includes/config.php';
foreach($config as $path) {
if(file_exists($path)) {
require_once $path;
}
}
}
/**
* Static method for adding a product to the updater and
* creating the new instance.
*
* @since 1.0
* @param array $args An array of settings for the product.
* @return void
*/
static public function add_product($args = array())
{
if(is_array($args) && isset($args['slug'])) {
if($args['type'] == 'plugin') {
if(file_exists(WP_CONTENT_DIR . '/plugins/' . $args['slug'])) {
self::$_products[$args['name']] = $args;
new FLUpdater(self::$_products[$args['name']]);
}
}
if($args['type'] == 'theme') {
if(file_exists(WP_CONTENT_DIR . '/themes/' . $args['slug'])) {
self::$_products[$args['name']] = $args;
new FLUpdater(self::$_products[$args['name']]);
}
}
}
}
/**
* Static method for rendering the license form.
*
* @since 1.0
* @return void
*/
static public function render_form()
{
// Activate a subscription?
if(isset($_POST['fl-updater-nonce'])) {
if(wp_verify_nonce($_POST['fl-updater-nonce'], 'updater-nonce')) {
self::save_subscription_license($_POST['license']);
}
}
$license = self::get_subscription_license();
$subscription = self::get_subscription_info();
// Include the form ui.
include FL_UPDATER_DIR . 'includes/form.php';
}
/**
* Static method for getting the subscription license key.
*
* @since 1.0
* @return string
*/
static public function get_subscription_license()
{
$value = get_site_option('fl_themes_subscription_email');
return $value ? $value : '';
}
/**
* Static method for updating the subscription license.
*
* @since 1.0
* @param string $license The new license key.
* @return void
*/
static public function save_subscription_license($license)
{
FLUpdater::api_request(self::$_updates_api_url, array(
'fl-api-method' => 'activate_domain',
'license' => $license,
'domain' => network_home_url(),
'products' => json_encode( self::$_products )
));
update_site_option('fl_themes_subscription_email', $license);
}
/**
* Static method for retrieving the subscription info.
*
* @since 1.0
* @return bool
*/
static public function get_subscription_info()
{
return self::api_request(self::$_updates_api_url, array(
'fl-api-method' => 'subscription_info',
'domain' => network_home_url(),
'license' => FLUpdater::get_subscription_license()
));
}
/**
* Returns an update message for if an update
* is available but there is no active subscription.
*
* @since 1.6.4.3
* @param array $plugin_data An array of data for this plugin.
* @return string
*/
static private function get_update_error_message( $plugin_data = null )
{
$message = '';
$message .= '<p style="padding:10px 20px; margin-top: 10px; background: #d54e21; color: #fff;">';
$message .= __( '<strong>UPDATE UNAVAILABLE!</strong>', 'fl-builder' );
$message .= ' ';
$message .= __('Please subscribe to enable automatic updates for this plugin.', 'fl-builder');
if ( $plugin_data && isset( $plugin_data['PluginURI'] ) ) {
$message .= ' <a href="' . $plugin_data['PluginURI'] . '" target="_blank" style="color: #fff; text-decoration: underline;">';
$message .= __('Subscribe Now', 'fl-builder');
$message .= ' »</a>';
}
$message .= '</p>';
return $message;
}
/**
* Static method for retrieving the plugin file path for a
* product relative to the plugins directory.
*
* @since 1.0
* @access private
* @param string $slug The product slug.
* @return string
*/
static private function get_plugin_file( $slug )
{
if ( 'bb-plugin' == $slug ) {
$file = $slug . '/fl-builder.php';
}
else {
$file = $slug . '/' . $slug . '.php';
}
return $file;
}
/**
* Static method for sending a request to the store
* or update API.
*
* @since 1.0
* @access private
* @param string $api_url The API URL to use.
* @param array $args An array of args to send along with the request.
* @return mixed The response or false if there is an error.
*/
static private function api_request($api_url = false, $args = array())
{
if($api_url) {
$params = array();
foreach($args as $key => $val) {
$params[] = $key . '=' . urlencode($val);
}
return self::remote_get($api_url . '?' . implode('&', $params));
}
return false;
}
/**
* Get a remote response.
*
* @since 1.0
* @access private
* @param string $url The URL to get.
* @return mixed The response or false if there is an error.
*/
static private function remote_get($url)
{
$request = wp_remote_get($url);
$error = new stdClass();
$error->error = 'connection';
if(is_wp_error($request)) {
return $error;
}
if(wp_remote_retrieve_response_code($request) != 200) {
return $error;
}
$body = wp_remote_retrieve_body($request);
if(is_wp_error($body)) {
return $error;
}
$body_decoded = json_decode($body);
if(!is_object($body_decoded)) {
return $error;
}
return $body_decoded;
}
}