PluginProbe
The WP Remote WordPress Plugin / 6.44
The WP Remote WordPress Plugin v6.44
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.44, 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 = 1.9;
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 $this->safe_require_once(ABSPATH.'wp-admin/includes/update-core.php');
60 }
61
62 function edit($args) {
63 $result = array();
64 if ($args['type'] == 'plugins') {
65 $result['plugins'] = $this->editPlugins($args);
66 } elseif ($args['type'] == 'themes') {
67 $result['themes'] = $this->editThemes($args);
68 } elseif ($args['type'] == 'users') {
69 $result['users'] = $this->editWpusers($args);
70 }
71 return $result;
72 }
73
74 function editPlugins($args) {
75 $result = array();
76 $plugins = $args['items'];
77 foreach ($plugins as $plugin) {
78 if (array_key_exists('network', $plugin)) {
79 $networkwide = $plugin['network'];
80 } else {
81 $networkwide = false;
82 }
83 switch ($args['action']) {
84 case 'activate':
85 $res = activate_plugin($plugin['file'], '', $networkwide);
86 break;
87 case 'deactivate':
88 $res = deactivate_plugins(array($plugin['file']), false, $networkwide);
89 break;
90 case 'delete':
91 $res = delete_plugins(array($plugin['file']));
92 break;
93 case 'deactivate_delete':
94 $res = deactivate_plugins(array($plugin['file']), false, $networkwide);
95 if ($res || is_wp_error($res))
96 break;
97 $res = delete_plugins(array($plugin['file']));
98 default:
99 break;
100 }
101 if (is_wp_error($res)) {
102 $res = array('status' => "Error", 'message' => $res->get_error_message());
103 } elseif ($res === false) {
104 $res = array('status' => "Error", 'message' => "Failed to perform action.");
105 } else {
106 $res = array('status' => "Done");
107 }
108 $result[$plugin['file']] = $res;
109 }
110 return $result;
111 }
112
113 function editThemes($args) {
114 $result = array();
115 $themes = $args['items'];
116 foreach ($themes as $theme) {
117 switch ($args['action']) {
118 case 'activate':
119 $res = switch_theme($theme['template'], $theme['stylesheet']);
120 break;
121 case 'delete':
122 $res = delete_theme($theme['stylesheet']);
123 break;
124 default:
125 break;
126 }
127
128 if (is_wp_error($res)) {
129 $res = array('status' => "Error", 'message' => $res->get_error_message());
130 } elseif ($res === false) {
131 $res = array('status' => "Error", 'message' => "Failed to perform action.");
132 } else {
133 $res = array( 'status' => "Done");
134 }
135 $result[$theme['template']] = $res;
136 }
137 return $result;
138 }
139
140 function editWpusers($args) {
141 $result = array();
142 $items = $args['items'];
143 foreach ($items as $item) {
144 $res = array();
145 $user = get_user_by('id', $item['id']);
146 if ($user) {
147 switch ($args['action']) {
148 case 'changerole':
149 $data = array();
150 $data['role'] = $item['newrole'];
151 $data['ID'] = $user->ID;
152 $res = wp_update_user($data);
153 break;
154 case 'changepass':
155 $data = array();
156 $data['user_pass'] = $item['newpass'];
157 $data['ID'] = $user->ID;
158 $res = wp_update_user($data);
159 break;
160 case 'delete':
161 if (array_key_exists('reassign', $args)) {
162 $user_to = get_user_by('id', $args['reassign']);
163 if ($user_to != false) {
164 $res = wp_delete_user($user->ID, $user_to->ID);
165 } else {
166 $res = array('status' => "Error", 'message' => 'Reassigned user doesnot exists');
167 }
168 } else {
169 $res = wp_delete_user($user->ID);
170 }
171 break;
172 }
173 if (is_wp_error($res)) {
174 $res = array('status' => "Error", 'message' => $res->get_error_message());
175 } else {
176 $res = array( 'status' => "Done");
177 }
178 } else {
179 $res = array('status' => "Error", 'message' => "Unable to find user");
180 }
181 $result[$item['id']] = $res;
182 }
183 return $result;
184 }
185
186 function addUser($args) {
187 if (username_exists($args['user_login'])) {
188 return array('status' => "Error", 'message' => "Username already exists");
189 }
190 if (email_exists($args['user_email'])) {
191 return array('status' => "Error", 'message' => "Email already exists");
192 }
193 $result = wp_insert_user($args);
194 if ( !is_wp_error( $result ) ) {
195 return array('status' => "Done", 'user_id' => $result);
196 } else {
197 return array('status' => "Error", 'message' => $this->getError($result));
198 }
199 }
200
201 function upgrade($params = null, $has_bv_skin = false, $bv_bulk_upgrade = false) {
202 $result = array();
203 $premium_upgrades = array();
204 if (array_key_exists('clear_filters', $params)) {
205 $filters = $params['clear_filters'];
206 foreach ($filters as $filter)
207 remove_all_filters($filter);
208 }
209 if (array_key_exists('core', $params) && !empty($params['core'])) {
210 $result['core'] = $this->upgradeCore($params['core']);
211 }
212 if (array_key_exists('translations', $params) && !empty($params['translations'])) {
213 $result['translations'] = $this->upgradeTranslations($params['translations'], $has_bv_skin);
214 }
215 if (array_key_exists('plugins', $params) && !empty($params['plugins'])) {
216 $result['plugins'] = $this->upgradePlugins($params['plugins'], $has_bv_skin, $bv_bulk_upgrade);
217 }
218 if (array_key_exists('themes', $params) && !empty($params['themes'])) {
219 $result['themes'] = $this->upgradeThemes($params['themes'], $has_bv_skin, $bv_bulk_upgrade);
220 }
221 return $result;
222 }
223
224 function get_translation_updates() {
225 $updates = array();
226 $transients = array( 'update_core' => 'core', 'update_plugins' => 'plugin', 'update_themes' => 'theme' );
227 foreach ( $transients as $transient => $type ) {
228 $transient = $this->settings->getTransient( $transient );
229 if ( empty( $transient->translations ) )
230 continue;
231
232 foreach ( $transient->translations as $translation ) {
233 $updates[] = (object) $translation;
234 }
235 }
236 return $updates;
237 }
238
239 function upgradeTranslations($translations, $has_bv_skin = false) {
240 $language_updates = $this->get_translation_updates();
241 $valid_updates = array();
242 $result = array();
243 if (!empty($language_updates)) {
244 foreach($language_updates as $update) {
245 if ($update && in_array($update->package, $translations)) {
246 $valid_updates[] = $update;
247 }
248 }
249 }
250 if (!empty($valid_updates)) {
251 if (class_exists('Language_Pack_Upgrader')) {
252 if ($has_bv_skin) {
253 require_once( "bv_upgrader_skin.php" );
254 $skin = new WPRUpgraderSkin("upgrade_translations");
255 $this->skin = $skin;
256 } else {
257 $skin = new Language_Pack_Upgrader_Skin(array());
258 }
259 $upgrader = new Language_Pack_Upgrader($skin);
260 $result = $upgrader->bulk_upgrade($valid_updates);
261 if (is_array($result) && !empty($result)) {
262 foreach ($result as $translate_tmp => $translate_info) {
263 if (is_wp_error($translate_info) || empty($translate_info)) {
264 $error = (!empty($translate_info)) ? is_wp_error($translate_info) : "Upgrade failed";
265 return array('status' => "Error", 'message' => $error);
266 }
267 }
268 }
269 return array('status' => "Done");
270 }
271 }
272 return array('status' => "Error", 'message' => "Upgrade failed");
273 }
274
275 function upgradeCore($args) {
276 global $wp_filesystem, $wp_version;
277 $to_update = null;
278
279 // Prefer validated update data from server (BlogVault), then site transient.
280 // This protects upgrades from poisoned/overridden transients.
281 $validated = null;
282 if (isset($args['core_validated_update']) && !empty($args['core_validated_update'])) {
283 $validated = $args['core_validated_update'];
284 }
285
286 if (!empty($validated) && is_array($validated) &&
287 isset($validated['version']) && !empty($validated['version']) &&
288 isset($validated['response']) && ($validated['response'] === 'upgrade')) {
289 $to_update = new stdClass();
290 $to_update->version = $validated['version'];
291 $to_update->response = $validated['response'];
292 $to_update->download = isset($validated['download']) ? $validated['download'] : '';
293
294 // Preserve all packages keys if provided; Core_Upgrader expects packages-like data.
295 if (isset($validated['packages']) && is_array($validated['packages'])) {
296 $to_update->packages = (object)$validated['packages'];
297 } else {
298 $to_update->packages = new stdClass();
299 }
300
301 // WordPress uses either ->package or ->packages->full depending on path/version.
302 if (isset($to_update->packages->full) && !empty($to_update->packages->full)) {
303 $to_update->package = $to_update->packages->full;
304 } else {
305 $to_update->package = $to_update->download;
306 $to_update->packages->full = $to_update->download;
307 }
308 } else {
309 // Fallback to transient-based approach
310 $core = $this->settings->getTransient('update_core');
311 $core_update_index = intval($args['coreupdateindex']);
312 if (isset($core->updates) && !empty($core->updates)) {
313 $to_update = $core->updates[$core_update_index];
314 } else {
315 return array('status' => "Error", "message" => "Updates not available");
316 }
317 }
318
319 if (!$to_update) {
320 return array('status' => "Error", "message" => "Updates not available");
321 }
322
323 $resp = array("Core_Upgrader", class_exists('Core_Upgrader'));
324 if (version_compare($wp_version, '3.1.9', '>')) {
325 $core = new Core_Upgrader();
326 $result = $core->upgrade($to_update);
327 if (is_wp_error($result)) {
328 return array('status' => "Error", "message" => $this->getError($result));
329 } else {
330 return array('status' => 'Done');
331 }
332 } else {
333 $resp = array("WP_Upgrader", class_exists('WP_Upgrader'));
334 if (class_exists('WP_Upgrader')) {
335 $upgrader = new WP_Upgrader();
336
337 $res = $upgrader->fs_connect(
338 array(
339 ABSPATH,
340 WP_CONTENT_DIR,
341 )
342 );
343 if (is_wp_error($res)) {
344 return array('status' => "Error", "message" => $this->getError($res));
345 }
346
347 $wp_dir = trailingslashit($wp_filesystem->abspath());
348
349 $core_package = false;
350 if (isset($to_update->package) && !empty($to_update->package)) {
351 $core_package = $to_update->package;
352 } elseif (isset($to_update->packages->full) && !empty($to_update->packages->full)) {
353 $core_package = $to_update->packages->full;
354 }
355
356 $download = $upgrader->download_package($core_package);
357 if (is_wp_error($download)) {
358 return array('status' => "Error", "message" => $this->getError($download));
359 }
360 $working_dir = $upgrader->unpack_package($download);
361 if (is_wp_error($working_dir)) {
362 return array('status' => "Error", "message" => $this->getError($working_dir));
363 }
364
365 if (!$wp_filesystem->copy($working_dir.'/wordpress/wp-admin/includes/update-core.php', $wp_dir.'wp-admin/includes/update-core.php', true)) {
366 $wp_filesystem->delete($working_dir, true);
367 return array('status' => "Error", "message" => "Unable to move files.");
368 }
369
370 $wp_filesystem->chmod($wp_dir.'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
371
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