home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-admin / includes / update.php < prev    next >
Encoding:
PHP Script  |  2008-07-12  |  7.1 KB  |  190 lines

  1. <?php
  2.  
  3. // The admin side of our 1.1 update system
  4.  
  5. function core_update_footer( $msg = '' ) {
  6.     if ( !current_user_can('manage_options') )
  7.         return sprintf( '| '.__( 'Version %s' ), $GLOBALS['wp_version'] );
  8.  
  9.     $cur = get_option( 'update_core' );
  10.  
  11.     switch ( $cur->response ) {
  12.     case 'development' :
  13.         return sprintf( '| '.__( 'You are using a development version (%s). Cool! Please <a href="%s">stay updated</a>.' ), $GLOBALS['wp_version'], $cur->url, $cur->current );
  14.     break;
  15.  
  16.     case 'upgrade' :
  17.         if ( current_user_can('manage_options') ) {
  18.             return sprintf( '| <strong>'.__( '<a href="%2$s">Get Version %3$s</a>' ).'</strong>', $GLOBALS['wp_version'], $cur->url, $cur->current );
  19.             break;
  20.         }
  21.  
  22.     case 'latest' :
  23.     default :
  24.         return sprintf( '| '.__( 'Version %s' ), $GLOBALS['wp_version'], $cur->url, $cur->current );
  25.     break;
  26.     }
  27. }
  28. add_filter( 'update_footer', 'core_update_footer' );
  29.  
  30. function update_nag() {
  31.     $cur = get_option( 'update_core' );
  32.  
  33.     if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
  34.         return false;
  35.  
  36.     if ( current_user_can('manage_options') )
  37.         $msg = sprintf( __('WordPress %2$s is available! <a href="%1$s">Please update now</a>.'), $cur->url, $cur->current );
  38.     else
  39.         $msg = sprintf( __('WordPress %2$s is available! Please notify the site administrator.'), $cur->url, $cur->current );
  40.  
  41.     echo "<div id='update-nag'>$msg</div>";
  42. }
  43. add_action( 'admin_notices', 'update_nag', 3 );
  44.  
  45. // Called directly from dashboard
  46. function update_right_now_message() {
  47.     $cur = get_option( 'update_core' );
  48.  
  49.     $msg = sprintf( __('This is WordPress version %s.'), $GLOBALS['wp_version'] );
  50.     if ( isset( $cur->response ) && $cur->response == 'upgrade' && current_user_can('manage_options') )
  51.         $msg .= " <a href='$cur->url' class='rbutton'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
  52.  
  53.     echo "<span id='wp-version-message'>$msg</span>";
  54. }
  55.  
  56. function wp_plugin_update_row( $file, $plugin_data ) {
  57.     $current = get_option( 'update_plugins' );
  58.     if ( !isset( $current->response[ $file ] ) )
  59.         return false;
  60.  
  61.     $r = $current->response[ $file ];
  62.  
  63.     echo '<tr><td colspan="5" class="plugin-update">';
  64.     if ( ! current_user_can('update_plugins') )
  65.         printf( __('There is a new version of %1$s available. <a href="%2$s">Download version %3$s here</a>.'), $plugin_data['Name'], $r->url, $r->new_version);
  66.     else if ( empty($r->package) )
  67.         printf( __('There is a new version of %1$s available. <a href="%2$s">Download version %3$s here</a> <em>automatic upgrade unavailable for this plugin</em>.'), $plugin_data['Name'], $r->url, $r->new_version);
  68.     else
  69.         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, wp_nonce_url('update.php?action=upgrade-plugin&plugin=' . $file, 'upgrade-plugin_' . $file) );
  70.     
  71.     echo '</td></tr>';
  72. }
  73. add_action( 'after_plugin_row', 'wp_plugin_update_row', 10, 2 );
  74.  
  75. function wp_update_plugin($plugin, $feedback = '') {
  76.     global $wp_filesystem;
  77.  
  78.     if ( !empty($feedback) )
  79.         add_filter('update_feedback', $feedback);
  80.  
  81.     // Is an update available?
  82.     $current = get_option( 'update_plugins' );
  83.     if ( !isset( $current->response[ $plugin ] ) )
  84.         return new WP_Error('up_to_date', __('The plugin is at the latest version.'));
  85.  
  86.     // Is a filesystem accessor setup?
  87.     if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
  88.         WP_Filesystem();
  89.  
  90.     if ( ! is_object($wp_filesystem) )
  91.         return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
  92.  
  93.     if ( $wp_filesystem->errors->get_error_code() )
  94.         return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
  95.  
  96.     //Get the base plugin folder
  97.     $plugins_dir = $wp_filesystem->wp_plugins_dir();
  98.     if ( empty($plugins_dir) )
  99.         return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
  100.  
  101.     //And the same for the Content directory.
  102.     $content_dir = $wp_filesystem->wp_content_dir();
  103.     if( empty($content_dir) )
  104.         return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
  105.     
  106.     $plugins_dir = trailingslashit( $plugins_dir );
  107.     $content_dir = trailingslashit( $content_dir );
  108.  
  109.     // Get the URL to the zip file
  110.     $r = $current->response[ $plugin ];
  111.  
  112.     if ( empty($r->package) )
  113.         return new WP_Error('no_package', __('Upgrade package not available.'));
  114.  
  115.     // Download the package
  116.     $package = $r->package;
  117.     apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
  118.     $download_file = download_url($package);
  119.  
  120.     if ( is_wp_error($download_file) )
  121.         return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
  122.  
  123.     $working_dir = $content_dir . 'upgrade/' . basename($plugin, '.php');
  124.  
  125.     // Clean up working directory
  126.     if ( $wp_filesystem->is_dir($working_dir) )
  127.         $wp_filesystem->delete($working_dir, true);
  128.  
  129.     apply_filters('update_feedback', __('Unpacking the update'));
  130.     // Unzip package to working directory
  131.     $result = unzip_file($download_file, $working_dir);
  132.     
  133.     // Once extracted, delete the package
  134.     unlink($download_file);
  135.     
  136.     if ( is_wp_error($result) ) {
  137.         $wp_filesystem->delete($working_dir, true);
  138.         return $result;
  139.     }
  140.  
  141.     if ( is_plugin_active($plugin) ) {
  142.         //Deactivate the plugin silently, Prevent deactivation hooks from running.
  143.         apply_filters('update_feedback', __('Deactivating the plugin'));
  144.         deactivate_plugins($plugin, true);
  145.     }
  146.  
  147.     // Remove the existing plugin.
  148.     apply_filters('update_feedback', __('Removing the old version of the plugin'));
  149.     $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
  150.     
  151.     // If plugin is in its own directory, recursively delete the directory.
  152.     if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
  153.         $deleted = $wp_filesystem->delete($this_plugin_dir, true);
  154.     else
  155.         $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
  156.  
  157.     if ( ! $deleted ) {
  158.         $wp_filesystem->delete($working_dir, true);
  159.         return new WP_Error('delete_failed', __('Could not remove the old plugin'));
  160.     }
  161.  
  162.     apply_filters('update_feedback', __('Installing the latest version'));
  163.     // Copy new version of plugin into place.
  164.     $result = copy_dir($working_dir, $plugins_dir);
  165.     if ( is_wp_error($result) ) {
  166.         //$wp_filesystem->delete($working_dir, true); //TODO: Uncomment? This DOES mean that the new files are available in the upgrade folder if it fails.
  167.         return $result;
  168.     }
  169.  
  170.     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
  171.     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
  172.  
  173.     // Remove working directory
  174.     $wp_filesystem->delete($working_dir, true);
  175.  
  176.     // Force refresh of plugin update information
  177.     delete_option('update_plugins');
  178.     
  179.     if( empty($filelist) )
  180.         return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
  181.     
  182.     $folder = $filelist[0];
  183.     $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
  184.     $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
  185.  
  186.     return  $folder . '/' . $pluginfiles[0];
  187. }
  188.  
  189. ?>
  190.