3 * A File upgrader class for WordPress.
5 * This set of classes are designed to be used to upgrade/install a local set of files on the filesystem via the Filesystem Abstraction classes.
7 * @link http://trac.wordpress.org/ticket/7875 consolidate plugin/theme/core upgrade/install functions
10 * @subpackage Upgrader
15 * WordPress Upgrader class for Upgrading/Installing a local set of files via the Filesystem Abstraction classes from a Zip file.
17 * @TODO More Detailed docs, for methods as well.
20 * @subpackage Upgrader
24 var $strings = array();
26 var $result = array();
28 function WP_Upgrader($skin = null) {
29 return $this->__construct($skin);
31 function __construct($skin = null) {
33 $this->skin = new WP_Upgrader_Skin();
39 $this->skin->set_upgrader($this);
40 $this->generic_strings();
43 function generic_strings() {
44 $this->strings['bad_request'] = __('Invalid Data provided.');
45 $this->strings['fs_unavailable'] = __('Could not access filesystem.');
46 $this->strings['fs_error'] = __('Filesystem error');
47 $this->strings['fs_no_root_dir'] = __('Unable to locate WordPress Root directory.');
48 $this->strings['fs_no_content_dir'] = __('Unable to locate WordPress Content directory (wp-content).');
49 $this->strings['fs_no_plugins_dir'] = __('Unable to locate WordPress Plugin directory.');
50 $this->strings['fs_no_themes_dir'] = __('Unable to locate WordPress Theme directory.');
51 $this->strings['fs_no_folder'] = __('Unable to locate needed folder (%s).');
53 $this->strings['download_failed'] = __('Download failed.');
54 $this->strings['installing_package'] = __('Installing the latest version.');
55 $this->strings['folder_exists'] = __('Destination folder already exists.');
56 $this->strings['mkdir_failed'] = __('Could not create directory.');
57 $this->strings['bad_package'] = __('Incompatible Archive');
59 $this->strings['maintenance_start'] = __('Enabling Maintenance mode.');
60 $this->strings['maintenance_end'] = __('Disabling Maintenance mode.');
63 function fs_connect( $directories = array() ) {
64 global $wp_filesystem;
66 if ( false === ($credentials = $this->skin->request_filesystem_credentials()) )
69 if ( ! WP_Filesystem($credentials) ) {
71 if ( is_object($wp_filesystem) && $wp_filesystem->errors->get_error_code() )
72 $error = $wp_filesystem->errors;
73 $this->skin->request_filesystem_credentials($error); //Failed to connect, Error and request again
77 if ( ! is_object($wp_filesystem) )
78 return new WP_Error('fs_unavailable', $this->strings['fs_unavailable'] );
80 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
81 return new WP_Error('fs_error', $this->strings['fs_error'], $wp_filesystem->errors);
83 foreach ( (array)$directories as $dir ) {
84 if ( ABSPATH == $dir && ! $wp_filesystem->abspath() )
85 return new WP_Error('fs_no_root_dir', $this->strings['fs_no_root_dir']);
87 elseif ( WP_CONTENT_DIR == $dir && ! $wp_filesystem->wp_content_dir() )
88 return new WP_Error('fs_no_content_dir', $this->strings['fs_no_content_dir']);
90 elseif ( WP_PLUGIN_DIR == $dir && ! $wp_filesystem->wp_plugins_dir() )
91 return new WP_Error('fs_no_plugins_dir', $this->strings['fs_no_plugins_dir']);
93 elseif ( WP_CONTENT_DIR . '/themes' == $dir && ! $wp_filesystem->find_folder(WP_CONTENT_DIR . '/themes') )
94 return new WP_Error('fs_no_themes_dir', $this->strings['fs_no_themes_dir']);
96 elseif ( ! $wp_filesystem->find_folder($dir) )
97 return new WP_Error('fs_no_folder', sprintf($strings['fs_no_folder'], $dir));
100 } //end fs_connect();
102 function download_package($package) {
104 if ( ! preg_match('!^(http|https|ftp)://!i', $package) && file_exists($package) ) //Local file or remote?
105 return $package; //must be a local file..
107 if ( empty($package) )
108 return new WP_Error('no_package', $this->strings['no_package']);
110 $this->skin->feedback('downloading_package', $package);
112 $download_file = download_url($package);
114 if ( is_wp_error($download_file) )
115 return new WP_Error('download_failed', $this->strings['download_failed'], $download_file->get_error_message());
117 return $download_file;
120 function unpack_package($package, $delete_package = true) {
121 global $wp_filesystem;
123 $this->skin->feedback('unpack_package');
125 $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
127 //Clean up contents of upgrade directory beforehand.
128 $upgrade_files = $wp_filesystem->dirlist($upgrade_folder);
129 if ( !empty($upgrade_files) ) {
130 foreach ( $upgrade_files as $file )
131 $wp_filesystem->delete($upgrade_folder . $file['name'], true);
134 //We need a working directory
135 $working_dir = $upgrade_folder . basename($package, '.zip');
137 // Clean up working directory
138 if ( $wp_filesystem->is_dir($working_dir) )
139 $wp_filesystem->delete($working_dir, true);
141 // Unzip package to working directory
142 $result = unzip_file($package, $working_dir); //TODO optimizations, Copy when Move/Rename would suffice?
144 // Once extracted, delete the package if required.
145 if ( $delete_package )
148 if ( is_wp_error($result) ) {
149 $wp_filesystem->delete($working_dir, true);
156 function install_package($args = array()) {
157 global $wp_filesystem;
158 $defaults = array( 'source' => '', 'destination' => '', //Please always pass these
159 'clear_destination' => false, 'clear_working' => false,
160 'hook_extra' => array());
162 $args = wp_parse_args($args, $defaults);
165 @set_time_limit( 300 );
167 if ( empty($source) || empty($destination) )
168 return new WP_Error('bad_request', $this->strings['bad_request']);
170 $this->skin->feedback('installing_package');
172 $res = apply_filters('upgrader_pre_install', true, $hook_extra);
173 if ( is_wp_error($res) )
176 //Retain the Original source and destinations
177 $remote_source = $source;
178 $local_destination = $destination;
180 $source_files = array_keys( $wp_filesystem->dirlist($remote_source) );
181 $remote_destination = $wp_filesystem->find_folder($local_destination);
183 //Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
184 if ( 1 == count($source_files) && $wp_filesystem->is_dir( trailingslashit($source) . $source_files[0] . '/') ) //Only one folder? Then we want its contents.
185 $source = trailingslashit($source) . trailingslashit($source_files[0]);
186 elseif ( count($source_files) == 0 )
187 return new WP_Error('bad_package', $this->strings['bad_package']); //There are no files?
188 //else //Its only a single file, The upgrader will use the foldername of this file as the destination folder. foldername is based on zip filename.
190 //Hook ability to change the source file location..
191 $source = apply_filters('upgrader_source_selection', $source, $remote_source, $this);
192 if ( is_wp_error($source) )
195 //Has the source location changed? If so, we need a new source_files list.
196 if ( $source !== $remote_source )
197 $source_files = array_keys( $wp_filesystem->dirlist($source) );
199 //Protection against deleting files in any important base directories.
200 if ( in_array( $destination, array(ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes') ) ) {
201 $remote_destination = trailingslashit($remote_destination) . trailingslashit(basename($source));
202 $destination = trailingslashit($destination) . trailingslashit(basename($source));
205 //If we're not clearing the destination folder, and something exists there allready, Bail.
206 if ( ! $clear_destination && $wp_filesystem->exists($remote_destination) ) {
207 $wp_filesystem->delete($remote_source, true); //Clear out the source files.
208 return new WP_Error('folder_exists', $this->strings['folder_exists'], $remote_destination );
209 } else if ( $clear_destination ) {
210 //We're going to clear the destination if theres something there
211 $this->skin->feedback('remove_old');
214 if ( $wp_filesystem->exists($remote_destination) )
215 $removed = $wp_filesystem->delete($remote_destination, true);
217 $removed = apply_filters('upgrader_clear_destination', $removed, $local_destination, $remote_destination, $hook_extra);
219 if ( is_wp_error($removed) )
221 else if ( ! $removed )
222 return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
225 //Create destination if needed
226 if ( !$wp_filesystem->exists($remote_destination) )
227 if ( !$wp_filesystem->mkdir($remote_destination, FS_CHMOD_DIR) )
228 return new WP_Error('mkdir_failed', $this->strings['mkdir_failed'], $remote_destination);
230 // Copy new version of item into place.
231 $result = copy_dir($source, $remote_destination);
232 if ( is_wp_error($result) ) {
233 if ( $clear_working )
234 $wp_filesystem->delete($remote_source, true);
238 //Clear the Working folder?
239 if ( $clear_working )
240 $wp_filesystem->delete($remote_source, true);
242 $destination_name = basename( str_replace($local_destination, '', $destination) );
243 if ( '.' == $destination_name )
244 $destination_name = '';
246 $this->result = compact('local_source', 'source', 'source_name', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination', 'delete_source_dir');
248 $res = apply_filters('upgrader_post_install', true, $hook_extra, $this->result);
249 if ( is_wp_error($res) ) {
250 $this->result = $res;
254 //Bombard the calling function will all the info which we've just used.
255 return $this->result;
258 function run($options) {
260 $defaults = array( 'package' => '', //Please always pass this.
261 'destination' => '', //And this
262 'clear_destination' => false,
263 'clear_working' => true,
264 'hook_extra' => array() //Pass any extra $hook_extra args here, this will be passed to any hooked filters.
267 $options = wp_parse_args($options, $defaults);
270 //Connect to the Filesystem first.
271 $res = $this->fs_connect( array(WP_CONTENT_DIR, $destination) );
272 if ( ! $res ) //Mainly for non-connected filesystem.
275 if ( is_wp_error($res) ) {
276 $this->skin->error($res);
280 $this->skin->header();
281 $this->skin->before();
283 //Download the package (Note, This just returns the filename of the file if the package is a local file)
284 $download = $this->download_package( $package );
285 if ( is_wp_error($download) ) {
286 $this->skin->error($download);
290 //Unzip's the file into a temporary directory
291 $working_dir = $this->unpack_package( $download );
292 if ( is_wp_error($working_dir) ) {
293 $this->skin->error($working_dir);
297 //With the given options, this installs it to the destination directory.
298 $result = $this->install_package( array(
299 'source' => $working_dir,
300 'destination' => $destination,
301 'clear_destination' => $clear_destination,
302 'clear_working' => $clear_working,
303 'hook_extra' => $hook_extra
305 $this->skin->set_result($result);
306 if ( is_wp_error($result) ) {
307 $this->skin->error($result);
308 $this->skin->feedback('process_failed');
311 $this->skin->feedback('process_success');
313 $this->skin->after();
314 $this->skin->footer();
318 function maintenance_mode($enable = false) {
319 global $wp_filesystem;
320 $file = $wp_filesystem->abspath() . '.maintenance';
322 $this->skin->feedback('maintenance_start');
323 // Create maintenance file to signal that we are upgrading
324 $maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
325 $wp_filesystem->delete($file);
326 $wp_filesystem->put_contents($file, $maintenance_string, FS_CHMOD_FILE);
327 } else if ( !$enable && $wp_filesystem->exists($file) ) {
328 $this->skin->feedback('maintenance_end');
329 $wp_filesystem->delete($file);
336 * Plugin Upgrader class for WordPress Plugins, It is designed to upgrade/install plugins from a local zip, remote zip URL, or uploaded zip file.
338 * @TODO More Detailed docs, for methods as well.
341 * @subpackage Upgrader
344 class Plugin_Upgrader extends WP_Upgrader {
348 function upgrade_strings() {
349 $this->strings['up_to_date'] = __('The plugin is at the latest version.');
350 $this->strings['no_package'] = __('Upgrade package not available.');
351 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>.');
352 $this->strings['unpack_package'] = __('Unpacking the update.');
353 $this->strings['deactivate_plugin'] = __('Deactivating the plugin.');
354 $this->strings['remove_old'] = __('Removing the old version of the plugin.');
355 $this->strings['remove_old_failed'] = __('Could not remove the old plugin.');
356 $this->strings['process_failed'] = __('Plugin upgrade Failed.');
357 $this->strings['process_success'] = __('Plugin upgraded successfully.');
360 function install_strings() {
361 $this->strings['no_package'] = __('Install package not available.');
362 $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>.');
363 $this->strings['unpack_package'] = __('Unpacking the package.');
364 $this->strings['installing_package'] = __('Installing the plugin.');
365 $this->strings['process_failed'] = __('Plugin Install Failed.');
366 $this->strings['process_success'] = __('Plugin Installed successfully.');
369 function install($package) {
372 $this->install_strings();
375 'package' => $package,
376 'destination' => WP_PLUGIN_DIR,
377 'clear_destination' => false, //Do not overwrite files.
378 'clear_working' => true,
379 'hook_extra' => array()
382 // Force refresh of plugin update information
383 delete_transient('update_plugins');
387 function upgrade($plugin) {
390 $this->upgrade_strings();
392 $current = get_transient( 'update_plugins' );
393 if ( !isset( $current->response[ $plugin ] ) ) {
394 $this->skin->set_result(false);
395 $this->skin->error('up_to_date');
396 $this->skin->after();
400 // Get the URL to the zip file
401 $r = $current->response[ $plugin ];
403 add_filter('upgrader_pre_install', array(&$this, 'deactivate_plugin_before_upgrade'), 10, 2);
404 add_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'), 10, 4);
405 //'source_selection' => array(&$this, 'source_selection'), //theres a track ticket to move up the directory for zip's which are made a bit differently, useful for non-.org plugins.
408 'package' => $r->package,
409 'destination' => WP_PLUGIN_DIR,
410 'clear_destination' => true,
411 'clear_working' => true,
412 'hook_extra' => array(
417 //Cleanup our hooks, incase something else does a upgrade on this connection.
418 remove_filter('upgrader_pre_install', array(&$this, 'deactivate_plugin_before_upgrade'));
419 remove_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'));
421 if ( ! $this->result || is_wp_error($this->result) )
422 return $this->result;
424 // Force refresh of plugin update information
425 delete_transient('update_plugins');
428 //return plugin info.
429 function plugin_info() {
430 if ( ! is_array($this->result) )
432 if ( empty($this->result['destination_name']) )
435 $plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
436 if ( empty($plugin) )
439 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
441 return $this->result['destination_name'] . '/' . $pluginfiles[0];
444 //Hooked to pre_install
445 function deactivate_plugin_before_upgrade($return, $plugin) {
447 if ( is_wp_error($return) ) //Bypass.
450 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
451 if ( empty($plugin) )
452 return new WP_Error('bad_request', $this->strings['bad_request']);
454 if ( is_plugin_active($plugin) ) {
455 $this->skin->feedback('deactivate_plugin');
456 //Deactivate the plugin silently, Prevent deactivation hooks from running.
457 deactivate_plugins($plugin, true);
461 //Hooked to upgrade_clear_destination
462 function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
463 global $wp_filesystem;
465 if ( is_wp_error($removed) )
466 return $removed; //Pass errors through.
468 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
469 if ( empty($plugin) )
470 return new WP_Error('bad_request', $this->strings['bad_request']);
472 $plugins_dir = $wp_filesystem->wp_plugins_dir();
473 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
475 if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If its already vanished.
478 // If plugin is in its own directory, recursively delete the directory.
479 if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
480 $deleted = $wp_filesystem->delete($this_plugin_dir, true);
482 $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
485 return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
492 * Theme Upgrader class for WordPress Themes, It is designed to upgrade/install themes from a local zip, remote zip URL, or uploaded zip file.
494 * @TODO More Detailed docs, for methods as well.
497 * @subpackage Upgrader
500 class Theme_Upgrader extends WP_Upgrader {
504 function upgrade_strings() {
505 $this->strings['up_to_date'] = __('The theme is at the latest version.');
506 $this->strings['no_package'] = __('Upgrade package not available.');
507 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>.');
508 $this->strings['unpack_package'] = __('Unpacking the update.');
509 $this->strings['remove_old'] = __('Removing the old version of the theme.');
510 $this->strings['remove_old_failed'] = __('Could not remove the old theme.');
511 $this->strings['process_failed'] = __('Theme upgrade Failed.');
512 $this->strings['process_success'] = __('Theme upgraded successfully.');
515 function install_strings() {
516 $this->strings['no_package'] = __('Install package not available.');
517 $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>.');
518 $this->strings['unpack_package'] = __('Unpacking the package.');
519 $this->strings['installing_package'] = __('Installing the theme.');
520 $this->strings['process_failed'] = __('Theme Install Failed.');
521 $this->strings['process_success'] = __('Theme Installed successfully.');
524 function install($package) {
527 $this->install_strings();
530 'package' => $package,
531 'destination' => WP_CONTENT_DIR . '/themes',
532 'clear_destination' => false, //Do not overwrite files.
533 'clear_working' => true
536 $this->run($options);
538 if ( ! $this->result || is_wp_error($this->result) )
539 return $this->result;
541 // Force refresh of theme update information
542 delete_transient('update_themes');
544 if ( empty($result['destination_name']) )
547 return $result['destination_name'];
550 function upgrade($theme) {
553 $this->upgrade_strings();
555 // Is an update available?
556 $current = get_transient( 'update_themes' );
557 if ( !isset( $current->response[ $theme ] ) ) {
558 $this->skin->set_result(false);
559 $this->skin->error('up_to_date');
560 $this->skin->after();
564 $r = $current->response[ $theme ];
566 add_filter('upgrader_pre_install', array(&$this, 'current_before'), 10, 2);
567 add_filter('upgrader_post_install', array(&$this, 'current_after'), 10, 2);
568 add_filter('upgrader_clear_destination', array(&$this, 'delete_old_theme'), 10, 4);
571 'package' => $r['package'],
572 'destination' => WP_CONTENT_DIR . '/themes',
573 'clear_destination' => true,
574 'clear_working' => true,
575 'hook_extra' => array(
580 $this->run($options);
582 if ( ! $this->result || is_wp_error($this->result) )
583 return $this->result;
585 // Force refresh of theme update information
586 delete_transient('update_themes');
591 function current_before($return, $theme) {
593 if ( is_wp_error($return) )
596 $theme = isset($theme['theme']) ? $theme['theme'] : '';
598 if ( $theme != get_stylesheet() ) //If not current
600 //Change to maintainence mode now.
601 $this->maintenance_mode(true);
605 function current_after($return, $theme) {
606 if ( is_wp_error($return) )
609 $theme = isset($theme['theme']) ? $theme['theme'] : '';
611 if ( $theme != get_stylesheet() ) //If not current
614 //Ensure stylesheet name hasnt changed after the upgrade:
615 if ( $theme == get_stylesheet() && $theme != $this->result['destination_name'] ) {
616 $theme_info = $this->theme_info();
617 $stylesheet = $this->result['destination_name'];
618 $template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
619 switch_theme($template, $stylesheet, true);
622 //Time to remove maintainence mode
623 $this->maintenance_mode(false);
627 function delete_old_theme($removed, $local_destination, $remote_destination, $theme) {
628 global $wp_filesystem;
630 $theme = isset($theme['theme']) ? $theme['theme'] : '';
632 if ( is_wp_error($removed) || empty($theme) )
633 return $removed; //Pass errors through.
635 $themes_dir = $wp_filesystem->wp_themes_dir();
636 if ( $wp_filesystem->exists( trailingslashit($themes_dir) . $theme ) )
637 if ( ! $wp_filesystem->delete( trailingslashit($themes_dir) . $theme, true ) )
642 function theme_info() {
643 if ( empty($this->result['destination_name']) )
645 return get_theme_data(WP_CONTENT_DIR . '/themes/' . $this->result['destination_name'] . '/style.css');
651 * Core Upgrader class for WordPress. It allows for WordPress to upgrade itself in combiantion with the wp-admin/includes/update-core.php file
653 * @TODO More Detailed docs, for methods as well.
656 * @subpackage Upgrader
659 class Core_Upgrader extends WP_Upgrader {
661 function upgrade_strings() {
662 $this->strings['up_to_date'] = __('WordPress is at the latest version.');
663 $this->strings['no_package'] = __('Upgrade package not available.');
664 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>.');
665 $this->strings['unpack_package'] = __('Unpacking the update.');
666 $this->strings['copy_failed'] = __('Could not copy files.');
669 function upgrade($current) {
670 global $wp_filesystem;
673 $this->upgrade_strings();
675 if ( !empty($feedback) )
676 add_filter('update_feedback', $feedback);
678 // Is an update available?
679 if ( !isset( $current->response ) || $current->response == 'latest' )
680 return new WP_Error('up_to_date', $this->strings['up_to_date']);
682 $res = $this->fs_connect( array(ABSPATH, WP_CONTENT_DIR) );
683 if ( is_wp_error($res) )
686 $wp_dir = trailingslashit($wp_filesystem->abspath());
688 $download = $this->download_package( $current->package );
689 if ( is_wp_error($download) )
692 $working_dir = $this->unpack_package( $download );
693 if ( is_wp_error($working_dir) )
696 // Copy update-core.php from the new version into place.
697 if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
698 $wp_filesystem->delete($working_dir, true);
699 return new WP_Error('copy_failed', $this->strings['copy_failed']);
701 $wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
703 require(ABSPATH . 'wp-admin/includes/update-core.php');
705 return update_core($working_dir, $wp_dir);
711 * Generic Skin for the WordPress Upgrader classes. This skin is designed to be extended for specific purposes.
713 * @TODO More Detailed docs, for methods as well.
716 * @subpackage Upgrader
719 class WP_Upgrader_Skin {
722 var $done_header = false;
724 function WP_Upgrader_Skin($args = array()) {
725 return $this->__construct($args);
727 function __construct($args = array()) {
728 $defaults = array( 'url' => '', 'nonce' => '', 'title' => '', 'context' => false );
729 $this->options = wp_parse_args($args, $defaults);
732 function set_upgrader(&$upgrader) {
733 if ( is_object($upgrader) )
734 $this->upgrader =& $upgrader;
736 function set_result($result) {
737 $this->result = $result;
740 function request_filesystem_credentials($error = false) {
741 $url = $this->options['url'];
742 $context = $this->options['context'];
743 if ( !empty($this->options['nonce']) )
744 $url = wp_nonce_url($url, $this->options['nonce']);
745 return request_filesystem_credentials($url, '', $error, $context); //Possible to bring inline, Leaving as is for now.
749 if ( $this->done_header )
751 $this->done_header = true;
752 echo '<div class="wrap">';
754 echo '<h2>' . $this->options['title'] . '</h2>';
760 function error($errors) {
761 if ( ! $this->done_header )
763 if ( is_string($errors) ) {
764 $this->feedback($errors);
765 } elseif ( is_wp_error($errors) && $errors->get_error_code() ) {
766 foreach ( $errors->get_error_messages() as $message ) {
767 if ( $errors->get_error_data() )
768 $this->feedback($message . ' ' . $errors->get_error_data() );
770 $this->feedback($message);
775 function feedback($string) {
776 if ( isset( $this->upgrader->strings[$string] ) )
777 $string = $this->upgrader->strings[$string];
779 if ( strpos($string, '%') !== false ) {
780 $args = func_get_args();
781 $args = array_splice($args, 1);
783 $string = vsprintf($string, $args);
785 if ( empty($string) )
787 show_message($string);
795 * Plugin Upgrader Skin for WordPress Plugin Upgrades.
797 * @TODO More Detailed docs, for methods as well.
800 * @subpackage Upgrader
803 class Plugin_Upgrader_Skin extends WP_Upgrader_Skin {
805 var $plugin_active = false;
807 function Plugin_Upgrader_Skin($args = array()) {
808 return $this->__construct($args);
811 function __construct($args = array()) {
812 $defaults = array( 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => __('Upgrade Plugin') );
813 $args = wp_parse_args($args, $defaults);
815 $this->plugin = $args['plugin'];
817 $this->plugin_active = is_plugin_active($this->plugin);
819 parent::__construct($args);
823 $this->plugin = $this->upgrader->plugin_info();
824 if( !empty($this->plugin) && !is_wp_error($this->result) && $this->plugin_active ){
825 show_message(__('Attempting reactivation of the plugin'));
826 echo '<iframe style="border:0;overflow:hidden" width="100%" height="170px" src="' . wp_nonce_url('update.php?action=activate-plugin&plugin=' . $this->plugin, 'activate-plugin_' . $this->plugin) .'"></iframe>';
828 $update_actions = array(
829 'activate_plugin' => '<a href="' . wp_nonce_url('plugins.php?action=activate&plugin=' . $this->plugin, 'activate-plugin_' . $this->plugin) . '" title="' . esc_attr__('Activate this plugin') . '" target="_parent">' . __('Activate Plugin') . '</a>',
830 'plugins_page' => '<a href="' . admin_url('plugins.php') . '" title="' . esc_attr__('Goto plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>'
832 if ( $this->plugin_active )
833 unset( $update_actions['activate_plugin'] );
834 if ( ! $this->result || is_wp_error($this->result) )
835 unset( $update_actions['activate_plugin'] );
837 $update_actions = apply_filters('update_plugin_complete_actions', $update_actions, $this->plugin);
838 if ( ! empty($update_actions) )
839 $this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$update_actions));
844 * Plugin Installer Skin for WordPress Plugin Installer.
846 * @TODO More Detailed docs, for methods as well.
849 * @subpackage Upgrader
852 class Plugin_Installer_Skin extends WP_Upgrader_Skin {
856 function Plugin_Installer_Skin($args = array()) {
857 return $this->__construct($args);
860 function __construct($args = array()) {
861 $defaults = array( 'type' => 'web', 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => '' );
862 $args = wp_parse_args($args, $defaults);
864 $this->type = $args['type'];
865 $this->api = isset($args['api']) ? $args['api'] : array();
867 parent::__construct($args);
871 if ( !empty($this->api) )
872 $this->upgrader->strings['process_success'] = sprintf( __('Successfully installed the plugin <strong>%s %s</strong>.'), $this->api->name, $this->api->version);
877 $plugin_file = $this->upgrader->plugin_info();
879 $install_actions = array(
880 'activate_plugin' => '<a href="' . wp_nonce_url('plugins.php?action=activate&plugin=' . $plugin_file, 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin') . '" target="_parent">' . __('Activate Plugin') . '</a>',
883 if ( $this->type == 'web' )
884 $install_actions['plugins_page'] = '<a href="' . admin_url('plugin-install.php') . '" title="' . esc_attr__('Return to Plugin Installer') . '" target="_parent">' . __('Return to Plugin Installer') . '</a>';
886 $install_actions['plugins_page'] = '<a href="' . admin_url('plugins.php') . '" title="' . esc_attr__('Return to Plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>';
889 if ( ! $this->result || is_wp_error($this->result) )
890 unset( $install_actions['activate_plugin'] );
892 $install_actions = apply_filters('install_plugin_complete_actions', $install_actions, $this->api, $plugin_file);
893 if ( ! empty($install_actions) )
894 $this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
899 * Theme Installer Skin for the WordPress Theme Installer.
901 * @TODO More Detailed docs, for methods as well.
904 * @subpackage Upgrader
907 class Theme_Installer_Skin extends WP_Upgrader_Skin {
911 function Theme_Installer_Skin($args = array()) {
912 return $this->__construct($args);
915 function __construct($args = array()) {
916 $defaults = array( 'type' => 'web', 'url' => '', 'theme' => '', 'nonce' => '', 'title' => '' );
917 $args = wp_parse_args($args, $defaults);
919 $this->type = $args['type'];
920 $this->api = isset($args['api']) ? $args['api'] : array();
922 parent::__construct($args);
926 if ( !empty($this->api) ) {
927 /* translators: 1: theme name, 2: version */
928 $this->upgrader->strings['process_success'] = sprintf( __('Successfully installed the theme <strong>%1$s %2$s</strong>.'), $this->api->name, $this->api->version);
933 if ( empty($this->upgrader->result['destination_name']) )
936 $theme_info = $this->upgrader->theme_info();
937 if ( empty($theme_info) )
939 $name = $theme_info['Name'];
940 $stylesheet = $this->upgrader->result['destination_name'];
941 $template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
943 $preview_link = htmlspecialchars( add_query_arg( array('preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'TB_iframe' => 'true' ), trailingslashit(esc_url(get_option('home'))) ) );
944 $activate_link = wp_nonce_url("themes.php?action=activate&template=" . urlencode($template) . "&stylesheet=" . urlencode($stylesheet), 'switch-theme_' . $template);
946 $install_actions = array(
947 'preview' => '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="' . esc_attr(sprintf(__('Preview “%s”'), $name)) . '">' . __('Preview') . '</a>',
948 'activate' => '<a href="' . $activate_link . '" class="activatelink" title="' . esc_attr( sprintf( __('Activate “%s”'), $name ) ) . '">' . __('Activate') . '</a>'
951 if ( $this->type == 'web' )
952 $install_actions['themes_page'] = '<a href="' . admin_url('theme-install.php') . '" title="' . esc_attr__('Return to Theme Installer') . '" target="_parent">' . __('Return to Theme Installer') . '</a>';
954 $install_actions['themes_page'] = '<a href="' . admin_url('themes.php') . '" title="' . esc_attr__('Themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>';
956 if ( ! $this->result || is_wp_error($this->result) )
957 unset( $install_actions['activate'], $install_actions['preview'] );
959 $install_actions = apply_filters('install_theme_complete_actions', $install_actions, $this->api, $stylesheet, $theme_info);
960 if ( ! empty($install_actions) )
961 $this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
966 * Theme Upgrader Skin for WordPress Theme Upgrades.
968 * @TODO More Detailed docs, for methods as well.
971 * @subpackage Upgrader
974 class Theme_Upgrader_Skin extends WP_Upgrader_Skin {
977 function Theme_Upgrader_Skin($args = array()) {
978 return $this->__construct($args);
981 function __construct($args = array()) {
982 $defaults = array( 'url' => '', 'theme' => '', 'nonce' => '', 'title' => __('Upgrade Theme') );
983 $args = wp_parse_args($args, $defaults);
985 $this->theme = $args['theme'];
987 parent::__construct($args);
992 if ( !empty($this->upgrader->result['destination_name']) &&
993 ($theme_info = $this->upgrader->theme_info()) &&
994 !empty($theme_info) ) {
996 $name = $theme_info['Name'];
997 $stylesheet = $this->upgrader->result['destination_name'];
998 $template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
1000 $preview_link = htmlspecialchars( add_query_arg( array('preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'TB_iframe' => 'true' ), trailingslashit(esc_url(get_option('home'))) ) );
1001 $activate_link = wp_nonce_url("themes.php?action=activate&template=" . urlencode($template) . "&stylesheet=" . urlencode($stylesheet), 'switch-theme_' . $template);
1003 $update_actions = array(
1004 'preview' => '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="' . esc_attr(sprintf(__('Preview “%s”'), $name)) . '">' . __('Preview') . '</a>',
1005 'activate' => '<a href="' . $activate_link . '" class="activatelink" title="' . esc_attr( sprintf( __('Activate “%s”'), $name ) ) . '">' . __('Activate') . '</a>',
1007 if ( ( ! $this->result || is_wp_error($this->result) ) || $stylesheet == get_stylesheet() )
1008 unset($update_actions['preview'], $update_actions['activate']);
1011 $update_actions['themes_page'] = '<a href="' . admin_url('themes.php') . '" title="' . esc_attr__('Return to Themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>';
1013 $update_actions = apply_filters('update_theme_complete_actions', $update_actions, $this->theme);
1014 if ( ! empty($update_actions) )
1015 $this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$update_actions));
1020 * Upgrade Skin helper for File uploads. This class handles the upload process and passes it as if its a local file to the Upgrade/Installer functions.
1022 * @TODO More Detailed docs, for methods as well.
1024 * @package WordPress
1025 * @subpackage Upgrader
1028 class File_Upload_Upgrader {
1032 function File_Upload_Upgrader($form, $urlholder) {
1033 return $this->__construct($form, $urlholder);
1035 function __construct($form, $urlholder) {
1036 if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
1037 wp_die($uploads['error']);
1039 if ( empty($_FILES[$form]['name']) && empty($_GET[$urlholder]) )
1040 wp_die(__('Please select a file'));
1042 if ( !empty($_FILES) )
1043 $this->filename = $_FILES[$form]['name'];
1044 else if ( isset($_GET[$urlholder]) )
1045 $this->filename = $_GET[$urlholder];
1047 //Handle a newly uploaded file, Else assume its already been uploaded
1048 if ( !empty($_FILES) ) {
1049 $this->filename = wp_unique_filename( $uploads['basedir'], $this->filename );
1050 $this->package = $uploads['basedir'] . '/' . $this->filename;
1052 // Move the file to the uploads dir
1053 if ( false === @ move_uploaded_file( $_FILES[$form]['tmp_name'], $this->package) )
1054 wp_die( sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path']));
1056 $this->package = $uploads['basedir'] . '/' . $this->filename;