Make WordPress Core


Ignore:
Timestamp:
04/19/2009 07:36:28 PM (15 years ago)
Author:
ryan
Message:

consolidate plugin/theme/core upgrade/install functions. Props DD32. see #7875

File:
1 edited

Legend:

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

    r10939 r11005  
    168168
    169169function wp_update_plugin($plugin, $feedback = '') {
    170     global $wp_filesystem;
    171170
    172171    if ( !empty($feedback) )
    173172        add_filter('update_feedback', $feedback);
    174173
    175     // Is an update available?
    176     $current = get_transient( 'update_plugins' );
    177     if ( !isset( $current->response[ $plugin ] ) )
    178         return new WP_Error('up_to_date', __('The plugin is at the latest version.'));
    179 
    180     // Is a filesystem accessor setup?
    181     if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
    182         WP_Filesystem();
    183 
    184     if ( ! is_object($wp_filesystem) )
    185         return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    186 
    187     if ( $wp_filesystem->errors->get_error_code() )
    188         return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
    189 
    190     //Get the base plugin folder
    191     $plugins_dir = $wp_filesystem->wp_plugins_dir();
    192     if ( empty($plugins_dir) )
    193         return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
    194 
    195     //And the same for the Content directory.
    196     $content_dir = $wp_filesystem->wp_content_dir();
    197     if( empty($content_dir) )
    198         return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
    199 
    200     $plugins_dir = trailingslashit( $plugins_dir );
    201     $content_dir = trailingslashit( $content_dir );
    202 
    203     // Get the URL to the zip file
    204     $r = $current->response[ $plugin ];
    205 
    206     if ( empty($r->package) )
    207         return new WP_Error('no_package', __('Upgrade package not available.'));
    208 
    209     // Download the package
    210     $package = $r->package;
    211     apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
    212     $download_file = download_url($package);
    213 
    214     if ( is_wp_error($download_file) )
    215         return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
    216 
    217     $working_dir = $content_dir . 'upgrade/' . basename($plugin, '.php');
    218 
    219     // Clean up working directory
    220     if ( $wp_filesystem->is_dir($working_dir) )
    221         $wp_filesystem->delete($working_dir, true);
    222 
    223     apply_filters('update_feedback', __('Unpacking the update'));
    224     // Unzip package to working directory
    225     $result = unzip_file($download_file, $working_dir);
    226 
    227     // Once extracted, delete the package
    228     unlink($download_file);
    229 
    230     if ( is_wp_error($result) ) {
    231         $wp_filesystem->delete($working_dir, true);
    232         return $result;
    233     }
    234 
    235     if ( is_plugin_active($plugin) ) {
    236         //Deactivate the plugin silently, Prevent deactivation hooks from running.
    237         apply_filters('update_feedback', __('Deactivating the plugin'));
    238         deactivate_plugins($plugin, true);
    239     }
    240 
    241     // Remove the existing plugin.
    242     apply_filters('update_feedback', __('Removing the old version of the plugin'));
    243     $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
    244 
    245     // If plugin is in its own directory, recursively delete the directory.
    246     if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
    247         $deleted = $wp_filesystem->delete($this_plugin_dir, true);
    248     else
    249         $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
    250 
    251     if ( ! $deleted ) {
    252         $wp_filesystem->delete($working_dir, true);
    253         return new WP_Error('delete_failed', __('Could not remove the old plugin'));
    254     }
    255 
    256     apply_filters('update_feedback', __('Installing the latest version'));
    257     // Copy new version of plugin into place.
    258     $result = copy_dir($working_dir, $plugins_dir);
    259     if ( is_wp_error($result) ) {
    260         $wp_filesystem->delete($working_dir, true);
    261         return $result;
    262     }
    263 
    264     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
    265     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    266 
    267     // Remove working directory
    268     $wp_filesystem->delete($working_dir, true);
    269 
    270     // Force refresh of plugin update information
    271     delete_transient('update_plugins');
    272 
    273     if( empty($filelist) )
    274         return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
    275 
    276     $folder = $filelist[0];
    277     $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
    278     $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
    279 
    280     return  $folder . '/' . $pluginfiles[0];
     174    include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
     175    $upgrader = new Plugin_Upgrader();
     176    return $upgrader->upgrade($plugin);
    281177}
    282178
    283179function wp_update_theme($theme, $feedback = '') {
    284     global $wp_filesystem;
    285 
     180   
    286181    if ( !empty($feedback) )
    287182        add_filter('update_feedback', $feedback);
    288183
    289     // Is an update available?
    290     $current = get_transient( 'update_themes' );
    291     if ( !isset( $current->response[ $theme ] ) )
    292         return new WP_Error('up_to_date', __('The theme is at the latest version.'));
    293 
    294     $r = $current->response[ $theme ];
    295 
    296     $themes = get_themes();
    297     foreach ( (array) $themes as $this_theme ) {
    298         if ( $this_theme['Stylesheet'] == $theme ) {
    299             $theme_directory = preg_replace('!^/themes/!i', '', $this_theme['Stylesheet Dir']);
    300             break;
    301         }
    302     }
    303     unset($themes);
    304 
    305     if ( empty($theme_directory) )
    306         return new WP_Error('theme_non_existant', __('Theme does not exist.'));
    307 
    308     // Is a filesystem accessor setup?
    309     if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
    310         WP_Filesystem();
    311 
    312     if ( ! is_object($wp_filesystem) )
    313         return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    314 
    315     if ( $wp_filesystem->errors->get_error_code() )
    316         return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
    317 
    318     //Get the base plugin folder
    319     $themes_dir = $wp_filesystem->wp_themes_dir();
    320     if ( empty($themes_dir) )
    321         return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress Theme directory.'));
    322 
    323     //And the same for the Content directory.
    324     $content_dir = $wp_filesystem->wp_content_dir();
    325     if( empty($content_dir) )
    326         return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
    327 
    328     $themes_dir = trailingslashit( $themes_dir );
    329     $content_dir = trailingslashit( $content_dir );
    330 
    331     if ( empty($r->package) )
    332         return new WP_Error('no_package', __('Upgrade package not available.'));
    333 
    334     // Download the package
    335     apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $r['package']));
    336     $download_file = download_url($r['package']);
    337 
    338     if ( is_wp_error($download_file) )
    339         return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
    340 
    341     $working_dir = $content_dir . 'upgrade/' . basename($theme_directory);
    342 
    343     // Clean up working directory
    344     if ( $wp_filesystem->is_dir($working_dir) )
    345         $wp_filesystem->delete($working_dir, true);
    346 
    347     apply_filters('update_feedback', __('Unpacking the update'));
    348     // Unzip package to working directory
    349     $result = unzip_file($download_file, $working_dir);
    350 
    351     // Once extracted, delete the package
    352     unlink($download_file);
    353 
    354     if ( is_wp_error($result) ) {
    355         $wp_filesystem->delete($working_dir, true);
    356         return $result;
    357     }
    358 
    359     //TODO: Is theme currently active? If so, set default theme
    360     /*
    361     if ( is_plugin_active($plugin) ) {
    362         //Deactivate the plugin silently, Prevent deactivation hooks from running.
    363         apply_filters('update_feedback', __('Deactivating the plugin'));
    364         deactivate_plugins($plugin, true);
    365     }*/
    366 
    367     // Remove the existing plugin.
    368     apply_filters('update_feedback', __('Removing the old version of the theme'));
    369     $deleted = $wp_filesystem->delete($themes_dir . $theme_directory, true);
    370 
    371     if ( ! $deleted ) {
    372         $wp_filesystem->delete($working_dir, true);
    373         return new WP_Error('delete_failed', __('Could not remove the old plugin'));
    374     }
    375 
    376     apply_filters('update_feedback', __('Installing the latest version'));
    377     // Copy new version of plugin into place.
    378     $result = copy_dir($working_dir, $themes_dir);
    379     if ( is_wp_error($result) ) {
    380         $wp_filesystem->delete($working_dir, true);
    381         return $result;
    382     }
    383 
    384     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
    385     //$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
    386 
    387     // Remove working directory
    388     $wp_filesystem->delete($working_dir, true);
    389 
    390     // Force refresh of plugin update information
    391     delete_transient('update_themes');
    392 
    393     /*if( empty($filelist) )
    394         return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
    395 
    396     $folder = $filelist[0];
    397     $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
    398     $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
    399 
    400     return  $folder . '/' . $pluginfiles[0];*/
     184    include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
     185    $upgrader = new Theme_Upgrader();
     186    return $upgrader->upgrade($theme);
    401187}
    402188
    403189
    404190function wp_update_core($current, $feedback = '') {
    405     global $wp_filesystem;
    406 
    407     @set_time_limit( 300 );
    408 
     191   
    409192    if ( !empty($feedback) )
    410193        add_filter('update_feedback', $feedback);
    411194
    412     // Is an update available?
    413     if ( !isset( $current->response ) || $current->response == 'latest' )
    414         return new WP_Error('up_to_date', __('WordPress is at the latest version.'));
    415 
    416     // Is a filesystem accessor setup?
    417     if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
    418         WP_Filesystem();
    419 
    420     if ( ! is_object($wp_filesystem) )
    421         return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    422 
    423     if ( $wp_filesystem->errors->get_error_code() )
    424         return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
    425 
    426     // Get the base WP folder
    427     $wp_dir = $wp_filesystem->abspath();
    428     if ( empty($wp_dir) )
    429         return new WP_Error('fs_no_wp_dir', __('Unable to locate WordPress directory.'));
    430 
    431     // And the same for the Content directory.
    432     $content_dir = $wp_filesystem->wp_content_dir();
    433     if( empty($content_dir) )
    434         return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
    435 
    436     $wp_dir = trailingslashit( $wp_dir );
    437     $content_dir = trailingslashit( $content_dir );
    438 
    439     // Get the URL to the zip file
    440     $package = $current->package;
    441 
    442     // Download the package
    443     apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
    444     $download_file = download_url($package);
    445 
    446     if ( is_wp_error($download_file) )
    447         return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
    448 
    449     $working_dir = $content_dir . 'upgrade/core';
    450     // Clean up working directory
    451     if ( $wp_filesystem->is_dir($working_dir) ) {
    452         $wp_filesystem->delete($working_dir, true);
    453     }
    454 
    455     apply_filters('update_feedback', __('Unpacking the core update'));
    456     // Unzip package to working directory
    457     $result = unzip_file($download_file, $working_dir);
    458     // Once extracted, delete the package
    459     unlink($download_file);
    460 
    461     if ( is_wp_error($result) ) {
    462         $wp_filesystem->delete($working_dir, true);
    463         return $result;
    464     }
    465 
    466     // Copy update-core.php from the new version into place.
    467     if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
    468         $wp_filesystem->delete($working_dir, true);
    469         return new WP_Error('copy_failed', __('Could not copy files'));
    470     }
    471     $wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
    472 
    473     require(ABSPATH . 'wp-admin/includes/update-core.php');
    474 
    475     return update_core($working_dir, $wp_dir);
     195    include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
     196    $upgrader = new Core_Upgrader();
     197    return $upgrader->upgrade($current);
     198
    476199}
    477200
Note: See TracChangeset for help on using the changeset viewer.