Make WordPress Core

Ticket #9757: upload_upgrade.diff

File upload_upgrade.diff, 5.4 KB (added by cyberhobo, 14 years ago)

Refresh, improvements

  • wp-admin/includes/plugin.php

     
    212212 * @since 1.5.0
    213213 *
    214214 * @param string $plugin_folder Optional. Relative path to single plugin folder.
     215 * @param string $plugin_root Optional. Allows data to be retrieved for plugin(s) in a working directory (e.g. for zip upgrades)
    215216 * @return array Key is the plugin file path and the value is an array of the plugin data.
    216217 */
    217 function get_plugins($plugin_folder = '') {
     218function get_plugins($plugin_folder = '', $plugin_root = '') {
    218219
    219         if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
     220        if ( empty( $plugin_root ) and ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
    220221                $cache_plugins = array();
    221222
    222223        if ( isset($cache_plugins[ $plugin_folder ]) )
    223224                return $cache_plugins[ $plugin_folder ];
    224225
    225226        $wp_plugins = array ();
    226         $plugin_root = WP_PLUGIN_DIR;
     227        if ( empty($plugin_root ) )
     228                $plugin_root = WP_PLUGIN_DIR;
    227229        if ( !empty($plugin_folder) )
    228230                $plugin_root .= $plugin_folder;
    229231
  • wp-admin/includes/plugin-install.php

     
    147147                <?php wp_nonce_field( 'plugin-upload') ?>
    148148                <label class="screen-reader-text" for="pluginzip"><?php _e('Plugin zip file'); ?></label>
    149149                <input type="file" id="pluginzip" name="pluginzip" />
     150                <input type="checkbox" id="upgrade_checked" name="upgrade_checked" />
     151                <label for="upgrade_checked"><?php _e('Replace current plugin'); ?></label>
    150152                <input type="submit" class="button" value="<?php esc_attr_e('Install Now') ?>" />
    151153        </form>
    152154<?php
  • wp-admin/includes/class-wp-upgrader.php

     
    270270                                                        'clear_destination' => false,
    271271                                                        'clear_working' => true,
    272272                                                        'is_multi' => false,
    273                                                         'hook_extra' => array() //Pass any extra $hook_extra args here, this will be passed to any hooked filters.
     273                                                        'hook_extra' => array(), //Pass any extra $hook_extra args here, this will be passed to any hooked filters.
     274                                                        'is_upload_upgrade' => false
    274275                                                );
    275276
    276277                $options = wp_parse_args($options, $defaults);
     
    309310                        return $working_dir;
    310311                }
    311312
     313                if ( $is_upload_upgrade and empty( $hook_extra ) ) {
     314
     315                        $plugin = get_plugins( '', $working_dir );
     316                        if ( is_wp_error($plugin) ) {
     317                                $this->skin->error($plugin);
     318                                return $plugin;
     319                        }
     320                        // Not (yet?) attempting to handle multple plugins in a package
     321                        if ( count( $plugin ) != 1 )
     322                                return $this->skin( new WP_Error('bad_package', $this->strings['bad_package']) );
     323
     324                        // Make sure the plugin name matches the one it will replace
     325                        $installed_plugins = get_plugins();
     326                        $plugin_paths = array_keys( $plugin );
     327                        $plugin_path = $plugin_paths[0];
     328                        if ( ! isset( $installed_plugins[$plugin_path] ) or $installed_plugins[$plugin_path]['Name'] != $plugin[$plugin_path]['Name'] ) {
     329                                // May want a different error message for this
     330                                $error = new WP_Error( 'bad_package', $this->strings['bad_package'] );
     331                                $this->skin->error( $error );
     332                                return $error;
     333                        }
     334
     335                        // This upgrade method depends on this identification of the plugin for hooks
     336                        $hook_extra = array( 'plugin' => $plugin_path );
     337                }
     338
    312339                //With the given options, this installs it to the destination directory.
    313340                $result = $this->install_package( array(
    314341                                                                                        'source' => $working_dir,
     
    404431
    405432        }
    406433
     434        function upload_upgrade($package) {
     435
     436                $this->init();
     437                $this->upgrade_strings();
     438
     439                add_filter('upgrader_pre_install', array(&$this, 'deactivate_plugin_before_upgrade'), 10, 2);
     440                add_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'), 10, 4);
     441
     442                $this->run(array(
     443                                        'package' => $package,
     444                                        'destination' => WP_PLUGIN_DIR,
     445                                        'clear_destination' => true,
     446                                        'clear_working' => true,
     447                                        'hook_extra' => array(), // run must supply this after looking in the package for plugin name
     448                                        'is_upload_upgrade' => true
     449                                ));
     450
     451                // Cleanup our hooks, in case something else does a upgrade on this connection.
     452                remove_filter('upgrader_pre_install', array(&$this, 'deactivate_plugin_before_upgrade'));
     453                remove_filter('upgrader_clear_destination', array(&$this, 'delete_old_plugin'));
     454
     455                if ( ! $this->result || is_wp_error($this->result) )
     456                        return $this->result;
     457
     458                // Force refresh of plugin update information
     459                delete_site_transient('update_plugins');
     460        }
     461
    407462        function upgrade($plugin) {
    408463
    409464                $this->init();
  • wp-admin/update.php

     
    138138                $type = 'upload'; //Install plugin type, From Web or an Upload.
    139139
    140140                $upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin( compact('type', 'title', 'nonce', 'url') ) );
    141                 $upgrader->install( $file_upload->package );
     141                if ( isset( $_REQUEST['upgrade_checked'] ) )
     142                        $upgrader->upload_upgrade( $file_upload->package );
     143                else
     144                        $upgrader->install( $file_upload->package );
    142145
    143146                include(ABSPATH . 'wp-admin/admin-footer.php');
    144147