PluginProbe
The WP Remote WordPress Plugin / 6.48
The WP Remote WordPress Plugin v6.48
6.72 6.69 6.65 6.62 6.48 6.47 4.87 4.97 5.05 5.09 5.16 5.22 5.24 5.25 5.38 5.41 5.42 5.45 5.47 5.53 5.56 5.65 5.68 5.72 5.73 All 53 releases
wpremote / callback / wings / manage.php

manage.php in The WP Remote WordPress Plugin 6.48, at callback/wings/manage.php

1,099 lines 35.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) exit;
4 if (!class_exists('WPRManageCallback')) :
5 class WPRManageCallback extends WPRCallbackBase {
6 public $settings;
7 public $skin;
8 public $bvinfo;
9
10 const MANAGE_WING_VERSION = 2.0;
11
12 public function __construct($callback_handler) {
13 $this->settings = $callback_handler->settings;
14 $this->bvinfo = new WPRInfo($this->settings);
15 }
16
17 function getError($err) {
18 return $this->objectToArray($err);
19 }
20
21 function is_pantheon() {
22 return (!empty($_ENV['PANTHEON_ENVIRONMENT']) && $_ENV['PANTHEON_ENVIRONMENT'] !== 'dev');
23 }
24
25 function isServerWritable() {
26 if ($this->is_pantheon()) {
27 return false;
28 }
29
30 if ((!defined('FTP_HOST') || !defined('FTP_USER')) && (get_filesystem_method(array(), false) != 'direct')) {
31 return false;
32 } else {
33 return true;
34 }
35 }
36
37 /**
38 * Load a file with require_once only if it exists, so missing files don't fatal the script.
39 */
40 function safe_require_once($path) {
41 if ($path && file_exists($path)) {
42 require_once $path;
43 }
44 }
45
46 function include_files() {
47 $this->safe_require_once(ABSPATH.'wp-admin/includes/file.php');
48 $this->safe_require_once(ABSPATH.'wp-admin/includes/plugin.php');
49 $this->safe_require_once(ABSPATH.'wp-admin/includes/theme.php');
50 $this->safe_require_once(ABSPATH.'wp-admin/includes/misc.php');
51 $this->safe_require_once(ABSPATH.'wp-admin/includes/template.php');
52 $this->safe_require_once(ABSPATH.'wp-includes/pluggable.php');
53 $this->safe_require_once(ABSPATH.'wp-admin/includes/class-wp-upgrader.php');
54 $this->safe_require_once(ABSPATH.'wp-admin/includes/class-theme-upgrader.php');
55 $this->safe_require_once(ABSPATH.'wp-admin/includes/class-plugin-upgrader.php');
56 $this->safe_require_once(ABSPATH.'wp-admin/includes/user.php');
57 $this->safe_require_once(ABSPATH.'wp-admin/includes/upgrade.php');
58 $this->safe_require_once(ABSPATH.'wp-admin/includes/update.php');
59 }
60
61 function edit($args) {
62 $result = array();
63 if ($args['type'] == 'plugins') {
64 $result['plugins'] = $this->editPlugins($args);
65 } elseif ($args['type'] == 'themes') {
66 $result['themes'] = $this->editThemes($args);
67 } elseif ($args['type'] == 'users') {
68 $result['users'] = $this->editWpusers($args);
69 }
70 return $result;
71 }
72
73 function editPlugins($args) {
74 $result = array();
75 $plugins = $args['items'];
76 foreach ($plugins as $plugin) {
77 if (array_key_exists('network', $plugin)) {
78 $networkwide = $plugin['network'];
79 } else {
80 $networkwide = false;
81 }
82 switch ($args['action']) {
83 case 'activate':
84 $res = activate_plugin($plugin['file'], '', $networkwide);
85 break;
86 case 'deactivate':
87 $res = deactivate_plugins(array($plugin['file']), false, $networkwide);
88 break;
89 case 'delete':
90 $res = delete_plugins(array($plugin['file']));
91 break;
92 case 'deactivate_delete':
93 $res = deactivate_plugins(array($plugin['file']), false, $networkwide);
94 if ($res || is_wp_error($res))
95 break;
96 $res = delete_plugins(array($plugin['file']));
97 default:
98 break;
99 }
100 if (is_wp_error($res)) {
101 $res = array('status' => "Error", 'message' => $res->get_error_message());
102 } elseif ($res === false) {
103 $res = array('status' => "Error", 'message' => "Failed to perform action.");
104 } else {
105 $res = array('status' => "Done");
106 }
107 $result[$plugin['file']] = $res;
108 }
109 return $result;
110 }
111
112 function editThemes($args) {
113 $result = array();
114 $themes = $args['items'];
115 foreach ($themes as $theme) {
116 switch ($args['action']) {
117 case 'activate':
118 $res = switch_theme($theme['template'], $theme['stylesheet']);
119 break;
120 case 'delete':
121 $res = delete_theme($theme['stylesheet']);
122 break;
123 default:
124 break;
125 }
126
127 if (is_wp_error($res)) {
128 $res = array('status' => "Error", 'message' => $res->get_error_message());
129 } elseif ($res === false) {
130 $res = array('status' => "Error", 'message' => "Failed to perform action.");
131 } else {
132 $res = array( 'status' => "Done");
133 }
134 $result[$theme['template']] = $res;
135 }
136 return $result;
137 }
138
139 function editWpusers($args) {
140 $result = array();
141 $items = $args['items'];
142 foreach ($items as $item) {
143 $res = array();
144 $user = get_user_by('id', $item['id']);
145 if ($user) {
146 switch ($args['action']) {
147 case 'changerole':
148 $data = array();
149 $data['role'] = $item['newrole'];
150 $data['ID'] = $user->ID;
151 $res = wp_update_user($data);
152 break;
153 case 'changepass':
154 $data = array();
155 $data['user_pass'] = $item['newpass'];
156 $data['ID'] = $user->ID;
157 $res = wp_update_user($data);
158 break;
159 case 'delete':
160 if (array_key_exists('reassign', $args)) {
161 $user_to = get_user_by('id', $args['reassign']);
162 if ($user_to != false) {
163 $res = wp_delete_user($user->ID, $user_to->ID);
164 } else {
165 $res = array('status' => "Error", 'message' => 'Reassigned user doesnot exists');
166 }
167 } else {
168 $res = wp_delete_user($user->ID);
169 }
170 break;
171 }
172 if (is_wp_error($res)) {
173 $res = array('status' => "Error", 'message' => $res->get_error_message());
174 } else {
175 $res = array( 'status' => "Done");
176 }
177 } else {
178 $res = array('status' => "Error", 'message' => "Unable to find user");
179 }
180 $result[$item['id']] = $res;
181 }
182 return $result;
183 }
184
185 function addUser($args) {
186 if (username_exists($args['user_login'])) {
187 return array('status' => "Error", 'message' => "Username already exists");
188 }
189 if (email_exists($args['user_email'])) {
190 return array('status' => "Error", 'message' => "Email already exists");
191 }
192 $result = wp_insert_user($args);
193 if ( !is_wp_error( $result ) ) {
194 return array('status' => "Done", 'user_id' => $result);
195 } else {
196 return array('status' => "Error", 'message' => $this->getError($result));
197 }
198 }
199
200 function upgrade($params = null, $has_bv_skin = false, $bv_bulk_upgrade = false) {
201 $result = array();
202 $premium_upgrades = array();
203 if (array_key_exists('clear_filters', $params)) {
204 $filters = $params['clear_filters'];
205 foreach ($filters as $filter)
206 remove_all_filters($filter);
207 }
208 if (array_key_exists('core', $params) && !empty($params['core'])) {
209 $result['core'] = $this->upgradeCore($params['core']);
210 }
211 if (array_key_exists('translations', $params) && !empty($params['translations'])) {
212 $result['translations'] = $this->upgradeTranslations($params['translations'], $has_bv_skin);
213 }
214 if (array_key_exists('plugins', $params) && !empty($params['plugins'])) {
215 $result['plugins'] = $this->upgradePlugins($params['plugins'], $has_bv_skin, $bv_bulk_upgrade);
216 }
217 if (array_key_exists('themes', $params) && !empty($params['themes'])) {
218 $result['themes'] = $this->upgradeThemes($params['themes'], $has_bv_skin, $bv_bulk_upgrade);
219 }
220 return $result;
221 }
222
223 function get_translation_updates() {
224 $updates = array();
225 $transients = array( 'update_core' => 'core', 'update_plugins' => 'plugin', 'update_themes' => 'theme' );
226 foreach ( $transients as $transient => $type ) {
227 $transient = $this->settings->getTransient( $transient );
228 if ( empty( $transient->translations ) )
229 continue;
230
231 foreach ( $transient->translations as $translation ) {
232 $updates[] = (object) $translation;
233 }
234 }
235 return $updates;
236 }
237
238 function upgradeTranslations($translations, $has_bv_skin = false) {
239 $language_updates = $this->get_translation_updates();
240 $valid_updates = array();
241 $result = array();
242 if (!empty($language_updates)) {
243 foreach($language_updates as $update) {
244 if ($update && in_array($update->package, $translations)) {
245 $valid_updates[] = $update;
246 }
247 }
248 }
249 if (!empty($valid_updates)) {
250 if (class_exists('Language_Pack_Upgrader')) {
251 if ($has_bv_skin) {
252 require_once( "bv_upgrader_skin.php" );
253 $skin = new WPRUpgraderSkin("upgrade_translations");
254 $this->skin = $skin;
255 } else {
256 $skin = new Language_Pack_Upgrader_Skin(array());
257 }
258 $upgrader = new Language_Pack_Upgrader($skin);
259 $result = $upgrader->bulk_upgrade($valid_updates);
260 if (is_array($result) && !empty($result)) {
261 foreach ($result as $translate_tmp => $translate_info) {
262 if (is_wp_error($translate_info) || empty($translate_info)) {
263 $error = (!empty($translate_info)) ? is_wp_error($translate_info) : "Upgrade failed";
264 return array('status' => "Error", 'message' => $error);
265 }
266 }
267 }
268 return array('status' => "Done");
269 }
270 }
271 return array('status' => "Error", 'message' => "Upgrade failed");
272 }
273
274 function upgradeCore($args) {
275 global $wp_filesystem, $wp_version;
276 $to_update = null;
277
278 // Prefer validated update data from server (BlogVault), then site transient.
279 // This protects upgrades from poisoned/overridden transients.
280 $validated = null;
281 if (isset($args['core_validated_update']) && !empty($args['core_validated_update'])) {
282 $validated = $args['core_validated_update'];
283 }
284
285 if (!empty($validated) && is_array($validated) &&
286 isset($validated['version']) && !empty($validated['version']) &&
287 isset($validated['response']) && ($validated['response'] === 'upgrade')) {
288 $to_update = new stdClass();
289 $to_update->version = $validated['version'];
290 $to_update->response = $validated['response'];
291 $to_update->download = isset($validated['download']) ? $validated['download'] : '';
292
293 // Preserve all packages keys if provided; Core_Upgrader expects packages-like data.
294 if (isset($validated['packages']) && is_array($validated['packages'])) {
295 $to_update->packages = (object)$validated['packages'];
296 } else {
297 $to_update->packages = new stdClass();
298 }
299
300 // WordPress uses either ->package or ->packages->full depending on path/version.
301 if (isset($to_update->packages->full) && !empty($to_update->packages->full)) {
302 $to_update->package = $to_update->packages->full;
303 } else {
304 $to_update->package = $to_update->download;
305 $to_update->packages->full = $to_update->download;
306 }
307 } else {
308 // Fallback to transient-based approach
309 $core = $this->settings->getTransient('update_core');
310 $core_update_index = intval($args['coreupdateindex']);
311 if (isset($core->updates) && !empty($core->updates)) {
312 $to_update = $core->updates[$core_update_index];
313 } else {
314 return array('status' => "Error", "message" => "Updates not available");
315 }
316 }
317
318 if (!$to_update) {
319 return array('status' => "Error", "message" => "Updates not available");
320 }
321
322 $resp = array("Core_Upgrader", class_exists('Core_Upgrader'));
323 if (version_compare($wp_version, '3.1.9', '>')) {
324 $core = new Core_Upgrader();
325 $result = $core->upgrade($to_update);
326 if (is_wp_error($result)) {
327 return array('status' => "Error", "message" => $this->getError($result));
328 } else {
329 return array('status' => 'Done');
330 }
331 } else {
332 $resp = array("WP_Upgrader", class_exists('WP_Upgrader'));
333 if (class_exists('WP_Upgrader')) {
334 $upgrader = new WP_Upgrader();
335
336 $res = $upgrader->fs_connect(
337 array(
338 ABSPATH,
339 WP_CONTENT_DIR,
340 )
341 );
342 if (is_wp_error($res)) {
343 return array('status' => "Error", "message" => $this->getError($res));
344 }
345
346 $wp_dir = trailingslashit($wp_filesystem->abspath());
347
348 $core_package = false;
349 if (isset($to_update->package) && !empty($to_update->package)) {
350 $core_package = $to_update->package;
351 } elseif (isset($to_update->packages->full) && !empty($to_update->packages->full)) {
352 $core_package = $to_update->packages->full;
353 }
354
355 $download = $upgrader->download_package($core_package);
356 if (is_wp_error($download)) {
357 return array('status' => "Error", "message" => $this->getError($download));
358 }
359 $working_dir = $upgrader->unpack_package($download);
360 if (is_wp_error($working_dir)) {
361 return array('status' => "Error", "message" => $this->getError($working_dir));
362 }
363
364 if (!$wp_filesystem->copy($working_dir.'/wordpress/wp-admin/includes/update-core.php', $wp_dir.'wp-admin/includes/update-core.php', true)) {
365 $wp_filesystem->delete($working_dir, true);
366 return array('status' => "Error", "message" => "Unable to move files.");
367 }
368
369 $wp_filesystem->chmod($wp_dir.'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
370
371 $this->safe_require_once(ABSPATH.'wp-admin/includes/update-core.php');
372 $result = update_core($working_dir, $wp_dir);
373
374 if (is_wp_error($result)) {
375 return array('status' => "Error", "message" => $this->getError($result));
376 }
377 return array('status' => 'Done');
378 }
379 }
380 }
381
382 function bv_plugin_bulk_upgrade($upgrader, $_plugins) {
383 global $wp_version;
384 $plugins = array_keys($_plugins);
385 $current = get_site_transient('update_plugins');
386 $args = array();
387 $defaults = array(
388 'clear_update_cache' => true,
389 );
390 $parsed_args = wp_parse_args($args, $defaults);
391 $upgrader->init();
392 $upgrader->bulk = true;
393 $upgrader->upgrade_strings();
394 add_filter('upgrader_clear_destination', array($upgrader, 'delete_old_plugin'), 10, 4);
395 $upgrader->skin->header();
396 $res = $upgrader->fs_connect(array(WP_CONTENT_DIR, WP_PLUGIN_DIR));
397 if (!$res) {
398 $upgrader->skin->footer();
399 return false;
400 }
401 $upgrader->skin->bulk_header();
402 $maintenance = (is_multisite() && ! empty($plugins));
403 foreach ($plugins as $plugin) {
404 $maintenance = $maintenance || (is_plugin_active($plugin));
405 }
406 if ($maintenance) {
407 $upgrader->maintenance_mode(true);
408 }
409 $results = array();
410 $upgrader->update_count = count($plugins);
411 $upgrader->update_current = 0;
412 foreach($plugins as $plugin) {
413 $upgrader->update_current++;
414 $upgrader->skin->plugin_info = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin, false, true);
415 $upgrader->skin->plugin_active = is_plugin_active($plugin);
416 $plugin_upgrade_data = $_plugins[$plugin];
417 if (isset($current->response[$plugin])) {
418 if (isset($current->response[$plugin]->requires)) {
419 $plugin_upgrade_data['requires'] = $current->response[$plugin]->requires;
420 }
421 if (isset($current->response[$plugin]->requires_php)) {
422 $plugin_upgrade_data['requires_php'] = $current->response[$plugin]->requires_php;
423 }
424 }
425 if ( isset( $plugin_upgrade_data['requires'] ) && function_exists('is_wp_version_compatible') && !is_wp_version_compatible( $plugin_upgrade_data['requires'] ) ) {
426 $result = new WP_Error(
427 'incompatible_wp_required_version',
428 sprintf(
429 __( 'Your WordPress version is %1$s, however the new plugin version requires %2$s.' ),
430 $wp_version,
431 $plugin_upgrade_data['requires']
432 )
433 );
434
435 $upgrader->skin->before( $result );
436 $upgrader->skin->error( $result );
437 $upgrader->skin->after();
438 } elseif ( isset( $plugin_upgrade_data['requires_php'] ) && function_exists('is_php_version_compatible') && !is_php_version_compatible( $plugin_upgrade_data['requires_php'] ) ) {
439
440 $result = new WP_Error(
441 'incompatible_php_required_version',
442 sprintf(
443 __( 'The PHP version on your server is %1$s, however the new plugin version requires %2$s.' ),
444 PHP_VERSION,
445 $plugin_upgrade_data['requires_php']
446 )
447 );
448
449 $upgrader->skin->before( $result );
450 $upgrader->skin->error( $result );
451 $upgrader->skin->after();
452 } else {
453 add_filter('upgrader_source_selection', array($upgrader, 'check_package'));
454 $result = $upgrader->run(
455 array(
456 'package' => $_plugins[$plugin]['package'],
457 'destination' => WP_PLUGIN_DIR,
458 'clear_destination' => true,
459 'clear_working' => true,
460 'is_multi' => true,
461 'hook_extra' => array(
462 'plugin' => $plugin,
463 ),
464 )
465 );
466 remove_filter('upgrader_source_selection', array($upgrader, 'check_package'));
467 }
468 $results[$plugin] = $result;
469 if (false === $result) {
470 break;
471 }
472 }
473 $upgrader->maintenance_mode(false);
474 wp_clean_plugins_cache($parsed_args['clear_update_cache']);
475 do_action(
476 'upgrader_process_complete',
477 $upgrader,
478 array(
479 'action' => 'update',
480 'type' => 'plugin',
481 'bulk' => true,
482 'plugins' => $plugins,
483 )
484 );
485 $upgrader->skin->bulk_footer();
486 $upgrader->skin->footer();
487 remove_filter('upgrader_clear_destination', array($upgrader, 'delete_old_plugin'));
488 $past_failure_emails = get_option('auto_plugin_theme_update_emails', array());
489 foreach ($results as $plugin => $result) {
490 if (!$result || is_wp_error($result) || !isset($past_failure_emails[$plugin])) {
491 continue;
492 }
493 unset($past_failure_emails[$plugin]);
494 }
495 update_option('auto_plugin_theme_update_emails', $past_failure_emails);
496 return $results;
497 }
498
499 function upgradePlugins($plugins, $has_bv_skin = false, $bv_bulk_upgrade = false) {
500 $result = array();
501 $_plugins = array();
502 $plugins_by_name = array();
503 foreach ($plugins as $plugin) {
504 $_plugins[$plugin['file']] = [
505 'package' => $plugin['package']
506 ];
507 if (isset($plugin['requires'])) {
508 $_plugins[$plugin['file']]['requires'] = $plugin['requires'];
509 }
510
511 if (isset($plugin['requires_php'])) {
512 $_plugins[$plugin['file']]['requires_php'] = $plugin['requires_php'];
513 }
514 $plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin['file'], false, true);
515 $plugins_by_name[$plugin_data['Name']] = $plugin['file'];
516 }
517 if (empty(array_keys($_plugins))) {
518 return $result;
519 }
520 if (class_exists('Plugin_Upgrader')) {
521 if ($has_bv_skin) {
522 require_once( "bv_upgrader_skin.php" );
523 $skin = new WPRUpgraderSkin("plugin_upgrade", $plugins_by_name);
524 $this->skin = $skin;
525 } else {
526 $skin = new Bulk_Plugin_Upgrader_Skin();
527 }
528 $upgrader = new Plugin_Upgrader($skin);
529
530 if ($bv_bulk_upgrade) {
531 $result = $this->bv_plugin_bulk_upgrade($upgrader, $_plugins);
532 } else {
533 $result = $upgrader->bulk_upgrade(array_keys($_plugins));
534 }
535 if (!is_array($result)) {
536 return array('status' => "Error", 'message' =>'result is not an array');
537 }
538 foreach (array_keys($_plugins) as $file) {
539 if (!array_key_exists($file, $result)) {
540 $result[$file] = array('status' => "Error");
541 } else {
542 $res = $result[$file];
543 if (!$res || is_wp_error($res)) {
544 $result[$file] = array('status' => "Error");
545 } else {
546 $result[$file] = array('status' => "Done");
547 }
548 }
549 }
550 }
551 return $result;
552 }
553
554 function bv_theme_bulk_upgrade($upgrader, $_themes) {
555 global $wp_version;
556 $themes = array_keys($_themes);
557 $current = get_site_transient('update_themes');
558 $args = array();
559 $defaults = array(
560 'clear_update_cache' => true,
561 );
562 $parsed_args = wp_parse_args($args, $defaults);
563 $upgrader->init();
564 $upgrader->bulk = true;
565 $upgrader->upgrade_strings();
566 add_filter('upgrader_pre_install', array($upgrader, 'current_before'), 10, 2);
567 add_filter('upgrader_post_install', array( $upgrader, 'current_after'), 10, 2);
568 add_filter('upgrader_clear_destination', array($upgrader, 'delete_old_theme'), 10, 4);
569 $upgrader->skin->header();
570 $res = $upgrader->fs_connect(array(WP_CONTENT_DIR));
571 if (!$res) {
572 $upgrader->skin->footer();
573 return false;
574 }
575 $upgrader->skin->bulk_header();
576 $maintenance = (is_multisite() && !empty($themes));
577 foreach ($themes as $theme) {
578 $maintenance = $maintenance || get_stylesheet() === $theme || get_template() === $theme;
579 }
580 if ($maintenance) {
581 $upgrader->maintenance_mode(true);
582 }
583 $results = array();
584 $upgrader->update_count = count($themes);
585 $upgrader->update_current = 0;
586 foreach ($themes as $theme) {
587 $upgrader->update_current++;
588 $upgrader->skin->theme_info = $upgrader->theme_info($theme);
589 $theme_upgrade_data = $_themes[$theme];
590 if (isset($current->response[$theme])) {
591 if (isset($current->response[$theme]['requires'])) {
592 $theme_upgrade_data['requires'] = $current->response[$theme]['requires'];
593 }
594 if (isset($current->response[$theme]['requires_php'])) {
595 $theme_upgrade_data['requires_php'] = $current->response[$theme]['requires_php'];
596 }
597 }
598 if ( isset( $theme_upgrade_data['requires'] ) && function_exists('is_wp_version_compatible') && !is_wp_version_compatible( $theme_upgrade_data['requires'] ) ) {
599 $result = new WP_Error(
600 'incompatible_wp_required_version',
601 sprintf(
602 __( 'Your WordPress version is %1$s, however the new theme version requires %2$s.' ),
603 $wp_version,
604 $theme_upgrade_data['requires']
605 )
606 );
607
608 $upgrader->skin->before( $result );
609 $upgrader->skin->error( $result );
610 $upgrader->skin->after();
611 } elseif ( isset( $theme_upgrade_data['requires_php'] ) && function_exists('is_php_version_compatible') && !is_php_version_compatible( $theme_upgrade_data['requires_php'] ) ) {
612 $result = new WP_Error(
613 'incompatible_php_required_version',
614 sprintf(
615 __( 'The PHP version on your server is %1$s, however the new theme version requires %2$s.' ),
616 PHP_VERSION,
617 $theme_upgrade_data['requires_php']
618 )
619 );
620
621 $upgrader->skin->before( $result );
622 $upgrader->skin->error( $result );
623 $upgrader->skin->after();
624 } else {
625 add_filter('upgrader_source_selection', array($upgrader, 'check_package'));
626 $result = $upgrader->run(
627 array(
628 'package' => $_themes[$theme]['package'],
629 'destination' => get_theme_root($theme),
630 'clear_destination' => true,
631 'clear_working' => true,
632 'is_multi' => true,
633 'hook_extra' => array(
634 'theme' => $theme,
635 ),
636 )
637 );
638 remove_filter('upgrader_source_selection', array($upgrader, 'check_package'));
639 }
640 $results[$theme] = $result;
641 if (false === $result) {
642 break;
643 }
644 }
645 $upgrader->maintenance_mode(false);
646 wp_clean_themes_cache($parsed_args['clear_update_cache']);
647 do_action(
648 'upgrader_process_complete',
649 $upgrader,
650 array(
651 'action' => 'update',
652 'type' => 'theme',
653 'bulk' => true,
654 'themes' => $themes,
655 )
656 );
657 $upgrader->skin->bulk_footer();
658 $upgrader->skin->footer();
659 remove_filter('upgrader_pre_install', array($upgrader, 'current_before'));
660 remove_filter('upgrader_post_install', array($upgrader, 'current_after'));
661 remove_filter('upgrader_clear_destination', array($upgrader, 'delete_old_theme'));
662 $past_failure_emails = get_option('auto_plugin_theme_update_emails', array());
663 foreach ($results as $theme => $result) {
664 if (!$result || is_wp_error($result) || !isset($past_failure_emails[$theme])) {
665 continue;
666 }
667 unset($past_failure_emails[$theme]);
668 }
669 update_option('auto_plugin_theme_update_emails', $past_failure_emails);
670 return $results;
671 }
672
673 function upgradeThemes($themes, $has_bv_skin = false, $bv_bulk_upgrade = false) {
674 $result = array();
675 $_themes = array();
676 foreach ($themes as $theme) {
677 $_themes[$theme['stylesheet']] = [
678 'package' => $theme['package']
679 ];
680 if (isset($theme['requires'])) {
681 $_themes[$theme['stylesheet']]['requires'] = $theme['requires'];
682 }
683 if (isset($theme['requires_php'])) {
684 $_themes[$theme['stylesheet']]['requires_php'] = $theme['requires_php'];
685 }
686 }
687 if (empty(array_keys($_themes))) {
688 return $result;
689 }
690 if (class_exists('Theme_Upgrader')) {
691 if ($has_bv_skin) {
692 require_once( "bv_upgrader_skin.php" );
693 $skin = new WPRUpgraderSkin("theme_upgrade");
694 $this->skin = $skin;
695 } else {
696 $skin = new Bulk_Theme_Upgrader_Skin();
697 }
698 $upgrader = new Theme_Upgrader($skin);
699 if ($bv_bulk_upgrade) {
700 $result = $this->bv_theme_bulk_upgrade($upgrader, $_themes);
701 } else {
702 $result = $upgrader->bulk_upgrade(array_keys($_themes));
703 }
704 if (!is_array($result)) {
705 return array('status' => "Error", 'message' =>'result is not an array');
706 }
707 foreach (array_keys($_themes) as $stylesheet) {
708 if (!array_key_exists($stylesheet, $result)) {
709 $result[$stylesheet] = array('status' => "Error");
710 } else {
711 $res = $result[$stylesheet];
712 if (!$res || is_wp_error($res)) {
713 $result[$stylesheet] = array('status' => "Error");
714 } else {
715 $result[$stylesheet] = array('status' => "Done");
716 }
717 }
718 }
719 }
720 return $result;
721 }
722
723 function install($params, $has_bv_skin = false) {
724 $result = array();
725 if (isset($params['plugins'])) {
726 foreach ($params['plugins'] as $plugin) {
727 if (!array_key_exists('plugins', $result))
728 $result["plugins"] = array();
729 $plugin['dest'] = WP_PLUGIN_DIR;
730 $res = $this->installPackage("plugin", $plugin, $has_bv_skin);
731 $pluginName = $plugin['package'];
732 $result["plugins"][$pluginName] = $res;
733 }
734 }
735 if (isset($params['themes'])) {
736 foreach ($params['themes'] as $theme) {
737 if (!array_key_exists('themes', $result))
738 $result["themes"] = array();
739 $theme['dest'] = WP_CONTENT_DIR.'/themes';
740 $res = $this->installPackage("theme", $theme, $has_bv_skin);
741 $themeName = $theme['package'];
742 $result["themes"][$themeName] = $res;
743 }
744 }
745 return $result;
746 }
747
748 function installPackage($type, $params, $has_bv_skin = false) {
749 global $wp_filesystem;
750
751 if (!isset($params['package']) || empty($params['package'])) {
752 return array('status' => "Error", 'message' => "No package is sent");
753 }
754 $valid_domain_regex = "/^(http|https):\/\/[\-\w]*\.(blogvault\.net|w\.org|wp\.org|wordpress\.org)\//";
755 if (WPRHelper::safePregMatch($valid_domain_regex, $params['package']) !== 1) {
756 return array('status' => "Error", 'message' => "Invalid package domain");
757 }
758 if ($has_bv_skin) {
759 require_once( "bv_upgrader_skin.php" );
760 $skin = new WPRUpgraderSkin("installer", array(), $params['package']);
761 $this->skin = $skin;
762 } else {
763 $skin = new WP_Upgrader_Skin();
764 }
765 if ("plugin" === $type) {
766 $upgrader = new Plugin_Upgrader($skin);
767 } elseif ("theme" === $type) {
768 $upgrader = new Theme_Upgrader($skin);
769 } else {
770 $upgrader = new WP_Upgrader($skin);
771 }
772 $upgrader->init();
773 $destination = $params['dest'];
774 $clear_destination = isset($params['cleardest']) ? $params['cleardest'] : false;
775 $package_url = $params['package'];
776 $is_parent_theme_install_disabled = isset($params['is_parent_theme_install_disabled']) ? $params['is_parent_theme_install_disabled'] : false;
777 $key = basename($package_url);
778 add_filter('upgrader_source_selection', array($upgrader, 'check_package'));
779 if ("theme" === $type && false === $is_parent_theme_install_disabled) {
780 add_filter('upgrader_post_install', array($upgrader, 'check_parent_theme_filter'), 10, 3);
781 }
782 $res = $upgrader->run(
783 array(
784 'package' => $package_url,
785 'destination' => $destination,
786 'clear_destination' => $clear_destination,
787 'clear_working' => true,
788 'hook_extra' => array(
789 "type" => $type,
790 "action" => "install"
791 ),
792 )
793 );
794 remove_filter('upgrader_source_selection', array($upgrader, 'check_package'));
795 if ("theme" === $type && false === $is_parent_theme_install_disabled) {
796 remove_filter('upgrader_post_install', array($upgrader, 'check_parent_theme_filter'), 10);
797 }
798 if (is_wp_error($res)) {
799 $res = array('status' => "Error", 'message' => $this->getError($res));
800 } else {
801 $res = array( 'status' => "Done");
802 }
803 return $res;
804 }
805
806 function getPremiumUpdates() {
807 return apply_filters( 'mwp_premium_update_notification', array() );
808 }
809
810 function getPremiumUpgradesInfo() {
811 return apply_filters( 'mwp_premium_perform_update', array() );
812 }
813
814 function autoLogin($username, $isHttps, $auto_login_token = null) {
815 # Validate nonce if provided (other plugin compatibility: allow login if token missing)
816 if ($auto_login_token !== null && $auto_login_token !== '') {
817 $validation_result = $this->validateAutoLoginToken($auto_login_token);
818 if ($validation_result !== true) {
819 return $validation_result;
820 }
821 }
822
823 $user = get_user_by('login', $username);
824 if ($user != FALSE) {
825 wp_set_current_user( $user->ID );
826 if ($isHttps) {
827 wp_set_auth_cookie( $user->ID, false, true );
828 } else {
829 # As we are not sure about wp-cofig.php settings for sure login
830 wp_set_auth_cookie( $user->ID, false, true );
831 wp_set_auth_cookie( $user->ID, false, false );
832 }
833 $redirect_to = get_admin_url();
834 wp_safe_redirect( $redirect_to );
835 exit;
836 }
837 }
838
839 private function validateAutoLoginToken($auto_login_token) {
840 # Get plugin name for transient key prefix
841 $plugname = $this->bvinfo->plugname;
842 if (empty($plugname)) {
843 return array(
844 'status' => 'Error',
845 'message' => 'PLUGIN_NAME_NOT_FOUND',
846 'error_code' => 'PLUGIN_NAME_ERROR'
847 );
848 }
849
850 # Construct transient key: {plugname}_auto_login_tk_{token}
851 $transient_key = $plugname . '_auto_login_tk_' . $auto_login_token;
852
853 # Check if token has already been used
854 $used_token = $this->settings->getTransient($transient_key);
855 if ($used_token !== false) {
856 # Token already used - prevent replay attack
857 return array(
858 'status' => 'Error',
859 'message' => 'AUTO_LOGIN_TOKEN_ALREADY_USED',
860 'error_code' => 'AUTO_LOGIN_TOKEN_ERROR'
861 );
862 }
863
864 # Store token as used for 24 hours to prevent reuse
865 # WordPress will automatically clean up expired transients
866 $this->settings->setTransient($transient_key, true, DAY_IN_SECONDS);
867
868 # Token is valid and has been marked as used
869 return true;
870 }
871
872 public function refreshPluginUpdates() {
873 global $wp_current_filter;
874 $wp_current_filter[] = 'load-update-core.php';
875
876 wp_update_plugins();
877
878 array_pop($wp_current_filter);
879
880 wp_update_plugins();
881
882 return true;
883 }
884
885 public function refreshThemeUpdates() {
886 global $wp_current_filter;
887 $wp_current_filter[] = 'load-update-core.php';
888
889 wp_update_themes();
890
891 array_pop($wp_current_filter);
892
893 wp_update_themes();
894
895 return true;
896 }
897
898 function upgradeElementorDB($file) {
899 try {
900 $managerClass = $file === "elementor/elementor.php" ? '\Elementor\Core\Upgrade\Manager' : '\ElementorPro\Core\Upgrade\Manager';
901
902 if (!class_exists($managerClass)) {
903 return array('status' => 'Error', 'error' => 'Elementor plugin not found or disabled', 'source' => 'BV');
904 }
905
906 $manager = new $managerClass();
907 $required_methods = array('get_task_runner', 'should_upgrade', 'on_runner_complete',
908 'get_current_version', 'get_new_version');
909 foreach ($required_methods as $method) {
910 if (!method_exists($manager, $method)) {
911 return array('status' => 'Error', 'error' => 'Required methods are missing', 'source' => 'BV');
912 }
913 }
914
915 $updateInfo = array(
916 'from' => $manager->get_current_version(),
917 'to' => $manager->get_new_version(),
918 );
919
920 if ($manager->should_upgrade()) {
921 $callbacks = $manager->get_upgrade_callbacks();
922
923 if (!empty($callbacks)) {
924 $manager->get_task_runner()->handle_immediately($callbacks);
925 $manager->on_runner_complete(true);
926 }
927 } else {
928 return array('status' => 'Error', 'error' => 'Database Update is not available currently',
929 'update_info' => $updateInfo, 'source' => 'BV');
930 }
931
932 return array('status' => 'Done', 'update_info' => $updateInfo);
933 } catch (Exception $e) {
934 return array('status' => 'Error', 'error' => $e->getMessage());
935 }
936 }
937
938 function upgradeWoocommerceDb() {
939 try {
940 if (!defined('WC_ABSPATH')) {
941 return array('status' => 'Error', 'error' => 'WC not found', 'source' => 'BV');
942 }
943
944 if (file_exists(WC_ABSPATH . 'includes/class-wc-install.php')) {
945 include_once WC_ABSPATH . 'includes/class-wc-install.php';
946 }
947
948 if (file_exists(WC_ABSPATH . 'includes/wc-update-functions.php')) {
949 include_once WC_ABSPATH . 'includes/wc-update-functions.php';
950 }
951
952 if (!class_exists('WC_Install') || !method_exists('WC_Install', 'needs_db_update') ||
953 !method_exists('WC_Install', 'get_db_update_callbacks')) {
954 return array('status' => 'Error', 'error' => 'WC files missing or corrupted', 'source' => 'BV');
955 }
956
957 $current_db_version = $this->settings->getOption('woocommerce_db_version');
958 if (!$current_db_version) {
959 return array('status' => 'Error', 'error' => 'Current WC DB version not available', 'source' => 'BV');
960 }
961
962 $latest_db_version = $this->bvinfo->getLatestWooCommerceDBVersion();
963 if ($current_db_version >= $latest_db_version) {
964 return array('status' => 'Error', 'error' => 'WC DB update not available', 'source' => 'BV');
965 }
966
967 $db_update_callbacks = WC_Install::get_db_update_callbacks();
968
969 $loop = 0;
970 foreach ($db_update_callbacks as $version => $update_callbacks) {
971 if (version_compare($current_db_version, $version, '<')) {
972 foreach ($update_callbacks as $update_callback) {
973 WC()->queue()->schedule_single(
974 time() + $loop,
975 'woocommerce_run_update_callback',
976 array('update_callback' => $update_callback),
977 'woocommerce-db-updates'
978 );
979 $loop++;
980 }
981 }
982 }
983
984 $current_wc_version = WC()->version;
985 if (version_compare($current_db_version, $current_wc_version, '<') &&
986 !WC()->queue()->get_next('woocommerce_update_db_to_current_version')) {
987 WC()->queue()->schedule_single(
988 time() + $loop,
989 'woocommerce_update_db_to_current_version',
990 array('version' => $current_wc_version),
991 'woocommerce-db-updates'
992 );
993 }
994
995 return array('status' => 'Done');
996 } catch (Exception $e) {
997 return array('status' => 'Error', 'error' => $e->getMessage());
998 }
999 }
1000
1001 function upgrade_db(){
1002 if (function_exists('wp_upgrade')) {
1003 wp_upgrade();
1004 return "Done";
1005 } else {
1006 return "NOUPGRADERFUNCTION";
1007 }
1008 }
1009
1010 function process($request) {
1011 global $wp_filesystem;
1012 $this->include_files();
1013
1014 if (!$this->is_pantheon() && !$wp_filesystem) {
1015 WP_Filesystem();
1016 }
1017
1018 $params = $request->params;
1019 $resp = array();
1020 switch ($request->method) {
1021 case "adusr":
1022 $resp = array("adduser" => $this->addUser($params['args']));
1023 break;
1024 case "upgrde":
1025 $has_bv_skin = array_key_exists('bvskin', $params);
1026 $bv_bulk_upgrade = array_key_exists('bv_bulk_update', $params['args']);
1027 $resp = array("upgrades" => $this->upgrade($params['args'], $has_bv_skin, $bv_bulk_upgrade));
1028 break;
1029 case "cmbneupgrde":
1030 $args = $params['args'];
1031 $has_bv_skin = array_key_exists('bvskin', $args);
1032 $bv_bulk_upgrade = array_key_exists('bv_bulk_update', $args);
1033
1034 $resp['upgrades'] = $this->upgrade($args['upgrades'], $has_bv_skin, $bv_bulk_upgrade);
1035
1036 if (isset($args['delete_transient'])) {
1037 $resp['delete_transient'] = $this->settings->deleteTransient($args['delete_transient']);
1038 }
1039 if (isset($args['wp_update_plugins'])) {
1040 $resp['wp_update_plugins'] = $this->refreshPluginUpdates();
1041 }
1042 if (isset($args['wp_update_themes'])) {
1043 $resp['wp_update_themes'] = $this->refreshThemeUpdates();
1044 }
1045 break;
1046 case "edt":
1047 $resp = array("edit" => $this->edit($params['args']));
1048 break;
1049 case "instl":
1050 $has_bv_skin = array_key_exists('bvskin', $params);
1051 $resp = array("install" => $this->install($params['args'], $has_bv_skin));
1052 break;
1053 case "getpremiumupdates":
1054 $resp = array("premiumupdates" => $this->getPremiumUpdates());
1055 break;
1056 case "getpremiumupgradesinfo":
1057 $resp = array("premiumupgradesinfo" => $this->getPremiumUpgradesInfo());
1058 break;
1059 case "wrteble":
1060 $resp = array("writeable" => $this->isServerWritable());
1061 break;
1062 case "atolgn":
1063 $isHttps = false;
1064 if (array_key_exists('https', $params))
1065 $isHttps = true;
1066 $auto_login_token = array_key_exists('auto_login_token', $params) ? $params['auto_login_token'] : null;
1067 $resp = array("autologin" => $this->autoLogin($params['username'], $isHttps, $auto_login_token));
1068 break;
1069 case "updatedb":
1070 $resp = array("status" => $this->upgrade_db());
1071 break;
1072 case "updateplgndb":
1073 $resp = array();
1074 $files = $params['files'];
1075
1076 foreach ($files as $file) {
1077 switch ($file) {
1078 case "woocommerce/woocommerce.php":
1079 $resp[$file] = $this->upgradeWoocommerceDb();
1080 break;
1081 case "elementor/elementor.php":
1082 case "elementor-pro/elementor-pro.php":
1083 $resp[$file] = $this->upgradeElementorDB($file);
1084 break;
1085 }
1086 }
1087
1088 break;
1089 default:
1090 $resp = false;
1091 }
1092 if ($this->skin && is_array($resp)) {
1093 $resp = array_merge($resp, $this->skin->status);
1094 }
1095 return $resp;
1096 }
1097 }
1098 endif;
1099