[ Root ] [ Search ] [ Index ]

PHP Cross Reference of WordPress MU 2.9.2

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


Generated: Mon May 3 12:25:32 2010 Cross-referenced by PHPXref 0.7