]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-upgrader.php
Wordpress 3.1.4
[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'] = __('Update 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 update failed.');
378                 $this->strings['process_success'] = __('Plugin updated 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                 // Only start maintenance mode if running in Multisite OR the plugin is in use
472                 $maintenance = is_multisite(); // @TODO: This should only kick in for individual sites if at all possible.
473                 foreach ( $plugins as $plugin )
474                         $maintenance = $maintenance || (is_plugin_active($plugin) && isset($current->response[ $plugin ]) ); // Only activate Maintenance mode if a plugin is active AND has an update available
475                 if ( $maintenance )
476                         $this->maintenance_mode(true);
477
478                 $results = array();
479
480                 $this->update_count = count($plugins);
481                 $this->update_current = 0;
482                 foreach ( $plugins as $plugin ) {
483                         $this->update_current++;
484                         $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
485
486                         if ( !isset( $current->response[ $plugin ] ) ) {
487                                 $this->skin->set_result(false);
488                                 $this->skin->before();
489                                 $this->skin->error('up_to_date');
490                                 $this->skin->after();
491                                 $results[$plugin] = false;
492                                 continue;
493                         }
494
495                         // Get the URL to the zip file
496                         $r = $current->response[ $plugin ];
497
498                         $this->skin->plugin_active = is_plugin_active($plugin);
499
500                         $result = $this->run(array(
501                                                 'package' => $r->package,
502                                                 'destination' => WP_PLUGIN_DIR,
503                                                 'clear_destination' => true,
504                                                 'clear_working' => true,
505                                                 'is_multi' => true,
506                                                 'hook_extra' => array(
507                                                                         'plugin' => $plugin
508                                                 )
509                                         ));
510
511                         $results[$plugin] = $this->result;
512
513                         // Prevent credentials auth screen from displaying multiple times
514                         if ( false === $result )
515                                 break;
516                 } //end foreach $plugins
517
518                 $this->maintenance_mode(false);
519
520                 $this->skin->bulk_footer();
521
522                 $this->skin->footer();
523
524                 // Cleanup our hooks, incase something else does a upgrade on this connection.
525                 remove_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'));
526
527                 // Force refresh of plugin update information
528                 delete_site_transient('update_plugins');
529
530                 return $results;
531         }
532
533         //return plugin info.
534         function plugin_info() {
535                 if ( ! is_array($this->result) )
536                         return false;
537                 if ( empty($this->result['destination_name']) )
538                         return false;
539
540                 $plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
541                 if ( empty($plugin) )
542                         return false;
543
544                 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
545
546                 return $this->result['destination_name'] . '/' . $pluginfiles[0];
547         }
548
549         //Hooked to pre_install
550         function deactivate_plugin_before_upgrade($return, $plugin) {
551
552                 if ( is_wp_error($return) ) //Bypass.
553                         return $return;
554
555                 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
556                 if ( empty($plugin) )
557                         return new WP_Error('bad_request', $this->strings['bad_request']);
558
559                 if ( is_plugin_active($plugin) ) {
560                         $this->skin->feedback('deactivate_plugin');
561                         //Deactivate the plugin silently, Prevent deactivation hooks from running.
562                         deactivate_plugins($plugin, true);
563                 }
564         }
565
566         //Hooked to upgrade_clear_destination
567         function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
568                 global $wp_filesystem;
569
570                 if ( is_wp_error($removed) )
571                         return $removed; //Pass errors through.
572
573                 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
574                 if ( empty($plugin) )
575                         return new WP_Error('bad_request', $this->strings['bad_request']);
576
577                 $plugins_dir = $wp_filesystem->wp_plugins_dir();
578                 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
579
580                 if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If its already vanished.
581                         return $removed;
582
583                 // If plugin is in its own directory, recursively delete the directory.
584                 if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
585                         $deleted = $wp_filesystem->delete($this_plugin_dir, true);
586                 else
587                         $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
588
589                 if ( ! $deleted )
590                         return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
591
592                 return $removed;
593         }
594 }
595
596 /**
597  * Theme Upgrader class for WordPress Themes, It is designed to upgrade/install themes from a local zip, remote zip URL, or uploaded zip file.
598  *
599  * @TODO More Detailed docs, for methods as well.
600  *
601  * @package WordPress
602  * @subpackage Upgrader
603  * @since 2.8.0
604  */
605 class Theme_Upgrader extends WP_Upgrader {
606
607         var $result;
608
609         function upgrade_strings() {
610                 $this->strings['up_to_date'] = __('The theme is at the latest version.');
611                 $this->strings['no_package'] = __('Update package not available.');
612                 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
613                 $this->strings['unpack_package'] = __('Unpacking the update&#8230;');
614                 $this->strings['remove_old'] = __('Removing the old version of the theme&#8230;');
615                 $this->strings['remove_old_failed'] = __('Could not remove the old theme.');
616                 $this->strings['process_failed'] = __('Theme update failed.');
617                 $this->strings['process_success'] = __('Theme updated successfully.');
618         }
619
620         function install_strings() {
621                 $this->strings['no_package'] = __('Install package not available.');
622                 $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>&#8230;');
623                 $this->strings['unpack_package'] = __('Unpacking the package&#8230;');
624                 $this->strings['installing_package'] = __('Installing the theme&#8230;');
625                 $this->strings['process_failed'] = __('Theme install failed.');
626                 $this->strings['process_success'] = __('Theme installed successfully.');
627         }
628
629         function install($package) {
630
631                 $this->init();
632                 $this->install_strings();
633
634                 $options = array(
635                                                 'package' => $package,
636                                                 'destination' => WP_CONTENT_DIR . '/themes',
637                                                 'clear_destination' => false, //Do not overwrite files.
638                                                 'clear_working' => true
639                                                 );
640
641                 $this->run($options);
642
643                 if ( ! $this->result || is_wp_error($this->result) )
644                         return $this->result;
645
646                 // Force refresh of theme update information
647                 delete_site_transient('update_themes');
648
649                 if ( empty($result['destination_name']) )
650                         return false;
651                 else
652                         return $result['destination_name'];
653         }
654
655         function upgrade($theme) {
656
657                 $this->init();
658                 $this->upgrade_strings();
659
660                 // Is an update available?
661                 $current = get_site_transient( 'update_themes' );
662                 if ( !isset( $current->response[ $theme ] ) ) {
663                         $this->skin->before();
664                         $this->skin->set_result(false);
665                         $this->skin->error('up_to_date');
666                         $this->skin->after();
667                         return false;
668                 }
669
670                 $r = $current->response[ $theme ];
671
672                 add_filter('upgrader_pre_install', array(&$this, 'current_before'), 10, 2);
673                 add_filter('upgrader_post_install', array(&$this, 'current_after'), 10, 2);
674                 add_filter('upgrader_clear_destination', array(&$this, 'delete_old_theme'), 10, 4);
675
676                 $options = array(
677                                                 'package' => $r['package'],
678                                                 'destination' => WP_CONTENT_DIR . '/themes',
679                                                 'clear_destination' => true,
680                                                 'clear_working' => true,
681                                                 'hook_extra' => array(
682                                                                                         'theme' => $theme
683                                                                                         )
684                                                 );
685
686                 $this->run($options);
687
688                 if ( ! $this->result || is_wp_error($this->result) )
689                         return $this->result;
690
691                 // Force refresh of theme update information
692                 delete_site_transient('update_themes');
693
694                 return true;
695         }
696
697         function bulk_upgrade($themes) {
698
699                 $this->init();
700                 $this->bulk = true;
701                 $this->upgrade_strings();
702
703                 $current = get_site_transient( 'update_themes' );
704
705                 add_filter('upgrader_pre_install', array(&$this, 'current_before'), 10, 2);
706                 add_filter('upgrader_post_install', array(&$this, 'current_after'), 10, 2);
707                 add_filter('upgrader_clear_destination', array(&$this, 'delete_old_theme'), 10, 4);
708
709                 $this->skin->header();
710
711                 // Connect to the Filesystem first.
712                 $res = $this->fs_connect( array(WP_CONTENT_DIR) );
713                 if ( ! $res ) {
714                         $this->skin->footer();
715                         return false;
716                 }
717
718                 $this->skin->bulk_header();
719
720                 // Only start maintenance mode if running in Multisite OR the theme is in use
721                 $maintenance = is_multisite(); // @TODO: This should only kick in for individual sites if at all possible.
722                 foreach ( $themes as $theme )
723                         $maintenance = $maintenance || $theme == get_stylesheet() || $theme == get_template();
724                 if ( $maintenance )
725                         $this->maintenance_mode(true);
726
727                 $results = array();
728
729                 $this->update_count = count($themes);
730                 $this->update_current = 0;
731                 foreach ( $themes as $theme ) {
732                         $this->update_current++;
733
734                         if ( !isset( $current->response[ $theme ] ) ) {
735                                 $this->skin->set_result(false);
736                                 $this->skin->before();
737                                 $this->skin->error('up_to_date');
738                                 $this->skin->after();
739                                 $results[$theme] = false;
740                                 continue;
741                         }
742
743                         $this->skin->theme_info = $this->theme_info($theme);
744
745                         // Get the URL to the zip file
746                         $r = $current->response[ $theme ];
747
748                         $options = array(
749                                                         'package' => $r['package'],
750                                                         'destination' => WP_CONTENT_DIR . '/themes',
751                                                         'clear_destination' => true,
752                                                         'clear_working' => true,
753                                                         'hook_extra' => array(
754                                                                                                 'theme' => $theme
755                                                                                                 )
756                                                         );
757
758                         $result = $this->run($options);
759
760                         $results[$theme] = $this->result;
761
762                         // Prevent credentials auth screen from displaying multiple times
763                         if ( false === $result )
764                                 break;
765                 } //end foreach $plugins
766
767                 $this->maintenance_mode(false);
768
769                 $this->skin->bulk_footer();
770
771                 $this->skin->footer();
772
773                 // Cleanup our hooks, incase something else does a upgrade on this connection.
774                 remove_filter('upgrader_pre_install', array(&$this, 'current_before'), 10, 2);
775                 remove_filter('upgrader_post_install', array(&$this, 'current_after'), 10, 2);
776                 remove_filter('upgrader_clear_destination', array(&$this, 'delete_old_theme'), 10, 4);
777
778                 // Force refresh of theme update information
779                 delete_site_transient('update_themes');
780
781                 return $results;
782         }
783
784         function current_before($return, $theme) {
785
786                 if ( is_wp_error($return) )
787                         return $return;
788
789                 $theme = isset($theme['theme']) ? $theme['theme'] : '';
790
791                 if ( $theme != get_stylesheet() ) //If not current
792                         return $return;
793                 //Change to maintenance mode now.
794                 if ( ! $this->bulk )
795                         $this->maintenance_mode(true);
796
797                 return $return;
798         }
799         function current_after($return, $theme) {
800                 if ( is_wp_error($return) )
801                         return $return;
802
803                 $theme = isset($theme['theme']) ? $theme['theme'] : '';
804
805                 if ( $theme != get_stylesheet() ) //If not current
806                         return $return;
807
808                 //Ensure stylesheet name hasnt changed after the upgrade:
809                 // @TODO: Note, This doesnt handle the Template changing, or the Template name changing.
810                 if ( $theme == get_stylesheet() && $theme != $this->result['destination_name'] ) {
811                         $theme_info = $this->theme_info();
812                         $stylesheet = $this->result['destination_name'];
813                         $template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
814                         switch_theme($template, $stylesheet, true);
815                 }
816
817                 //Time to remove maintenance mode
818                 if ( ! $this->bulk )
819                         $this->maintenance_mode(false);
820                 return $return;
821         }
822
823         function delete_old_theme($removed, $local_destination, $remote_destination, $theme) {
824                 global $wp_filesystem;
825
826                 $theme = isset($theme['theme']) ? $theme['theme'] : '';
827
828                 if ( is_wp_error($removed) || empty($theme) )
829                         return $removed; //Pass errors through.
830
831                 $themes_dir = $wp_filesystem->wp_themes_dir();
832                 if ( $wp_filesystem->exists( trailingslashit($themes_dir) . $theme ) )
833                         if ( ! $wp_filesystem->delete( trailingslashit($themes_dir) . $theme, true ) )
834                                 return false;
835                 return true;
836         }
837
838         function theme_info($theme = null) {
839
840                 if ( empty($theme) ) {
841                         if ( !empty($this->result['destination_name']) )
842                                 $theme = $this->result['destination_name'];
843                         else
844                                 return false;
845                 }
846                 return get_theme_data(WP_CONTENT_DIR . '/themes/' . $theme . '/style.css');
847         }
848
849 }
850
851 /**
852  * Core Upgrader class for WordPress. It allows for WordPress to upgrade itself in combiantion with the wp-admin/includes/update-core.php file
853  *
854  * @TODO More Detailed docs, for methods as well.
855  *
856  * @package WordPress
857  * @subpackage Upgrader
858  * @since 2.8.0
859  */
860 class Core_Upgrader extends WP_Upgrader {
861
862         function upgrade_strings() {
863                 $this->strings['up_to_date'] = __('WordPress is at the latest version.');
864                 $this->strings['no_package'] = __('Update package not available.');
865                 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
866                 $this->strings['unpack_package'] = __('Unpacking the update&#8230;');
867                 $this->strings['copy_failed'] = __('Could not copy files.');
868         }
869
870         function upgrade($current) {
871                 global $wp_filesystem;
872
873                 $this->init();
874                 $this->upgrade_strings();
875
876                 if ( !empty($feedback) )
877                         add_filter('update_feedback', $feedback);
878
879                 // Is an update available?
880                 if ( !isset( $current->response ) || $current->response == 'latest' )
881                         return new WP_Error('up_to_date', $this->strings['up_to_date']);
882
883                 $res = $this->fs_connect( array(ABSPATH, WP_CONTENT_DIR) );
884                 if ( is_wp_error($res) )
885                         return $res;
886
887                 $wp_dir = trailingslashit($wp_filesystem->abspath());
888
889                 $download = $this->download_package( $current->package );
890                 if ( is_wp_error($download) )
891                         return $download;
892
893                 $working_dir = $this->unpack_package( $download );
894                 if ( is_wp_error($working_dir) )
895                         return $working_dir;
896
897                 // Copy update-core.php from the new version into place.
898                 if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
899                         $wp_filesystem->delete($working_dir, true);
900                         return new WP_Error('copy_failed', $this->strings['copy_failed']);
901                 }
902                 $wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
903
904                 require(ABSPATH . 'wp-admin/includes/update-core.php');
905
906                 return update_core($working_dir, $wp_dir);
907         }
908
909 }
910
911 /**
912  * Generic Skin for the WordPress Upgrader classes. This skin is designed to be extended for specific purposes.
913  *
914  * @TODO More Detailed docs, for methods as well.
915  *
916  * @package WordPress
917  * @subpackage Upgrader
918  * @since 2.8.0
919  */
920 class WP_Upgrader_Skin {
921
922         var $upgrader;
923         var $done_header = false;
924         var $result = false;
925
926         function WP_Upgrader_Skin($args = array()) {
927                 return $this->__construct($args);
928         }
929         function __construct($args = array()) {
930                 $defaults = array( 'url' => '', 'nonce' => '', 'title' => '', 'context' => false );
931                 $this->options = wp_parse_args($args, $defaults);
932         }
933
934         function set_upgrader(&$upgrader) {
935                 if ( is_object($upgrader) )
936                         $this->upgrader =& $upgrader;
937                 $this->add_strings();
938         }
939
940         function add_strings() {
941         }
942
943         function set_result($result) {
944                 $this->result = $result;
945         }
946
947         function request_filesystem_credentials($error = false) {
948                 $url = $this->options['url'];
949                 $context = $this->options['context'];
950                 if ( !empty($this->options['nonce']) )
951                         $url = wp_nonce_url($url, $this->options['nonce']);
952                 return request_filesystem_credentials($url, '', $error, $context); //Possible to bring inline, Leaving as is for now.
953         }
954
955         function header() {
956                 if ( $this->done_header )
957                         return;
958                 $this->done_header = true;
959                 echo '<div class="wrap">';
960                 echo screen_icon();
961                 echo '<h2>' . $this->options['title'] . '</h2>';
962         }
963         function footer() {
964                 echo '</div>';
965         }
966
967         function error($errors) {
968                 if ( ! $this->done_header )
969                         $this->header();
970                 if ( is_string($errors) ) {
971                         $this->feedback($errors);
972                 } elseif ( is_wp_error($errors) && $errors->get_error_code() ) {
973                         foreach ( $errors->get_error_messages() as $message ) {
974                                 if ( $errors->get_error_data() )
975                                         $this->feedback($message . ' ' . $errors->get_error_data() );
976                                 else
977                                         $this->feedback($message);
978                         }
979                 }
980         }
981
982         function feedback($string) {
983                 if ( isset( $this->upgrader->strings[$string] ) )
984                         $string = $this->upgrader->strings[$string];
985
986                 if ( strpos($string, '%') !== false ) {
987                         $args = func_get_args();
988                         $args = array_splice($args, 1);
989                         if ( !empty($args) )
990                                 $string = vsprintf($string, $args);
991                 }
992                 if ( empty($string) )
993                         return;
994                 show_message($string);
995         }
996         function before() {}
997         function after() {}
998
999 }
1000
1001 /**
1002  * Plugin Upgrader Skin for WordPress Plugin Upgrades.
1003  *
1004  * @TODO More Detailed docs, for methods as well.
1005  *
1006  * @package WordPress
1007  * @subpackage Upgrader
1008  * @since 2.8.0
1009  */
1010 class Plugin_Upgrader_Skin extends WP_Upgrader_Skin {
1011         var $plugin = '';
1012         var $plugin_active = false;
1013         var $plugin_network_active = false;
1014
1015         function Plugin_Upgrader_Skin($args = array()) {
1016                 return $this->__construct($args);
1017         }
1018
1019         function __construct($args = array()) {
1020                 $defaults = array( 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => __('Update Plugin') );
1021                 $args = wp_parse_args($args, $defaults);
1022
1023                 $this->plugin = $args['plugin'];
1024
1025                 $this->plugin_active = is_plugin_active( $this->plugin );
1026                 $this->plugin_network_active = is_plugin_active_for_network( $this->plugin );
1027
1028                 parent::__construct($args);
1029         }
1030
1031         function after() {
1032                 $this->plugin = $this->upgrader->plugin_info();
1033                 if ( !empty($this->plugin) && !is_wp_error($this->result) && $this->plugin_active ){
1034                         show_message(__('Reactivating the plugin&#8230;'));
1035                         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>';
1036                 }
1037
1038                 $update_actions =  array(
1039                         '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>',
1040                         'plugins_page' => '<a href="' . self_admin_url('plugins.php') . '" title="' . esc_attr__('Go to plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>'
1041                 );
1042                 if ( $this->plugin_active )
1043                         unset( $update_actions['activate_plugin'] );
1044                 if ( ! $this->result || is_wp_error($this->result) )
1045                         unset( $update_actions['activate_plugin'] );
1046
1047                 $update_actions = apply_filters('update_plugin_complete_actions', $update_actions, $this->plugin);
1048                 if ( ! empty($update_actions) )
1049                         $this->feedback(implode(' | ', (array)$update_actions));
1050         }
1051
1052         function before() {
1053                 if ( $this->upgrader->show_before ) {
1054                         echo $this->upgrader->show_before;
1055                         $this->upgrader->show_before = '';
1056                 }
1057         }
1058 }
1059
1060 /**
1061  * Plugin Upgrader Skin for WordPress Plugin Upgrades.
1062  *
1063  * @package WordPress
1064  * @subpackage Upgrader
1065  * @since 3.0.0
1066  */
1067 class Bulk_Upgrader_Skin extends WP_Upgrader_Skin {
1068         var $in_loop = false;
1069         var $error = false;
1070
1071         function Bulk_Upgrader_Skin($args = array()) {
1072                 return $this->__construct($args);
1073         }
1074
1075         function __construct($args = array()) {
1076                 $defaults = array( 'url' => '', 'nonce' => '' );
1077                 $args = wp_parse_args($args, $defaults);
1078
1079                 parent::__construct($args);
1080         }
1081
1082         function add_strings() {
1083                 $this->upgrader->strings['skin_upgrade_start'] = __('The update process is starting. This process may take a while on some hosts, so please be patient.');
1084                 $this->upgrader->strings['skin_update_failed_error'] = __('An error occurred while updating %1$s: <strong>%2$s</strong>.');
1085                 $this->upgrader->strings['skin_update_failed'] = __('The update of %1$s failed.');
1086                 $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>';
1087                 $this->upgrader->strings['skin_upgrade_end'] = __('All updates have been completed.');
1088         }
1089
1090         function feedback($string) {
1091                 if ( isset( $this->upgrader->strings[$string] ) )
1092                         $string = $this->upgrader->strings[$string];
1093
1094                 if ( strpos($string, '%') !== false ) {
1095                         $args = func_get_args();
1096                         $args = array_splice($args, 1);
1097                         if ( !empty($args) )
1098                                 $string = vsprintf($string, $args);
1099                 }
1100                 if ( empty($string) )
1101                         return;
1102                 if ( $this->in_loop )
1103                         echo "$string<br />\n";
1104                 else
1105                         echo "<p>$string</p>\n";
1106         }
1107
1108         function header() {
1109                 // Nothing, This will be displayed within a iframe.
1110         }
1111
1112         function footer() {
1113                 // Nothing, This will be displayed within a iframe.
1114         }
1115         function error($error) {
1116                 if ( is_string($error) && isset( $this->upgrader->strings[$error] ) )
1117                         $this->error = $this->upgrader->strings[$error];
1118
1119                 if ( is_wp_error($error) ) {
1120                         foreach ( $error->get_error_messages() as $emessage ) {
1121                                 if ( $error->get_error_data() )
1122                                         $messages[] = $emessage . ' ' . $error->get_error_data();
1123                                 else
1124                                         $messages[] = $emessage;
1125                         }
1126                         $this->error = implode(', ', $messages);
1127                 }
1128                 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').hide();</script>';
1129         }
1130
1131         function bulk_header() {
1132                 $this->feedback('skin_upgrade_start');
1133         }
1134
1135         function bulk_footer() {
1136                 $this->feedback('skin_upgrade_end');
1137         }
1138
1139         function before($title = '') {
1140                 $this->in_loop = true;
1141                 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);
1142                 echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').show();</script>';
1143                 echo '<div class="update-messages hide-if-js" id="progress-' . esc_attr($this->upgrader->update_current) . '"><p>';
1144                 $this->flush_output();
1145         }
1146
1147         function after($title = '') {
1148                 echo '</p></div>';
1149                 if ( $this->error || ! $this->result ) {
1150                         if ( $this->error )
1151                                 echo '<div class="error"><p>' . sprintf($this->upgrader->strings['skin_update_failed_error'], $title, $this->error) . '</p></div>';
1152                         else
1153                                 echo '<div class="error"><p>' . sprintf($this->upgrader->strings['skin_update_failed'], $title) . '</p></div>';
1154
1155                         echo '<script type="text/javascript">jQuery(\'#progress-' . esc_js($this->upgrader->update_current) . '\').show();</script>';
1156                 }
1157                 if ( !empty($this->result) && !is_wp_error($this->result) ) {
1158                         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>';
1159                         echo '<script type="text/javascript">jQuery(\'.waiting-' . esc_js($this->upgrader->update_current) . '\').hide();</script>';
1160                 }
1161
1162                 $this->reset();
1163                 $this->flush_output();
1164         }
1165
1166         function reset() {
1167                 $this->in_loop = false;
1168                 $this->error = false;
1169         }
1170
1171         function flush_output() {
1172                 wp_ob_end_flush_all();
1173                 flush();
1174         }
1175 }
1176
1177 class Bulk_Plugin_Upgrader_Skin extends Bulk_Upgrader_Skin {
1178         var $plugin_info = array(); // Plugin_Upgrader::bulk() will fill this in.
1179         function Plugin_Upgrader_Skin($args = array()) {
1180                 parent::__construct($args);
1181         }
1182
1183         function add_strings() {
1184                 parent::add_strings();
1185                 $this->upgrader->strings['skin_before_update_header'] = __('Updating Plugin %1$s (%2$d/%3$d)');
1186         }
1187
1188         function before() {
1189                 parent::before($this->plugin_info['Title']);
1190         }
1191
1192         function after() {
1193                 parent::after($this->plugin_info['Title']);
1194         }
1195         function bulk_footer() {
1196                 parent::bulk_footer();
1197                 $update_actions =  array(
1198                         'plugins_page' => '<a href="' . self_admin_url('plugins.php') . '" title="' . esc_attr__('Go to plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>',
1199                         '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>'
1200                 );
1201
1202                 $update_actions = apply_filters('update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info);
1203                 if ( ! empty($update_actions) )
1204                         $this->feedback(implode(' | ', (array)$update_actions));
1205         }
1206 }
1207
1208 class Bulk_Theme_Upgrader_Skin extends Bulk_Upgrader_Skin {
1209         var $theme_info = array(); // Theme_Upgrader::bulk() will fill this in.
1210         function Theme_Upgrader_Skin($args = array()) {
1211                 parent::__construct($args);
1212         }
1213
1214         function add_strings() {
1215                 parent::add_strings();
1216                 $this->upgrader->strings['skin_before_update_header'] = __('Updating Theme %1$s (%2$d/%3$d)');
1217         }
1218
1219         function before() {
1220                 parent::before($this->theme_info['Name']);
1221         }
1222
1223         function after() {
1224                 parent::after($this->theme_info['Name']);
1225         }
1226         function bulk_footer() {
1227                 parent::bulk_footer();
1228                 $update_actions =  array(
1229                         'themes_page' => '<a href="' . self_admin_url('themes.php') . '" title="' . esc_attr__('Go to themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>',
1230                         '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>'
1231                 );
1232
1233                 $update_actions = apply_filters('update_bulk_theme_complete_actions', $update_actions, $this->theme_info);
1234                 if ( ! empty($update_actions) )
1235                         $this->feedback(implode(' | ', (array)$update_actions));
1236         }
1237 }
1238
1239 /**
1240  * Plugin Installer Skin for WordPress Plugin Installer.
1241  *
1242  * @TODO More Detailed docs, for methods as well.
1243  *
1244  * @package WordPress
1245  * @subpackage Upgrader
1246  * @since 2.8.0
1247  */
1248 class Plugin_Installer_Skin extends WP_Upgrader_Skin {
1249         var $api;
1250         var $type;
1251
1252         function Plugin_Installer_Skin($args = array()) {
1253                 return $this->__construct($args);
1254         }
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 Theme_Installer_Skin($args = array()) {
1321                 return $this->__construct($args);
1322         }
1323
1324         function __construct($args = array()) {
1325                 $defaults = array( 'type' => 'web', 'url' => '', 'theme' => '', 'nonce' => '', 'title' => '' );
1326                 $args = wp_parse_args($args, $defaults);
1327
1328                 $this->type = $args['type'];
1329                 $this->api = isset($args['api']) ? $args['api'] : array();
1330
1331                 parent::__construct($args);
1332         }
1333
1334         function before() {
1335                 if ( !empty($this->api) ) {
1336                         /* translators: 1: theme name, 2: version */
1337                         $this->upgrader->strings['process_success'] = sprintf( __('Successfully installed the theme <strong>%1$s %2$s</strong>.'), $this->api->name, $this->api->version);
1338                 }
1339         }
1340
1341         function after() {
1342                 if ( empty($this->upgrader->result['destination_name']) )
1343                         return;
1344
1345                 $theme_info = $this->upgrader->theme_info();
1346                 if ( empty($theme_info) )
1347                         return;
1348                 $name = $theme_info['Name'];
1349                 $stylesheet = $this->upgrader->result['destination_name'];
1350                 $template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
1351
1352                 $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'))) ) );
1353                 $activate_link = wp_nonce_url("themes.php?action=activate&amp;template=" . urlencode($template) . "&amp;stylesheet=" . urlencode($stylesheet), 'switch-theme_' . $template);
1354
1355                 $install_actions = array(
1356                         'preview' => '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="' . esc_attr(sprintf(__('Preview &#8220;%s&#8221;'), $name)) . '">' . __('Preview') . '</a>',
1357                         'activate' => '<a href="' . $activate_link .  '" class="activatelink" title="' . esc_attr( sprintf( __('Activate &#8220;%s&#8221;'), $name ) ) . '">' . __('Activate') . '</a>'
1358                                                         );
1359
1360                 if ( $this->type == 'web' )
1361                         $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>';
1362                 else
1363                         $install_actions['themes_page'] = '<a href="' . self_admin_url('themes.php') . '" title="' . esc_attr__('Themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>';
1364
1365                 if ( ! $this->result || is_wp_error($this->result) || is_network_admin() )
1366                         unset( $install_actions['activate'], $install_actions['preview'] );
1367
1368                 $install_actions = apply_filters('install_theme_complete_actions', $install_actions, $this->api, $stylesheet, $theme_info);
1369                 if ( ! empty($install_actions) )
1370                         $this->feedback(implode(' | ', (array)$install_actions));
1371         }
1372 }
1373
1374 /**
1375  * Theme Upgrader Skin for WordPress Theme Upgrades.
1376  *
1377  * @TODO More Detailed docs, for methods as well.
1378  *
1379  * @package WordPress
1380  * @subpackage Upgrader
1381  * @since 2.8.0
1382  */
1383 class Theme_Upgrader_Skin extends WP_Upgrader_Skin {
1384         var $theme = '';
1385
1386         function Theme_Upgrader_Skin($args = array()) {
1387                 return $this->__construct($args);
1388         }
1389
1390         function __construct($args = array()) {
1391                 $defaults = array( 'url' => '', 'theme' => '', 'nonce' => '', 'title' => __('Update Theme') );
1392                 $args = wp_parse_args($args, $defaults);
1393
1394                 $this->theme = $args['theme'];
1395
1396                 parent::__construct($args);
1397         }
1398
1399         function after() {
1400
1401                 $update_actions = array();
1402                 if ( !empty($this->upgrader->result['destination_name']) &&
1403                         ($theme_info = $this->upgrader->theme_info()) &&
1404                         !empty($theme_info) ) {
1405
1406                         $name = $theme_info['Name'];
1407                         $stylesheet = $this->upgrader->result['destination_name'];
1408                         $template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
1409
1410                         $preview_link = htmlspecialchars( add_query_arg( array('preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'TB_iframe' => 'true' ), trailingslashit(esc_url(get_option('home'))) ) );
1411                         $activate_link = wp_nonce_url("themes.php?action=activate&amp;template=" . urlencode($template) . "&amp;stylesheet=" . urlencode($stylesheet), 'switch-theme_' . $template);
1412
1413                         $update_actions['preview'] = '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="' . esc_attr(sprintf(__('Preview &#8220;%s&#8221;'), $name)) . '">' . __('Preview') . '</a>';
1414                         $update_actions['activate'] = '<a href="' . $activate_link .  '" class="activatelink" title="' . esc_attr( sprintf( __('Activate &#8220;%s&#8221;'), $name ) ) . '">' . __('Activate') . '</a>';
1415
1416                         if ( ( ! $this->result || is_wp_error($this->result) ) || $stylesheet == get_stylesheet() )
1417                                 unset($update_actions['preview'], $update_actions['activate']);
1418                 }
1419
1420                 $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>';
1421
1422                 $update_actions = apply_filters('update_theme_complete_actions', $update_actions, $this->theme);
1423                 if ( ! empty($update_actions) )
1424                         $this->feedback(implode(' | ', (array)$update_actions));
1425         }
1426 }
1427
1428 /**
1429  * 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.
1430  *
1431  * @TODO More Detailed docs, for methods as well.
1432  *
1433  * @package WordPress
1434  * @subpackage Upgrader
1435  * @since 2.8.0
1436  */
1437 class File_Upload_Upgrader {
1438         var $package;
1439         var $filename;
1440
1441         function File_Upload_Upgrader($form, $urlholder) {
1442                 return $this->__construct($form, $urlholder);
1443         }
1444         function __construct($form, $urlholder) {
1445                 if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
1446                         wp_die($uploads['error']);
1447
1448                 if ( empty($_FILES[$form]['name']) && empty($_GET[$urlholder]) )
1449                         wp_die(__('Please select a file'));
1450
1451                 if ( !empty($_FILES) )
1452                         $this->filename = $_FILES[$form]['name'];
1453                 else if ( isset($_GET[$urlholder]) )
1454                         $this->filename = $_GET[$urlholder];
1455
1456                 //Handle a newly uploaded file, Else assume its already been uploaded
1457                 if ( !empty($_FILES) ) {
1458                         $this->filename = wp_unique_filename( $uploads['basedir'], $this->filename );
1459                         $this->package = $uploads['basedir'] . '/' . $this->filename;
1460
1461                         // Move the file to the uploads dir
1462                         if ( false === @ move_uploaded_file( $_FILES[$form]['tmp_name'], $this->package) )
1463                                 wp_die( sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path']));
1464                 } else {
1465                         $this->package = $uploads['basedir'] . '/' . $this->filename;
1466                 }
1467         }
1468 }