[ XREF Home ] [ Index ]

PHP Cross Reference of WordPress Trunk

Provided by Yoast

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


Generated: Wed Jun 1 08:30:02 2011 Cross-referenced by PHPXref 0.7
Provided by Yoast and awesome WordPress Hosting