| 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 |
$all_plugins = get_plugins(); |
| 80 |
foreach ($all_plugins as $plugin_slug => $plugin) { |
| 81 |
$plugin_dir = preg_split('/\//', $plugin_slug); |
| 82 |
foreach ($install_info as $key => $install) { |
| 83 |
if (!$install || is_wp_error($install)) |
| 84 |
continue; |
| 85 |
|
| 86 |
if ($install['destination_name'] == $plugin_dir[0]) { |
| 87 |
$install_info[$key]['activated'] = activate_plugin($plugin_slug, '', false); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
ob_clean(); |
| 93 |
return $install_info; |
| 94 |
} |
| 95 |
|
| 96 |
function do_upgrade($params = null){ |
| 97 |
|
| 98 |
if($params == null || empty($params)) |
| 99 |
return array('failed' => 'No upgrades passed.'); |
| 100 |
|
| 101 |
if ((!defined('FTP_HOST') || !defined('FTP_USER') || !defined('FTP_PASS')) && (get_filesystem_method(array(), false) != 'direct')) { |
| 102 |
return array( |
| 103 |
'error' => 'Failed, please <a target="_blank" href="http://managewp.com/user-guide#ftp">add FTP details</a></a>' |
| 104 |
); |
| 105 |
} |
| 106 |
$params = isset($params['upgrades_all']) ? $params['upgrades_all'] : $params; |
| 107 |
|
| 108 |
$core_upgrade = isset($params['wp_upgrade']) ? $params['wp_upgrade'] : array(); |
| 109 |
$upgrade_plugins = isset($params['upgrade_plugins']) ? $params['upgrade_plugins'] : array(); |
| 110 |
$upgrade_themes = isset($params['upgrade_themes']) ? $params['upgrade_themes'] : array(); |
| 111 |
|
| 112 |
$upgrades = array(); |
| 113 |
if(!empty($core_upgrade)){ |
| 114 |
$upgrades['core'] = $this->upgrade_core($core_upgrade); |
| 115 |
} |
| 116 |
if(!empty($upgrade_plugins)){ |
| 117 |
$plugin_files = array(); |
| 118 |
foreach($upgrade_plugins as $plugin){ |
| 119 |
$plugin_files[] = $plugin->file; |
| 120 |
} |
| 121 |
|
| 122 |
$upgrades['plugins'] = $this->upgrade_plugins($plugin_files); |
| 123 |
$this->mmb_maintenance_mode(false); |
| 124 |
} |
| 125 |
|
| 126 |
if(!empty($upgrade_themes)){ |
| 127 |
$theme_temps = array(); |
| 128 |
foreach($upgrade_themes as $theme){ |
| 129 |
$theme_temps[] = $theme['theme_tmp']; |
| 130 |
} |
| 131 |
|
| 132 |
$upgrades['themes'] = $this->upgrade_themes($theme_temps); |
| 133 |
$this->mmb_maintenance_mode(false); |
| 134 |
} |
| 135 |
ob_clean(); |
| 136 |
return $upgrades; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Upgrades WordPress locally |
| 141 |
* |
| 142 |
*/ |
| 143 |
function upgrade_core($current) |
| 144 |
{ |
| 145 |
ob_start(); |
| 146 |
global $mmb_wp_version, $wp_filesystem; |
| 147 |
$upgrader = new WP_Upgrader(); |
| 148 |
|
| 149 |
// Is an update available? |
| 150 |
if (!isset($current->response) || $current->response == 'latest') |
| 151 |
return array( |
| 152 |
'upgraded' => ' Upgraded successfully.' |
| 153 |
); |
| 154 |
|
| 155 |
$res = $upgrader->fs_connect(array( |
| 156 |
ABSPATH, |
| 157 |
WP_CONTENT_DIR |
| 158 |
)); |
| 159 |
if (is_wp_error($res)) |
| 160 |
return array( |
| 161 |
'error' => $this->mmb_get_error($res) |
| 162 |
); |
| 163 |
|
| 164 |
$wp_dir = trailingslashit($wp_filesystem->abspath()); |
| 165 |
|
| 166 |
$download = $upgrader->download_package($current->package); |
| 167 |
if (is_wp_error($download)) |
| 168 |
return array( |
| 169 |
'error' => $this->mmb_get_error($download) |
| 170 |
); |
| 171 |
|
| 172 |
$working_dir = $upgrader->unpack_package($download); |
| 173 |
if (is_wp_error($working_dir)) |
| 174 |
return array( |
| 175 |
'error' => $this->mmb_get_error($working_dir) |
| 176 |
); |
| 177 |
|
| 178 |
if (!$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true)) { |
| 179 |
$wp_filesystem->delete($working_dir, true); |
| 180 |
return array( |
| 181 |
'error' => 'Unable to move update files.' |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
$wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE); |
| 186 |
|
| 187 |
require(ABSPATH . 'wp-admin/includes/update-core.php'); |
| 188 |
|
| 189 |
|
| 190 |
$update_core = update_core($working_dir, $wp_dir); |
| 191 |
ob_end_clean(); |
| 192 |
|
| 193 |
if (is_wp_error($update_core)) |
| 194 |
return array( |
| 195 |
'error' => $this->mmb_get_error($update_core) |
| 196 |
); |
| 197 |
ob_end_flush(); |
| 198 |
return array( |
| 199 |
'upgraded' => ' Upgraded successfully.' |
| 200 |
); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
function upgrade_plugins($plugins = false){ |
| 205 |
if(!$plugins || empty($plugins)) |
| 206 |
return array( |
| 207 |
'error' => 'No plugin files for upgrade.' |
| 208 |
); |
| 209 |
|
| 210 |
if (class_exists('Plugin_Upgrader') && class_exists('Bulk_Plugin_Upgrader_Skin')) { |
| 211 |
$upgrader = new Plugin_Upgrader(new Bulk_Plugin_Upgrader_Skin(compact('nonce', 'url'))); |
| 212 |
$result = $upgrader->bulk_upgrade($plugins); |
| 213 |
ob_end_clean(); |
| 214 |
if (!empty($result)) { |
| 215 |
foreach ($result as $plugin_slug => $plugin_info) { |
| 216 |
$data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin_slug); |
| 217 |
if (!$plugin_info || is_wp_error($plugin_info)) { |
| 218 |
$return .= '<code title="Please upgrade manually">' . $data['Name'] . '</code> was not upgraded.<br />'; |
| 219 |
} else { |
| 220 |
$return .= '<code>' . $data['Name'] . '</code> successfully upgraded.<br />'; |
| 221 |
} |
| 222 |
} |
| 223 |
ob_end_clean(); |
| 224 |
return array( |
| 225 |
'upgraded' => $return |
| 226 |
); |
| 227 |
} |
| 228 |
else |
| 229 |
return array( |
| 230 |
'error' => 'Upgrade failed.' |
| 231 |
); |
| 232 |
} else { |
| 233 |
ob_end_clean(); |
| 234 |
return array( |
| 235 |
'error' => 'WordPress update required first' |
| 236 |
); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
function upgrade_themes($themes = false){ |
| 241 |
if(!$themes || empty($themes)) |
| 242 |
return array( |
| 243 |
'error' => 'No theme files for upgrade.' |
| 244 |
); |
| 245 |
|
| 246 |
if (class_exists('Theme_Upgrader') && class_exists('Bulk_Theme_Upgrader_Skin')) { |
| 247 |
$upgrader = new Theme_Upgrader(new Bulk_Theme_Upgrader_Skin(compact('title', 'nonce', 'url', 'theme'))); |
| 248 |
|
| 249 |
$result = $upgrader->bulk_upgrade($themes); |
| 250 |
|
| 251 |
$results = array(); |
| 252 |
if (!empty($result)) { |
| 253 |
foreach ($result as $theme_tmp => $info) { |
| 254 |
if (is_wp_error($info) || !$info) { |
| 255 |
$results[$theme_tmp] = '<code title="Please upgrade manually">' . $theme_tmp . '</code> was not upgraded.<br />'; |
| 256 |
} else { |
| 257 |
$results[$theme_tmp] = '<code>' . $theme_tmp . '</code> succesfully upgraded.<br />'; |
| 258 |
} |
| 259 |
} |
| 260 |
return array( |
| 261 |
'upgraded' => implode('', $results) |
| 262 |
); |
| 263 |
} else |
| 264 |
return array( |
| 265 |
'error' => 'Upgrade failed.' |
| 266 |
); |
| 267 |
} else { |
| 268 |
ob_end_clean(); |
| 269 |
return array( |
| 270 |
'error' => 'WordPress update required first' |
| 271 |
); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
function get_upgradable_plugins() |
| 276 |
{ |
| 277 |
$current = $this->mmb_get_transient('update_plugins'); |
| 278 |
$upgradable_plugins = array(); |
| 279 |
if (!empty($current->response)) { |
| 280 |
foreach ($current->response as $plugin_path => $plugin_data) { |
| 281 |
if (!function_exists('get_plugin_data')) |
| 282 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 283 |
$data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin_path); |
| 284 |
|
| 285 |
$current->response[$plugin_path]->name = $data['Name']; |
| 286 |
$current->response[$plugin_path]->old_version = $data['Version']; |
| 287 |
$current->response[$plugin_path]->file = $plugin_path; |
| 288 |
$upgradable_plugins[] = $current->response[$plugin_path]; |
| 289 |
} |
| 290 |
return $upgradable_plugins; |
| 291 |
} else |
| 292 |
return array(); |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
function get_upgradable_themes() |
| 297 |
{ |
| 298 |
$all_themes = get_themes(); |
| 299 |
$upgrade_themes = array(); |
| 300 |
|
| 301 |
$current = $this->mmb_get_transient('update_themes'); |
| 302 |
foreach ((array) $all_themes as $theme_template => $theme_data) { |
| 303 |
if (!empty($current->response)) { |
| 304 |
foreach ($current->response as $current_themes => $theme) { |
| 305 |
if ($theme_data['Template'] == $current_themes) { |
| 306 |
$current->response[$current_themes]['name'] = $theme_data['Name']; |
| 307 |
$current->response[$current_themes]['old_version'] = $theme_data['Version']; |
| 308 |
$current->response[$current_themes]['theme_tmp'] = $theme_data['Template']; |
| 309 |
$upgrade_themes[] = $current->response[$current_themes]; |
| 310 |
continue; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
return $upgrade_themes; |
| 317 |
} |
| 318 |
} |
| 319 |
?> |