public static function installPlugin($url, $activatePlugin = false) { $hasWPFileSystem = MainWP_Utility::getWPFilesystem(); /** @global WP_Filesystem_Base $wp_filesystem */ global $wp_filesystem; if (file_exists(ABSPATH . '/wp-admin/includes/screen.php')) { include_once ABSPATH . '/wp-admin/includes/screen.php'; } include_once ABSPATH . '/wp-admin/includes/template.php'; include_once ABSPATH . '/wp-admin/includes/misc.php'; include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php'; include_once ABSPATH . '/wp-admin/includes/plugin.php'; $installer = new WP_Upgrader(); $ssl_verifyhost = get_option('mainwp_sslVerifyCertificate'); $ssl_api_verifyhost = get_option('mainwp_api_sslVerifyCertificate') === false || get_option('mainwp_api_sslVerifyCertificate') == 1 ? 1 : 0; if ($ssl_verifyhost === '0' || $ssl_api_verifyhost == 0) { add_filter('http_request_args', array(MainWP_Extensions::getClassName(), 'noSSLFilterFunction'), 99, 2); } add_filter('http_request_args', array(MainWP_Extensions::getClassName(), 'http_request_reject_unsafe_urls'), 99, 2); $result = $installer->run(array('package' => $url, 'destination' => WP_PLUGIN_DIR, 'clear_destination' => false, 'clear_working' => true, 'hook_extra' => array())); remove_filter('http_request_args', array(MainWP_Extensions::getClassName(), 'http_request_reject_unsafe_urls'), 99, 2); if ($ssl_verifyhost === '0') { remove_filter('http_request_args', array(MainWP_Extensions::getClassName(), 'noSSLFilterFunction'), 99); } $error = $output = $plugin_slug = null; if (is_wp_error($result)) { $error = $result->get_error_codes(); if (is_array($error)) { if ($error[0] == 'folder_exists') { $error = __('Destination folder already exists.', 'mainwp'); } else { $error = implode(', ', $error); } } } else { $path = $result['destination']; foreach ($result['source_files'] as $srcFile) { // to fix bug if ($srcFile == 'readme.txt') { continue; } $thePlugin = get_plugin_data($path . $srcFile); if ($thePlugin != null && $thePlugin != '' && $thePlugin['Name'] != '') { $output .= __('Successfully installed the plugin', 'mainwp') . ' ' . $thePlugin['Name'] . ' ' . $thePlugin['Version']; $plugin_slug = $result['destination_name'] . '/' . $srcFile; if ($activatePlugin) { activate_plugin($path . $srcFile, '', false, true); do_action('mainwp_api_extension_activated', $path . $srcFile); } break; } } } if (!empty($error)) { $return['error'] = $error; } else { MainWP_Extensions::enableExtension($plugin_slug); $return['result'] = 'SUCCESS'; $return['output'] = $output; $return['slug'] = $plugin_slug; } return $return; }
public function upgrade_premium($premium = false) { if (!class_exists('WP_Upgrader')) { include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; } if (!$premium || empty($premium)) { return array('error' => 'No premium files for upgrade.'); } $upgrader = false; $pr_update = array(); $themes = array(); $plugins = array(); $result = array(); $premium_update = array(); $premium_update = apply_filters('mwp_premium_perform_update', $premium_update); if (!empty($premium_update)) { foreach ($premium as $pr) { foreach ($premium_update as $key => $update) { $update = array_change_key_case($update, CASE_LOWER); if ($update['name'] == $pr['name']) { // prepare bulk updates for premiums that use WordPress upgrader if (isset($update['type'])) { if ($update['type'] == 'plugin') { if (isset($update['slug']) && !empty($update['slug'])) { $plugins[$update['slug']] = $update; } } if ($update['type'] == 'theme') { if (isset($update['template']) && !empty($update['template'])) { $themes[$update['template']] = $update; } } } } else { unset($premium_update[$key]); } } } // try default wordpress upgrader if (!empty($plugins)) { $updateplugins = $this->upgrade_plugins(array_values($plugins)); if (!empty($updateplugins) && isset($updateplugins['upgraded'])) { foreach ($premium_update as $key => $update) { $update = array_change_key_case($update, CASE_LOWER); foreach ($updateplugins['upgraded'] as $slug => $upgrade) { if (isset($update['slug']) && $update['slug'] == $slug) { if ($upgrade == 1) { unset($premium_update[$key]); } $pr_update['plugins']['upgraded'][md5($update['name'])] = $upgrade; } } } } } //try direct install with overwrite if (!empty($premium_update)) { foreach ($premium_update as $update) { $update = array_change_key_case($update, CASE_LOWER); $update_result = false; if (isset($update['url'])) { if (defined('WP_INSTALLING') && file_exists(ABSPATH . '.maintenance')) { $pr_update[$update['type'] . 's']['upgraded'][md5($update['name'])] = 'Site under maintanace.'; } /** @handled class */ $upgrader_skin = new WP_Upgrader_Skin(); $upgrader_skin->done_header = true; /** @handled class */ $upgrader = new WP_Upgrader(); @($update_result = $upgrader->run(array('package' => $update['url'], 'destination' => isset($update['type']) && $update['type'] == 'theme' ? WP_CONTENT_DIR . '/themes' : WP_PLUGIN_DIR, 'clear_destination' => true, 'clear_working' => true, 'is_multi' => true, 'hook_extra' => array()))); $update_result = !$update_result || is_wp_error($update_result) ? $this->mmb_get_error($update_result) : 1; } else { if (isset($update['callback'])) { if (is_array($update['callback'])) { $update_result = call_user_func(array($update['callback'][0], $update['callback'][1])); } else { if (is_string($update['callback'])) { $update_result = call_user_func($update['callback']); } else { $update_result = 'Upgrade function "' . $update['callback'] . '" does not exists.'; } } $update_result = $update_result !== true ? $this->mmb_get_error($update_result) : 1; } else { $update_result = 'Bad update params.'; } } $pr_update[$update['type'] . 's']['upgraded'][md5($update['name'])] = $update_result; } } return $pr_update; } else { foreach ($premium as $pr) { $result[$pr['type'] . 's']['upgraded'][md5($pr['name'])] = 'This premium update is not registered.'; } return $result; } }
/** * Expects $_POST['type'] == plugin/theme * $_POST['list'] == 'theme1,theme2' or 'plugin1,plugin2' */ function upgradePluginTheme() { //Prevent disable/re-enable at upgrade define('DOING_CRON', true); MainWP_Helper::getWPFilesystem(); include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php'; // if (file_exists(ABSPATH . '/wp-admin/includes/deprecated.php')) include_once(ABSPATH . '/wp-admin/includes/deprecated.php'); if (file_exists(ABSPATH . '/wp-admin/includes/screen.php')) { include_once ABSPATH . '/wp-admin/includes/screen.php'; } if (file_exists(ABSPATH . '/wp-admin/includes/template.php')) { include_once ABSPATH . '/wp-admin/includes/template.php'; } if (file_exists(ABSPATH . '/wp-admin/includes/misc.php')) { include_once ABSPATH . '/wp-admin/includes/misc.php'; } include_once ABSPATH . '/wp-admin/includes/file.php'; include_once ABSPATH . '/wp-admin/includes/plugin.php'; $information = array(); $information['upgrades'] = array(); $mwp_premium_updates_todo = array(); $mwp_premium_updates_todo_slugs = array(); if (isset($_POST['type']) && 'plugin' === $_POST['type']) { include_once ABSPATH . '/wp-admin/includes/update.php'; if (null !== $this->filterFunction) { add_filter('pre_site_transient_update_plugins', $this->filterFunction, 99); } $plugins = explode(',', urldecode($_POST['list'])); // To fix: backupbuddy update if (in_array('backupbuddy/backupbuddy.php', $plugins)) { if (isset($GLOBALS['ithemes_updater_path'])) { if (!class_exists('Ithemes_Updater_Settings')) { require $GLOBALS['ithemes_updater_path'] . '/settings.php'; } if (class_exists('Ithemes_Updater_Settings')) { $ithemes_updater = new Ithemes_Updater_Settings(); $ithemes_updater->update(); } } } //// global $wp_current_filter; $wp_current_filter[] = 'load-plugins.php'; @wp_update_plugins(); $information['plugin_updates'] = get_plugin_updates(); $plugins = explode(',', urldecode($_POST['list'])); $premiumPlugins = array(); $premiumUpdates = get_option('mainwp_premium_updates'); if (is_array($premiumUpdates)) { $newPlugins = array(); foreach ($plugins as $plugin) { if (in_array($plugin, $premiumUpdates)) { $premiumPlugins[] = $plugin; } else { $newPlugins[] = $plugin; } } $plugins = $newPlugins; } if (count($plugins) > 0) { //@see wp-admin/update.php $upgrader = new Plugin_Upgrader(new Bulk_Plugin_Upgrader_Skin(compact('nonce', 'url'))); $result = $upgrader->bulk_upgrade($plugins); if (!empty($result)) { foreach ($result as $plugin => $info) { if (empty($info)) { $information['upgrades'][$plugin] = false; } else { $information['upgrades'][$plugin] = true; } } } else { MainWP_Helper::error(__('Bad request', 'mainwp-child')); } } if (count($premiumPlugins) > 0) { $mwp_premium_updates = apply_filters('mwp_premium_perform_update', array()); if (is_array($mwp_premium_updates) && is_array($premiumPlugins)) { foreach ($premiumPlugins as $premiumPlugin) { foreach ($mwp_premium_updates as $key => $update) { $slug = isset($update['slug']) ? $update['slug'] : $update['Name']; if (0 === strcmp($slug, $premiumPlugin)) { $mwp_premium_updates_todo[$key] = $update; $mwp_premium_updates_todo_slugs[] = $premiumPlugin; } } } } unset($mwp_premium_updates); $premiumUpgrader = new Plugin_Upgrader(new Bulk_Plugin_Upgrader_Skin(compact('nonce', 'url'))); } if (count($plugins) <= 0 && count($premiumPlugins) <= 0) { MainWP_Helper::error(__('Bad request', 'mainwp-child')); } if (null !== $this->filterFunction) { remove_filter('pre_site_transient_update_plugins', $this->filterFunction, 99); } } else { if (isset($_POST['type']) && 'theme' === $_POST['type']) { include_once ABSPATH . '/wp-admin/includes/update.php'; if (null !== $this->filterFunction) { add_filter('pre_site_transient_update_themes', $this->filterFunction, 99); } @wp_update_themes(); include_once ABSPATH . '/wp-admin/includes/theme.php'; $information['theme_updates'] = $this->upgrade_get_theme_updates(); $themes = explode(',', $_POST['list']); $premiumThemes = array(); $premiumUpdates = get_option('mainwp_premium_updates'); if (is_array($premiumUpdates)) { $newThemes = array(); foreach ($themes as $theme) { if (in_array($theme, $premiumUpdates)) { $premiumThemes[] = $theme; } else { $newThemes[] = $theme; } } $themes = $newThemes; } if (count($themes) > 0) { // To fix: optimizePressTheme update $addFilterToFixUpdate_optimizePressTheme = false; if (in_array('optimizePressTheme', $themes)) { $addFilterToFixUpdate_optimizePressTheme = true; add_filter('site_transient_update_themes', array($this, 'hookFixOptimizePressThemeUpdate'), 99); } //@see wp-admin/update.php $upgrader = new Theme_Upgrader(new Bulk_Theme_Upgrader_Skin(compact('nonce', 'url'))); $result = $upgrader->bulk_upgrade($themes); if ($addFilterToFixUpdate_optimizePressTheme) { remove_filter('site_transient_update_themes', array($this, 'hookFixOptimizePressThemeUpdate'), 99); } if (!empty($result)) { foreach ($result as $theme => $info) { if (empty($info)) { $information['upgrades'][$theme] = false; } else { $information['upgrades'][$theme] = true; } } } else { MainWP_Helper::error(__('Bad request', 'mainwp-child')); } } if (count($premiumThemes) > 0) { $mwp_premium_updates = apply_filters('mwp_premium_perform_update', array()); $mwp_premium_updates_todo = array(); $mwp_premium_updates_todo_slugs = array(); if (is_array($premiumThemes) && is_array($mwp_premium_updates)) { foreach ($premiumThemes as $premiumTheme) { foreach ($mwp_premium_updates as $key => $update) { $slug = isset($update['slug']) ? $update['slug'] : $update['Name']; if (0 === strcmp($slug, $premiumTheme)) { $mwp_premium_updates_todo[$key] = $update; $mwp_premium_updates_todo_slugs[] = $slug; } } } } unset($mwp_premium_updates); $premiumUpgrader = new Theme_Upgrader(new Bulk_Theme_Upgrader_Skin(compact('nonce', 'url'))); } if (count($themes) <= 0 && count($premiumThemes) <= 0) { MainWP_Helper::error(__('Bad request', 'mainwp-child')); } if (null !== $this->filterFunction) { remove_filter('pre_site_transient_update_themes', $this->filterFunction, 99); } } else { MainWP_Helper::error(__('Bad request', 'mainwp-child')); } } if (count($mwp_premium_updates_todo) > 0) { //Upgrade via WP //@see wp-admin/update.php $result = $premiumUpgrader->bulk_upgrade($mwp_premium_updates_todo_slugs); if (!empty($result)) { foreach ($result as $plugin => $info) { if (!empty($info)) { $information['upgrades'][$plugin] = true; foreach ($mwp_premium_updates_todo as $key => $update) { $slug = isset($update['slug']) ? $update['slug'] : $update['Name']; if (0 === strcmp($slug, $plugin)) { //unset($mwp_premium_updates_todo[$key]); } } } } } //Upgrade via callback foreach ($mwp_premium_updates_todo as $update) { $slug = isset($update['slug']) ? $update['slug'] : $update['Name']; if (isset($update['url'])) { $installer = new WP_Upgrader(); //@see wp-admin/includes/class-wp-upgrader.php $result = $installer->run(array('package' => $update['url'], 'destination' => 'plugin' === $update['type'] ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/themes', 'clear_destination' => true, 'clear_working' => true, 'hook_extra' => array())); $information['upgrades'][$slug] = !is_wp_error($result) && !empty($result); } else { if (isset($update['callback'])) { if (is_array($update['callback']) && isset($update['callback'][0]) && isset($update['callback'][1])) { $update_result = @call_user_func(array($update['callback'][0], $update['callback'][1])); $information['upgrades'][$slug] = $update_result && true; } else { if (is_string($update['callback'])) { $update_result = @call_user_func($update['callback']); $information['upgrades'][$slug] = $update_result && true; } else { $information['upgrades'][$slug] = false; } } } else { $information['upgrades'][$slug] = false; } } } } $information['sync'] = $this->getSiteStats(array(), false); MainWP_Helper::write($information); }
public static function settings_panel() { if (isset($_GET['wpak_action']) && $_GET['wpak_action'] == 'upload-theme') { if (!current_user_can('upload_plugins') && !current_user_can('wpak_edit_apps')) { wp_die(__('You do not have sufficient permissions to install WP AppKit themes on this site.', WpAppKit::i18n_domain)); } check_admin_referer('wpak-theme-upload'); include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; $file_upload = new File_Upload_Upgrader('themezip', 'package'); $file_type = wp_check_filetype($file_upload->filename); if ($file_type['ext'] == 'zip' && $file_type['type'] == 'application/zip') { $title = sprintf(__('Installing WP AppKit from uploaded file: %s', WpAppKit::i18n_domain), esc_html(basename($file_upload->filename))); $nonce = 'wpak-theme-upload'; $url = add_query_arg(array('package' => $file_upload->id)); // A nonce is passed to WP_Upgrader_Skin class, so wp_nonce_url() is called and url is escaped there... $upgrader = new WP_Upgrader(new WP_Upgrader_Skin(compact('title', 'nonce', 'url'))); $destination_folder_name = basename(sanitize_file_name($file_upload->filename), ".zip"); $result = $upgrader->run(array('package' => $file_upload->package, 'destination' => WpakThemes::get_themes_directory() . '/' . $destination_folder_name, 'clear_destination' => true, 'clear_working' => true, 'hook_extra' => array())); if ($result || is_wp_error($result)) { $file_upload->cleanup(); } if (!is_wp_error($result)) { echo sprintf(__("WP AppKit theme '%s' installed successfully!", WpAppKit::i18n_domain), $destination_folder_name); } else { _e('An error occured', WpAppKit::i18n_domain); echo ' : ' . $result->get_error_message(); } echo '<br/><br/><a href="' . esc_url(remove_query_arg('wpak_action')) . '">' . __('Back to theme upload form', WpAppKit::i18n_domain) . '</a>'; echo '<br/><br/><a href="' . admin_url() . '/edit.php?post_type=wpak_apps">' . __('Go to my WP AppKit app list', WpAppKit::i18n_domain) . '</a>'; } else { _e("Uploaded file must be a valid zip file", WpAppKit::i18n_domain); } } else { ?> <div class="wrap" id="wpak-settings"> <h2><?php _e('WP AppKit Themes upload', WpAppKit::i18n_domain); ?> </h2> <?php if (!empty($result['message'])) { ?> <div class="<?php echo $result['type']; ?> " ><p><?php echo $result['message']; ?> </p></div> <?php } ?> <div class="upload-plugin"> <p class="install-help"><?php _e('If you have a WP AppKit theme in a .zip format, you may install it by uploading it here.'); ?> </p> <form method="post" enctype="multipart/form-data" class="wp-upload-form" action="<?php echo esc_url(add_query_arg(array('wpak_action' => 'upload-theme'))); ?> "> <?php wp_nonce_field('wpak-theme-upload'); ?> <label class="screen-reader-text" for="themezip"><?php _e('WP AppKit Theme zip file', WpAppKit::i18n_domain); ?> </label> <input type="file" id="themezip" name="themezip" /> <?php submit_button(__('Install Now'), 'button', 'install-theme-submit', false); ?> </form> </div> </div> <?php } }