[ Index ]

PHP Cross Reference of WordPress 3.0 beta 1

[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

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

   1  <?php
   2  /**
   3   * A simple set of functions to check our version 1.0 update service.
   4   *
   5   * @package WordPress
   6   * @since 2.3.0
   7   */
   8  
   9  /**
  10   * Check WordPress version against the newest version.
  11   *
  12   * The WordPress version, PHP version, and Locale is sent. Checks against the
  13   * WordPress server at api.wordpress.org server. Will only check if WordPress
  14   * isn't installing.
  15   *
  16   * @package WordPress
  17   * @since 2.3.0
  18   * @uses $wp_version Used to check against the newest WordPress version.
  19   *
  20   * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
  21   */
  22  function wp_version_check() {
  23      if ( defined('WP_INSTALLING') )
  24          return;
  25  
  26      global $wp_version, $wpdb, $wp_local_package;
  27      $php_version = phpversion();
  28  
  29      $current = get_site_transient( 'update_core' );
  30      if ( ! is_object($current) ) {
  31          $current = new stdClass;
  32          $current->updates = array();
  33          $current->version_checked = $wp_version;
  34      }
  35  
  36      $locale = apply_filters( 'core_version_check_locale', get_locale() );
  37  
  38      // Update last_checked for current to prevent multiple blocking requests if request hangs
  39      $current->last_checked = time();
  40      set_site_transient( 'update_core', $current );
  41  
  42      if ( method_exists( $wpdb, 'db_version' ) )
  43          $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
  44      else
  45          $mysql_version = 'N/A';
  46      $local_package = isset( $wp_local_package )? $wp_local_package : '';
  47      $url = "http://api.wordpress.org/core/version-check/1.4/?version=$wp_version&php=$php_version&locale=$locale&mysql=$mysql_version&local_package=$local_package";
  48  
  49      $options = array(
  50          'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
  51          'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
  52      );
  53  
  54      $response = wp_remote_get($url, $options);
  55  
  56      if ( is_wp_error( $response ) )
  57          return false;
  58  
  59      if ( 200 != $response['response']['code'] )
  60          return false;
  61  
  62      $body = trim( $response['body'] );
  63      $body = str_replace(array("\r\n", "\r"), "\n", $body);
  64      $new_options = array();
  65      foreach ( explode( "\n\n", $body ) as $entry ) {
  66          $returns = explode("\n", $entry);
  67          $new_option = new stdClass();
  68          $new_option->response = esc_attr( $returns[0] );
  69          if ( isset( $returns[1] ) )
  70              $new_option->url = esc_url( $returns[1] );
  71          if ( isset( $returns[2] ) )
  72              $new_option->package = esc_url( $returns[2] );
  73          if ( isset( $returns[3] ) )
  74              $new_option->current = esc_attr( $returns[3] );
  75          if ( isset( $returns[4] ) )
  76              $new_option->locale = esc_attr( $returns[4] );
  77          if ( isset( $returns[5] ) )
  78              $new_option->php_version = esc_attr( $returns[5] );
  79          if ( isset( $returns[6] ) )
  80              $new_option->mysql_version = esc_attr( $returns[6] );
  81          $new_options[] = $new_option;
  82      }
  83  
  84      $updates = new stdClass();
  85      $updates->updates = $new_options;
  86      $updates->last_checked = time();
  87      $updates->version_checked = $wp_version;
  88      set_site_transient( 'update_core',  $updates);
  89  }
  90  
  91  /**
  92   * Check plugin versions against the latest versions hosted on WordPress.org.
  93   *
  94   * The WordPress version, PHP version, and Locale is sent along with a list of
  95   * all plugins installed. Checks against the WordPress server at
  96   * api.wordpress.org. Will only check if WordPress isn't installing.
  97   *
  98   * @package WordPress
  99   * @since 2.3.0
 100   * @uses $wp_version Used to notidy the WordPress version.
 101   *
 102   * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
 103   */
 104  function wp_update_plugins() {
 105      global $wp_version;
 106  
 107      if ( defined('WP_INSTALLING') )
 108          return false;
 109  
 110      // If running blog-side, bail unless we've not checked in the last 12 hours
 111      if ( !function_exists( 'get_plugins' ) )
 112          require_once ( ABSPATH . 'wp-admin/includes/plugin.php' );
 113  
 114      $plugins = get_plugins();
 115      $active  = get_option( 'active_plugins', array() );
 116      $current = get_site_transient( 'update_plugins' );
 117      if ( ! is_object($current) )
 118          $current = new stdClass;
 119  
 120      $new_option = new stdClass;
 121      $new_option->last_checked = time();
 122      $timeout = 'load-plugins.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours
 123      $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
 124  
 125      $plugin_changed = false;
 126      foreach ( $plugins as $file => $p ) {
 127          $new_option->checked[ $file ] = $p['Version'];
 128  
 129          if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
 130              $plugin_changed = true;
 131      }
 132  
 133      if ( isset ( $current->response ) && is_array( $current->response ) ) {
 134          foreach ( $current->response as $plugin_file => $update_details ) {
 135              if ( ! isset($plugins[ $plugin_file ]) ) {
 136                  $plugin_changed = true;
 137                  break;
 138              }
 139          }
 140      }
 141  
 142      // Bail if we've checked in the last 12 hours and if nothing has changed
 143      if ( $time_not_changed && !$plugin_changed )
 144          return false;
 145  
 146      // Update last_checked for current to prevent multiple blocking requests if request hangs
 147      $current->last_checked = time();
 148      set_site_transient( 'update_plugins', $current );
 149  
 150      $to_send = (object) compact('plugins', 'active');
 151  
 152      $options = array(
 153          'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
 154          'body' => array( 'plugins' => serialize( $to_send ) ),
 155          'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
 156      );
 157  
 158      $raw_response = wp_remote_post('http://api.wordpress.org/plugins/update-check/1.0/', $options);
 159  
 160      if ( is_wp_error( $raw_response ) )
 161          return false;
 162  
 163      if ( 200 != $raw_response['response']['code'] )
 164          return false;
 165  
 166      $response = unserialize( $raw_response['body'] );
 167  
 168      if ( false !== $response )
 169          $new_option->response = $response;
 170      else
 171          $new_option->response = array();
 172  
 173      set_site_transient( 'update_plugins', $new_option );
 174  }
 175  
 176  /**
 177   * Check theme versions against the latest versions hosted on WordPress.org.
 178   *
 179   * A list of all themes installed in sent to WP. Checks against the
 180   * WordPress server at api.wordpress.org. Will only check if WordPress isn't
 181   * installing.
 182   *
 183   * @package WordPress
 184   * @since 2.7.0
 185   * @uses $wp_version Used to notidy the WordPress version.
 186   *
 187   * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
 188   */
 189  function wp_update_themes( ) {
 190      global $wp_version;
 191  
 192      if ( defined( 'WP_INSTALLING' ) )
 193          return false;
 194  
 195      if ( !function_exists( 'get_themes' ) )
 196          require_once ( ABSPATH . 'wp-includes/theme.php' );
 197  
 198      $installed_themes = get_themes( );
 199      $current_theme = get_site_transient( 'update_themes' );
 200      if ( ! is_object($current_theme) )
 201          $current_theme = new stdClass;
 202  
 203      $new_option = new stdClass;
 204      $new_option->last_checked = time( );
 205      $timeout = 'load-themes.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours
 206      $time_not_changed = isset( $current_theme->last_checked ) && $timeout > ( time( ) - $current_theme->last_checked );
 207  
 208      $themes = array();
 209      $checked = array();
 210      $themes['current_theme'] = (array) $current_theme;
 211      foreach ( (array) $installed_themes as $theme_title => $theme ) {
 212          $themes[$theme['Stylesheet']] = array();
 213          $checked[$theme['Stylesheet']] = $theme['Version'];
 214  
 215          foreach ( (array) $theme as $key => $value )
 216              $themes[$theme['Stylesheet']][$key] = $value;
 217      }
 218  
 219      $theme_changed = false;
 220      foreach ( $checked as $slug => $v ) {
 221          $new_option->checked[ $slug ] = $v;
 222  
 223          if ( !isset( $current_theme->checked[ $slug ] ) || strval($current_theme->checked[ $slug ]) !== strval($v) )
 224              $theme_changed = true;
 225      }
 226  
 227      if ( isset ( $current_theme->response ) && is_array( $current_theme->response ) ) {
 228          foreach ( $current_theme->response as $slug => $update_details ) {
 229              if ( ! isset($checked[ $slug ]) ) {
 230                  $theme_changed = true;
 231                  break;
 232              }
 233          }
 234      }
 235  
 236      if ( $time_not_changed && !$theme_changed )
 237          return false;
 238  
 239      // Update last_checked for current to prevent multiple blocking requests if request hangs
 240      $current_theme->last_checked = time();
 241      set_site_transient( 'update_themes', $current_theme );
 242  
 243      $current_theme->template = get_option( 'template' );
 244  
 245      $options = array(
 246          'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
 247          'body'            => array( 'themes' => serialize( $themes ) ),
 248          'user-agent'    => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
 249      );
 250  
 251      $raw_response = wp_remote_post( 'http://api.wordpress.org/themes/update-check/1.0/', $options );
 252  
 253      if ( is_wp_error( $raw_response ) )
 254          return false;
 255  
 256      if ( 200 != $raw_response['response']['code'] )
 257          return false;
 258  
 259      $response = unserialize( $raw_response['body'] );
 260      if ( $response ) {
 261          $new_option->checked = $checked;
 262          $new_option->response = $response;
 263      }
 264  
 265      set_site_transient( 'update_themes', $new_option );
 266  }
 267  
 268  function _maybe_update_core() {
 269      global $wp_version;
 270  
 271      $current = get_site_transient( 'update_core' );
 272  
 273      if ( isset( $current->last_checked ) &&
 274          43200 > ( time() - $current->last_checked ) &&
 275          isset( $current->version_checked ) &&
 276          $current->version_checked == $wp_version )
 277          return;
 278  
 279      wp_version_check();
 280  }
 281  /**
 282   * Check the last time plugins were run before checking plugin versions.
 283   *
 284   * This might have been backported to WordPress 2.6.1 for performance reasons.
 285   * This is used for the wp-admin to check only so often instead of every page
 286   * load.
 287   *
 288   * @since 2.7.0
 289   * @access private
 290   */
 291  function _maybe_update_plugins() {
 292      $current = get_site_transient( 'update_plugins' );
 293      if ( isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked ) )
 294          return;
 295      wp_update_plugins();
 296  }
 297  
 298  /**
 299   * Check themes versions only after a duration of time.
 300   *
 301   * This is for performance reasons to make sure that on the theme version
 302   * checker is not run on every page load.
 303   *
 304   * @since 2.7.0
 305   * @access private
 306   */
 307  function _maybe_update_themes( ) {
 308      $current = get_site_transient( 'update_themes' );
 309      if ( isset( $current->last_checked ) && 43200 > ( time( ) - $current->last_checked ) )
 310          return;
 311  
 312      wp_update_themes();
 313  }
 314  
 315  add_action( 'admin_init', '_maybe_update_core' );
 316  add_action( 'wp_version_check', 'wp_version_check' );
 317  
 318  add_action( 'load-plugins.php', 'wp_update_plugins' );
 319  add_action( 'load-update.php', 'wp_update_plugins' );
 320  add_action( 'load-update-core.php', 'wp_update_plugins' );
 321  add_action( 'admin_init', '_maybe_update_plugins' );
 322  add_action( 'wp_update_plugins', 'wp_update_plugins' );
 323  
 324  add_action( 'load-themes.php', 'wp_update_themes' );
 325  add_action( 'load-update.php', 'wp_update_themes' );
 326  add_action( 'load-update-core.php', 'wp_update_themes' );
 327  add_action( 'admin_init', '_maybe_update_themes' );
 328  add_action( 'wp_update_themes', 'wp_update_themes' );
 329  
 330  if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
 331      wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
 332  
 333  if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
 334      wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
 335  
 336  if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
 337      wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
 338  
 339  ?>


Generated: Mon Apr 5 14:26:09 2010 Cross-referenced by PHPXref 0.7