]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-upgrader.php
Wordpress 2.8
[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                 $this->strings['fs_no_folder'] = __('Unable to locate needed folder (%s).');
52
53                 $this->strings['download_failed'] = __('Download failed.');
54                 $this->strings['installing_package'] = __('Installing the latest version.');
55                 $this->strings['folder_exists'] = __('Destination folder already exists.');
56                 $this->strings['mkdir_failed'] = __('Could not create directory.');
57                 $this->strings['bad_package'] = __('Incompatible Archive');
58
59                 $this->strings['maintenance_start'] = __('Enabling Maintenance mode.');
60                 $this->strings['maintenance_end'] = __('Disabling Maintenance mode.');
61         }
62
63         function fs_connect( $directories = array() ) {
64                 global $wp_filesystem;
65
66                 if ( false === ($credentials = $this->skin->request_filesystem_credentials()) )
67                         return false;
68
69                 if ( ! WP_Filesystem($credentials) ) {
70                         $error = true;
71                         if ( is_object($wp_filesystem) && $wp_filesystem->errors->get_error_code() )
72                                 $error = $wp_filesystem->errors;
73                         $this->skin->request_filesystem_credentials($error); //Failed to connect, Error and request again
74                         return false;
75                 }
76
77                 if ( ! is_object($wp_filesystem) )
78                         return new WP_Error('fs_unavailable', $this->strings['fs_unavailable'] );
79
80                 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
81                         return new WP_Error('fs_error', $this->strings['fs_error'], $wp_filesystem->errors);
82
83                 foreach ( (array)$directories as $dir ) {
84                         if ( ABSPATH == $dir && ! $wp_filesystem->abspath() )
85                                 return new WP_Error('fs_no_root_dir', $this->strings['fs_no_root_dir']);
86
87                         elseif ( WP_CONTENT_DIR == $dir && ! $wp_filesystem->wp_content_dir() )
88                                 return new WP_Error('fs_no_content_dir', $this->strings['fs_no_content_dir']);
89
90                         elseif ( WP_PLUGIN_DIR == $dir && ! $wp_filesystem->wp_plugins_dir() )
91                                 return new WP_Error('fs_no_plugins_dir', $this->strings['fs_no_plugins_dir']);
92
93                         elseif ( WP_CONTENT_DIR . '/themes' == $dir && ! $wp_filesystem->find_folder(WP_CONTENT_DIR . '/themes') )
94                                 return new WP_Error('fs_no_themes_dir', $this->strings['fs_no_themes_dir']);
95
96                         elseif ( ! $wp_filesystem->find_folder($dir) )
97                                 return new WP_Error('fs_no_folder', sprintf($strings['fs_no_folder'], $dir));
98                 }
99                 return true;
100         } //end fs_connect();
101
102         function download_package($package) {
103
104                 if ( ! preg_match('!^(http|https|ftp)://!i', $package) && file_exists($package) ) //Local file or remote?
105                         return $package; //must be a local file..
106
107                 if ( empty($package) )
108                         return new WP_Error('no_package', $this->strings['no_package']);
109
110                 $this->skin->feedback('downloading_package', $package);
111
112                 $download_file = download_url($package);
113
114                 if ( is_wp_error($download_file) )
115                         return new WP_Error('download_failed', $this->strings['download_failed'], $download_file->get_error_message());
116
117                 return $download_file;
118         }
119
120         function unpack_package($package, $delete_package = true) {
121                 global $wp_filesystem;
122
123                 $this->skin->feedback('unpack_package');
124
125                 $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
126
127                 //Clean up contents of upgrade directory beforehand.
128                 $upgrade_files = $wp_filesystem->dirlist($upgrade_folder);
129                 if ( !empty($upgrade_files) ) {
130                         foreach ( $upgrade_files as $file )
131                                 $wp_filesystem->delete($upgrade_folder . $file['name'], true);
132                 }
133
134                 //We need a working directory
135                 $working_dir = $upgrade_folder . basename($package, '.zip');
136
137                 // Clean up working directory
138                 if ( $wp_filesystem->is_dir($working_dir) )
139                         $wp_filesystem->delete($working_dir, true);
140
141                 // Unzip package to working directory
142                 $result = unzip_file($package, $working_dir); //TODO optimizations, Copy when Move/Rename would suffice?
143
144                 // Once extracted, delete the package if required.
145                 if ( $delete_package )
146                         unlink($package);
147
148                 if ( is_wp_error($result) ) {
149                         $wp_filesystem->delete($working_dir, true);
150                         return $result;
151                 }
152
153                 return $working_dir;
154         }
155
156         function install_package($args = array()) {
157                 global $wp_filesystem;
158                 $defaults = array( 'source' => '', 'destination' => '', //Please always pass these
159                                                 'clear_destination' => false, 'clear_working' => false,
160                                                 'hook_extra' => array());
161
162                 $args = wp_parse_args($args, $defaults);
163                 extract($args);
164
165                 @set_time_limit( 300 );
166
167                 if ( empty($source) || empty($destination) )
168                         return new WP_Error('bad_request', $this->strings['bad_request']);
169
170                 $this->skin->feedback('installing_package');
171
172                 $res = apply_filters('upgrader_pre_install', true, $hook_extra);
173                 if ( is_wp_error($res) )
174                         return $res;
175
176                 //Retain the Original source and destinations
177                 $remote_source = $source;
178                 $local_destination = $destination;
179
180                 $source_files = array_keys( $wp_filesystem->dirlist($remote_source) );
181                 $remote_destination = $wp_filesystem->find_folder($local_destination);
182
183                 //Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
184                 if ( 1 == count($source_files) && $wp_filesystem->is_dir( trailingslashit($source) . $source_files[0] . '/') ) //Only one folder? Then we want its contents.
185                         $source = trailingslashit($source) . trailingslashit($source_files[0]);
186                 elseif ( count($source_files) == 0 )
187                         return new WP_Error('bad_package', $this->strings['bad_package']); //There are no files?
188                 //else //Its only a single file, The upgrader will use the foldername of this file as the destination folder. foldername is based on zip filename.
189
190                 //Hook ability to change the source file location..
191                 $source = apply_filters('upgrader_source_selection', $source, $remote_source, $this);
192                 if ( is_wp_error($source) )
193                         return $source;
194
195                 //Has the source location changed? If so, we need a new source_files list.
196                 if ( $source !== $remote_source )
197                         $source_files = array_keys( $wp_filesystem->dirlist($source) );
198
199                 //Protection against deleting files in any important base directories.
200                 if ( in_array( $destination, array(ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes') ) ) {
201                         $remote_destination = trailingslashit($remote_destination) . trailingslashit(basename($source));
202                         $destination = trailingslashit($destination) . trailingslashit(basename($source));
203                 }
204
205                 //If we're not clearing the destination folder, and something exists there allready, Bail.
206                 if ( ! $clear_destination && $wp_filesystem->exists($remote_destination) ) {
207                         $wp_filesystem->delete($remote_source, true); //Clear out the source files.
208                         return new WP_Error('folder_exists', $this->strings['folder_exists'], $remote_destination );
209                 } else if ( $clear_destination ) {
210                         //We're going to clear the destination if theres something there
211                         $this->skin->feedback('remove_old');
212
213                         $removed = true;
214                         if ( $wp_filesystem->exists($remote_destination) )
215                                 $removed = $wp_filesystem->delete($remote_destination, true);
216
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                 }
224
225                 //Create destination if needed
226                 if ( !$wp_filesystem->exists($remote_destination) )
227                         if ( !$wp_filesystem->mkdir($remote_destination, FS_CHMOD_DIR) )
228                                 return new WP_Error('mkdir_failed', $this->strings['mkdir_failed'], $remote_destination);
229
230                 // Copy new version of item into place.
231                 $result = copy_dir($source, $remote_destination);
232                 if ( is_wp_error($result) ) {
233                         if ( $clear_working )
234                                 $wp_filesystem->delete($remote_source, true);
235                         return $result;
236                 }
237
238                 //Clear the Working folder?
239                 if ( $clear_working )
240                         $wp_filesystem->delete($remote_source, true);
241
242                 $destination_name = basename( str_replace($local_destination, '', $destination) );
243                 if ( '.' == $destination_name )
244                         $destination_name = '';
245
246                 $this->result = compact('local_source', 'source', 'source_name', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination', 'delete_source_dir');
247
248                 $res = apply_filters('upgrader_post_install', true, $hook_extra, $this->result);
249                 if ( is_wp_error($res) ) {
250                         $this->result = $res;
251                         return $res;
252                 }
253
254                 //Bombard the calling function will all the info which we've just used.
255                 return $this->result;
256         }
257
258         function run($options) {
259
260                 $defaults = array(      'package' => '', //Please always pass this.
261                                                         'destination' => '', //And this
262                                                         'clear_destination' => false,
263                                                         'clear_working' => true,
264                                                         'hook_extra' => array() //Pass any extra $hook_extra args here, this will be passed to any hooked filters.
265                                                 );
266
267                 $options = wp_parse_args($options, $defaults);
268                 extract($options);
269
270                 //Connect to the Filesystem first.
271                 $res = $this->fs_connect( array(WP_CONTENT_DIR, $destination) );
272                 if ( ! $res ) //Mainly for non-connected filesystem.
273                         return false;
274
275                 if ( is_wp_error($res) ) {
276                         $this->skin->error($res);
277                         return $res;
278                 }
279
280                 $this->skin->header();
281                 $this->skin->before();
282
283                 //Download the package (Note, This just returns the filename of the file if the package is a local file)
284                 $download = $this->download_package( $package );
285                 if ( is_wp_error($download) ) {
286                         $this->skin->error($download);
287                         return $download;
288                 }
289
290                 //Unzip's the file into a temporary directory
291                 $working_dir = $this->unpack_package( $download );
292                 if ( is_wp_error($working_dir) ) {
293                         $this->skin->error($working_dir);
294                         return $working_dir;
295                 }
296
297                 //With the given options, this installs it to the destination directory.
298                 $result = $this->install_package( array(
299                                                                                         'source' => $working_dir,
300                                                                                         'destination' => $destination,
301                                                                                         'clear_destination' => $clear_destination,
302                                                                                         'clear_working' => $clear_working,
303                                                                                         'hook_extra' => $hook_extra
304                                                                                 ) );
305                 $this->skin->set_result($result);
306                 if ( is_wp_error($result) ) {
307                         $this->skin->error($result);
308                         $this->skin->feedback('process_failed');
309                 } else {
310                         //Install Suceeded
311                         $this->skin->feedback('process_success');
312                 }
313                 $this->skin->after();
314                 $this->skin->footer();
315                 return $result;
316         }
317
318         function maintenance_mode($enable = false) {
319                 global $wp_filesystem;
320                 $file = $wp_filesystem->abspath() . '.maintenance';
321                 if ( $enable ) {
322                         $this->skin->feedback('maintenance_start');
323                         // Create maintenance file to signal that we are upgrading
324                         $maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
325                         $wp_filesystem->delete($file);
326                         $wp_filesystem->put_contents($file, $maintenance_string, FS_CHMOD_FILE);
327                 } else if ( !$enable && $wp_filesystem->exists($file) ) {
328                         $this->skin->feedback('maintenance_end');
329                         $wp_filesystem->delete($file);
330                 }
331         }
332
333 }
334
335 /**
336  * Plugin Upgrader class for WordPress Plugins, It is designed to upgrade/install plugins from a local zip, remote zip URL, or uploaded zip file.
337  *
338  * @TODO More Detailed docs, for methods as well.
339  *
340  * @package WordPress
341  * @subpackage Upgrader
342  * @since 2.8.0
343  */
344 class Plugin_Upgrader extends WP_Upgrader {
345
346         var $result;
347
348         function upgrade_strings() {
349                 $this->strings['up_to_date'] = __('The plugin is at the latest version.');
350                 $this->strings['no_package'] = __('Upgrade package not available.');
351                 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>.');
352                 $this->strings['unpack_package'] = __('Unpacking the update.');
353                 $this->strings['deactivate_plugin'] = __('Deactivating the plugin.');
354                 $this->strings['remove_old'] = __('Removing the old version of the plugin.');
355                 $this->strings['remove_old_failed'] = __('Could not remove the old plugin.');
356                 $this->strings['process_failed'] = __('Plugin upgrade Failed.');
357                 $this->strings['process_success'] = __('Plugin upgraded successfully.');
358         }
359
360         function install_strings() {
361                 $this->strings['no_package'] = __('Install package not available.');
362                 $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>.');
363                 $this->strings['unpack_package'] = __('Unpacking the package.');
364                 $this->strings['installing_package'] = __('Installing the plugin.');
365                 $this->strings['process_failed'] = __('Plugin Install Failed.');
366                 $this->strings['process_success'] = __('Plugin Installed successfully.');
367         }
368
369         function install($package) {
370
371                 $this->init();
372                 $this->install_strings();
373
374                 $this->run(array(
375                                         'package' => $package,
376                                         'destination' => WP_PLUGIN_DIR,
377                                         'clear_destination' => false, //Do not overwrite files.
378                                         'clear_working' => true,
379                                         'hook_extra' => array()
380                                         ));
381
382                 // Force refresh of plugin update information
383                 delete_transient('update_plugins');
384
385         }
386
387         function upgrade($plugin) {
388
389                 $this->init();
390                 $this->upgrade_strings();
391
392                 $current = get_transient( 'update_plugins' );
393                 if ( !isset( $current->response[ $plugin ] ) ) {
394                         $this->skin->set_result(false);
395                         $this->skin->error('up_to_date');
396                         $this->skin->after();
397                         return false;
398                 }
399
400                 // Get the URL to the zip file
401                 $r = $current->response[ $plugin ];
402
403                 add_filter('upgrader_pre_install', array(&$this, 'deactivate_plugin_before_upgrade'), 10, 2);
404                 add_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'), 10, 4);
405                 //'source_selection' => array(&$this, 'source_selection'), //theres a track ticket to move up the directory for zip's which are made a bit differently, useful for non-.org plugins.
406
407                 $this->run(array(
408                                         'package' => $r->package,
409                                         'destination' => WP_PLUGIN_DIR,
410                                         'clear_destination' => true,
411                                         'clear_working' => true,
412                                         'hook_extra' => array(
413                                                                 'plugin' => $plugin
414                                         )
415                                 ));
416
417                 //Cleanup our hooks, incase something else does a upgrade on this connection.
418                 remove_filter('upgrader_pre_install', array(&$this, 'deactivate_plugin_before_upgrade'));
419                 remove_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'));
420
421                 if ( ! $this->result || is_wp_error($this->result) )
422                         return $this->result;
423
424                 // Force refresh of plugin update information
425                 delete_transient('update_plugins');
426         }
427
428         //return plugin info.
429         function plugin_info() {
430                 if ( ! is_array($this->result) )
431                         return false;
432                 if ( empty($this->result['destination_name']) )
433                         return false;
434
435                 $plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
436                 if ( empty($plugin) )
437                         return false;
438
439                 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
440
441                 return $this->result['destination_name'] . '/' . $pluginfiles[0];
442         }
443
444         //Hooked to pre_install
445         function deactivate_plugin_before_upgrade($return, $plugin) {
446
447                 if ( is_wp_error($return) ) //Bypass.
448                         return $return;
449
450                 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
451                 if ( empty($plugin) )
452                         return new WP_Error('bad_request', $this->strings['bad_request']);
453
454                 if ( is_plugin_active($plugin) ) {
455                         $this->skin->feedback('deactivate_plugin');
456                         //Deactivate the plugin silently, Prevent deactivation hooks from running.
457                         deactivate_plugins($plugin, true);
458                 }
459         }
460
461         //Hooked to upgrade_clear_destination
462         function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
463                 global $wp_filesystem;
464
465                 if ( is_wp_error($removed) )
466                         return $removed; //Pass errors through.
467
468                 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
469                 if ( empty($plugin) )
470                         return new WP_Error('bad_request', $this->strings['bad_request']);
471
472                 $plugins_dir = $wp_filesystem->wp_plugins_dir();
473                 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
474
475                 if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If its already vanished.
476                         return $removed;
477
478                 // If plugin is in its own directory, recursively delete the directory.
479                 if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
480                         $deleted = $wp_filesystem->delete($this_plugin_dir, true);
481                 else
482                         $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
483
484                 if ( ! $deleted )
485                         return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
486
487                 return $removed;
488         }
489 }
490
491 /**
492  * Theme Upgrader class for WordPress Themes, It is designed to upgrade/install themes from a local zip, remote zip URL, or uploaded zip file.
493  *
494  * @TODO More Detailed docs, for methods as well.
495  *
496  * @package WordPress
497  * @subpackage Upgrader
498  * @since 2.8.0
499  */
500 class Theme_Upgrader extends WP_Upgrader {
501
502         var $result;
503
504         function upgrade_strings() {
505                 $this->strings['up_to_date'] = __('The theme is at the latest version.');
506                 $this->strings['no_package'] = __('Upgrade package not available.');
507                 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>.');
508                 $this->strings['unpack_package'] = __('Unpacking the update.');
509                 $this->strings['remove_old'] = __('Removing the old version of the theme.');
510                 $this->strings['remove_old_failed'] = __('Could not remove the old theme.');
511                 $this->strings['process_failed'] = __('Theme upgrade Failed.');
512                 $this->strings['process_success'] = __('Theme upgraded successfully.');
513         }
514
515         function install_strings() {
516                 $this->strings['no_package'] = __('Install package not available.');
517                 $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>.');
518                 $this->strings['unpack_package'] = __('Unpacking the package.');
519                 $this->strings['installing_package'] = __('Installing the theme.');
520                 $this->strings['process_failed'] = __('Theme Install Failed.');
521                 $this->strings['process_success'] = __('Theme Installed successfully.');
522         }
523
524         function install($package) {
525
526                 $this->init();
527                 $this->install_strings();
528
529                 $options = array(
530                                                 'package' => $package,
531                                                 'destination' => WP_CONTENT_DIR . '/themes',
532                                                 'clear_destination' => false, //Do not overwrite files.
533                                                 'clear_working' => true
534                                                 );
535
536                 $this->run($options);
537
538                 if ( ! $this->result || is_wp_error($this->result) )
539                         return $this->result;
540
541                 // Force refresh of theme update information
542                 delete_transient('update_themes');
543
544                 if ( empty($result['destination_name']) )
545                         return false;
546                 else
547                         return $result['destination_name'];
548         }
549
550         function upgrade($theme) {
551
552                 $this->init();
553                 $this->upgrade_strings();
554
555                 // Is an update available?
556                 $current = get_transient( 'update_themes' );
557                 if ( !isset( $current->response[ $theme ] ) ) {
558                         $this->skin->set_result(false);
559                         $this->skin->error('up_to_date');
560                         $this->skin->after();
561                         return false;
562                 }
563                 
564                 $r = $current->response[ $theme ];
565
566                 add_filter('upgrader_pre_install', array(&$this, 'current_before'), 10, 2);
567                 add_filter('upgrader_post_install', array(&$this, 'current_after'), 10, 2);
568                 add_filter('upgrader_clear_destination', array(&$this, 'delete_old_theme'), 10, 4);
569
570                 $options = array(
571                                                 'package' => $r['package'],
572                                                 'destination' => WP_CONTENT_DIR . '/themes',
573                                                 'clear_destination' => true,
574                                                 'clear_working' => true,
575                                                 'hook_extra' => array(
576                                                                                         'theme' => $theme
577                                                                                         )
578                                                 );
579
580                 $this->run($options);
581
582                 if ( ! $this->result || is_wp_error($this->result) )
583                         return $this->result;
584
585                 // Force refresh of theme update information
586                 delete_transient('update_themes');
587
588                 return true;
589         }
590
591         function current_before($return, $theme) {
592
593                 if ( is_wp_error($return) )
594                         return $return;
595
596                 $theme = isset($theme['theme']) ? $theme['theme'] : '';
597
598                 if ( $theme != get_stylesheet() ) //If not current
599                         return $return;
600                 //Change to maintainence mode now.
601                 $this->maintenance_mode(true);
602
603                 return $return;
604         }
605         function current_after($return, $theme) {
606                 if ( is_wp_error($return) )
607                         return $return;
608
609                 $theme = isset($theme['theme']) ? $theme['theme'] : '';
610
611                 if ( $theme != get_stylesheet() ) //If not current
612                         return $return;
613
614                 //Ensure stylesheet name hasnt changed after the upgrade:
615                 if ( $theme == get_stylesheet() && $theme != $this->result['destination_name'] ) {
616                         $theme_info = $this->theme_info();
617                         $stylesheet = $this->result['destination_name'];
618                         $template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
619                         switch_theme($template, $stylesheet, true);
620                 }
621
622                 //Time to remove maintainence mode
623                 $this->maintenance_mode(false);
624                 return $return;
625         }
626
627         function delete_old_theme($removed, $local_destination, $remote_destination, $theme) {
628                 global $wp_filesystem;
629
630                 $theme = isset($theme['theme']) ? $theme['theme'] : '';
631
632                 if ( is_wp_error($removed) || empty($theme) )
633                         return $removed; //Pass errors through.
634
635                 $themes_dir = $wp_filesystem->wp_themes_dir();
636                 if ( $wp_filesystem->exists( trailingslashit($themes_dir) . $theme ) )
637                         if ( ! $wp_filesystem->delete( trailingslashit($themes_dir) . $theme, true ) )
638                                 return false;
639                 return true;
640         }
641
642         function theme_info() {
643                 if ( empty($this->result['destination_name']) )
644                         return false;
645                 return get_theme_data(WP_CONTENT_DIR . '/themes/' . $this->result['destination_name'] . '/style.css');
646         }
647
648 }
649
650 /**
651  * Core Upgrader class for WordPress. It allows for WordPress to upgrade itself in combiantion with the wp-admin/includes/update-core.php file
652  *
653  * @TODO More Detailed docs, for methods as well.
654  *
655  * @package WordPress
656  * @subpackage Upgrader
657  * @since 2.8.0
658  */
659 class Core_Upgrader extends WP_Upgrader {
660
661         function upgrade_strings() {
662                 $this->strings['up_to_date'] = __('WordPress is at the latest version.');
663                 $this->strings['no_package'] = __('Upgrade package not available.');
664                 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>.');
665                 $this->strings['unpack_package'] = __('Unpacking the update.');
666                 $this->strings['copy_failed'] = __('Could not copy files.');
667         }
668
669         function upgrade($current) {
670                 global $wp_filesystem;
671
672                 $this->init();
673                 $this->upgrade_strings();
674
675                 if ( !empty($feedback) )
676                         add_filter('update_feedback', $feedback);
677
678                 // Is an update available?
679                 if ( !isset( $current->response ) || $current->response == 'latest' )
680                         return new WP_Error('up_to_date', $this->strings['up_to_date']);
681
682                 $res = $this->fs_connect( array(ABSPATH, WP_CONTENT_DIR) );
683                 if ( is_wp_error($res) )
684                         return $res;
685
686                 $wp_dir = trailingslashit($wp_filesystem->abspath());
687
688                 $download = $this->download_package( $current->package );
689                 if ( is_wp_error($download) )
690                         return $download;
691
692                 $working_dir = $this->unpack_package( $download );
693                 if ( is_wp_error($working_dir) )
694                         return $working_dir;
695
696                 // Copy update-core.php from the new version into place.
697                 if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
698                         $wp_filesystem->delete($working_dir, true);
699                         return new WP_Error('copy_failed', $this->strings['copy_failed']);
700                 }
701                 $wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
702
703                 require(ABSPATH . 'wp-admin/includes/update-core.php');
704
705                 return update_core($working_dir, $wp_dir);
706         }
707
708 }
709
710 /**
711  * Generic Skin for the WordPress Upgrader classes. This skin is designed to be extended for specific purposes.
712  *
713  * @TODO More Detailed docs, for methods as well.
714  *
715  * @package WordPress
716  * @subpackage Upgrader
717  * @since 2.8.0
718  */
719 class WP_Upgrader_Skin {
720
721         var $upgrader;
722         var $done_header = false;
723
724         function WP_Upgrader_Skin($args = array()) {
725                 return $this->__construct($args);
726         }
727         function __construct($args = array()) {
728                 $defaults = array( 'url' => '', 'nonce' => '', 'title' => '', 'context' => false );
729                 $this->options = wp_parse_args($args, $defaults);
730         }
731
732         function set_upgrader(&$upgrader) {
733                 if ( is_object($upgrader) )
734                         $this->upgrader =& $upgrader;
735         }
736         function set_result($result) {
737                 $this->result = $result;
738         }
739
740         function request_filesystem_credentials($error = false) {
741                 $url = $this->options['url'];
742                 $context = $this->options['context'];
743                 if ( !empty($this->options['nonce']) )
744                         $url = wp_nonce_url($url, $this->options['nonce']);
745                 return request_filesystem_credentials($url, '', $error, $context); //Possible to bring inline, Leaving as is for now.
746         }
747
748         function header() {
749                 if ( $this->done_header )
750                         return;
751                 $this->done_header = true;
752                 echo '<div class="wrap">';
753                 echo screen_icon();
754                 echo '<h2>' . $this->options['title'] . '</h2>';
755         }
756         function footer() {
757                 echo '</div>';
758         }
759
760         function error($errors) {
761                 if ( ! $this->done_header )
762                         $this->header();
763                 if ( is_string($errors) ) {
764                         $this->feedback($errors);
765                 } elseif ( is_wp_error($errors) && $errors->get_error_code() ) {
766                         foreach ( $errors->get_error_messages() as $message ) {
767                                 if ( $errors->get_error_data() )
768                                         $this->feedback($message . ' ' . $errors->get_error_data() );
769                                 else
770                                         $this->feedback($message);
771                         }
772                 }
773         }
774
775         function feedback($string) {
776                 if ( isset( $this->upgrader->strings[$string] ) )
777                         $string = $this->upgrader->strings[$string];
778
779                 if ( strpos($string, '%') !== false ) {
780                         $args = func_get_args();
781                         $args = array_splice($args, 1);
782                         if ( !empty($args) )
783                                 $string = vsprintf($string, $args);
784                 }
785                 if ( empty($string) )
786                         return;
787                 show_message($string);
788         }
789         function before() {}
790         function after() {}
791
792 }
793
794 /**
795  * Plugin Upgrader Skin for WordPress Plugin Upgrades.
796  *
797  * @TODO More Detailed docs, for methods as well.
798  *
799  * @package WordPress
800  * @subpackage Upgrader
801  * @since 2.8.0
802  */
803 class Plugin_Upgrader_Skin extends WP_Upgrader_Skin {
804         var $plugin = '';
805         var $plugin_active = false;
806
807         function Plugin_Upgrader_Skin($args = array()) {
808                 return $this->__construct($args);
809         }
810
811         function __construct($args = array()) {
812                 $defaults = array( 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => __('Upgrade Plugin') );
813                 $args = wp_parse_args($args, $defaults);
814
815                 $this->plugin = $args['plugin'];
816
817                 $this->plugin_active = is_plugin_active($this->plugin);
818
819                 parent::__construct($args);
820         }
821
822         function after() {
823                 $this->plugin = $this->upgrader->plugin_info();
824                 if( !empty($this->plugin) && !is_wp_error($this->result) && $this->plugin_active ){
825                         show_message(__('Attempting reactivation of the plugin'));
826                         echo '<iframe style="border:0;overflow:hidden" width="100%" height="170px" src="' . wp_nonce_url('update.php?action=activate-plugin&plugin=' . $this->plugin, 'activate-plugin_' . $this->plugin) .'"></iframe>';
827                 }
828                 $update_actions =  array(
829                         '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>',
830                         'plugins_page' => '<a href="' . admin_url('plugins.php') . '" title="' . esc_attr__('Goto plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>'
831                 );
832                 if ( $this->plugin_active )
833                         unset( $update_actions['activate_plugin'] );
834                 if ( ! $this->result || is_wp_error($this->result) )
835                         unset( $update_actions['activate_plugin'] );
836
837                 $update_actions = apply_filters('update_plugin_complete_actions', $update_actions, $this->plugin);
838                 if ( ! empty($update_actions) )
839                         $this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$update_actions));
840         }
841 }
842
843 /**
844  * Plugin Installer Skin for WordPress Plugin Installer.
845  *
846  * @TODO More Detailed docs, for methods as well.
847  *
848  * @package WordPress
849  * @subpackage Upgrader
850  * @since 2.8.0
851  */
852 class Plugin_Installer_Skin extends WP_Upgrader_Skin {
853         var $api;
854         var $type;
855
856         function Plugin_Installer_Skin($args = array()) {
857                 return $this->__construct($args);
858         }
859
860         function __construct($args = array()) {
861                 $defaults = array( 'type' => 'web', 'url' => '', 'plugin' => '', 'nonce' => '', 'title' => '' );
862                 $args = wp_parse_args($args, $defaults);
863
864                 $this->type = $args['type'];
865                 $this->api = isset($args['api']) ? $args['api'] : array();
866
867                 parent::__construct($args);
868         }
869
870         function before() {
871                 if ( !empty($this->api) )
872                         $this->upgrader->strings['process_success'] = sprintf( __('Successfully installed the plugin <strong>%s %s</strong>.'), $this->api->name, $this->api->version);
873         }
874
875         function after() {
876
877                 $plugin_file = $this->upgrader->plugin_info();
878
879                 $install_actions = array(
880                         '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>',
881                                                         );
882
883                 if ( $this->type == 'web' )
884                         $install_actions['plugins_page'] = '<a href="' . admin_url('plugin-install.php') . '" title="' . esc_attr__('Return to Plugin Installer') . '" target="_parent">' . __('Return to Plugin Installer') . '</a>';
885                 else
886                         $install_actions['plugins_page'] = '<a href="' . admin_url('plugins.php') . '" title="' . esc_attr__('Return to Plugins page') . '" target="_parent">' . __('Return to Plugins page') . '</a>';
887
888
889                 if ( ! $this->result || is_wp_error($this->result) )
890                         unset( $install_actions['activate_plugin'] );
891
892                 $install_actions = apply_filters('install_plugin_complete_actions', $install_actions, $this->api, $plugin_file);
893                 if ( ! empty($install_actions) )
894                         $this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
895         }
896 }
897
898 /**
899  * Theme Installer Skin for the WordPress Theme Installer.
900  *
901  * @TODO More Detailed docs, for methods as well.
902  *
903  * @package WordPress
904  * @subpackage Upgrader
905  * @since 2.8.0
906  */
907 class Theme_Installer_Skin extends WP_Upgrader_Skin {
908         var $api;
909         var $type;
910
911         function Theme_Installer_Skin($args = array()) {
912                 return $this->__construct($args);
913         }
914
915         function __construct($args = array()) {
916                 $defaults = array( 'type' => 'web', 'url' => '', 'theme' => '', 'nonce' => '', 'title' => '' );
917                 $args = wp_parse_args($args, $defaults);
918
919                 $this->type = $args['type'];
920                 $this->api = isset($args['api']) ? $args['api'] : array();
921
922                 parent::__construct($args);
923         }
924
925         function before() {
926                 if ( !empty($this->api) ) {
927                         /* translators: 1: theme name, 2: version */
928                         $this->upgrader->strings['process_success'] = sprintf( __('Successfully installed the theme <strong>%1$s %2$s</strong>.'), $this->api->name, $this->api->version);
929                 }
930         }
931
932         function after() {
933                 if ( empty($this->upgrader->result['destination_name']) )
934                         return;
935
936                 $theme_info = $this->upgrader->theme_info();
937                 if ( empty($theme_info) )
938                         return;
939                 $name = $theme_info['Name'];
940                 $stylesheet = $this->upgrader->result['destination_name'];
941                 $template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
942
943                 $preview_link = htmlspecialchars( add_query_arg( array('preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'TB_iframe' => 'true' ), trailingslashit(esc_url(get_option('home'))) ) );
944                 $activate_link = wp_nonce_url("themes.php?action=activate&amp;template=" . urlencode($template) . "&amp;stylesheet=" . urlencode($stylesheet), 'switch-theme_' . $template);
945
946                 $install_actions = array(
947                         'preview' => '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="' . esc_attr(sprintf(__('Preview &#8220;%s&#8221;'), $name)) . '">' . __('Preview') . '</a>',
948                         'activate' => '<a href="' . $activate_link .  '" class="activatelink" title="' . esc_attr( sprintf( __('Activate &#8220;%s&#8221;'), $name ) ) . '">' . __('Activate') . '</a>'
949                                                         );
950
951                 if ( $this->type == 'web' )
952                         $install_actions['themes_page'] = '<a href="' . admin_url('theme-install.php') . '" title="' . esc_attr__('Return to Theme Installer') . '" target="_parent">' . __('Return to Theme Installer') . '</a>';
953                 else
954                         $install_actions['themes_page'] = '<a href="' . admin_url('themes.php') . '" title="' . esc_attr__('Themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>';
955
956                 if ( ! $this->result || is_wp_error($this->result) )
957                         unset( $install_actions['activate'], $install_actions['preview'] );
958
959                 $install_actions = apply_filters('install_theme_complete_actions', $install_actions, $this->api, $stylesheet, $theme_info);
960                 if ( ! empty($install_actions) )
961                         $this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$install_actions));
962         }
963 }
964
965 /**
966  * Theme Upgrader Skin for WordPress Theme Upgrades.
967  *
968  * @TODO More Detailed docs, for methods as well.
969  *
970  * @package WordPress
971  * @subpackage Upgrader
972  * @since 2.8.0
973  */
974 class Theme_Upgrader_Skin extends WP_Upgrader_Skin {
975         var $theme = '';
976
977         function Theme_Upgrader_Skin($args = array()) {
978                 return $this->__construct($args);
979         }
980
981         function __construct($args = array()) {
982                 $defaults = array( 'url' => '', 'theme' => '', 'nonce' => '', 'title' => __('Upgrade Theme') );
983                 $args = wp_parse_args($args, $defaults);
984
985                 $this->theme = $args['theme'];
986
987                 parent::__construct($args);
988         }
989
990         function after() {
991
992                 if ( !empty($this->upgrader->result['destination_name']) &&
993                         ($theme_info = $this->upgrader->theme_info()) &&
994                         !empty($theme_info) ) {
995
996                         $name = $theme_info['Name'];
997                         $stylesheet = $this->upgrader->result['destination_name'];
998                         $template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
999         
1000                         $preview_link = htmlspecialchars( add_query_arg( array('preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'TB_iframe' => 'true' ), trailingslashit(esc_url(get_option('home'))) ) );
1001                         $activate_link = wp_nonce_url("themes.php?action=activate&amp;template=" . urlencode($template) . "&amp;stylesheet=" . urlencode($stylesheet), 'switch-theme_' . $template);
1002         
1003                         $update_actions =  array(
1004                                 'preview' => '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="' . esc_attr(sprintf(__('Preview &#8220;%s&#8221;'), $name)) . '">' . __('Preview') . '</a>',
1005                                 'activate' => '<a href="' . $activate_link .  '" class="activatelink" title="' . esc_attr( sprintf( __('Activate &#8220;%s&#8221;'), $name ) ) . '">' . __('Activate') . '</a>',
1006                         );
1007                         if ( ( ! $this->result || is_wp_error($this->result) ) || $stylesheet == get_stylesheet() )
1008                                 unset($update_actions['preview'], $update_actions['activate']);
1009                 }
1010
1011                 $update_actions['themes_page'] = '<a href="' . admin_url('themes.php') . '" title="' . esc_attr__('Return to Themes page') . '" target="_parent">' . __('Return to Themes page') . '</a>';
1012
1013                 $update_actions = apply_filters('update_theme_complete_actions', $update_actions, $this->theme);
1014                 if ( ! empty($update_actions) )
1015                         $this->feedback('<strong>' . __('Actions:') . '</strong> ' . implode(' | ', (array)$update_actions));
1016         }
1017 }
1018
1019 /**
1020  * Upgrade Skin helper for File uploads. This class handles the upload process and passes it as if its a local file to the Upgrade/Installer functions.
1021  *
1022  * @TODO More Detailed docs, for methods as well.
1023  *
1024  * @package WordPress
1025  * @subpackage Upgrader
1026  * @since 2.8.0
1027  */
1028 class File_Upload_Upgrader {
1029         var $package;
1030         var $filename;
1031
1032         function File_Upload_Upgrader($form, $urlholder) {
1033                 return $this->__construct($form, $urlholder);
1034         }
1035         function __construct($form, $urlholder) {
1036                 if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
1037                         wp_die($uploads['error']);
1038
1039                 if ( empty($_FILES[$form]['name']) && empty($_GET[$urlholder]) )
1040                         wp_die(__('Please select a file'));
1041
1042                 if ( !empty($_FILES) )
1043                         $this->filename = $_FILES[$form]['name'];
1044                 else if ( isset($_GET[$urlholder]) )
1045                         $this->filename = $_GET[$urlholder];
1046
1047                 //Handle a newly uploaded file, Else assume its already been uploaded
1048                 if ( !empty($_FILES) ) {
1049                         $this->filename = wp_unique_filename( $uploads['basedir'], $this->filename );
1050                         $this->package = $uploads['basedir'] . '/' . $this->filename;
1051
1052                         // Move the file to the uploads dir
1053                         if ( false === @ move_uploaded_file( $_FILES[$form]['tmp_name'], $this->package) )
1054                                 wp_die( sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path']));
1055                 } else {
1056                         $this->package = $uploads['basedir'] . '/' . $this->filename;
1057                 }
1058         }
1059 }