| 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 |
if(!class_exists('WP_Upgrader')) |
| 63 |
include_once(ABSPATH.'wp-admin/includes/class-wp-upgrader.php'); |
| 64 |
|
| 65 |
$upgrader_skin = new WP_Upgrader_Skin(); |
| 66 |
$upgrader_skin->done_header = true; |
| 67 |
|
| 68 |
$upgrader = new WP_Upgrader($upgrader_skin); |
| 69 |
$destination = $type == 'themes' ? WP_CONTENT_DIR . '/themes' : WP_PLUGIN_DIR; |
| 70 |
$clear_destination = isset($clear_destination) ? $clear_destination : false; |
| 71 |
|
| 72 |
foreach ($package as $package_url) { |
| 73 |
$key = basename($package_url); |
| 74 |
$install_info[$key] = @$upgrader->run(array( |
| 75 |
'package' => $package_url, |
| 76 |
'destination' => $destination, |
| 77 |
'clear_destination' => $clear_destination, //Do not overwrite files. |
| 78 |
'clear_working' => true, |
| 79 |
'hook_extra' => array() |
| 80 |
)); |
| 81 |
} |
| 82 |
|
| 83 |
if ($activate) { |
| 84 |
if($type == 'plugins'){ |
| 85 |
include_once(ABSPATH.'wp-admin/includes/plugin.php'); |
| 86 |
$all_plugins = get_plugins(); |
| 87 |
foreach ($all_plugins as $plugin_slug => $plugin) { |
| 88 |
$plugin_dir = preg_split('/\//', $plugin_slug); |
| 89 |
foreach ($install_info as $key => $install) { |
| 90 |
if (!$install || is_wp_error($install)) |
| 91 |
continue; |
| 92 |
if ($install['destination_name'] == $plugin_dir[0]) { |
| 93 |
$install_info[$key]['activated'] = activate_plugin($plugin_slug, '', false); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
}else if(count($install_info) == 1){ |
| 98 |
global $wp_themes; |
| 99 |
include_once(ABSPATH.'wp-includes/theme.php'); |
| 100 |
|
| 101 |
$wp_themes = null; unset($wp_themes); //prevent theme data caching |
| 102 |
|
| 103 |
$all_themes = get_themes(); |
| 104 |
foreach ($all_themes as $theme_name => $theme_data) { |
| 105 |
foreach ($install_info as $key => $install) { |
| 106 |
if (!$install || is_wp_error($install)) |
| 107 |
continue; |
| 108 |
|
| 109 |
if ($theme_data['Template'] == $install['destination_name']) { |
| 110 |
$install_info[$key]['activated'] = switch_theme($theme_data['Template'], $theme_data['Stylesheet']); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
ob_clean(); |
| 117 |
$this->mmb_maintenance_mode(false); |
| 118 |
return $install_info; |
| 119 |
} |
| 120 |
|
| 121 |
function do_upgrade($params = null){ |
| 122 |
|
| 123 |
if($params == null || empty($params)) |
| 124 |
return array('failed' => 'No upgrades passed.'); |
| 125 |
|
| 126 |
if (!$this->is_server_writable()) { |
| 127 |
return array( |
| 128 |
'error' => 'Failed, please <a target="_blank" href="http://managewp.com/user-guide#ftp">add FTP details</a></a>' |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
$params = isset($params['upgrades_all']) ? $params['upgrades_all'] : $params; |
| 133 |
|
| 134 |
$core_upgrade = isset($params['wp_upgrade']) ? $params['wp_upgrade'] : array(); |
| 135 |
$upgrade_plugins = isset($params['upgrade_plugins']) ? $params['upgrade_plugins'] : array(); |
| 136 |
$upgrade_themes = isset($params['upgrade_themes']) ? $params['upgrade_themes'] : array(); |
| 137 |
|
| 138 |
$upgrades = array(); |
| 139 |
$premium_upgrades = array(); |
| 140 |
if(!empty($core_upgrade)){ |
| 141 |
$upgrades['core'] = $this->upgrade_core($core_upgrade); |
| 142 |
} |
| 143 |
|
| 144 |
if(!empty($upgrade_plugins)){ |
| 145 |
$plugin_files = array(); |
| 146 |
foreach($upgrade_plugins as $plugin){ |
| 147 |
if(isset($plugin->file)) |
| 148 |
$plugin_files[$plugin->file] = $plugin->old_version; |
| 149 |
else |
| 150 |
$premium_upgrades[md5($plugin->name)] = $plugin; |
| 151 |
} |
| 152 |
if(!empty($plugin_files)) |
| 153 |
$upgrades['plugins'] = $this->upgrade_plugins($plugin_files); |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
if(!empty($upgrade_themes)){ |
| 158 |
$theme_temps = array(); |
| 159 |
foreach($upgrade_themes as $theme){ |
| 160 |
if(isset($theme['theme_tmp'])) |
| 161 |
$theme_temps[] = $theme['theme_tmp']; |
| 162 |
else |
| 163 |
$premium_upgrades[md5($theme['name'])] = $theme; |
| 164 |
} |
| 165 |
|
| 166 |
if(!empty($theme_temps)) |
| 167 |
$upgrades['themes'] = $this->upgrade_themes($theme_temps); |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
if(!empty($premium_upgrades)) { |
| 172 |
$premium_upgrades = $this->upgrade_premium($premium_upgrades); |
| 173 |
if(!empty($premium_upgrades)){ |
| 174 |
if(!empty($upgrades)){ |
| 175 |
foreach($upgrades as $key => $val){ |
| 176 |
if(isset($premium_upgrades[$key])){ |
| 177 |
$upgrades[$key] = array_merge_recursive($upgrades[$key], $premium_upgrades[$key]); |
| 178 |
} |
| 179 |
} |
| 180 |
} else { |
| 181 |
$upgrades = $premium_upgrades; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
ob_clean(); |
| 186 |
$this->mmb_maintenance_mode(false); |
| 187 |
return $upgrades; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Upgrades WordPress locally |
| 192 |
* |
| 193 |
*/ |
| 194 |
function upgrade_core($current) |
| 195 |
{ |
| 196 |
ob_start(); |
| 197 |
if(!function_exists('wp_version_check')) |
| 198 |
include_once(ABSPATH.'/wp-admin/includes/update.php'); |
| 199 |
|
| 200 |
@wp_version_check(); |
| 201 |
|
| 202 |
$current_update = false; |
| 203 |
ob_end_flush(); |
| 204 |
ob_end_clean(); |
| 205 |
$core = $this->mmb_get_transient('update_core'); |
| 206 |
|
| 207 |
if(isset($core->updates) && !empty($core->updates)){ |
| 208 |
$updates = $core->updates[0]; |
| 209 |
$updated = $core->updates[0]; |
| 210 |
if ( !isset( $updated->response ) || $updated->response == 'latest' ) |
| 211 |
return array('upgraded' => ' updated'); |
| 212 |
|
| 213 |
if ($updated->response == "development" && $current->response == "upgrade") { |
| 214 |
return array('upgraded' => '<font color="#900">Unexpected error. Please upgrade manually.</font>'); |
| 215 |
} |
| 216 |
else if ($updated->response == $current->response || ($updated->response == "upgrade" && $current->response == "development")){ |
| 217 |
if($updated->locale != $current->locale){ |
| 218 |
foreach($updates as $update){ |
| 219 |
if($update->locale == $current->locale){ |
| 220 |
$current_update = $update; |
| 221 |
break; |
| 222 |
} |
| 223 |
} |
| 224 |
if($current_update == false) |
| 225 |
return array('error' => ' Localization mismatch. Try again.'); |
| 226 |
} else { |
| 227 |
$current_update = $updated; |
| 228 |
} |
| 229 |
} |
| 230 |
else |
| 231 |
return array('error' => ' Transient mismatch. Try again.'); |
| 232 |
} else |
| 233 |
return array('error' => ' Refresh transient failed. Try again.'); |
| 234 |
if($current_update != false){ |
| 235 |
global $mmb_wp_version, $wp_filesystem, $wp_version; |
| 236 |
|
| 237 |
if (version_compare($wp_version, '3.1.9', '>')) { |
| 238 |
if(!class_exists('Core_Upgrader')) |
| 239 |
include_once(ABSPATH.'wp-admin/includes/class-wp-upgrader.php'); |
| 240 |
|
| 241 |
$core = new Core_Upgrader(); |
| 242 |
$result = $core->upgrade($current_update); |
| 243 |
if(is_wp_error($result)){ |
| 244 |
return array( |
| 245 |
'error' => $this->mmb_get_error($result) |
| 246 |
); |
| 247 |
} |
| 248 |
else |
| 249 |
return array( |
| 250 |
'upgraded' => ' updated' |
| 251 |
); |
| 252 |
|
| 253 |
} else { |
| 254 |
if(!class_exists('WP_Upgrader')){ |
| 255 |
include_once(ABSPATH.'wp-admin/includes/update.php'); |
| 256 |
if(function_exists('wp_update_core')){ |
| 257 |
$result = wp_update_core($current_update); |
| 258 |
if(is_wp_error($result)){ |
| 259 |
return array( |
| 260 |
'error' => $this->mmb_get_error($result) |
| 261 |
); |
| 262 |
} |
| 263 |
else |
| 264 |
return array( |
| 265 |
'upgraded' => ' updated' |
| 266 |
); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
if(class_exists('WP_Upgrader')){ |
| 271 |
$upgrader_skin = new WP_Upgrader_Skin(); |
| 272 |
$upgrader_skin->done_header = true; |
| 273 |
|
| 274 |
$upgrader = new WP_Upgrader( $upgrader_skin ); |
| 275 |
|
| 276 |
// Is an update available? |
| 277 |
if (!isset($current_update->response) || $current_update->response == 'latest') |
| 278 |
return array( |
| 279 |
'upgraded' => ' updated' |
| 280 |
); |
| 281 |
|
| 282 |
$res = $upgrader->fs_connect(array( |
| 283 |
ABSPATH, |
| 284 |
WP_CONTENT_DIR |
| 285 |
)); |
| 286 |
if (is_wp_error($res)) |
| 287 |
return array( |
| 288 |
'error' => $this->mmb_get_error($res) |
| 289 |
); |
| 290 |
|
| 291 |
$wp_dir = trailingslashit($wp_filesystem->abspath()); |
| 292 |
|
| 293 |
$core_package = false; |
| 294 |
if(isset($current_update->package) && !empty($current_update->package)) |
| 295 |
$core_package = $current_update->package; |
| 296 |
elseif (isset($current_update->packages->full) && !empty($current_update->packages->full)) |
| 297 |
$core_package = $current_update->packages->full; |
| 298 |
|
| 299 |
$download = $upgrader->download_package($core_package); |
| 300 |
if (is_wp_error($download)) |
| 301 |
return array( |
| 302 |
'error' => $this->mmb_get_error($download) |
| 303 |
); |
| 304 |
|
| 305 |
$working_dir = $upgrader->unpack_package($download); |
| 306 |
if (is_wp_error($working_dir)) |
| 307 |
return array( |
| 308 |
'error' => $this->mmb_get_error($working_dir) |
| 309 |
); |
| 310 |
|
| 311 |
if (!$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true)) { |
| 312 |
$wp_filesystem->delete($working_dir, true); |
| 313 |
return array( |
| 314 |
'error' => 'Unable to move update files.' |
| 315 |
); |
| 316 |
} |
| 317 |
|
| 318 |
$wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE); |
| 319 |
|
| 320 |
require(ABSPATH . 'wp-admin/includes/update-core.php'); |
| 321 |
|
| 322 |
|
| 323 |
$update_core = update_core($working_dir, $wp_dir); |
| 324 |
ob_end_clean(); |
| 325 |
|
| 326 |
if (is_wp_error($update_core)) |
| 327 |
return array( |
| 328 |
'error' => $this->mmb_get_error($update_core) |
| 329 |
); |
| 330 |
ob_end_flush(); |
| 331 |
return array( |
| 332 |
'upgraded' => 'updated' |
| 333 |
); |
| 334 |
} else { |
| 335 |
return array( |
| 336 |
'error' => 'failed' |
| 337 |
); |
| 338 |
} |
| 339 |
} |
| 340 |
} else { |
| 341 |
return array( |
| 342 |
'error' => 'failed' |
| 343 |
); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
function upgrade_plugins($plugins = false){ |
| 348 |
if(!$plugins || empty($plugins)) |
| 349 |
return array( |
| 350 |
'error' => 'No plugin files for upgrade.' |
| 351 |
); |
| 352 |
$return = array(); |
| 353 |
if (class_exists('Plugin_Upgrader') && class_exists('Bulk_Plugin_Upgrader_Skin')) { |
| 354 |
|
| 355 |
$upgrader = new Plugin_Upgrader(new Bulk_Plugin_Upgrader_Skin(compact('nonce', 'url'))); |
| 356 |
$result = $upgrader->bulk_upgrade(array_keys($plugins)); |
| 357 |
|
| 358 |
if( !function_exists('wp_update_plugins') ) |
| 359 |
include_once(ABSPATH . 'wp-includes/update.php'); |
| 360 |
|
| 361 |
@wp_update_plugins(); |
| 362 |
$current_plugins = $this->mmb_get_transient('update_plugins'); |
| 363 |
|
| 364 |
if (!empty($result)) { |
| 365 |
foreach ($result as $plugin_slug => $plugin_info) { |
| 366 |
if (!$plugin_info || is_wp_error($plugin_info)) { |
| 367 |
$return[$plugin_slug] = $this->mmb_get_error($plugin_info); |
| 368 |
} else { |
| 369 |
if(isset($current_plugins->response[$plugin_slug]) && !empty($current_plugins->response[$plugin_slug])){ |
| 370 |
$return[$plugin_slug] = false; |
| 371 |
} else { |
| 372 |
$return[$plugin_slug] = 1; |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
ob_end_clean(); |
| 377 |
return array( |
| 378 |
'upgraded' => $return |
| 379 |
); |
| 380 |
} |
| 381 |
else |
| 382 |
return array( |
| 383 |
'error' => 'Upgrade failed.' |
| 384 |
); |
| 385 |
} else { |
| 386 |
ob_end_clean(); |
| 387 |
return array( |
| 388 |
'error' => 'WordPress update required first.' |
| 389 |
); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
function upgrade_themes($themes = false){ |
| 394 |
if(!$themes || empty($themes)) |
| 395 |
return array( |
| 396 |
'error' => 'No theme files for upgrade.' |
| 397 |
); |
| 398 |
if (class_exists('Theme_Upgrader') && class_exists('Bulk_Theme_Upgrader_Skin')) { |
| 399 |
|
| 400 |
$upgrader = new Theme_Upgrader(new Bulk_Theme_Upgrader_Skin(compact('title', 'nonce', 'url', 'theme'))); |
| 401 |
$result = $upgrader->bulk_upgrade($themes); |
| 402 |
|
| 403 |
if( !function_exists('wp_update_themes') ) |
| 404 |
include_once(ABSPATH . 'wp-includes/update.php'); |
| 405 |
|
| 406 |
@wp_update_themes(); |
| 407 |
$current_themes = $this->mmb_get_transient('update_themes'); |
| 408 |
|
| 409 |
$return = array(); |
| 410 |
if (!empty($result)) { |
| 411 |
foreach ($result as $theme_tmp => $theme_info) { |
| 412 |
if (is_wp_error($theme_info) || !$theme_info) { |
| 413 |
$return[$theme_tmp] = $this->mmb_get_error($theme_info); |
| 414 |
} else { |
| 415 |
if(isset($current_themes->response[$theme_tmp]) && !empty($current_themes->response[$theme_tmp])){ |
| 416 |
$return[$theme_tmp] = false; |
| 417 |
} else { |
| 418 |
$return[$theme_tmp] = 1; |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
return array( |
| 424 |
'upgraded' => $return |
| 425 |
); |
| 426 |
} else |
| 427 |
return array( |
| 428 |
'error' => 'Upgrade failed.' |
| 429 |
); |
| 430 |
} else { |
| 431 |
ob_end_clean(); |
| 432 |
return array( |
| 433 |
'error' => 'WordPress update required first' |
| 434 |
); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
function upgrade_premium($premium = false){ |
| 439 |
|
| 440 |
if(!class_exists('WP_Upgrader')) |
| 441 |
include_once(ABSPATH.'wp-admin/includes/class-wp-upgrader.php'); |
| 442 |
|
| 443 |
if(!$premium || empty($premium)) |
| 444 |
return array( |
| 445 |
'error' => 'No premium files for upgrade.' |
| 446 |
); |
| 447 |
|
| 448 |
$upgrader = false; |
| 449 |
$pr_update = array(); |
| 450 |
$result = array(); |
| 451 |
$premium_update = array(); |
| 452 |
$premium_update = apply_filters('mwp_premium_perform_update', $premium_update); |
| 453 |
|
| 454 |
if(!empty($premium_update)){ |
| 455 |
foreach ($premium as $pr) { |
| 456 |
foreach($premium_update as $update){ |
| 457 |
$update = array_change_key_case($update, CASE_LOWER); |
| 458 |
|
| 459 |
if( $update['name'] == $pr['name'] ){ |
| 460 |
$update_result = false; |
| 461 |
if( isset($update['url']) ){ |
| 462 |
|
| 463 |
if (defined('WP_INSTALLING') && file_exists(ABSPATH . '.maintenance')) |
| 464 |
$pr_update[$update['type'].'s']['upgraded'][md5($update['name'])] = 'Site under maintanace.'; |
| 465 |
|
| 466 |
if($upgrader == false){ |
| 467 |
$upgrader_skin = new WP_Upgrader_Skin(); |
| 468 |
$upgrader_skin->done_header = true; |
| 469 |
|
| 470 |
$upgrader = new WP_Upgrader(); |
| 471 |
} |
| 472 |
|
| 473 |
@$update_result = $upgrader->run(array( |
| 474 |
'package' => $update['url'], |
| 475 |
'destination' => isset($update['type']) && $update['type'] == 'theme' ? WP_CONTENT_DIR . '/themes' : WP_PLUGIN_DIR, |
| 476 |
'clear_destination' => true, |
| 477 |
'clear_working' => true, |
| 478 |
'hook_extra' => array() |
| 479 |
)); |
| 480 |
$update_result = !$update_result || is_wp_error($update_result) ? $this->mmb_get_error($update_result) : 1; |
| 481 |
|
| 482 |
} else if( isset($update['callback'])) { |
| 483 |
if( is_array($update['callback']) ) { |
| 484 |
$update_result = call_user_func( array( $update['callback'][0], $update['callback'][1] ) ); |
| 485 |
} |
| 486 |
else if ( is_string($update['callback']) ) { |
| 487 |
$update_result = call_user_func($update['callback']); |
| 488 |
} |
| 489 |
else { |
| 490 |
$update_result = 'Upgrade function "'.$update['callback'].'" does not exists.'; |
| 491 |
} |
| 492 |
|
| 493 |
$update_result = $update_result !== true ? $this->mmb_get_error($update_result) : 1; |
| 494 |
} else |
| 495 |
$update_result = 'Bad update params.'; |
| 496 |
|
| 497 |
$pr_update[$update['type'].'s']['upgraded'][md5($update['name'])] = $update_result; |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
return $pr_update; |
| 502 |
} else { |
| 503 |
foreach ($premium as $pr) { |
| 504 |
$result[$pr['type'].'s']['upgraded'][md5($pr['name'])] = 'This premium update is not registered.'; |
| 505 |
} |
| 506 |
return $result; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
function get_upgradable_plugins() { |
| 511 |
|
| 512 |
$current = $this->mmb_get_transient('update_plugins'); |
| 513 |
$upgradable_plugins = array(); |
| 514 |
if (!empty($current->response)) { |
| 515 |
if (!function_exists('get_plugin_data')) |
| 516 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 517 |
foreach ($current->response as $plugin_path => $plugin_data) { |
| 518 |
if($plugin_path == 'worker/init.php') |
| 519 |
continue; |
| 520 |
|
| 521 |
$data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin_path); |
| 522 |
if(strlen($data['Name']) > 0 && strlen($data['Version']) > 0) { |
| 523 |
$current->response[$plugin_path]->name = $data['Name']; |
| 524 |
$current->response[$plugin_path]->old_version = $data['Version']; |
| 525 |
$current->response[$plugin_path]->file = $plugin_path; |
| 526 |
$upgradable_plugins[] = $current->response[$plugin_path]; |
| 527 |
} |
| 528 |
} |
| 529 |
return $upgradable_plugins; |
| 530 |
} else |
| 531 |
return array(); |
| 532 |
} |
| 533 |
|
| 534 |
function get_upgradable_themes(){ |
| 535 |
|
| 536 |
$all_themes = get_themes(); |
| 537 |
$upgrade_themes = array(); |
| 538 |
|
| 539 |
$current = $this->mmb_get_transient('update_themes'); |
| 540 |
foreach ((array) $all_themes as $theme_template => $theme_data) { |
| 541 |
if (!empty($current->response)) { |
| 542 |
foreach ($current->response as $current_themes => $theme) { |
| 543 |
if ($theme_data['Template'] == $current_themes) { |
| 544 |
if(strlen($theme_data['Name']) > 0 && strlen($theme_data['Version']) > 0) { |
| 545 |
$current->response[$current_themes]['name'] = $theme_data['Name']; |
| 546 |
$current->response[$current_themes]['old_version'] = $theme_data['Version']; |
| 547 |
$current->response[$current_themes]['theme_tmp'] = $theme_data['Template']; |
| 548 |
$upgrade_themes[] = $current->response[$current_themes]; |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
return $upgrade_themes; |
| 556 |
} |
| 557 |
|
| 558 |
function get($args) |
| 559 |
{ |
| 560 |
if(empty($args)) |
| 561 |
return false; |
| 562 |
|
| 563 |
//Args: $items('plugins,'themes'), $type (active || inactive), $search(name string) |
| 564 |
|
| 565 |
$return = array(); |
| 566 |
if(is_array($args['items']) && in_array('plugins',$args['items'])){ |
| 567 |
$return['plugins'] = $this->get_plugins($args); |
| 568 |
} |
| 569 |
if(is_array($args['items']) && in_array('themes',$args['items'])){ |
| 570 |
$return['themes'] = $this->get_themes($args); |
| 571 |
} |
| 572 |
|
| 573 |
return $return; |
| 574 |
} |
| 575 |
|
| 576 |
function get_plugins($args) |
| 577 |
{ |
| 578 |
|
| 579 |
if(empty($args)) |
| 580 |
return false; |
| 581 |
|
| 582 |
extract($args); |
| 583 |
|
| 584 |
if(!function_exists('get_plugins')){ |
| 585 |
include_once(ABSPATH.'wp-admin/includes/plugin.php'); |
| 586 |
} |
| 587 |
$all_plugins = get_plugins(); |
| 588 |
$plugins = array('active' => array(), 'inactive' => array()); |
| 589 |
if(is_array($all_plugins) && !empty($all_plugins)){ |
| 590 |
$activated_plugins = get_option('active_plugins'); |
| 591 |
if(!$activated_plugins) |
| 592 |
$activated_plugins = array(); |
| 593 |
|
| 594 |
$br_a= 0; |
| 595 |
$br_i = 0; |
| 596 |
foreach($all_plugins as $path => $plugin){ |
| 597 |
if($plugin['Name'] != 'ManageWP - Worker'){ |
| 598 |
if(in_array($path,$activated_plugins)){ |
| 599 |
$plugins['active'][$br_a]['path'] = $path; |
| 600 |
$plugins['active'][$br_a]['name'] = $plugin['Name']; |
| 601 |
$br_a++; |
| 602 |
} |
| 603 |
|
| 604 |
if(!in_array($path,$activated_plugins)){ |
| 605 |
$plugins['inactive'][$br_i]['path'] = $path; |
| 606 |
$plugins['inactive'][$br_i]['name'] = $plugin['Name']; |
| 607 |
$br_i++; |
| 608 |
} |
| 609 |
|
| 610 |
} |
| 611 |
|
| 612 |
if($search){ |
| 613 |
foreach($plugins['active'] as $k => $plugin){ |
| 614 |
if(!stristr($plugin['name'],$search)){ |
| 615 |
unset($plugins['active'][$k]); |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
foreach($plugins['inactive'] as $k => $plugin){ |
| 620 |
if(!stristr($plugin['name'],$search)){ |
| 621 |
unset($plugins['inactive'][$k]); |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
return $plugins; |
| 629 |
} |
| 630 |
|
| 631 |
function get_themes($args) |
| 632 |
{ |
| 633 |
if(empty($args)) |
| 634 |
return false; |
| 635 |
|
| 636 |
extract($args); |
| 637 |
|
| 638 |
if(!function_exists('get_themes')){ |
| 639 |
include_once(ABSPATH.WPINC.'/theme.php'); |
| 640 |
} |
| 641 |
$all_themes = get_themes(); |
| 642 |
$themes = array('active' => array(), 'inactive' => array()); |
| 643 |
|
| 644 |
if(is_array($all_themes) && !empty($all_themes)){ |
| 645 |
$current_theme = get_current_theme(); |
| 646 |
|
| 647 |
$br_a = 0; |
| 648 |
$br_i = 0; |
| 649 |
foreach($all_themes as $theme_name => $theme){ |
| 650 |
if($current_theme == $theme_name){ |
| 651 |
$themes['active'][$br_a]['path'] = $theme['Template']; |
| 652 |
$themes['active'][$br_a]['name'] = $theme['Name']; |
| 653 |
$themes['active'][$br_a]['stylesheet'] = $theme['Stylesheet']; |
| 654 |
$br_a++; |
| 655 |
} |
| 656 |
|
| 657 |
if($current_theme != $theme_name){ |
| 658 |
$themes['inactive'][$br_i]['path'] = $theme['Template']; |
| 659 |
$themes['inactive'][$br_i]['name'] = $theme['Name']; |
| 660 |
$themes['inactive'][$br_i]['stylesheet'] = $theme['Stylesheet']; |
| 661 |
$br_i++; |
| 662 |
} |
| 663 |
|
| 664 |
} |
| 665 |
|
| 666 |
if($search){ |
| 667 |
foreach($themes['active'] as $k => $theme){ |
| 668 |
if(!stristr($theme['name'],$search)){ |
| 669 |
unset($themes['active'][$k]); |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
foreach($themes['inactive'] as $k => $theme){ |
| 674 |
if(!stristr($theme['name'],$search)){ |
| 675 |
unset($themes['inactive'][$k]); |
| 676 |
} |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
return $themes; |
| 682 |
} |
| 683 |
|
| 684 |
function edit($args) |
| 685 |
{ |
| 686 |
|
| 687 |
extract($args); |
| 688 |
$return = array(); |
| 689 |
if($type == 'plugins'){ |
| 690 |
$return['plugins'] = $this->edit_plugins($args); |
| 691 |
} elseif($type == 'themes'){ |
| 692 |
$return['themes'] = $this->edit_themes($args); |
| 693 |
} |
| 694 |
return $return; |
| 695 |
} |
| 696 |
|
| 697 |
function edit_plugins($args) |
| 698 |
{ |
| 699 |
extract($args); |
| 700 |
$return = array(); |
| 701 |
foreach($items as $item){ |
| 702 |
switch($items_edit_action){ |
| 703 |
case 'activate': |
| 704 |
$result = activate_plugin($item['path']); break; |
| 705 |
case 'deactivate': |
| 706 |
$result = deactivate_plugins(array($item['path'])); break; |
| 707 |
case 'delete': |
| 708 |
$result = delete_plugins(array($item['path'])); break; |
| 709 |
default: break; |
| 710 |
} |
| 711 |
|
| 712 |
if(is_wp_error($result)){ |
| 713 |
$result = array('error' => $result->get_error_message()); |
| 714 |
} elseif ($result === false) { |
| 715 |
$result = array('error'=>"Failed to perform action."); |
| 716 |
} else { |
| 717 |
$result = "OK"; |
| 718 |
} |
| 719 |
$return[$item['name']] = $result; |
| 720 |
} |
| 721 |
|
| 722 |
return $return; |
| 723 |
} |
| 724 |
|
| 725 |
function edit_themes($args) |
| 726 |
{ |
| 727 |
extract($args); |
| 728 |
$return = array(); |
| 729 |
foreach($items as $item){ |
| 730 |
switch($items_edit_action){ |
| 731 |
case 'activate': |
| 732 |
switch_theme($item['path'], $item['stylesheet']); break; |
| 733 |
case 'delete': |
| 734 |
$result = delete_theme($item['path']); break; |
| 735 |
default: break; |
| 736 |
} |
| 737 |
|
| 738 |
if(is_wp_error($result)){ |
| 739 |
$result = array('error' => $result->get_error_message()); |
| 740 |
} elseif ($result === false) { |
| 741 |
$result = array('error'=>"Failed to perform action."); |
| 742 |
} else { |
| 743 |
$result = "OK"; |
| 744 |
} |
| 745 |
$return[$item['name']] = $result; |
| 746 |
} |
| 747 |
|
| 748 |
return $return; |
| 749 |
|
| 750 |
} |
| 751 |
} |
| 752 |
?> |