Make WordPress Core


Ignore:
Timestamp:
09/26/2008 06:43:53 AM (18 years ago)
Author:
westi
Message:

Theme update UI first pass. See #7519 props DD32.

File:
1 edited

Legend:

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

    r8852 r8989  
    171171    $result = copy_dir($working_dir, $plugins_dir);
    172172    if ( is_wp_error($result) ) {
    173         //$wp_filesystem->delete($working_dir, true); //TODO: Uncomment? This DOES mean that the new files are available in the upgrade folder if it fails.
     173        $wp_filesystem->delete($working_dir, true);
    174174        return $result;
    175175    }
     
    193193    return  $folder . '/' . $pluginfiles[0];
    194194}
     195
     196function wp_update_theme($theme, $feedback = '') {
     197    global $wp_filesystem;
     198
     199    if ( !empty($feedback) )
     200        add_filter('update_feedback', $feedback);
     201
     202    // Is an update available?
     203    $current = get_option( 'update_themes' );
     204    if ( !isset( $current->response[ $theme ] ) )
     205        return new WP_Error('up_to_date', __('The theme is at the latest version.'));
     206
     207    $r = $current->response[ $theme ];
     208   
     209    $themes = get_themes();
     210    foreach ( (array) $themes as $this_theme ) {
     211        if ( $this_theme['Stylesheet'] == $theme ) {
     212            $theme_directory = preg_replace('!^/themes/!i', '', $this_theme['Stylesheet Dir']);
     213            break;
     214        }
     215    }
     216    unset($themes);
     217
     218    if ( empty($theme_directory) )
     219        return new WP_Error('theme_non_existant', __('Theme does not exist.'));
     220
     221    // Is a filesystem accessor setup?
     222    if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
     223        WP_Filesystem();
     224
     225    if ( ! is_object($wp_filesystem) )
     226        return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
     227
     228    if ( $wp_filesystem->errors->get_error_code() )
     229        return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
     230
     231    //Get the base plugin folder
     232    $themes_dir = $wp_filesystem->wp_themes_dir();
     233    if ( empty($themes_dir) )
     234        return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress Theme directory.'));
     235
     236    //And the same for the Content directory.
     237    $content_dir = $wp_filesystem->wp_content_dir();
     238    if( empty($content_dir) )
     239        return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
     240
     241    $themes_dir = trailingslashit( $themes_dir );
     242    $content_dir = trailingslashit( $content_dir );
     243
     244    if ( empty($r->package) )
     245        return new WP_Error('no_package', __('Upgrade package not available.'));
     246
     247    // Download the package
     248    apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $r['package']));
     249    $download_file = download_url($r['package']);
     250
     251    if ( is_wp_error($download_file) )
     252        return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
     253
     254    $working_dir = $content_dir . 'upgrade/' . basename($theme_directory);
     255
     256    // Clean up working directory
     257    if ( $wp_filesystem->is_dir($working_dir) )
     258        $wp_filesystem->delete($working_dir, true);
     259
     260    apply_filters('update_feedback', __('Unpacking the update'));
     261    // Unzip package to working directory
     262    $result = unzip_file($download_file, $working_dir);
     263
     264    // Once extracted, delete the package
     265    unlink($download_file);
     266
     267    if ( is_wp_error($result) ) {
     268        $wp_filesystem->delete($working_dir, true);
     269        return $result;
     270    }
     271
     272    //TODO: Is theme currently active? If so, set default theme
     273    /*
     274    if ( is_plugin_active($plugin) ) {
     275        //Deactivate the plugin silently, Prevent deactivation hooks from running.
     276        apply_filters('update_feedback', __('Deactivating the plugin'));
     277        deactivate_plugins($plugin, true);
     278    }*/
     279
     280    // Remove the existing plugin.
     281    apply_filters('update_feedback', __('Removing the old version of the theme'));
     282    $deleted = $wp_filesystem->delete($themes_dir . $theme_directory, true);
     283
     284    if ( ! $deleted ) {
     285        $wp_filesystem->delete($working_dir, true);
     286        return new WP_Error('delete_failed', __('Could not remove the old plugin'));
     287    }
     288
     289    apply_filters('update_feedback', __('Installing the latest version'));
     290    // Copy new version of plugin into place.
     291    $result = copy_dir($working_dir, $themes_dir);
     292    if ( is_wp_error($result) ) {
     293        $wp_filesystem->delete($working_dir, true);
     294        return $result;
     295    }
     296
     297    //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
     298    //$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
     299
     300    // Remove working directory
     301    $wp_filesystem->delete($working_dir, true);
     302
     303    // Force refresh of plugin update information
     304    delete_option('update_themes');
     305
     306    /*if( empty($filelist) )
     307        return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
     308
     309    $folder = $filelist[0];
     310    $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
     311    $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
     312
     313    return  $folder . '/' . $pluginfiles[0];*/
     314}
     315
    195316
    196317function wp_update_core($feedback = '') {
Note: See TracChangeset for help on using the changeset viewer.