Wordpress 3.0
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-upgrader.php
1 <?php
2 /**
3  * A File upgrader class for WordPress.
4  *
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.
6  *
7  * @link http://trac.wordpress.org/ticket/7875 consolidate plugin/theme/core upgrade/install functions
8  *
9  * @package WordPress
10  * @subpackage Upgrader
11  * @since 2.8.0
12  */
13
14 /**
15  * WordPress Upgrader class for Upgrading/Installing a local set of files via the Filesystem Abstraction classes from a Zip file.
16  *
17  * @TODO More Detailed docs, for methods as well.
18  *
19  * @package WordPress
20  * @subpackage Upgrader
21  * @since 2.8.0
22  */
23 class WP_Upgrader {
24         var $strings = array();
25         var $skin = null;
26         var $result = array();
27
28         function WP_Upgrader($skin = null) {
29                 return $this->__construct($skin);
30         }
31         function __construct($skin = null) {
32                 if ( null == $skin )
33                         $this->skin = new WP_Upgrader_Skin();
34                 else
35                         $this->skin = $skin;
36         }
37
38         function init() {
39                 $this->skin->set_upgrader($this);
40                 $this->generic_strings();
41         }
42
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                 /* translators: %s: directory name */
52                 $this->strings['fs_no_folder'] = __('Unable to locate needed folder (%s).');
53
54                 $this->strings['download_failed'] = __('Download failed.');
55                 $this->strings['installing_package'] = __('Installing the latest version&#8230;');
56                 $this->strings['folder_exists'] = __('Destination folder already exists.');
57                 $this->strings['mkdir_failed'] = __('Could not create directory.');
58                 $this->strings['bad_package'] = __('Incompatible Archive.');
59
60                 $this->strings['maintenance_start'] = __('Enabling Maintenance mode&#8230;');
61                 $this->strings['maintenance_end'] = __('Disabling Maintenance mode&#8230;');
62         }
63
64         function fs_connect( $directories = array() ) {
65                 global $wp_filesystem;
66
67                 if ( false === ($credentials = $this->skin->request_filesystem_credentials()) )
68                         return false;
69
70                 if ( ! WP_Filesystem($credentials) ) {
71                         $error = true;
72                         if ( is_object($wp_filesystem) && $wp_filesystem->errors->get_error_code() )
73                                 $error = $wp_filesystem->errors;
74                         $this->skin->request_filesystem_credentials($error); //Failed to connect, Error and request again
75                         return false;
76                 }
77
78                 if ( ! is_object($wp_filesystem) )
79                         return new WP_Error('fs_unavailable', $this->strings['fs_unavailable'] );
80
81                 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
82                         return new WP_Error('fs_error', $this->strings['fs_error'], $wp_filesystem->errors);
83
84                 foreach ( (array)$directories as $dir ) {
85                         switch ( $dir ) {
86                                 case ABSPATH:
87                                         if ( ! $wp_filesystem->abspath() )
88                                                 return new WP_Error('fs_no_root_dir', $this->strings['fs_no_root_dir']);
89                                         break;
90                                 case WP_CONTENT_DIR:
91                                         if ( ! $wp_filesystem->wp_content_dir() )
92                                                 return new WP_Error('fs_no_content_dir', $this->strings['fs_no_content_dir']);
93                                         break;
94                                 case WP_PLUGIN_DIR:
95                                         if ( ! $wp_filesystem->wp_plugins_dir() )
96                                                 return new WP_Error('fs_no_plugins_dir', $this->strings['fs_no_plugins_dir']);
97                                         break;
98                                 case WP_CONTENT_DIR . '/themes':
99                                         if ( ! $wp_filesystem->find_folder(WP_CONTENT_DIR . '/themes') )
100                                                 return new WP_Error('fs_no_themes_dir', $this->strings['fs_no_themes_dir']);
101                                         break;
102                                 default:
103                                         if ( ! $wp_filesystem->find_folder($dir) )
104                                                 return new WP_Error('fs_no_folder', sprintf($this->strings['fs_no_folder'], $dir));
105                                         break;
106                         }
107                 }
108                 return true;
109         } //end fs_connect();
110
111         function download_package($package) {
112
113                 if ( ! preg_match('!^(http|https|ftp)://!i', $package) && file_exists($package) ) //Local file or remote?
114                         return $package; //must be a local file..
115
116                 if ( empty($package) )
117                         return new WP_Error('no_package', $this->strings['no_package']);
118
119                 $this->skin->feedback('downloading_package', $package);
120
121                 $download_file = download_url($package);
122
123                 if ( is_wp_error($download_file) )
124                         return new WP_Error('download_failed', $this->strings['download_failed'], $download_file->get_error_message());
125
126                 return $download_file;
127         }
128
129         function unpack_package($package, $delete_package = true) {
130                 global $wp_filesystem;
131
132                 $this->skin->feedback('unpack_package');
133
134                 $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
135
136                 //Clean up contents of upgrade directory beforehand.
137                 $upgrade_files = $wp_filesystem->dirlist($upgrade_folder);
138                 if ( !empty($upgrade_files) ) {
139                         foreach ( $upgrade_files as $file )
140                                 $wp_filesystem->delete($upgrade_folder . $file['name'], true);
141                 }
142
143                 //We need a working directory
144                 $working_dir = $upgrade_folder . basename($package, '.zip');
145
146                 // Clean up working directory
147                 if ( $wp_filesystem->is_dir($working_dir) )
148                         $wp_filesystem->delete($working_dir, true);
149
150                 // Unzip package to working directory
151                 $result = unzip_file($package, $working_dir); //TODO optimizations, Copy when Move/Rename would suffice?
152
153                 // Once extracted, delete the package if required.
154                 if ( $delete_package )
155                         unlink($package);
156
157                 if ( is_wp_error($result) ) {
158                         $wp_filesystem->delete($working_dir, true);
159                         return $result;
160                 }
161
162                 return $working_dir;
163         }
164
165         function install_package($args = array()) {
166                 global $wp_filesystem;
167                 $defaults = array( 'source' => '', 'destination' => '', //Please always pass these
168                                                 'clear_destination' => false, 'clear_working' => false,
169                                                 'hook_extra' => array());
170
171                 $args = wp_parse_args($args, $defaults);
172                 extract($args);
173
174                 @set_time_limit( 300 );
175
176                 if ( empty($source) || empty($destination) )
177                         return new WP_Error('bad_request', $this->strings['bad_request']);
178
179                 $this->skin->feedback('installing_package');
180
181                 $res = apply_filters('upgrader_pre_install', true, $hook_extra);
182                 if ( is_wp_error($res) )
183                         return $res;
184
185                 //Retain the Original source and destinations
186                 $remote_source = $source;
187                 $local_destination = $destination;
188
189                 $source_files = array_keys( $wp_filesystem->dirlist($remote_source) );
190                 $remote_destination = $wp_filesystem->find_folder($local_destination);
191
192                 //Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
193                 if ( 1 == count($source_files) && $wp_filesystem->is_dir( trailingslashit($source) . $source_files[0] . '/') ) //Only one folder? Then we want its contents.
194                         $source = trailingslashit($source) . trailingslashit($source_files[0]);
195                 elseif ( count($source_files) == 0 )
196                         return new WP_Error('bad_package', $this->strings['bad_package']); //There are no files?
197                 //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.
198
199                 //Hook ability to change the source file location..
200                 $source = apply_filters('upgrader_source_selection', $source, $remote_source, $this);
201                 if ( is_wp_error($source) )
202                         return $source;
203
204                 //Has the source location changed? If so, we need a new source_files list.
205                 if ( $source !== $remote_source )
206                         $source_files = array_keys( $wp_filesystem->dirlist($source) );
207
208                 //Protection against deleting files in any important base directories.
209                 if ( in_array( $destination, array(ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes') ) ) {
210                         $remote_destination = trailingslashit($remote_destination) . trailingslashit(basename($source));
211                         $destination = trailingslashit($destination) . trailingslashit(basename($source));
212                 }
213
214                 if ( $wp_filesystem->exists($remote_destination) ) {
215                         if ( $clear_destination ) {
216                                 //We're going to clear the destination if theres something there
217                                 $this->skin->feedback('remove_old');
218                                 $removed = $wp_filesystem->delete($remote_destination, true);
219                                 $removed = apply_filters('upgrader_clear_destination', $removed, $local_destination, $remote_destination, $hook_extra);
220
221                                 if ( is_wp_error($removed) )
222                                         return $removed;
223                                 else if ( ! $removed )
224                                         return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
225                         } else {
226                                 //If we're not clearing the destination folder and something exists there allready, Bail.
227                                 //But first check to see if there are actually any files in the folder.
228                                 $_files = $wp_filesystem->dirlist($remote_destination);
229                                 if ( ! empty($_files) ) {
230                                         $wp_filesystem->delete($remote_source, true); //Clear out the source files.
231                                         return new WP_Error('folder_exists', $this->strings['folder_exists'], $remote_destination );
232                                 }
233                         }
234                 }
235
236                 //Create destination if needed
237                 if ( !$wp_filesystem->exists($remote_destination) )
238                         if ( !$wp_filesystem->mkdir($remote_destination, FS_CHMOD_DIR) )
239                                 return new WP_Error('mkdir_failed', $this->strings['mkdir_failed'], $remote_destination);
240
241                 // Copy new version of item into place.
242                 $result = copy_dir($source, $remote_destination);
243                 if ( is_wp_error($result) ) {
244                         if ( $clear_working )
245                                 $wp_filesystem->delete($remote_source, true);
246                         return $result;
247                 }
248
249                 //Clear the Working folder?
250                 if ( $clear_working )
251                         $wp_filesystem->delete($remote_source, true);
252
253                 $destination_name = basename( str_replace($local_destination, '', $destination) );
254                 if ( '.' == $destination_name )
255                         $destination_name = '';
256
257                 $this->result = compact('local_source', 'source', 'source_name', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination', 'delete_source_dir');
258
259                 $res = apply_filters('upgrader_post_install', true, $hook_extra, $this->result);
260                 if ( is_wp_error($res) ) {
261                         $this->result = $res;
262                         return $res;
263                 }
264
265                 //Bombard the calling function will all the info which we've just used.
266                 return $this->result;
267         }
268
269         function run($options) {
270
271                 $defaults = array(      'package' => '', //Please always pass this.
272                                                         'destination' => '', //And this
273                                                         'clear_destination' => false,
274                                                         'clear_working' => true,
275                                                         'is_multi' => false,
276                                                         'hook_extra' => array() //Pass any extra $hook_extra args here, this will be passed to any hooked filters.
277                                                 );
278
279                 $options = wp_parse_args($options, $defaults);
280                 extract($options);
281
282                 //Connect to the Filesystem first.
283                 $res = $this->fs_connect( array(WP_CONTENT_DIR, $destination) );
284                 if ( ! $res ) //Mainly for non-connected filesystem.
285                         return false;
286
287                 if ( is_wp_error($res) ) {
288                         $this->skin->error($res);
289                         return $res;
290                 }
291
292                 if ( !$is_multi ) // call $this->header separately if running multiple times
293                         $this->skin->header();
294
295                 $this->skin->before();
296
297                 //Download the package (Note, This just returns the filename of the file if the package is a local file)
298                 $download = $this->download_package( $package );
299                 if ( is_wp_error($download) ) {
300                         $this->skin->error($download);
301                         $this->skin->after();
302                         return $download;
303                 }
304
305                 //Unzip's the file into a temporary directory
306                 $working_dir = $this->unpack_package( $download );
307                 if ( is_wp_error($working_dir) ) {
308                         $this->skin->error($working_dir);
309                         $this->skin->after();
310                         return $working_dir;
311                 }
312
313                 //With the given options, this installs it to the destination directory.
314                 $result = $this->install_package( array(
315                                                                                         'source' => $working_dir,
316                                                                                         'destination' => $destination,
317                                                                                         'clear_destination' => $clear_destination,
318                                                                                         'clear_working' => $clear_working,
319                                                                                         'hook_extra' => $hook_extra
320                                                                                 ) );
321                 $this->skin->set_result($result);
322                 if ( is_wp_error($result) ) {
323                         $this->skin->error($result);
324                         $this->skin->feedback('process_failed');
325                 } else {
326                         //Install Suceeded
327                         $this->skin->feedback('process_success');
328                 }
329                 $this->skin->after();
330
331                 if ( !$is_multi )
332                         $this->skin->footer();
333
334                 return $result;
335         }
336
337         function maintenance_mode($enable = false) {
338                 global $wp_filesystem;
339                 $file = $wp_filesystem->abspath() . '.maintenance';
340                 if ( $enable ) {
341                         $this->skin->feedback('maintenance_start');
342                         // Create maintenance file to signal that we are upgrading
343                         $maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
344                         $wp_filesystem->delete($file);
345                         $wp_filesystem->put_contents($file, $maintenance_string, FS_CHMOD_FILE);
346                 } else if ( !$enable && $wp_filesystem->exists($file) ) {
347                         $this->skin->feedback('maintenance_end');
348                         $wp_filesystem->delete($file);
349                 }
350         }
351
352 }
353
354 /**
355  * Plugin Upgrader class for WordPress Plugins, It is designed to upgrade/install plugins from a local zip, remote zip URL, or uploaded zip file.
356  *
357  * @TODO More Detailed docs, for methods as well.
358  *
359  * @package WordPress
360  * @subpackage Upgrader
361  * @since 2.8.0
362  */
363 class Plugin_Upgrader extends WP_Upgrader {
364
365         var $result;
366         var $bulk = false;
367         var $show_before = '';
368
369         function upgrade_strings() {
370                 $this->strings['up_to_date'] = __('The plugin is at the latest version.');
371                 $this->strings['no_package'] = __('Upgrade package not available.');
372                 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
373                 $this->strings['unpack_package'] = __('Unpacking the update&#8230;');
374                 $this->strings['deactivate_plugin'] = __('Deactivating the plugin&#8230;');
375                 $this->strings['remove_old'] = __('Removing the old version of the plugin&#8230;');
376                 $this->strings['remove_old_failed'] = __('Could not remove the old plugin.');
377                 $this->strings['process_failed'] = __('Plugin upgrade failed.');
378                 $this->strings['process_success'] = __('Plugin upgraded successfully.');
379         }
380
381         function install_strings() {
382                 $this->strings['no_package'] = __('Install package not available.');
383                 $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>&#8230;');
384                 $this->strings['unpack_package'] = __('Unpacking the package&#8230;');
385                 $this->strings['installing_package'] = __('Installing the plugin&#8230;');
386                 $this->strings['process_failed'] = __('Plugin install failed.');
387                 $this->strings['process_success'] = __('Plugin installed successfully.');
388         }
389
390         function install($package) {
391
392                 $this->init();
393                 $this->install_strings();
394
395                 $this->run(array(
396                                         'package' => $package,
397                                         'destination' => WP_PLUGIN_DIR,
398                                         'clear_destination' => false, //Do not overwrite files.
399                                         'clear_working' => true,
400                                         'hook_extra' => array()
401                                         ));
402
403                 // Force refresh of plugin update information
404                 delete_site_transient('update_plugins');
405
406         }
407
408         function upgrade($plugin) {
409
410                 $this->init();
411                 $this->upgrade_strings();
412
413                 $current = get_site_transient( 'update_plugins' );
414                 if ( !isset( $current->response[ $plugin ] ) ) {
415                         $this->skin->before();
416                         $this->skin->set_result(false);
417                         $this->skin->error('up_to_date');
418                         $this->skin->after();
419                         return false;
420                 }
421
422                 // Get the URL to the zip file
423                 $r = $current->response[ $plugin ];
424
425                 add_filter('upgrader_pre_install', array(&$this, 'deactivate_plugin_before_upgrade'), 10, 2);
426                 add_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'), 10, 4);
427                 //'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.
428
429                 $this->run(array(
430                                         'package' => $r->package,
431                                         'destination' => WP_PLUGIN_DIR,
432                                         'clear_destination' => true,
433                                         'clear_working' => true,
434                                         'hook_extra' => array(
435                                                                 'plugin' => $plugin
436                                         )
437                                 ));
438
439                 // Cleanup our hooks, incase something else does a upgrade on this connection.
440                 remove_filter('upgrader_pre_install', array(&$this, 'deactivate_plugin_before_upgrade'));
441                 remove_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'));
442
443                 if ( ! $this->result || is_wp_error($this->result) )
444                         return $this->result;
445
446                 // Force refresh of plugin update information
447                 delete_site_transient('update_plugins');
448         }
449
450         function bulk_upgrade($plugins) {
451
452                 $this->init();
453                 $this->bulk = true;
454                 $this->upgrade_strings();
455
456                 $current = get_site_transient( 'update_plugins' );
457
458                 add_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'), 10, 4);
459
460                 $this->skin->header();
461
462                 // Connect to the Filesystem first.
463                 $res = $this->fs_connect( array(WP_CONTENT_DIR, WP_PLUGIN_DIR) );
464                 if ( ! $res ) {
465                         $this->skin->footer();
466                         return false;
467                 }
468
469                 $this->skin->bulk_header();
470
471                 $this->maintenance_mode(true);
472
473                 $results = array();
474
475                 $this->update_count = count($plugins);
476                 $this->update_current = 0;
477                 foreach ( $plugins as $plugin ) {
478                         $this->update_current++;
479                         $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
480
481                         if ( !isset( $current->response[ $plugin ] ) ) {
482                                 $this->skin->set_result(false);
483                                 $this->skin->before();
484                                 $this->skin->error('up_to_date');
485                                 $this->skin->after();
486                                 $results[$plugin] = false;
487                                 continue;
488                         }
489
490                         // Get the URL to the zip file
491                         $r = $current->response[ $plugin ];
492
493                         $this->skin->plugin_active = is_plugin_active($plugin);
494
495                         $result = $this->run(array(
496                                                 'package' => $r->package,
497                                                 'destination' => WP_PLUGIN_DIR,
498                                                 'clear_destination' => true,
499                                                 'clear_working' => true,
500                                                 'is_multi' => true,
501                                                 'hook_extra' => array(
502                                                                         'plugin' => $plugin
503                                                 )
504                                         ));
505
506                         $results[$plugin] = $this->result;
507
508                         // Prevent credentials auth screen from displaying multiple times
509                         if ( false === $result )
510                                 break;
511                 } //end foreach $plugins
512
513                 $this->maintenance_mode(false);
514
515                 $this->skin->bulk_footer();
516
517                 $this->skin->footer();
518
519                 // Cleanup our hooks, incase something else does a upgrade on this connection.
520                 remove_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'));
521
522                 // Force refresh of plugin update information
523                 delete_site_transient('update_plugins');
524
525                 return $results;
526         }
527
528         //return plugin info.
529         function plugin_info() {
530                 if ( ! is_array($this->result) )
531                         return false;
532                 if ( empty($this->result['destination_name']) )
533                         return false;
534
535                 $plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
536                 if ( empty($plugin) )
537                         return false;
538
539                 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
540
541                 return $this->result['destination_name'] . '/' . $pluginfiles[0];
542         }
543
544         //Hooked to pre_install
545         function deactivate_plugin_before_upgrade($return, $plugin) {
546
547                 if ( is_wp_error($return) ) //Bypass.
548                         return $return;
549
550                 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
551                 if ( empty($plugin) )
552                         return new WP_Error('bad_request', $this->strings['bad_request']);
553
554                 if ( is_plugin_active($plugin) ) {
555                         $this->skin->feedback('deactivate_plugin');
556                         //Deactivate the plugin silently, Prevent deactivation hooks from running.
557                         deactivate_plugins($plugin, true);
558                 }
559         }
560
561         //Hooked to upgrade_clear_destination
562         function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
563                 global $wp_filesystem;
564
565                 if ( is_wp_error($removed) )
566                         return $removed; //Pass errors through.
567
568                 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
569                 if ( empty($plugin) )
570                         return new WP_Error('bad_request', $this->strings['bad_request']);
571
572                 $plugins_dir = $wp_filesystem->wp_plugins_dir();
573                 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
574
575                 if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If its already vanished.
576                         return $removed;
577
578                 // If plugin is in its own directory, recursively delete the directory.
579                 if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
580                         $deleted = $wp_filesystem->delete($this_plugin_dir, true);
581                 else
582                         $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
583
584                 if ( ! $deleted )
585                         return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
586
587                 return $removed;
588         }
589 }
590
591 /**
592  * Theme Upgrader class for WordPress Themes, It is designed to upgrade/install themes from a local zip, remote zip URL, or uploaded zip file.
593  *
594  * @TODO More Detailed docs, for methods as well.
595  *
596  * @package WordPress
597  * @subpackage Upgrader
598  * @since 2.8.0
599  */
600 class Theme_Upgrader extends WP_Upgrader {
601
602         var $result;
603
604         function upgrade_strings() {
605                 $this->strings['up_to_date'] = __('The theme is at the latest version.');
606                 $this->strings['no_package'] = __('Upgrade package not available.');
607                 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
608                 $this->strings['unpack_package'] = __('Unpacking the update&#8230;');
609                 $this->strings['remove_old'] = __('Removing the old version of the theme&#8230;');
610                 $this->strings['remove_old_failed'] = __('Could not remove the old theme.');
611                 $this->strings['process_failed'] = __('Theme upgrade failed.');
612                 $this->strings['process_success'] = __('Theme upgraded successfully.');
613         }
614
615         function install_strings() {
616                 $this->strings['no_package'] = __('Install package not available.');
617                 $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>&#8230;');
618                 $this->strings['unpack_package'] = __('Unpacking the package&#8230;');
619                 $this->strings['installing_package'] = __('Installing the theme&#8230;');
620                 $this->strings['process_failed'] = __('Theme install failed.');
621                 $this->strings['process_success'] = __('Theme installed successfully.');
622         }
623
624         function install($package) {
625
626                 $this->init();
627                 $this->install_strings();
628
629                 $options = array(
630                                                 'package' => $package,
631                                                 'destination' => WP_CONTENT_DIR . '/themes',
632                                                 'clear_destination' => false, //Do not overwrite files.
633                                                 'clear_working' => true
634                                                 );
635
636                 $this->run($options);
637
638                 if ( ! $this->result || is_wp_error($this->result) )
639                         return $this->result;
640
641                 // Force refresh of theme update information
642                 delete_site_transient('update_themes');
643
644                 if ( empty($result['destination_name']) )
645                         return false;
646                 else
647                         return $result['destination_name'];
648         }
649
650         function upgrade($theme) {
651
652                 $this->init();
653                 $this->upgrade_strings();
654
655                 // Is an update available?
656                 $current = get_site_transient( 'update_themes' );
657                 if ( !isset( $current->response[ $theme ] ) ) {
658                         $this->skin->before();
659                         $this->skin->set_result(false);
660                         $this->skin->error('up_to_date');
661                         $this->skin->after();
662                         return false;
663                 }
664
665                 $r = $current->response[ $theme ];
666
667                 add_filter('upgrader_pre_install', array(&$this, 'current_before'), 10, 2);
668                 add_filter('upgrader_post_install', array(&$this, 'current_after'), 10, 2);
669                 add_filter('upgrader_clear_destination', array(&$this, 'delete_old_theme'), 10, 4);
670
671                 $options = array(
672                                                 'package' => $r['package'],
673                                                 'destination' => WP_CONTENT_DIR . '/themes',
674                                                 'clear_destination' => true,
675                                                 'clear_working' => true,
676                                                 'hook_extra' => array(
677                                                                                         'theme' => $theme
678                                                                                         )
679                                                 );
680
681                 $this->run($options);
682
683                 if ( ! $this->result || is_wp_error($this->result) )
684                         return $this->result;
685
686                 // Force refresh of theme update information
687                 delete_site_transient('update_themes');
688
689                 return true;
690         }
691
692         function bulk_upgrade($themes) {
693
694                 $this->init();
695                 $this->bulk = true;
696                 $this->upgrade_strings();
697
698                 $current = get_site_transient( 'update_themes' );
699
700                 add_filter('upgrader_pre_install', array(&$this, 'current_before'), 10, 2);
701                 add_filter('upgrader_post_install', array(&$this, 'current_after'), 10, 2);
702                 add_filter('upgrader_clear_destination', array(&$this, 'delete_old_theme'), 10, 4);
703
704                 $this->skin->header();
705
706                 // Connect to the Filesystem first.
707                 $res = $this->fs_connect( array(WP_CONTENT_DIR) );
708                 if ( ! $res ) {
709                         $this->skin->footer();
710                         return false;
711                 }
712
713                 $this->skin->bulk_header();
714
715                 $this->maintenance_mode(true);
716
717                 $results = array();
718
719                 $this->update_count = count($themes);
720                 $this->update_current = 0;
721                 foreach ( $themes as $theme ) {
722                         $this->update_current++;
723
724                         if ( !isset( $current->response[ $theme ] ) ) {
725                                 $this->skin->set_result(false);
726                                 $this->skin->before();
727                                 $this->skin->error('up_to_date');
728                                 $this->skin->after();
729                                 $results[$theme] = false;
730                                 continue;
731                         }
732
733                         $this->skin->theme_info = $this->theme_info($theme);
734
735                         // Get the URL to the zip file
736                         $r = $current->response[ $theme ];
737
738                         $options = array(
739                                                         'package' => $r['package'],
740                                                         'destination' => WP_CONTENT_DIR . '/themes',
741                                                         'clear_destination' => true,
742                                                         'clear_working' => true,
743                                                         'hook_extra' => array(
744                                                                                                 'theme' => $theme
745                                                                                                 )
746                                                         );
747
748                         $result = $this->run($options);
749
750                         $results[$theme] = $this->result;
751
752                         // Prevent credentials auth screen from displaying multiple times
753                         if ( false === $result )
754                                 break;
755                 } //end foreach $plugins
756
757                 $this->maintenance_mode(false);
758
759                 $this->skin->bulk_footer();
760
761                 $this->skin->footer();
762
763                 // Cleanup our hooks, incase something else does a upgrade on this connection.
764                 remove_filter('upgrader_pre_install', array(&$this, 'current_before'), 10, 2);
765                 remove_filter('upgrader_post_install', array(&$this, 'current_after'), 10, 2);
766                 remove_filter('upgrader_clear_destination', array(&$this, 'delete_old_theme'), 10, 4);
767
768                 // Force refresh of theme update information
769                 delete_site_transient('update_themes');
770
771                 return $results;
772         }
773
774         function current_before($return, $theme) {
775
776                 if ( is_wp_error($return) )
777                         return $return;
778
779                 $theme = isset($theme['theme']) ? $theme['theme'] : '';
780
781                 if ( $theme != get_stylesheet() ) //If not current
782                         return $return;
783                 //Change to maintainence mode now.
784                 if ( ! $this->bulk )
785                         $this->maintenance_mode(true);
786
787                 return $return;
788         }
789         function current_after($return, $theme) {
790                 if ( is_wp_error($return) )
791                         return $return;
792
793                 $theme = isset($theme['theme']) ? $theme['theme'] : '';
794
795                 if ( $theme != get_stylesheet() ) //If not current
796                         return $return;
797
798                 //Ensure stylesheet name hasnt changed after the upgrade:
799                 if ( $theme == get_stylesheet() && $theme != $this->result['destination_name'] ) {
800                         $theme_info = $this->theme_info();
801                         $stylesheet = $this->result['destination_name'];
802                         $template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
803                         switch_theme($template, $stylesheet, true);
804                 }
805
806                 //Time to remove maintainence mode
807                 if ( ! $this->bulk )
808                         $this->maintenance_mode(false);
809                 return $return;
810         }
811
812         function delete_old_theme($removed, $local_destination, $remote_destination, $theme) {
813                 global $wp_filesystem;
814
815                 $theme = isset($theme['theme']) ? $theme['theme'] : '';
816
817                 if ( is_wp_error($removed) || empty($theme) )
818                         return $removed; //Pass errors through.
819
820                 $themes_dir = $wp_filesystem->wp_themes_dir();
821                 if ( $wp_filesystem->exists( trailingslashit($themes_dir) . $theme ) )
822                         if ( ! $wp_filesystem->delete( trailingslashit($themes_dir) . $theme, true ) )
823                                 return false;
824                 return true;
825         }
826
827         function theme_info($theme = null) {
828
829                 if ( empty($theme) ) {
830                         if ( !empty($this->result['destination_name']) )
831                                 $theme = $this->result['destination_name'];
832                         else
833                                 return false;
834                 }
835                 return get_theme_data(WP_CONTENT_DIR . '/themes/' . $theme . '/style.css');
836         }
837
838 }
839
840 /**
841  * Core Upgrader class for WordPress. It allows for WordPress to upgrade itself in combiantion with the wp-admin/includes/update-core.php file
842  *
843  * @TODO More Detailed docs, for methods as well.
844  *
845  * @package WordPress
846  * @subpackage Upgrader
847  * @since 2.8.0
848  */
849 class Core_Upgrader extends WP_Upgrader {
850
851         function upgrade_strings() {
852                 $this->strings['up_to_date'] = __('WordPress is at the latest version.');
853                 $this->strings['no_package'] = __('Upgrade package not available.');
854                 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
855                 $this->strings['unpack_package'] = __('Unpacking the update&#8230;');
856                 $this->strings['copy_failed'] = __('Could not copy files.');
857         }
858
859         function upgrade($current) {
860                 global $wp_filesystem;
861
862                 $this->init();
863                 $this->upgrade_strings();
864
865                 if ( !empty($feedback) )
866                         add_filter('update_feedback', $feedback);
867
868                 // Is an update available?
869                 if ( !isset( $current->response ) || $current->response == 'latest' )
870                         return new WP_Error('up_to_date', $this->strings['up_to_date']);
871
872                 $res = $this->fs_connect( array(ABSPATH, WP_CONTENT_DIR) );
873                 if ( is_wp_error($res) )
874                         return $res;
875
876                 $wp_dir = trailingslashit($wp_filesystem->abspath());
877
878                 $download = $this->download_package( $current->package );
879                 if ( is_wp_error($download) )
880                         return $download;
881
882                 $working_dir = $this->unpack_package( $download );
883                 if ( is_wp_error($working_dir) )
884                         return $working_dir;
885
886                 // Copy update-core.php from the new version into place.
887                 if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
888                         $wp_filesystem->delete($working_dir, true);
889                         return new WP_Error('copy_failed', $this->strings['copy_failed']);
890                 }
891                 $wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
892
893                 require(ABSPATH . 'wp-admin/includes/update-core.php');
894
895                 return update_core($working_dir, $wp_dir);
896         }
897
898 }
899
900 /**
901  * Generic Skin for the WordPress Upgrader classes. This skin is designed to be extended for specific purposes.
902  *
903  * @TODO More Detailed docs, for methods as well.
904  *
905  * @package WordPress
906  * @subpackage Upgrader
907  * @since 2.8.0
908  */
909 class WP_Upgrader_Skin {
910
911         var $upgrader;
912         var $done_header = false;
913         var $result = false;
914
915         function WP_Upgrader_Skin($args = array()) {
916                 return $this->__construct($args);
917         }
918         function __construct($args = array()) {
919                 $defaults = array( 'url' => '', 'nonce' => '', 'title' => '', 'context' => false );
920                 $this->options = wp_parse_args($args, $defaults);
921         }
922
923         function set_upgrader(&$upgrader) {
924                 if ( is_object($upgrader) )
925                         $this->upgrader =& $upgrader;
926                 $this->add_strings();
927         }
928
929         function add_strings() {
930         }
931
932         function set_result($result) {
933                 $this->result = $result;
934         }
935
936         function request_filesystem_credentials($error = false) {
937                 $url = $this->options['url'];
938                 $context = $this->options['context'];
939                 if ( !empty($this->options['nonce']) )
940                         $url = wp_nonce_url($url, $this->options['nonce']);
941                 return request_filesystem_credentials($url, '', $error, $context); //Possible to bring inline, Leaving as is for now.
942         }
943
944         function header() {
945                 if ( $this->done_header )
946                         return;
947                 $this->done_header = true;
948                 echo '<div class="wrap">';
949                 echo screen_icon();
950                 echo '<h2>' . $this->options['title'] . '</h2>';
951         }
952         function footer() {
953                 echo '</div>';
954         }
955
956         function error($errors) {
957                 if ( ! $this->done_header )
958                         $this->header();
959                 if ( is_string($errors) ) {
960                         $this->feedback($errors);
961                 } elseif ( is_wp_error($errors) && $errors->get_error_code() ) {
962                         foreach ( $errors->get_error_messages() as $message ) {
963                                 if ( $errors->get_error_data() )
964                                         $this->feedback($message . ' ' . $errors->get_error_data() );
965                                 else
966                                         $this->feedback($message);
967                         }
968                 }
969         }
970
971         function feedback($string) {
972                 if ( isset( $this->upgrader->strings[$string] ) )
973                         $string = $this->upgrader->strings[$string];
974
975                 if ( strpos($string, '%') !== false ) {
976                         $args = func_get_args();
977                         $args = array_splice($args, 1);
978                         if ( !empty($args) )
979                                 $string = vsprintf($string, $args);
980                 }
981                 if ( empty($string) )
982                         return;
983                 show_message($string);
984         }
985         function before() {}
986         function after() {}
987
988 }
989
990 /**
991  * Plugin Upgrader Skin for WordPress Plugin Upgrades.
992  *
993  * @TODO More Detailed docs, for methods as well.
994  *
995  * @package WordPress
996  * @subpackage Upgrader
997  * @since 2.8.0
998  */
999 class Plugin_Upgrader_Skin extends WP_Upgrader_Skin {
1000         var $plugin = '';
1001         var $plugin_active = false;
1002         var $plugin_network_active = false;
1003
1004         function Plugin_Upgrader_Skin($args = array()) {
1005                 return $this->__construct($args);
1006         }
1007
1008         function __construct($args = array()) {
1009                 $defaults = array( 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => __('Upgrade Plugin') );
1010                 $args = wp_parse_args($args, $defaults);
1011
1012                 $this->plugin = $args['plugin'];
1013
1014                 $this->plugin_active = is_plugin_active( $this->plugin );
1015                 $this->plugin_network_active = is_plugin_active_for_network( $this->plugin );
1016
1017                 parent::__construct($args);
1018         }
1019
1020         function after() {
1021                 $this->plugin = $this->upgrader->plugin_info();
1022                 if ( !empty($this->plugin) && !is_wp_error($this->result) && $this->plugin_active ){
1023                         show_message(__('Reactivating the plugin&#8230;'));
1024                         echo '<iframe style="border:0;overflow:hidden" width="100%" height="170px" src="' . wp_nonce_url('update.php?action=activate-plugin&networkwide=' . $this->plugin_network_active . '&plugin=' . $this->plugin, 'activate-plugin_' . $this->plugin) .'"></iframe>';
1025                 }
1026
1027                 $update_actions =  array(
1028                         'activate_plugin' => '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;plugin=' . $this->plugin, 'activate-plugin_' . $this->plugin) . '" title="' . esc_attr__('Activate this plugin') . '" target="_parent">' . __('Activate Plugin') . '</a>',
1029                         'plugins_page' => '<a href="' . admin_url('plugins.php') . '" title="' . esc_attr__('Goto plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>'
1030                 );
1031                 if ( $this->plugin_active )
1032                         unset( $update_actions['activate_plugin'] );
1033                 if ( ! $this->result || is_wp_error($this->result) )
1034                         unset( $update_actions['activate_plugin'] );
1035
1036                 $update_actions = apply_filters('update_plugin_complete_actions', $update_actions, $this->plugin);
1037                 if ( ! empty($update_actions) )
1038                         $this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$update_actions));
1039         }
1040
1041         function before() {
1042                 if ( $this->upgrader->show_before ) {
1043                         echo $this->upgrader->show_before;
1044                         $this->upgrader->show_before = '';
1045                 }
1046         }
1047 }
1048
1049 /**
1050  * Plugin Upgrader Skin for WordPress Plugin Upgrades.
1051  *
1052  * @package WordPress
1053  * @subpackage Upgrader
1054  * @since 3.0.0
1055  */
1056 class Bulk_Upgrader_Skin extends WP_Upgrader_Skin {
1057         var $in_loop = false;
1058         var $error = false;
1059
1060         function Bulk_Upgrader_Skin($args = array()) {
1061                 return $this->__construct($args);
1062         }
1063
1064         function __construct($args = array()) {
1065                 $defaults = array( 'url' => '', 'nonce' => '' );
1066                 $args = wp_parse_args($args, $defaults);
1067
1068                 parent::__construct($args);
1069         }
1070
1071         function add_strings() {
1072                 $this->upgrader->strings['skin_upgrade_start'] = __('The update process is starting. This process may take awhile on some hosts, so please be patient.');
1073                 $this->upgrader->strings['skin_update_failed_error'] = __('An error occured while updating %1$s: <strong>%2$s</strong>.');
1074                 $this->upgrader->strings['skin_update_failed'] = __('The update of %1$s failed.');
1075                 $this->upgrader->strings['skin_update_successful'] = __('%1$s updated successfully.').' <a onclick="%2$s" href="#" class="hide-if-no-js"><span>'.__('Show Details').'</span><span class="hidden">'.__('Hide Details').'</span>.</a>';
1076                 $this->upgrader->strings['skin_upgrade_end'] = __('All updates have been completed.');
1077         }
1078
1079         function feedback($string) {
1080                 if ( isset( $this->upgrader->strings[$string] ) )
1081                         $string = $this->upgrader->strings[$string];
1082
1083                 if ( strpos($string, '%') !== false ) {
1084                         $args = func_get_args();
1085                         $args = array_splice($args, 1);
1086                         if ( !empty($args) )
1087                                 $string = vsprintf($string, $args);
1088                 }
1089                 if ( empty($string) )
1090                         return;
1091                 if ( $this->in_loop )
1092                         echo "$string<br />\n";
1093                 else
1094                         echo "<p>$string</p>\n";
1095         }
1096
1097         function header() {
1098                 // Nothing, This will be displayed within a iframe.
1099         }
1100
1101         function footer() {
1102                 // Nothing, This will be displayed within a iframe.
1103         }
1104         function error($error) {
1105                 if ( is_string($error) && isset( $this->upgrader->strings[$error] ) )
1106                         $this->error = $this->upgrader->strings[$error];
1107
1108                 if ( is_wp_error($error) ) {
1109                         foreach ( $error->get_error_messages() as $emessage ) {
1110                                 if ( $error->get_error_data() )
1111                                         $messages[] = $emessage . ' ' . $error->get_error_data();
1112                                 else
1113                                         $messages[] = $emessage;
1114                         }
1115                         $this->error = implode(', ', $messages);
1116                 }
1117                 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').hide();</script>';
1118         }
1119
1120         function bulk_header() {
1121                 $this->feedback('skin_upgrade_start');
1122         }
1123
1124         function bulk_footer() {
1125                 $this->feedback('skin_upgrade_end');
1126         }
1127
1128         function before($title = '') {
1129                 $this->in_loop = true;
1130                 printf( '<h4>' . $this->upgrader->strings['skin_before_update_header'] . ' <img alt="" src="' . admin_url( 'images/wpspin_light.gif' ) . '" class="hidden waiting-' . $this->upgrader->update_current . '" style="vertical-align:middle;"></h4>',  $title, $this->upgrader->update_current, $this->upgrader->update_count);
1131                 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').show();</script>';
1132                 echo '<div class="update-messages hide-if-js" id="progress-' . esc_attr($this->upgrader->update_current) . '"><p>';
1133                 $this->flush_output();
1134         }
1135
1136         function after($title = '') {
1137                 echo '</p></div>';
1138                 if ( $this->error || ! $this->result ) {
1139                         if ( $this->error )
1140                                 echo '<div class="error"><p>' . sprintf($this->upgrader->strings['skin_update_failed_error'], $title, $this->error) . '</p></div>';
1141                         else
1142                                 echo '<div class="error"><p>' . sprintf($this->upgrader->strings['skin_update_failed'], $title) . '</p></div>';
1143
1144                         echo '<script type="text/javascript">jQuery(\'#progress-' . esc_js($this->upgrader->update_current) . '\').show();</script>';
1145                 }
1146                 if ( !empty($this->result) && !is_wp_error($this->result) ) {
1147                         echo '<div class="updated"><p>' . sprintf($this->upgrader->strings['skin_update_successful'], $title, 'jQuery(\'#progress-' . esc_js($this->upgrader->update_current) . '\').toggle();jQuery(\'span\', this).toggle(); return false;') . '</p></div>';
1148                         echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').hide();</script>';
1149                 }
1150
1151                 $this->reset();
1152                 $this->flush_output();
1153         }
1154
1155         function reset() {
1156                 $this->in_loop = false;
1157                 $this->error = false;
1158         }
1159
1160         function flush_output() {
1161                 wp_ob_end_flush_all();
1162                 flush();
1163         }
1164 }
1165
1166 class Bulk_Plugin_Upgrader_Skin extends Bulk_Upgrader_Skin {
1167         var $plugin_info = array(); // Plugin_Upgrader::bulk() will fill this in.
1168         function Plugin_Upgrader_Skin($args = array()) {
1169                 parent::__construct($args);
1170         }
1171
1172         function add_strings() {
1173                 parent::add_strings();
1174                 $this->upgrader->strings['skin_before_update_header'] = __('Updating Plugin %1$s (%2$d/%3$d)');
1175         }
1176
1177         function before() {
1178                 parent::before($this->plugin_info['Title']);
1179         }
1180
1181         function after() {
1182                 parent::after($this->plugin_info['Title']);
1183         }
1184         function bulk_footer() {
1185                 parent::bulk_footer();
1186                 $update_actions =  array(
1187                         'plugins_page' => '<a href="' . admin_url('plugins.php') . '" title="' . esc_attr__('Goto plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>',
1188                         'updates_page' => '<a href="' . admin_url('update-core.php') . '" title="' . esc_attr__('Goto WordPress Updates page') . '" target="_parent">' . __('Return to WordPress Updates') . '</a>'
1189                 );
1190
1191                 $update_actions = apply_filters('update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info);
1192                 if ( ! empty($update_actions) )
1193                         $this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$update_actions));
1194         }
1195 }
1196
1197 class Bulk_Theme_Upgrader_Skin extends Bulk_Upgrader_Skin {
1198         var $theme_info = array(); // Theme_Upgrader::bulk() will fill this in.
1199         function Theme_Upgrader_Skin($args = array()) {
1200                 parent::__construct($args);
1201         }
1202
1203         function add_strings() {
1204                 parent::add_strings();
1205                 $this->upgrader->strings['skin_before_update_header'] = __('Updating Theme %1$s (%2$d/%3$d)');
1206         }
1207
1208         function before() {
1209                 parent::before($this->theme_info['Name']);
1210         }
1211
1212         function after() {
1213                 parent::after($this->theme_info['Name']);
1214         }
1215         function bulk_footer() {
1216                 parent::bulk_footer();
1217                 $update_actions =  array(
1218                         'themes_page' => '<a href="' . admin_url('themes.php') . '" title="' . esc_attr__('Goto themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>',
1219                         'updates_page' => '<a href="' . admin_url('update-core.php') . '" title="' . esc_attr__('Goto WordPress Updates page') . '" target="_parent">' . __('Return to WordPress Updates') . '</a>'
1220                 );
1221
1222                 $update_actions = apply_filters('update_bulk_theme_complete_actions', $update_actions, $this->theme_info);
1223                 if ( ! empty($update_actions) )
1224                         $this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$update_actions));
1225         }
1226 }
1227
1228 /**
1229  * Plugin Installer Skin for WordPress Plugin Installer.
1230  *
1231  * @TODO More Detailed docs, for methods as well.
1232  *
1233  * @package WordPress
1234  * @subpackage Upgrader
1235  * @since 2.8.0
1236  */
1237 class Plugin_Installer_Skin extends WP_Upgrader_Skin {
1238         var $api;
1239         var $type;
1240
1241         function Plugin_Installer_Skin($args = array()) {
1242                 return $this->__construct($args);
1243         }
1244
1245         function __construct($args = array()) {
1246                 $defaults = array( 'type' => 'web', 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => '' );
1247                 $args = wp_parse_args($args, $defaults);
1248
1249                 $this->type = $args['type'];
1250                 $this->api = isset($args['api']) ? $args['api'] : array();
1251
1252                 parent::__construct($args);
1253         }
1254
1255         function before() {
1256                 if ( !empty($this->api) )
1257                         $this->upgrader->strings['process_success'] = sprintf( __('Successfully installed the plugin <strong>%s %s</strong>.'), $this->api->name, $this->api->version);
1258         }
1259
1260         function after() {
1261
1262                 $plugin_file = $this->upgrader->plugin_info();
1263
1264                 $install_actions = array();
1265
1266                 $from = isset($_GET['from']) ? stripslashes($_GET['from']) : 'plugins';
1267
1268                 if ( 'import' == $from )
1269                         $install_actions['activate_plugin'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;from=import&amp;plugin=' . $plugin_file, 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin') . '" target="_parent">' . __('Activate Plugin &amp; Run Importer') . '</a>';
1270                 else
1271                         $install_actions['activate_plugin'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;plugin=' . $plugin_file, 'activate-plugin_' . $plugin_file) . '" title="' . esc_attr__('Activate this plugin') . '" target="_parent">' . __('Activate Plugin') . '</a>';
1272
1273                 if ( is_multisite() && current_user_can( 'manage_network_plugins' ) )
1274                         $install_actions['network_activate'] = '<a href="' . wp_nonce_url('plugins.php?action=activate&amp;networkwide=1&amp;plugin=' . $plugin_file, 'activate-plugin_' . $plugin_file) . '" title="' . __('Activate this plugin for all sites in this network') . '" target="_parent">' . __('Network Activate') . '</a>';
1275
1276                 if ( 'import' == $from )
1277                         $install_actions['importers_page'] = '<a href="' . admin_url('import.php') . '" title="' . esc_attr__('Return to Importers') . '" target="_parent">' . __('Return to Importers') . '</a>';
1278                 else if ( $this->type == 'web' )
1279                         $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>';
1280                 else
1281                         $install_actions['plugins_page'] = '<a href="' . admin_url('plugins.php') . '" title="' . esc_attr__('Return to Plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>';
1282
1283
1284                 if ( ! $this->result || is_wp_error($this->result) ) {
1285                         unset( $install_actions['activate_plugin'] );
1286                         unset( $install_actions['network_activate'] );
1287                 }
1288                 $install_actions = apply_filters('install_plugin_complete_actions', $install_actions, $this->api, $plugin_file);
1289                 if ( ! empty($install_actions) )
1290                         $this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
1291         }
1292 }
1293
1294 /**
1295  * Theme Installer Skin for the WordPress Theme Installer.
1296  *
1297  * @TODO More Detailed docs, for methods as well.
1298  *
1299  * @package WordPress
1300  * @subpackage Upgrader
1301  * @since 2.8.0
1302  */
1303 class Theme_Installer_Skin extends WP_Upgrader_Skin {
1304         var $api;
1305         var $type;
1306
1307         function Theme_Installer_Skin($args = array()) {
1308                 return $this->__construct($args);
1309         }
1310
1311         function __construct($args = array()) {
1312                 $defaults = array( 'type' => 'web', 'url' => '', 'theme' => '', 'nonce' => '', 'title' => '' );
1313                 $args = wp_parse_args($args, $defaults);
1314
1315                 $this->type = $args['type'];
1316                 $this->api = isset($args['api']) ? $args['api'] : array();
1317
1318                 parent::__construct($args);
1319         }
1320
1321         function before() {
1322                 if ( !empty($this->api) ) {
1323                         /* translators: 1: theme name, 2: version */
1324                         $this->upgrader->strings['process_success'] = sprintf( __('Successfully installed the theme <strong>%1$s %2$s</strong>.'), $this->api->name, $this->api->version);
1325                 }
1326         }
1327
1328         function after() {
1329                 if ( empty($this->upgrader->result['destination_name']) )
1330                         return;
1331
1332                 $theme_info = $this->upgrader->theme_info();
1333                 if ( empty($theme_info) )
1334                         return;
1335                 $name = $theme_info['Name'];
1336                 $stylesheet = $this->upgrader->result['destination_name'];
1337                 $template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
1338
1339                 $preview_link = htmlspecialchars( add_query_arg( array('preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'TB_iframe' => 'true' ), trailingslashit(esc_url(get_option('home'))) ) );
1340                 $activate_link = wp_nonce_url("themes.php?action=activate&amp;template=" . urlencode($template) . "&amp;stylesheet=" . urlencode($stylesheet), 'switch-theme_' . $template);
1341
1342                 $install_actions = array(
1343                         'preview' => '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="' . esc_attr(sprintf(__('Preview &#8220;%s&#8221;'), $name)) . '">' . __('Preview') . '</a>',
1344                         'activate' => '<a href="' . $activate_link .  '" class="activatelink" title="' . esc_attr( sprintf( __('Activate &#8220;%s&#8221;'), $name ) ) . '">' . __('Activate') . '</a>'
1345                                                         );
1346
1347                 if ( $this->type == 'web' )
1348                         $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>';
1349                 else
1350                         $install_actions['themes_page'] = '<a href="' . admin_url('themes.php') . '" title="' . esc_attr__('Themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>';
1351
1352                 if ( ! $this->result || is_wp_error($this->result) )
1353                         unset( $install_actions['activate'], $install_actions['preview'] );
1354
1355                 $install_actions = apply_filters('install_theme_complete_actions', $install_actions, $this->api, $stylesheet, $theme_info);
1356                 if ( ! empty($install_actions) )
1357                         $this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
1358         }
1359 }
1360
1361 /**
1362  * Theme Upgrader Skin for WordPress Theme Upgrades.
1363  *
1364  * @TODO More Detailed docs, for methods as well.
1365  *
1366  * @package WordPress
1367  * @subpackage Upgrader
1368  * @since 2.8.0
1369  */
1370 class Theme_Upgrader_Skin extends WP_Upgrader_Skin {
1371         var $theme = '';
1372
1373         function Theme_Upgrader_Skin($args = array()) {
1374                 return $this->__construct($args);
1375         }
1376
1377         function __construct($args = array()) {
1378                 $defaults = array( 'url' => '', 'theme' => '', 'nonce' => '', 'title' => __('Upgrade Theme') );
1379                 $args = wp_parse_args($args, $defaults);
1380
1381                 $this->theme = $args['theme'];
1382
1383                 parent::__construct($args);
1384         }
1385
1386         function after() {
1387
1388                 $update_actions = array();
1389                 if ( !empty($this->upgrader->result['destination_name']) &&
1390                         ($theme_info = $this->upgrader->theme_info()) &&
1391                         !empty($theme_info) ) {
1392
1393                         $name = $theme_info['Name'];
1394                         $stylesheet = $this->upgrader->result['destination_name'];
1395                         $template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
1396
1397                         $preview_link = htmlspecialchars( add_query_arg( array('preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'TB_iframe' => 'true' ), trailingslashit(esc_url(get_option('home'))) ) );
1398                         $activate_link = wp_nonce_url("themes.php?action=activate&amp;template=" . urlencode($template) . "&amp;stylesheet=" . urlencode($stylesheet), 'switch-theme_' . $template);
1399
1400                         $update_actions['preview'] = '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="' . esc_attr(sprintf(__('Preview &#8220;%s&#8221;'), $name)) . '">' . __('Preview') . '</a>';
1401                         $update_actions['activate'] = '<a href="' . $activate_link .  '" class="activatelink" title="' . esc_attr( sprintf( __('Activate &#8220;%s&#8221;'), $name ) ) . '">' . __('Activate') . '</a>';
1402
1403                         if ( ( ! $this->result || is_wp_error($this->result) ) || $stylesheet == get_stylesheet() )
1404                                 unset($update_actions['preview'], $update_actions['activate']);
1405                 }
1406
1407                 $update_actions['themes_page'] = '<a href="' . admin_url('themes.php') . '" title="' . esc_attr__('Return to Themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>';
1408
1409                 $update_actions = apply_filters('update_theme_complete_actions', $update_actions, $this->theme);
1410                 if ( ! empty($update_actions) )
1411                         $this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$update_actions));
1412         }
1413 }
1414
1415 /**
1416  * 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.
1417  *
1418  * @TODO More Detailed docs, for methods as well.
1419  *
1420  * @package WordPress
1421  * @subpackage Upgrader
1422  * @since 2.8.0
1423  */
1424 class File_Upload_Upgrader {
1425         var $package;
1426         var $filename;
1427
1428         function File_Upload_Upgrader($form, $urlholder) {
1429                 return $this->__construct($form, $urlholder);
1430         }
1431         function __construct($form, $urlholder) {
1432                 if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
1433                         wp_die($uploads['error']);
1434
1435                 if ( empty($_FILES[$form]['name']) && empty($_GET[$urlholder]) )
1436                         wp_die(__('Please select a file'));
1437
1438                 if ( !empty($_FILES) )
1439                         $this->filename = $_FILES[$form]['name'];
1440                 else if ( isset($_GET[$urlholder]) )
1441                         $this->filename = $_GET[$urlholder];
1442
1443                 //Handle a newly uploaded file, Else assume its already been uploaded
1444                 if ( !empty($_FILES) ) {
1445                         $this->filename = wp_unique_filename( $uploads['basedir'], $this->filename );
1446                         $this->package = $uploads['basedir'] . '/' . $this->filename;
1447
1448                         // Move the file to the uploads dir
1449                         if ( false === @ move_uploaded_file( $_FILES[$form]['tmp_name'], $this->package) )
1450                                 wp_die( sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path']));
1451                 } else {
1452                         $this->package = $uploads['basedir'] . '/' . $this->filename;
1453                 }
1454         }
1455 }