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