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