Make WordPress Core

Ticket #10541: class-wp-upgrader.php

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