| 1 |
<?php |
| 2 |
/************************************************************* |
| 3 |
* |
| 4 |
* installer.class.php |
| 5 |
* |
| 6 |
* Upgrade WordPress |
| 7 |
* |
| 8 |
* |
| 9 |
* Copyright (c) 2011 Prelovac Media |
| 10 |
* www.prelovac.com |
| 11 |
**************************************************************/ |
| 12 |
|
| 13 |
class MMB_Installer extends MMB_Core |
| 14 |
{ |
| 15 |
function __construct() |
| 16 |
{ |
| 17 |
@set_time_limit(300); |
| 18 |
parent::__construct(); |
| 19 |
include_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 20 |
include_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
| 21 |
include_once(ABSPATH . 'wp-admin/includes/theme.php'); |
| 22 |
include_once(ABSPATH . 'wp-admin/includes/misc.php'); |
| 23 |
include_once(ABSPATH . 'wp-admin/includes/template.php'); |
| 24 |
@include_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'); |
| 25 |
|
| 26 |
global $wp_filesystem; |
| 27 |
if (!$wp_filesystem) |
| 28 |
WP_Filesystem(); |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
function mmb_maintenance_mode($enable = false, $maintenance_message = '') |
| 33 |
{ |
| 34 |
global $wp_filesystem; |
| 35 |
|
| 36 |
$maintenance_message .= '<?php $upgrading = ' . time() . '; ?>'; |
| 37 |
|
| 38 |
$file = $wp_filesystem->abspath() . '.maintenance'; |
| 39 |
if ($enable) { |
| 40 |
$wp_filesystem->delete($file); |
| 41 |
$wp_filesystem->put_contents($file, $maintenance_message, FS_CHMOD_FILE); |
| 42 |
} else { |
| 43 |
$wp_filesystem->delete($file); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
function install_remote_file($params) |
| 48 |
{ |
| 49 |
global $wp_filesystem; |
| 50 |
extract($params); |
| 51 |
|
| 52 |
if (!isset($package) || empty($package)) |
| 53 |
return array( |
| 54 |
'error' => '<p>No files received. Internal error.</p>' |
| 55 |
); |
| 56 |
|
| 57 |
if (defined('WP_INSTALLING') && file_exists(ABSPATH . '.maintenance')) |
| 58 |
return array( |
| 59 |
'error' => '<p>Site under maintanace.</p>' |
| 60 |
); |
| 61 |
; |
| 62 |
|
| 63 |
$upgrader = new WP_Upgrader(); |
| 64 |
$destination = $type == 'themes' ? WP_CONTENT_DIR . '/themes' : WP_PLUGIN_DIR; |
| 65 |
|
| 66 |
|
| 67 |
foreach ($package as $package_url) { |
| 68 |
$key = basename($package_url); |
| 69 |
$install_info[$key] = @$upgrader->run(array( |
| 70 |
'package' => $package_url, |
| 71 |
'destination' => $destination, |
| 72 |
'clear_destination' => false, //Do not overwrite files. |
| 73 |
'clear_working' => true, |
| 74 |
'hook_extra' => array() |
| 75 |
)); |
| 76 |
} |
| 77 |
|
| 78 |
if ($activate) { |
| 79 |
if($type == 'plugins'){ |
| 80 |
include_once(ABSPATH.'wp-admin/includes/plugin.php'); |
| 81 |
$all_plugins = get_plugins(); |
| 82 |
foreach ($all_plugins as $plugin_slug => $plugin) { |
| 83 |
$plugin_dir = preg_split('/\//', $plugin_slug); |
| 84 |
foreach ($install_info as $key => $install) { |
| 85 |
if (!$install || is_wp_error($install)) |
| 86 |
continue; |
| 87 |
|
| 88 |
if ($install['destination_name'] == $plugin_dir[0]) { |
| 89 |
$install_info[$key]['activated'] = activate_plugin($plugin_slug, '', false); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
}else if(count($install_info) == 1){ |
| 94 |
global $wp_themes; |
| 95 |
include_once(ABSPATH.'wp-includes/theme.php'); |
| 96 |
|
| 97 |
$wp_themes = null; unset($wp_themes); //prevent theme data caching |
| 98 |
|
| 99 |
$all_themes = get_themes(); |
| 100 |
foreach ($all_themes as $theme_name => $theme_data) { |
| 101 |
foreach ($install_info as $key => $install) { |
| 102 |
if (!$install || is_wp_error($install)) |
| 103 |
continue; |
| 104 |
|
| 105 |
if ($theme_data['Template'] == $install['destination_name']) { |
| 106 |
$install_info[$key]['activated'] = switch_theme($theme_data['Template'], $theme_data['Stylesheet']); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
ob_clean(); |
| 113 |
$this->mmb_maintenance_mode(false); |
| 114 |
return $install_info; |
| 115 |
} |
| 116 |
|
| 117 |
function do_upgrade($params = null){ |
| 118 |
|
| 119 |
if($params == null || empty($params)) |
| 120 |
return array('failed' => 'No upgrades passed.'); |
| 121 |
|
| 122 |
if (!$this->is_server_writable()) { |
| 123 |
return array( |
| 124 |
'error' => 'Failed, please <a target="_blank" href="http://managewp.com/user-guide#ftp">add FTP details</a></a>' |
| 125 |
); |
| 126 |
} |
| 127 |
$params = isset($params['upgrades_all']) ? $params['upgrades_all'] : $params; |
| 128 |
|
| 129 |
$core_upgrade = isset($params['wp_upgrade']) ? $params['wp_upgrade'] : array(); |
| 130 |
$upgrade_plugins = isset($params['upgrade_plugins']) ? $params['upgrade_plugins'] : array(); |
| 131 |
$upgrade_themes = isset($params['upgrade_themes']) ? $params['upgrade_themes'] : array(); |
| 132 |
|
| 133 |
|
| 134 |
$upgrades = array(); |
| 135 |
if(!empty($core_upgrade)){ |
| 136 |
$upgrades['core'] = $this->upgrade_core($core_upgrade); |
| 137 |
} |
| 138 |
|
| 139 |
if(!empty($upgrade_plugins)){ |
| 140 |
$plugin_files = array(); |
| 141 |
foreach($upgrade_plugins as $plugin){ |
| 142 |
$plugin_files[$plugin->file] = $plugin->old_version; |
| 143 |
} |
| 144 |
|
| 145 |
$upgrades['plugins'] = $this->upgrade_plugins($plugin_files); |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
if(!empty($upgrade_themes)){ |
| 150 |
$theme_temps = array(); |
| 151 |
foreach($upgrade_themes as $theme){ |
| 152 |
$theme_temps[] = $theme['theme_tmp']; |
| 153 |
} |
| 154 |
|
| 155 |
$upgrades['themes'] = $this->upgrade_themes($theme_temps); |
| 156 |
$this->mmb_maintenance_mode(false); |
| 157 |
} |
| 158 |
ob_clean(); |
| 159 |
$this->mmb_maintenance_mode(false); |
| 160 |
return $upgrades; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Upgrades WordPress locally |
| 165 |
* |
| 166 |
*/ |
| 167 |
function upgrade_core($current) |
| 168 |
{ |
| 169 |
ob_start(); |
| 170 |
if(!function_exists('wp_version_check')) |
| 171 |
include_once(ABSPATH.'/wp-admin/includes/update.php'); |
| 172 |
|
| 173 |
@wp_version_check(); |
| 174 |
|
| 175 |
if(!function_exists('get_core_updates')) |
| 176 |
include_once(ABSPATH.'/wp-admin/includes/update.php'); |
| 177 |
|
| 178 |
$updates = get_core_updates(); |
| 179 |
|
| 180 |
$current_update = false; |
| 181 |
ob_end_flush(); |
| 182 |
ob_end_clean(); |
| 183 |
|
| 184 |
if(!empty($updates)){ |
| 185 |
$updated = $updates[0]; |
| 186 |
if ( !isset( $updated->response ) || $updated->response == 'latest' ) |
| 187 |
return array('upgraded' => ' updated'); |
| 188 |
|
| 189 |
if ($updated->response == "development" && $current->response == "upgrade") { |
| 190 |
return array('upgraded' => '<font color="#900">Unexpected error. Please upgrade manually.</font>'); |
| 191 |
} |
| 192 |
else if ($updated->response == $current->response || ($updated->response == "upgrade" && $current->response == "development")){ |
| 193 |
if($updated->locale != $current->locale){ |
| 194 |
foreach($updates as $update){ |
| 195 |
if($update->locale == $current->locale){ |
| 196 |
$current_update = $update; |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
if($current_update == false) |
| 201 |
return array('error' => ' Localization mismatch. Try again.'); |
| 202 |
} else { |
| 203 |
$current_update = $updated; |
| 204 |
} |
| 205 |
} |
| 206 |
else |
| 207 |
return array('error' => ' Transient mismatch. Try again.'); |
| 208 |
} else |
| 209 |
return array('error' => ' Refresh transient failed. Try again.'); |
| 210 |
if($current_update != false){ |
| 211 |
global $mmb_wp_version, $wp_filesystem, $wp_version; |
| 212 |
|
| 213 |
if (version_compare($wp_version, '3.1.9', '>')) { |
| 214 |
if(!class_exists('Core_Upgrader')) |
| 215 |
include_once(ABSPATH.'wp-admin/includes/class-wp-upgrader.php'); |
| 216 |
|
| 217 |
$core = new Core_Upgrader(); |
| 218 |
$result = $core->upgrade($current_update); |
| 219 |
if(is_wp_error($result)){ |
| 220 |
return array( |
| 221 |
'error' => $this->mmb_get_error($result) |
| 222 |
); |
| 223 |
} |
| 224 |
else |
| 225 |
return array( |
| 226 |
'upgraded' => ' updated' |
| 227 |
); |
| 228 |
|
| 229 |
} else { |
| 230 |
if(!class_exists('WP_Upgrader')){ |
| 231 |
include_once(ABSPATH.'wp-admin/includes/update.php'); |
| 232 |
if(function_exists('wp_update_core')){ |
| 233 |
$result = wp_update_core($current_update); |
| 234 |
if(is_wp_error($result)){ |
| 235 |
return array( |
| 236 |
'error' => $this->mmb_get_error($result) |
| 237 |
); |
| 238 |
} |
| 239 |
else |
| 240 |
return array( |
| 241 |
'upgraded' => ' updated' |
| 242 |
); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
if(class_exists('WP_Upgrader')){ |
| 247 |
$upgrader = new WP_Upgrader(); |
| 248 |
|
| 249 |
// Is an update available? |
| 250 |
if (!isset($current_update->response) || $current_update->response == 'latest') |
| 251 |
return array( |
| 252 |
'upgraded' => ' updated' |
| 253 |
); |
| 254 |
|
| 255 |
$res = $upgrader->fs_connect(array( |
| 256 |
ABSPATH, |
| 257 |
WP_CONTENT_DIR |
| 258 |
)); |
| 259 |
if (is_wp_error($res)) |
| 260 |
return array( |
| 261 |
'error' => $this->mmb_get_error($res) |
| 262 |
); |
| 263 |
|
| 264 |
$wp_dir = trailingslashit($wp_filesystem->abspath()); |
| 265 |
|
| 266 |
$core_package = false; |
| 267 |
if(isset($current_update->package) && !empty($current_update->package)) |
| 268 |
$core_package = $current_update->package; |
| 269 |
elseif (isset($current_update->packages->full) && !empty($current_update->packages->full)) |
| 270 |
$core_package = $current_update->packages->full; |
| 271 |
|
| 272 |
$download = $upgrader->download_package($core_package); |
| 273 |
if (is_wp_error($download)) |
| 274 |
return array( |
| 275 |
'error' => $this->mmb_get_error($download) |
| 276 |
); |
| 277 |
|
| 278 |
$working_dir = $upgrader->unpack_package($download); |
| 279 |
if (is_wp_error($working_dir)) |
| 280 |
return array( |
| 281 |
'error' => $this->mmb_get_error($working_dir) |
| 282 |
); |
| 283 |
|
| 284 |
if (!$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true)) { |
| 285 |
$wp_filesystem->delete($working_dir, true); |
| 286 |
return array( |
| 287 |
'error' => 'Unable to move update files.' |
| 288 |
); |
| 289 |
} |
| 290 |
|
| 291 |
$wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE); |
| 292 |
|
| 293 |
require(ABSPATH . 'wp-admin/includes/update-core.php'); |
| 294 |
|
| 295 |
|
| 296 |
$update_core = update_core($working_dir, $wp_dir); |
| 297 |
ob_end_clean(); |
| 298 |
|
| 299 |
if (is_wp_error($update_core)) |
| 300 |
return array( |
| 301 |
'error' => $this->mmb_get_error($update_core) |
| 302 |
); |
| 303 |
ob_end_flush(); |
| 304 |
return array( |
| 305 |
'upgraded' => 'updated' |
| 306 |
); |
| 307 |
} else { |
| 308 |
return array( |
| 309 |
'error' => 'failed' |
| 310 |
); |
| 311 |
} |
| 312 |
} |
| 313 |
} else { |
| 314 |
return array( |
| 315 |
'error' => 'failed' |
| 316 |
); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
function upgrade_plugins($plugins = false){ |
| 321 |
if(!$plugins || empty($plugins)) |
| 322 |
return array( |
| 323 |
'error' => 'No plugin files for upgrade.' |
| 324 |
); |
| 325 |
$return = array(); |
| 326 |
if (class_exists('Plugin_Upgrader') && class_exists('Bulk_Plugin_Upgrader_Skin')) { |
| 327 |
|
| 328 |
$upgrader = new Plugin_Upgrader(new Bulk_Plugin_Upgrader_Skin(compact('nonce', 'url'))); |
| 329 |
$result = $upgrader->bulk_upgrade(array_keys($plugins)); |
| 330 |
|
| 331 |
if( !function_exists('wp_update_plugins') ) |
| 332 |
include_once(ABSPATH . 'wp-includes/update.php'); |
| 333 |
|
| 334 |
@wp_update_plugins(); |
| 335 |
$current_plugins = $this->mmb_get_transient('update_plugins'); |
| 336 |
|
| 337 |
if (!empty($result)) { |
| 338 |
foreach ($result as $plugin_slug => $plugin_info) { |
| 339 |
if (!$plugin_info || is_wp_error($plugin_info)) { |
| 340 |
$return[$plugin_slug] = $this->mmb_get_error($plugin_info); |
| 341 |
} else { |
| 342 |
if(isset($current_plugins->response[$plugin_slug]) && !empty($current_plugins->response[$plugin_slug])){ |
| 343 |
$return[$plugin_slug] = false; |
| 344 |
} else { |
| 345 |
$return[$plugin_slug] = 1; |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
ob_end_clean(); |
| 350 |
return array( |
| 351 |
'upgraded' => $return |
| 352 |
); |
| 353 |
} |
| 354 |
else |
| 355 |
return array( |
| 356 |
'error' => 'Upgrade failed.' |
| 357 |
); |
| 358 |
} else { |
| 359 |
ob_end_clean(); |
| 360 |
return array( |
| 361 |
'error' => 'WordPress update required first.' |
| 362 |
); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
function upgrade_themes($themes = false){ |
| 367 |
if(!$themes || empty($themes)) |
| 368 |
return array( |
| 369 |
'error' => 'No theme files for upgrade.' |
| 370 |
); |
| 371 |
if (class_exists('Theme_Upgrader') && class_exists('Bulk_Theme_Upgrader_Skin')) { |
| 372 |
|
| 373 |
|
| 374 |
$upgrader = new Theme_Upgrader(new Bulk_Theme_Upgrader_Skin(compact('title', 'nonce', 'url', 'theme'))); |
| 375 |
$result = $upgrader->bulk_upgrade($themes); |
| 376 |
|
| 377 |
if( !function_exists('wp_update_themes') ) |
| 378 |
include_once(ABSPATH . 'wp-includes/update.php'); |
| 379 |
|
| 380 |
@wp_update_themes(); |
| 381 |
$current_themes = $this->mmb_get_transient('update_themes'); |
| 382 |
|
| 383 |
$return = array(); |
| 384 |
if (!empty($result)) { |
| 385 |
foreach ($result as $theme_tmp => $theme_info) { |
| 386 |
if (is_wp_error($theme_info) || !$theme_info) { |
| 387 |
$return[$theme_tmp] = $this->mmb_get_error($theme_info); |
| 388 |
} else { |
| 389 |
if(isset($current_themes->response[$theme_tmp]) && !empty($current_themes->response[$theme_tmp])){ |
| 390 |
$return[$theme_tmp] = false; |
| 391 |
} else { |
| 392 |
$return[$theme_tmp] = 1; |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
return array( |
| 398 |
'upgraded' => $return |
| 399 |
); |
| 400 |
} else |
| 401 |
return array( |
| 402 |
'error' => 'Upgrade failed.' |
| 403 |
); |
| 404 |
} else { |
| 405 |
ob_end_clean(); |
| 406 |
return array( |
| 407 |
'error' => 'WordPress update required first' |
| 408 |
); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
function get_upgradable_plugins() |
| 413 |
{ |
| 414 |
$current = $this->mmb_get_transient('update_plugins'); |
| 415 |
$upgradable_plugins = array(); |
| 416 |
if (!empty($current->response)) { |
| 417 |
if (!function_exists('get_plugin_data')) |
| 418 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 419 |
foreach ($current->response as $plugin_path => $plugin_data) { |
| 420 |
if($plugin_path == 'worker/init.php') |
| 421 |
continue; |
| 422 |
|
| 423 |
$data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin_path); |
| 424 |
if(strlen($data['Name']) > 0 && strlen($data['Version']) > 0) { |
| 425 |
$current->response[$plugin_path]->name = $data['Name']; |
| 426 |
$current->response[$plugin_path]->old_version = $data['Version']; |
| 427 |
$current->response[$plugin_path]->file = $plugin_path; |
| 428 |
$upgradable_plugins[] = $current->response[$plugin_path]; |
| 429 |
} |
| 430 |
} |
| 431 |
return $upgradable_plugins; |
| 432 |
} else |
| 433 |
return array(); |
| 434 |
|
| 435 |
} |
| 436 |
|
| 437 |
function get_upgradable_themes() |
| 438 |
{ |
| 439 |
$all_themes = get_themes(); |
| 440 |
$upgrade_themes = array(); |
| 441 |
|
| 442 |
$current = $this->mmb_get_transient('update_themes'); |
| 443 |
foreach ((array) $all_themes as $theme_template => $theme_data) { |
| 444 |
if (!empty($current->response)) { |
| 445 |
foreach ($current->response as $current_themes => $theme) { |
| 446 |
if ($theme_data['Template'] == $current_themes) { |
| 447 |
if(strlen($theme_data['Name']) > 0 && strlen($theme_data['Version']) > 0) { |
| 448 |
$current->response[$current_themes]['name'] = $theme_data['Name']; |
| 449 |
$current->response[$current_themes]['old_version'] = $theme_data['Version']; |
| 450 |
$current->response[$current_themes]['theme_tmp'] = $theme_data['Template']; |
| 451 |
$upgrade_themes[] = $current->response[$current_themes]; |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
return $upgrade_themes; |
| 459 |
} |
| 460 |
} |
| 461 |
?> |