[ Index ]

PHP Cross Reference of Wordpress 2.7.1

title

Body

[close]

/wp-admin/includes/ -> update.php (source)

   1  <?php
   2  /**
   3   * WordPress Administration Update API
   4   *
   5   * @package WordPress
   6   * @subpackage Admin
   7   */
   8  
   9  // The admin side of our 1.1 update system
  10  
  11  /**
  12   * Selects the first update version from the update_core option
  13   *
  14   * @return object the response from the API
  15   */
  16  function get_preferred_from_update_core() {
  17      $updates = get_core_updates();
  18      if ( !is_array( $updates ) )
  19          return false;
  20      if ( empty( $updates ) )
  21          return (object)array('response' => 'latest');
  22      return $updates[0];
  23  }
  24  
  25  /**
  26   * Get available core updates
  27   *
  28   * @param array $options Set $options['dismissed'] to true to show dismissed upgrades too,
  29   *     set $options['available'] to false to skip not-dimissed updates.
  30   * @return array Array of the update objects
  31   */
  32  function get_core_updates( $options = array() ) {
  33      $options = array_merge( array('available' => true, 'dismissed' => false ), $options );
  34      $dismissed = get_option( 'dismissed_update_core' );
  35      if ( !is_array( $dismissed ) ) $dismissed = array();
  36      $from_api = get_option( 'update_core' );
  37      if ( empty($from_api) )
  38          return false;
  39      if ( !is_array( $from_api->updates ) ) return false;
  40      $updates = $from_api->updates;
  41      if ( !is_array( $updates ) ) return false;
  42      $result = array();
  43      foreach($updates as $update) {
  44          if ( array_key_exists( $update->current.'|'.$update->locale, $dismissed ) ) {
  45              if ( $options['dismissed'] ) {
  46                  $update->dismissed = true;
  47                  $result[]= $update;
  48              }
  49          } else {
  50              if ( $options['available'] ) {
  51                  $update->dismissed = false;
  52                  $result[]= $update;
  53              }
  54          }
  55      }
  56      return $result;
  57  }
  58  
  59  function dismiss_core_update( $update ) {
  60      $dismissed = get_option( 'dismissed_update_core' );
  61      $dismissed[ $update->current.'|'.$update->locale ] = true;
  62      return update_option( 'dismissed_update_core', $dismissed );
  63  }
  64  
  65  function undismiss_core_update( $version, $locale ) {
  66      $dismissed = get_option( 'dismissed_update_core' );
  67      $key = $version.'|'.$locale;
  68      if ( !isset( $dismissed[$key] ) ) return false;
  69      unset( $dismissed[$key] );
  70      return update_option( 'dismissed_update_core', $dismissed );
  71  }
  72  
  73  function find_core_update( $version, $locale ) {
  74      $from_api = get_option( 'update_core' );
  75      if ( !is_array( $from_api->updates ) ) return false;
  76      $updates = $from_api->updates;
  77      foreach($updates as $update) {
  78          if ( $update->current == $version && $update->locale == $locale )
  79              return $update;
  80      }
  81      return false;
  82  }
  83  
  84  function core_update_footer( $msg = '' ) {
  85      if ( !current_user_can('manage_options') )
  86          return sprintf( '| '.__( 'Version %s' ), $GLOBALS['wp_version'] );
  87  
  88      $cur = get_preferred_from_update_core();
  89      if ( ! isset( $cur->current ) )
  90          $cur->current = '';
  91  
  92      if ( ! isset( $cur->url ) )
  93          $cur->url = '';
  94  
  95      if ( ! isset( $cur->response ) )
  96          $cur->response = '';
  97  
  98      switch ( $cur->response ) {
  99      case 'development' :
 100          return sprintf( __( 'You are using a development version (%1$s). Cool! Please <a href="%2$s">stay updated</a>.' ), $GLOBALS['wp_version'], 'update-core.php');
 101      break;
 102  
 103      case 'upgrade' :
 104          if ( current_user_can('manage_options') ) {
 105              return sprintf( '<strong>'.__( '<a href="%1$s">Get Version %2$s</a>' ).'</strong>', 'update-core.php', $cur->current);
 106              break;
 107          }
 108  
 109      case 'latest' :
 110      default :
 111          return sprintf( __( 'Version %s' ), $GLOBALS['wp_version'] );
 112      break;
 113      }
 114  }
 115  add_filter( 'update_footer', 'core_update_footer' );
 116  
 117  function update_nag() {
 118      global $pagenow;
 119  
 120      if ( 'update-core.php' == $pagenow )
 121          return;
 122  
 123      $cur = get_preferred_from_update_core();
 124  
 125      if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
 126          return false;
 127  
 128      if ( current_user_can('manage_options') )
 129          $msg = sprintf( __('WordPress %1$s is available! <a href="%2$s">Please update now</a>.'), $cur->current, 'update-core.php' );
 130      else
 131          $msg = sprintf( __('WordPress %1$s is available! Please notify the site administrator.'), $cur->current );
 132  
 133      echo "<div id='update-nag'>$msg</div>";
 134  }
 135  add_action( 'admin_notices', 'update_nag', 3 );
 136  
 137  // Called directly from dashboard
 138  function update_right_now_message() {
 139      $cur = get_preferred_from_update_core();
 140  
 141      $msg = sprintf( __('You are using <span class="b">WordPress %s</span>.'), $GLOBALS['wp_version'] );
 142      if ( isset( $cur->response ) && $cur->response == 'upgrade' && current_user_can('manage_options') )
 143          $msg .= " <a href='update-core.php' class='button'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
 144  
 145      echo "<span id='wp-version-message'>$msg</span>";
 146  }
 147  
 148  function wp_plugin_update_row( $file, $plugin_data ) {
 149      $current = get_option( 'update_plugins' );
 150      if ( !isset( $current->response[ $file ] ) )
 151          return false;
 152  
 153      $r = $current->response[ $file ];
 154  
 155      $details_url = admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&TB_iframe=true&width=600&height=800');
 156  
 157      echo '<tr><td colspan="5" class="plugin-update">';
 158      if ( ! current_user_can('update_plugins') )
 159          printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s Details</a>.'), $plugin_data['Name'], $details_url, $r->new_version);
 160      else if ( empty($r->package) )
 161          printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s Details</a> <em>automatic upgrade unavailable for this plugin</em>.'), $plugin_data['Name'], $details_url, $r->new_version);
 162      else
 163          printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s Details</a> or <a href="%4$s">upgrade automatically</a>.'), $plugin_data['Name'], $details_url, $r->new_version, wp_nonce_url('update.php?action=upgrade-plugin&amp;plugin=' . $file, 'upgrade-plugin_' . $file) );
 164  
 165      echo '</td></tr>';
 166  }
 167  add_action( 'after_plugin_row', 'wp_plugin_update_row', 10, 2 );
 168  
 169  function wp_update_plugin($plugin, $feedback = '') {
 170      global $wp_filesystem;
 171  
 172      if ( !empty($feedback) )
 173          add_filter('update_feedback', $feedback);
 174  
 175      // Is an update available?
 176      $current = get_option( '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_option('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];
 281  }
 282  
 283  function wp_update_theme($theme, $feedback = '') {
 284      global $wp_filesystem;
 285  
 286      if ( !empty($feedback) )
 287          add_filter('update_feedback', $feedback);
 288  
 289      // Is an update available?
 290      $current = get_option( '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_option('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];*/
 401  }
 402  
 403  
 404  function wp_update_core($current, $feedback = '') {
 405      global $wp_filesystem;
 406  
 407      @set_time_limit( 300 );
 408  
 409      if ( !empty($feedback) )
 410          add_filter('update_feedback', $feedback);
 411  
 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);
 476  }
 477  
 478  function maintenance_nag() {
 479      global $upgrading;
 480      if ( ! isset( $upgrading ) )
 481          return false;
 482  
 483      if ( current_user_can('manage_options') )
 484          $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
 485      else
 486          $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
 487  
 488      echo "<div id='update-nag'>$msg</div>";
 489  }
 490  add_action( 'admin_notices', 'maintenance_nag' );
 491  
 492  ?>


Generated: Mon Mar 23 16:23:02 2009 Cross-referenced by PHPXref 0.7