forked from WooRockets/WR-PageBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addons.php
439 lines (370 loc) · 13.7 KB
/
addons.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
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
436
437
438
439
<?php
/**
* @version $Id$
* @package WR_Library
* @author WooRockets Team <support@woorockets.com>
* @copyright Copyright (C) 2014 WooRockets.com. All Rights Reserved.
* @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
*
* Websites: http://www.woorockets.com
*/
if ( ! class_exists( 'WR_Pb_Product_Addons' ) ) :
/**
* Class that provides support for addons installation and management.
*
* @package WR_Library
* @since 1.0.0
*/
class WR_Pb_Product_Addons extends WR_Pb_Product_Info {
/**
* Define valid addon manipulation actions and required capabilities.
*
* @var array
*/
protected static $actions = array(
'authenticate' => '',
'install' => 'install_plugins',
'update' => 'install_plugins',
'uninstall' => 'delete_plugins',
);
/**
* Hook into WordPress.
*
* @return void
*/
public static function hook() {
// Register Ajax action
static $registered;
if ( ! isset( $registered ) ) {
add_action( 'wp_ajax_wr-addons-management', array( __CLASS__, 'manage' ) );
$registered = true;
}
}
/**
* Render addons installation and management screen.
*
* @param string $plugin_file Either absolute path to plugin main file or plugin's identified name defined in WooRockets server.
*
* @return void
*/
public static function init( $plugin_file ) {
// Hook into WordPress
self::hook();
// Get template path
if ( $tmpl = WR_Pb_Loader::get_path( 'product/tmpl/addons.php' ) ) {
// Get product information
$plugin = self::get( $plugin_file );
if ( ! $plugin ) {
die( __( 'Cannot get addons information for current product.', WR_LIBRARY_TEXTDOMAIN ) );
}
// Check if user has customer account saved
$customer_account = get_option( 'wr_customer_account', null );
$has_customer_account = ( is_array( $customer_account ) && ! @empty( $customer_account['username'] ) && ! @empty( $customer_account['password'] ) );
// Load template
include_once $tmpl;
}
}
/**
* Handle addons management actions.
*
* @return void
*/
public static function manage() {
// Try to verify input data then execute action
try {
// Get addon to be manipulated
$addon = isset( $_GET['addon'] ) ? $_GET['addon'] : null;
if ( empty( $addon ) ) {
throw new Exception( __( 'Missing addon to be installed.', WR_LIBRARY_TEXTDOMAIN ) );
}
// Get management action
$action = isset( $_GET['do'] ) ? $_GET['do'] : null;
if ( empty( $action ) || ! array_key_exists( $action, self::$actions ) ) {
throw new Exception( __( 'The requested action is invalid.', WR_LIBRARY_TEXTDOMAIN ) );
}
// Execute requested action for provided addon
$result = self::execute( $addon, $action );
// Send response back
echo json_encode( $result );
// Exit immediately to prevent WordPress from processing further
exit;
} catch ( Exception $e ) {
die(
json_encode(
array(
'success' => false,
'addon' => $addon,
'action' => $action,
'message' => $e->getMessage(),
)
)
);
}
}
/**
* Manipulate addon.
*
* @param string $addon Addon to manipulate.
* @param string $action Action to execute.
*
* @return array
*/
protected static function execute( $addon, $action ) {
// Check capabilities
foreach ( self::$actions as $do => $capability ) {
if ( $action == $do && ! empty( $capability ) && ! current_user_can( $capability ) ) {
throw new Exception( __( 'You do not have sufficient permissions to either add or delete plugins for this site.' ) );
}
}
// Check if addon should be updated or removed
if ( 'update' == $action || 'uninstall' == $action ) {
// Get plugin slug
$plugin = self::check( $addon, false );
if ( empty( $plugin ) ) {
throw new Exception(
__( 'update' == $action ? 'Cannot detect plugin to be updated.' : 'Cannot detect plugin to be removed.', WR_LIBRARY_TEXTDOMAIN )
);
}
}
// Check if addon should be removed
if ( 'uninstall' == $action ) {
$result = delete_plugins( array( $plugin ) );
// Verify uninstallation result
if ( is_wp_error( $result ) ) {
throw new Exception( $result->get_error_message() );
}
} else {
// Verify authentication data
$authentication = (boolean) $_GET['authentication'];
$username = isset( $_POST['username'] ) ? $_POST['username'] : '';
$password = isset( $_POST['password'] ) ? $_POST['password'] : '';
if ( $authentication && ( empty( $username ) || empty( $password ) ) ) {
// Check if user has customer account saved
$customer_account = get_option( 'wr_customer_account', null );
if ( is_array( $customer_account ) && ! @empty( $customer_account['username'] ) && ! @empty( $customer_account['password'] ) ) {
$username = $customer_account['username'];
$password = $customer_account['password'];
} else {
throw new Exception( null );
}
}
// Try to authenticate or download addon installation package
try {
$package = self::download( $addon, $authentication, $username, $password, 'authenticate' == $action );
} catch ( Exception $e ) {
throw $e;
}
// Get WordPress's WordPress Filesystem Abstraction object
$wp_filesystem = WR_Pb_Init_File_System::get_instance();
// Check if addon should be installed or updated
if ( 'authenticate' != $action ) {
// Verify core and add-on compatibility
if ( isset( $_GET['core'] ) && ( $core = self::get( $_GET['core'] ) ) ) {
// Extract downloaded add-on package
$tmp_dir = substr( $package, 0, -4 );
$result = unzip_file( $package, $tmp_dir );
if ( is_wp_error( $result ) ) {
throw new Exception( $result->get_error_message() );
}
// Find constant definition file
if ( @is_file( "{$tmp_dir}/defines.php" ) ) {
include "{$tmp_dir}/defines.php";
} elseif ( count( $defines = glob( "{$tmp_dir}/*/defines.php" ) ) ) {
include current( $defines );
}
// Get minimum core version required for this add-on
if ( defined( $core_version = strtoupper( $addon ) . '_CORE_VERSION' ) ) {
eval( '$core_version = ' . $core_version . ';' );
}
if ( $core_version && version_compare( $core_version, $core['Version'], '>' ) ) {
// Delete downloaded add-on package and clean-up temporary directory
$wp_filesystem->delete( $package );
$wp_filesystem->delete( $tmp_dir, true );
// Skip add-on installation
throw new Exception(
sprintf(
__( "Cannot install %1\$s v%2\$s.\nThis version requires %3\$s v%4\$s while you are using %5\$s v%6\$s.", WR_LIBRARY_TEXTDOMAIN ),
$core['Addons'][ $addon ]->name,
$core['Addons'][ $addon ]->version,
$core['Name'],
$core_version,
$core['Name'],
$core['Version']
)
);
}
// Verification done, clean-up temporary directory
$wp_filesystem->delete( $tmp_dir, true );
}
// Init WordPress Plugin Upgrader
class_exists( 'Plugin_Upgrader' ) || include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
function_exists( 'screen_icon' ) || include_once ABSPATH . 'wp-admin/includes/screen.php';
function_exists( 'show_message' ) || include_once ABSPATH . 'wp-admin/includes/misc.php';
function_exists( 'get_plugin_data' ) || include_once ABSPATH . 'wp-admin/includes/plugin.php';
// Either install or update add-on
$upgrader = new Plugin_Upgrader();
if ( 'install' == $action ) {
// Install plugin
$result = $upgrader->install( $package );
// Verify installation result
if ( is_wp_error( $result ) ) {
throw new Exception( $result->get_error_message() );
}
} else {
// Update plugin
add_filter( 'upgrader_pre_install', array( $upgrader, 'deactivate_plugin_before_upgrade' ), 10, 2 );
add_filter( 'upgrader_clear_destination', array( $upgrader, 'delete_old_plugin' ), 10, 4 );
$upgrader->run(
array(
'package' => $package,
'destination' => WP_PLUGIN_DIR,
'clear_destination' => true,
'clear_working' => true,
'hook_extra' => array(
'plugin' => $plugin,
'type' => 'plugin',
'action' => 'update',
),
)
);
remove_filter( 'upgrader_pre_install', array( $upgrader, 'deactivate_plugin_before_upgrade' ) );
remove_filter( 'upgrader_clear_destination', array( $upgrader, 'delete_old_plugin' ) );
if ( is_wp_error( $upgrader->result ) ) {
throw new Exception( $upgrader->result->get_error_message() );
}
// Force refresh of plugin update information
if ( function_exists( 'wp_clean_plugins_cache' ) ) {
wp_clean_plugins_cache( true );
}
}
// Try to activate plugin
try {
$result = self::activate( $addon, $action );
} catch ( Exception $e ) {
throw $e;
}
// Remove downloaded add-on installation package
$wp_filesystem->delete( $package );
} else {
// Check if user want to save customer account
if ( isset( $_POST['remember'] ) && (boolean) $_POST['remember'] ) {
update_option( 'wr_customer_account', array( 'username' => $username, 'password' => $password ) );
}
}
}
// Return data
return is_array( $result ) ? $result : array(
'success' => true,
'addon' => $addon,
'action' => $action,
'message' => ( 'authenticate' == $action ) ? json_decode( $wp_filesystem->get_contents( $package ) ) : null,
);
}
/**
* Method to download add-on package.
*
* @param string $addon Identified name of add-on (as defined in WooRockets server).
* @param boolean $authentication Whether authentication is required to be able to download add-on package?
* @param string $username Username for authentication (only need if authentication is required).
* @param string $password Password for authentication (only need if authentication is required).
* @param boolean $authenticate_only Whether to authenticate or download add-on installation package?
*
* @return string Path to downloaded package.
*/
protected static function download( $addon, $authentication = false, $username = null, $password = null, $authenticate_only = false ) {
// Get WordPress's WordPress Filesystem Abstraction object
$wp_filesystem = WR_Pb_Init_File_System::get_instance();
// Build query string
$query[] = 'identified_name=' . $addon;
if ( $authentication ) {
$query[] = 'username=' . urlencode( $username );
$query[] = 'password=' . urlencode( $password );
$query[] = 'product_attr={%22authentication%22:%22true%22}';
} else {
$query[] = 'product_attr={%22authentication%22:%22false%22}';
}
if ( $authenticate_only ) {
$query[] = 'upgrade=no';
} else {
$query[] = 'upgrade=yes';
$query[] = 'file_attr={%22type%22:%22install%22}';
}
// Build final URL for downloading update package
$url = self::$product_download . '&' . implode( '&', $query );
// Download update package
$target = wp_upload_dir();
$target = $target['basedir'] . '/' . $addon . '_install.zip';
$result = download_url( $url );
if ( is_wp_error( $result ) ) {
throw new Exception( $result->get_error_message() );
}
if ( $wp_filesystem->size( $result ) < 10 ) {
$content = $wp_filesystem->get_contents( $result );
switch ( $content ) {
case 'ERR00':
throw new Exception( __( 'Invalid Parameters! Can not verify your product information.', WR_LIBRARY_TEXTDOMAIN ) );
break;
case 'ERR01':
throw new Exception( __( 'Invalid username or password.', WR_LIBRARY_TEXTDOMAIN ) );
break;
case 'ERR02':
throw new Exception( __( 'We could not find the product in your order list. Seems like you did not purchase it yet.', WR_LIBRARY_TEXTDOMAIN ) );
break;
case 'ERR03':
throw new Exception( __( 'Requested file is not found on server.', WR_LIBRARY_TEXTDOMAIN ) );
break;
default:
if ( 'true' != $content && 'yes' != $content && '1' != $content ) {
throw new Exception( __( 'Cannot connect to WooRockets.com server.', WR_LIBRARY_TEXTDOMAIN ) );
}
break;
}
}
// Create a local file by copying temporary file
if ( ! $authenticate_only ) {
if ( ! $wp_filesystem->copy( $result, $target, true, FS_CHMOD_FILE ) ) {
// If copy failed, chmod file to 0644 and try again
$wp_filesystem->chmod( $target, 0644 );
if ( ! $wp_filesystem->copy( $result, $target, true, FS_CHMOD_FILE ) ) {
throw new Exception( __( 'Cannot store add-on package to local file system.', WR_LIBRARY_TEXTDOMAIN ) );
}
}
}
// Remove temporary file
$wp_filesystem->delete( $result );
// Finished
return $target;
}
/**
* Detect plugin file of installed add-on and activate plugin.
*
* @param string $addon Identified name of add-on (as defined in WooRockets server).
* @param string $action Last executed action.
*
* return void
*/
protected static function activate( $addon, $action = '' ) {
// Check capabilities
if ( ! current_user_can( 'activate_plugins' ) ) {
throw new Exception( __( 'You do not have sufficient permissions to activate plugins for this site.' ) );
}
// Get WordPress's WordPress Filesystem Abstraction object
$wp_filesystem = WR_Pb_Init_File_System::get_instance();
// Get plugin slug
$plugin = self::check( $addon, false );
if ( empty( $plugin ) ) {
throw new Exception( __( 'Cannot detect plugin to activate.', WR_LIBRARY_TEXTDOMAIN ) );
}
// Activate plugin
$result = activate_plugin( $plugin, '', is_network_admin() );
if ( is_wp_error( $result ) ) {
return array(
'success' => true,
'addon' => $addon,
'action' => $action,
'message' => $result->get_error_message(),
);
}
}
}
endif;