- }
-
- //Hooked to upgrade_clear_destination
- function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
- global $wp_filesystem;
-
- if ( is_wp_error($removed) )
- return $removed; //Pass errors through.
-
- $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
- if ( empty($plugin) )
- return new WP_Error('bad_request', $this->strings['bad_request']);
-
- $plugins_dir = $wp_filesystem->wp_plugins_dir();
- $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
-
- if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If its already vanished.
- return $removed;
-
- // If plugin is in its own directory, recursively delete the directory.
- if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
- $deleted = $wp_filesystem->delete($this_plugin_dir, true);
- else
- $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
-
- if ( ! $deleted )
- return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
-
- return $removed;
- }
-}
-
-/**
- * Theme Upgrader class for WordPress Themes, It is designed to upgrade/install themes from a local zip, remote zip URL, or uploaded zip file.
- *
- * @TODO More Detailed docs, for methods as well.
- *
- * @package WordPress
- * @subpackage Upgrader
- * @since 2.8.0
- */
-class Theme_Upgrader extends WP_Upgrader {
-
- var $result;
-
- function upgrade_strings() {
- $this->strings['up_to_date'] = __('The theme is at the latest version.');
- $this->strings['no_package'] = __('Update package not available.');
- $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>…');
- $this->strings['unpack_package'] = __('Unpacking the update…');
- $this->strings['remove_old'] = __('Removing the old version of the theme…');
- $this->strings['remove_old_failed'] = __('Could not remove the old theme.');
- $this->strings['process_failed'] = __('Theme update failed.');
- $this->strings['process_success'] = __('Theme updated successfully.');
- }
-
- function install_strings() {
- $this->strings['no_package'] = __('Install package not available.');
- $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>…');
- $this->strings['unpack_package'] = __('Unpacking the package…');
- $this->strings['installing_package'] = __('Installing the theme…');
- $this->strings['process_failed'] = __('Theme install failed.');
- $this->strings['process_success'] = __('Theme installed successfully.');
- }
-
- function install($package) {
-
- $this->init();
- $this->install_strings();
-
- $options = array(
- 'package' => $package,
- 'destination' => WP_CONTENT_DIR . '/themes',
- 'clear_destination' => false, //Do not overwrite files.
- 'clear_working' => true
- );
-
- $this->run($options);
-
- if ( ! $this->result || is_wp_error($this->result) )
- return $this->result;
-
- // Force refresh of theme update information
- delete_site_transient('update_themes');
-
- if ( empty($result['destination_name']) )
- return false;
- else
- return $result['destination_name'];
- }
-
- function upgrade($theme) {
-
- $this->init();
- $this->upgrade_strings();
-
- // Is an update available?
- $current = get_site_transient( 'update_themes' );
- if ( !isset( $current->response[ $theme ] ) ) {
- $this->skin->before();
- $this->skin->set_result(false);
- $this->skin->error('up_to_date');
- $this->skin->after();
- return false;
- }
-
- $r = $current->response[ $theme ];
-
- add_filter('upgrader_pre_install', array(&$this, 'current_before'), 10, 2);
- add_filter('upgrader_post_install', array(&$this, 'current_after'), 10, 2);
- add_filter('upgrader_clear_destination', array(&$this, 'delete_old_theme'), 10, 4);
-
- $options = array(
- 'package' => $r['package'],
- 'destination' => WP_CONTENT_DIR . '/themes',
- 'clear_destination' => true,
- 'clear_working' => true,
- 'hook_extra' => array(
- 'theme' => $theme
- )
- );
-
- $this->run($options);
-
- if ( ! $this->result || is_wp_error($this->result) )
- return $this->result;
-
- // Force refresh of theme update information
- delete_site_transient('update_themes');
-
- return true;
- }
-
- function bulk_upgrade($themes) {
-
- $this->init();
- $this->bulk = true;
- $this->upgrade_strings();
-
- $current = get_site_transient( 'update_themes' );
-
- add_filter('upgrader_pre_install', array(&$this, 'current_before'), 10, 2);
- add_filter('upgrader_post_install', array(&$this, 'current_after'), 10, 2);
- add_filter('upgrader_clear_destination', array(&$this, 'delete_old_theme'), 10, 4);
-
- $this->skin->header();
-
- // Connect to the Filesystem first.
- $res = $this->fs_connect( array(WP_CONTENT_DIR) );
- if ( ! $res ) {
- $this->skin->footer();
- return false;
- }
-
- $this->skin->bulk_header();
-
- // Only start maintenance mode if running in Multisite OR the theme is in use
- $maintenance = is_multisite(); // @TODO: This should only kick in for individual sites if at all possible.
- foreach ( $themes as $theme )
- $maintenance = $maintenance || $theme == get_stylesheet() || $theme == get_template();
- if ( $maintenance )
- $this->maintenance_mode(true);
-
- $results = array();
-
- $this->update_count = count($themes);
- $this->update_current = 0;
- foreach ( $themes as $theme ) {
- $this->update_current++;
-
- if ( !isset( $current->response[ $theme ] ) ) {
- $this->skin->set_result(false);
- $this->skin->before();
- $this->skin->error('up_to_date');
- $this->skin->after();
- $results[$theme] = false;
- continue;
- }
-
- $this->skin->theme_info = $this->theme_info($theme);
-
- // Get the URL to the zip file
- $r = $current->response[ $theme ];
-
- $options = array(
- 'package' => $r['package'],
- 'destination' => WP_CONTENT_DIR . '/themes',
- 'clear_destination' => true,
- 'clear_working' => true,
- 'hook_extra' => array(
- 'theme' => $theme
- )
- );
-
- $result = $this->run($options);
-
- $results[$theme] = $this->result;
-
- // Prevent credentials auth screen from displaying multiple times
- if ( false === $result )
- break;
- } //end foreach $plugins
-
- $this->maintenance_mode(false);
-
- $this->skin->bulk_footer();
-
- $this->skin->footer();
-
- // Cleanup our hooks, incase something else does a upgrade on this connection.
- remove_filter('upgrader_pre_install', array(&$this, 'current_before'), 10, 2);
- remove_filter('upgrader_post_install', array(&$this, 'current_after'), 10, 2);
- remove_filter('upgrader_clear_destination', array(&$this, 'delete_old_theme'), 10, 4);
-
- // Force refresh of theme update information
- delete_site_transient('update_themes');
-
- return $results;
- }
-
- function current_before($return, $theme) {
-
- if ( is_wp_error($return) )
- return $return;
-
- $theme = isset($theme['theme']) ? $theme['theme'] : '';
-
- if ( $theme != get_stylesheet() ) //If not current
- return $return;
- //Change to maintenance mode now.
- if ( ! $this->bulk )
- $this->maintenance_mode(true);
-
- return $return;
- }
- function current_after($return, $theme) {
- if ( is_wp_error($return) )
- return $return;
-
- $theme = isset($theme['theme']) ? $theme['theme'] : '';
-
- if ( $theme != get_stylesheet() ) //If not current
- return $return;
-
- //Ensure stylesheet name hasnt changed after the upgrade:
- // @TODO: Note, This doesnt handle the Template changing, or the Template name changing.
- if ( $theme == get_stylesheet() && $theme != $this->result['destination_name'] ) {
- $theme_info = $this->theme_info();
- $stylesheet = $this->result['destination_name'];
- $template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
- switch_theme($template, $stylesheet, true);
- }
-
- //Time to remove maintenance mode
- if ( ! $this->bulk )
- $this->maintenance_mode(false);
- return $return;
- }
-
- function delete_old_theme($removed, $local_destination, $remote_destination, $theme) {
- global $wp_filesystem;
-
- $theme = isset($theme['theme']) ? $theme['theme'] : '';