Make WordPress Core


Ignore:
Timestamp:
02/11/2008 05:45:54 AM (17 years ago)
Author:
ryan
Message:

First cut at plugin update. Props DD32 for the filesystem abstraction. see #5586

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/update.php

    r6551 r6779  
    115115
    116116    echo "<tr><td colspan='5' class='plugin-update'>";
    117     printf( __('There is a new version of %s available. <a href="%s">Download version %s here</a>.'), $plugin_data['Name'], $r->url, $r->new_version );
     117    printf( __('There is a new version of %1$s available. <a href="%2$s">Download version %3$s here</a> or <a href="%4$s">upgrade automatically</a>.'), $plugin_data['Name'], $r->url, $r->new_version, "update.php?action=upgrade-plugin&amp;plugin=$file" );
    118118    echo "</td></tr>";
    119119}
    120120add_action( 'after_plugin_row', 'wp_plugin_update_row' );
    121121
     122function wp_update_plugin($plugin, $feedback = '') {
     123    global $wp_filesystem;
     124
     125    if ( !empty($feedback) )
     126        add_filter('update_feedback', $feedback);
     127
     128    // Is an update available?
     129    $current = get_option( 'update_plugins' );
     130    if ( !isset( $current->response[ $plugin ] ) )
     131        return new WP_Error('up_to_date', __('The plugin is at the latest version.'));
     132
     133    // Is a filesystem accessor setup?
     134    if ( ! $wp_filesystem || !is_object($wp_filesystem) )
     135        WP_Filesystem();
     136
     137    if ( ! is_object($wp_filesystem) )
     138        return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
     139
     140    if ( $wp_filesystem->errors->get_error_code() )
     141        return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
     142
     143    // Get the URL to the zip file
     144    $r = $current->response[ $plugin ];
     145
     146    if ( empty($r->package) )
     147        return new WP_Error('no_package', __('Upgrade package not available.'));
     148
     149    // Download the package
     150    $package = $r->package;
     151    apply_filters('update_feedback', __("Downloading update from $package"));
     152    $file = download_url($package);
     153
     154    if ( !$file )
     155        return new WP_Error('download_failed', __('Download failed.'));
     156
     157    $name = basename($plugin, '.php');
     158    $working_dir = ABSPATH . 'wp-content/upgrade/' . $name;
     159
     160    // Clean up working directory
     161    $wp_filesystem->delete($working_dir, true);
     162
     163    apply_filters('update_feedback', __("Unpacking the update"));
     164    // Unzip package to working directory
     165    $result = unzip_file($file, $working_dir);
     166    if ( is_wp_error($result) ) {
     167        unlink($file);
     168        $wp_filesystem->delete($working_dir, true);
     169        return $result;
     170    }
     171
     172    // Once installed, delete the package
     173    unlink($file);
     174   
     175    // Remove the existing plugin.
     176    apply_filters('update_feedback', __("Removing the old version of the plugin"));
     177    $wp_filesystem->delete(ABSPATH . PLUGINDIR . "/$plugin");
     178    $plugin_dir = dirname(ABSPATH . PLUGINDIR . "/$plugin");
     179
     180    // If plugin is in its own directory, recursively delete the directory.
     181    if ( '.' != $plugin_dir )
     182        $wp_filesystem->delete($plugin_dir, true);
     183
     184    apply_filters('update_feedback', __("Installing the latest version"));
     185    // Copy new version of plugin into place.
     186    copy_dir($working_dir, ABSPATH . PLUGINDIR);
     187
     188    // Remove working directory
     189    $wp_filesystem->delete($working_dir, true);
     190
     191    // Force refresh of plugin update information
     192    delete_option('update_plugins');
     193}
     194
    122195?>
Note: See TracChangeset for help on using the changeset viewer.