/*************************************************************
* Copyright (c) 2011 Prelovac Media
**************************************************************/
class MMB_Installer extends MMB_Core
public function __construct()
@include_once ABSPATH.'wp-admin/includes/file.php';
@include_once ABSPATH.'wp-admin/includes/plugin.php';
@include_once ABSPATH.'wp-admin/includes/theme.php';
@include_once ABSPATH.'wp-admin/includes/misc.php';
@include_once ABSPATH.'wp-admin/includes/template.php';
@include_once ABSPATH.'wp-admin/includes/class-wp-upgrader.php';
if (!$this->check_if_pantheon() && !$wp_filesystem) {
public function mmb_maintenance_mode($enable = false, $maintenance_message = '')
$maintenance_message .= '<?php $upgrading = '.time().'; ?>';
$file = $wp_filesystem->abspath().'.maintenance';
$wp_filesystem->delete($file);
$wp_filesystem->put_contents($file, $maintenance_message, FS_CHMOD_FILE);
$wp_filesystem->delete($file);
public function install_remote_file($params)
$network_activate = isset($params['network_activate']) ? $params['network_activate'] : false;
if (!isset($package) || empty($package)) {
'error' => '<p>No files received. Internal error.</p>',
if (!$this->is_server_writable()) {
'error' => 'Failed, please <a target="_blank" href="http://managewp.com/user-guide/faq/my-pluginsthemes-fail-to-update-or-i-receive-a-yellow-ftp-warning">add FTP details</a>',
if (defined('WP_INSTALLING') && file_exists(ABSPATH.'.maintenance')) {
'error' => '<p>Site under maintanace.</p>',
if (!class_exists('WP_Upgrader')) {
include_once ABSPATH.'wp-admin/includes/class-wp-upgrader.php';
$upgrader = new WP_Upgrader(mwp_container()->getUpdaterSkin());
$destination = $type == 'themes' ? WP_CONTENT_DIR.'/themes' : WP_PLUGIN_DIR;
$clear_destination = isset($clear_destination) ? $clear_destination : false;
foreach ($package as $package_url) {
$key = basename($package_url);
$install_info[$key] = @$upgrader->run(
'package' => $package_url,
'destination' => $destination,
'clear_destination' => $clear_destination, //Do not overwrite files.
if (defined('WP_ADMIN') && WP_ADMIN) {
global $wp_current_filter;
$wp_current_filter[] = 'load-update-core.php';
if (function_exists('wp_clean_update_cache')) {
array_pop($wp_current_filter);
do_action('load-update-core.php');
wp_version_check(array(), true);
if ($type == 'plugins') {
include_once ABSPATH.'wp-admin/includes/plugin.php';
$all_plugins = get_plugins();
foreach ($all_plugins as $plugin_slug => $plugin) {
$plugin_dir = preg_split('/\//', $plugin_slug);
foreach ($install_info as $key => $install) {
if (!$install || is_wp_error($install)) {
if ($install['destination_name'] == $plugin_dir[0]) {
$install_info[$key]['activated'] = activate_plugin($plugin_slug, '', $network_activate);
$install_info[$key]['basename'] = $plugin_slug;
$install_info[$key]['full_name'] = $plugin['Name'];
$install_info[$key]['version'] = $plugin['Version'];
if (count($install_info) == 1) {
include_once ABSPATH.'wp-includes/theme.php';
unset($wp_themes); //prevent theme data caching
if (function_exists('wp_get_theme')) {
foreach ($install_info as $key => $install) {
if (!$install || is_wp_error($install)) {
$theme = wp_get_theme($install['destination_name']);
if ($theme->errors() !== false) {
$install_info[$key] = $theme->errors();
$install_info[$key]['activated'] = switch_theme($theme->Template, $theme->Stylesheet);
$install_info[$key]['full_name'] = $theme->name;
$install_info[$key]['version'] = $theme->version;
$all_themes = get_themes();
foreach ($all_themes as $theme_name => $theme_data) {
foreach ($install_info as $key => $install) {
if (!$install || is_wp_error($install)) {
if ($theme_data['Template'] == $install['destination_name'] || $theme_data['Stylesheet'] == $install['destination_name']) {
$install_info[$key]['activated'] = switch_theme($theme_data['Template'], $theme_data['Stylesheet']);
$install_info[$key]['full_name'] = $theme_data->name;
$install_info[$key]['version'] = $theme_data->version;
// Can generate "E_NOTICE: ob_clean(): failed to delete buffer. No buffer to delete."
$this->mmb_maintenance_mode(false);
if (mwp_container()->getRequestStack()->getMasterRequest()->getProtocol() >= 1) {
// WP_Error won't get JSON encoded, so unwrap the error here.
foreach ($install_info as $key => $value) {
if ($value instanceof WP_Error) {
$install_info[$key] = array(
'error' => $value->get_error_message(),
'code' => $value->get_error_code(),
} elseif ($wrongFileType) {
$otherType = $type === 'themes' ? 'plugins' : $type;
$install_info[$key] = array(
'error' => 'You can\'t install '.$type.' on '.$otherType.' page.',
'code' => 'wrong_type_of_file',
private function ithemes_updater_compatiblity()
// Check for the iThemes updater class
if (empty($GLOBALS['ithemes_updater_path']) ||
!file_exists($GLOBALS['ithemes_updater_path'].'/settings.php')
// Include iThemes updater
require_once $GLOBALS['ithemes_updater_path'].'/settings.php';
// Check if the updater is instantiated
if (empty($GLOBALS['ithemes-updater-settings'])) {
// Update the download link
$GLOBALS['ithemes-updater-settings']->flush('forced');
public function do_upgrade($params = null)
if ($params == null || empty($params)) {
'error' => 'No upgrades passed.',
if (!$this->is_server_writable()) {
'error' => 'Failed, please <a target="_blank" href="http://managewp.com/user-guide/faq/my-pluginsthemes-fail-to-update-or-i-receive-a-yellow-ftp-warning">add FTP details</a>',
$params = isset($params['upgrades_all']) ? $params['upgrades_all'] : $params;
$core_upgrade = isset($params['wp_upgrade']) ? $params['wp_upgrade'] : array();
$upgrade_plugins = isset($params['upgrade_plugins']) ? $params['upgrade_plugins'] : array();
$upgrade_themes = isset($params['upgrade_themes']) ? $params['upgrade_themes'] : array();
$upgrade_translations = isset($params['upgrade_translations']) ? $params['upgrade_translations'] : false;
$premium_upgrades = array();
if (!empty($core_upgrade)) {
$upgrades['core'] = $this->upgrade_core($core_upgrade);
if (!empty($upgrade_plugins)) {
$this->ithemes_updater_compatiblity();
foreach ($upgrade_plugins as $plugin) {
if (isset($plugin['envatoPlugin']) && $plugin['envatoPlugin'] === true) {
$upgrades['plugins'] = $this->upgrade_envato_component($plugin);
if (isset($plugin['file'])) {
$plugin_files[$plugin['file']] = $plugin['old_version'];
$premium_upgrades[md5($plugin['name'])] = $plugin;
if (!empty($plugin_files)) {
$upgrades['plugins'] = $this->upgrade_plugins($plugin_files);
$this->ithemes_updater_compatiblity();
if (!empty($upgrade_themes)) {
foreach ($upgrade_themes as $theme) {
if (isset($theme['envatoTheme']) && $theme['envatoTheme'] === true) {
$upgrades['themes'] = $this->upgrade_envato_component($theme);
if (isset($theme['theme_tmp'])) {
$theme_temps[] = $theme['theme_tmp'];
$premium_upgrades[md5($theme['name'])] = $theme;
if (!empty($theme_temps)) {
$upgrades['themes'] = $this->upgrade_themes($theme_temps);
if (!empty($upgrade_translations)) {
$upgrades['translations'] = $this->upgrade_translations();
$this->mmb_maintenance_mode(false);
* @param array $component
private function upgrade_envato_component(array $component)
$result = $this->install_remote_file($component);
'error' => 'Upgrade failed.',
foreach ($result as $component_slug => $component_info) {
if (!$component_info || is_wp_error($component_info)) {
$return[$component_slug] = $this->mmb_get_error($component_info);
$return[$component_info['destination_name']] = 1;
* Upgrades WordPress locally
public function upgrade_core($current)
if (file_exists(ABSPATH.'/wp-admin/includes/update.php')) {
include_once ABSPATH.'/wp-admin/includes/update.php';
$core = $this->mmb_get_transient('update_core');
if (isset($core->updates) && !empty($core->updates)) {
$updates = $core->updates[0];
$updated = $core->updates[0];
if (!isset($updated->response) || $updated->response == 'latest') {
'upgraded' => ' updated',
if ($updated->response == "development" && $current['response'] == "upgrade") {
'error' => '<font color="#900">Unexpected error. Please upgrade manually.</font>',
if ($updated->response == $current['response'] || ($updated->response == "upgrade" && $current['response'] == "development")) {
if ($updated->locale != $current['locale']) {
foreach ($updates as $update) {
if ($update->locale == $current['locale']) {
$current_update = $update;
if ($current_update == false) {
'error' => ' Localization mismatch. Try again.',
$current_update = $updated;
'error' => ' Transient mismatch. Try again.',
'error' => ' Refresh transient failed. Try again.',
if ($current_update != false) {
global $wp_filesystem, $wp_version;
if (version_compare($wp_version, '3.1.9', '>')) {
if (!class_exists('Core_Upgrader')) {
include_once ABSPATH.'wp-admin/includes/class-wp-upgrader.php';
$core = new Core_Upgrader(mwp_container()->getUpdaterSkin());
$result = $core->upgrade($current_update);
$this->mmb_maintenance_mode(false);
if (is_wp_error($result)) {
'error' => $this->mmb_get_error($result),
'upgraded' => ' updated',
if (!class_exists('WP_Upgrader')) {
include_once ABSPATH.'wp-admin/includes/update.php';
if (function_exists('wp_update_core')) {
$result = wp_update_core($current_update);
if (is_wp_error($result)) {
'error' => $this->mmb_get_error($result),
'upgraded' => ' updated',
if (class_exists('WP_Upgrader')) {
$upgrader_skin = new WP_Upgrader_Skin();
$upgrader_skin->done_header = true;
$upgrader = new WP_Upgrader($upgrader_skin);
// Is an update available?
if (!isset($current_update->response) || $current_update->response == 'latest') {
'upgraded' => ' updated',
$res = $upgrader->fs_connect(
'error' => $this->mmb_get_error($res),
$wp_dir = trailingslashit($wp_filesystem->abspath());
if (isset($current_update->package) && !empty($current_update->package)) {
$core_package = $current_update->package;
} elseif (isset($current_update->packages->full) && !empty($current_update->packages->full)) {
$core_package = $current_update->packages->full;
$download = $upgrader->download_package($core_package);
if (is_wp_error($download)) {
'error' => $this->mmb_get_error($download),
$working_dir = $upgrader->unpack_package($download);
if (is_wp_error($working_dir)) {
'error' => $this->mmb_get_error($working_dir),
if (!$wp_filesystem->copy($working_dir.'/wordpress/wp-admin/includes/update-core.php', $wp_dir.'wp-admin/includes/update-core.php', true)) {
$wp_filesystem->delete($working_dir, true);
'error' => 'Unable to move update files.',
$wp_filesystem->chmod($wp_dir.'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
require ABSPATH.'wp-admin/includes/update-core.php';