update.php 24.1 KB
Newer Older
godog's avatar
godog committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php
/**
 * A simple set of functions to check our version 1.0 update service.
 *
 * @package WordPress
 * @since 2.3.0
 */

/**
 * Check WordPress version against the newest version.
 *
 * The WordPress version, PHP version, and Locale is sent. Checks against the
 * WordPress server at api.wordpress.org server. Will only check if WordPress
 * isn't installing.
 *
 * @since 2.3.0
lechuck's avatar
lechuck committed
17 18 19
 * @global string $wp_version Used to check against the newest WordPress version.
 * @global wpdb   $wpdb
 * @global string $wp_local_package
godog's avatar
godog committed
20
 *
lucha's avatar
lucha committed
21
 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
lechuck's avatar
lechuck committed
22
 * @param bool  $force_check Whether to bypass the transient cache and force a fresh update check. Defaults to false, true if $extra_stats is set.
godog's avatar
godog committed
23
 */
lucha's avatar
lucha committed
24
function wp_version_check( $extra_stats = array(), $force_check = false ) {
lechuck's avatar
lechuck committed
25
	if ( wp_installing() ) {
godog's avatar
godog committed
26
		return;
lechuck's avatar
lechuck committed
27
	}
godog's avatar
godog committed
28

lechuck's avatar
lechuck committed
29
	global $wpdb, $wp_local_package;
lechuck's avatar
lechuck committed
30 31
	// include an unmodified $wp_version
	include( ABSPATH . WPINC . '/version.php' );
godog's avatar
godog committed
32 33 34
	$php_version = phpversion();

	$current = get_site_transient( 'update_core' );
lucha's avatar
lucha committed
35 36
	$translations = wp_get_installed_translations( 'core' );

lucha's avatar
lucha committed
37 38 39 40
	// Invalidate the transient when $wp_version changes
	if ( is_object( $current ) && $wp_version != $current->version_checked )
		$current = false;

godog's avatar
godog committed
41 42 43 44 45 46
	if ( ! is_object($current) ) {
		$current = new stdClass;
		$current->updates = array();
		$current->version_checked = $wp_version;
	}

lucha's avatar
lucha committed
47 48 49
	if ( ! empty( $extra_stats ) )
		$force_check = true;

lechuck's avatar
lechuck committed
50 51 52
	// Wait 60 seconds between multiple version check requests
	$timeout = 60;
	$time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
lechuck's avatar
lechuck committed
53 54 55
	if ( ! $force_check && $time_not_changed ) {
		return;
	}
lechuck's avatar
lechuck committed
56

lucha's avatar
lucha committed
57
	/**
lucha's avatar
lucha committed
58
	 * Filters the locale requested for WordPress core translations.
lucha's avatar
lucha committed
59 60 61 62 63
	 *
	 * @since 2.8.0
	 *
	 * @param string $locale Current locale.
	 */
lechuck's avatar
lechuck committed
64
	$locale = apply_filters( 'core_version_check_locale', get_locale() );
godog's avatar
godog committed
65 66 67 68 69 70 71 72 73 74

	// Update last_checked for current to prevent multiple blocking requests if request hangs
	$current->last_checked = time();
	set_site_transient( 'update_core', $current );

	if ( method_exists( $wpdb, 'db_version' ) )
		$mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
	else
		$mysql_version = 'N/A';

lechuck's avatar
lechuck committed
75 76 77 78
	if ( is_multisite() ) {
		$user_count = get_user_count();
		$num_blogs = get_blog_count();
		$wp_install = network_site_url();
godog's avatar
godog committed
79
		$multisite_enabled = 1;
root's avatar
root committed
80
	} else {
lechuck's avatar
lechuck committed
81
		$user_count = count_users();
lechuck's avatar
lechuck committed
82
		$user_count = $user_count['total_users'];
root's avatar
root committed
83 84 85
		$multisite_enabled = 0;
		$num_blogs = 1;
		$wp_install = home_url( '/' );
godog's avatar
godog committed
86 87
	}

root's avatar
root committed
88
	$query = array(
lechuck's avatar
lechuck committed
89 90 91 92 93 94 95 96 97
		'version'            => $wp_version,
		'php'                => $php_version,
		'locale'             => $locale,
		'mysql'              => $mysql_version,
		'local_package'      => isset( $wp_local_package ) ? $wp_local_package : '',
		'blogs'              => $num_blogs,
		'users'              => $user_count,
		'multisite_enabled'  => $multisite_enabled,
		'initial_db_version' => get_site_option( 'initial_db_version' ),
lucha's avatar
lucha committed
98 99
	);

lucha's avatar
lucha committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	/**
	 * Filter the query arguments sent as part of the core version check.
	 *
	 * WARNING: Changing this data may result in your site not receiving security updates.
	 * Please exercise extreme caution.
	 *
	 * @since 4.9.0
	 *
	 * @param array $query {
	 *     Version check query arguments. 
	 *
	 *     @type string $version            WordPress version number.
	 *     @type string $php                PHP version number.
	 *     @type string $locale             The locale to retrieve updates for.
	 *     @type string $mysql              MySQL version number.
	 *     @type string $local_package      The value of the $wp_local_package global, when set.
	 *     @type int    $blogs              Number of sites on this WordPress installation.
	 *     @type int    $users              Number of users on this WordPress installation.
	 *     @type int    $multisite_enabled  Whether this WordPress installation uses Multisite.
	 *     @type int    $initial_db_version Database version of WordPress at time of installation.
	 * }
	 */
	$query = apply_filters( 'core_version_check_query_args', $query );

lucha's avatar
lucha committed
124
	$post_body = array(
lechuck's avatar
lechuck committed
125
		'translations' => wp_json_encode( $translations ),
root's avatar
root committed
126 127
	);

lucha's avatar
lucha committed
128
	if ( is_array( $extra_stats ) )
lucha's avatar
lucha committed
129 130 131 132 133
		$post_body = array_merge( $post_body, $extra_stats );

	$url = $http_url = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, null, '&' );
	if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
		$url = set_url_scheme( $url, 'https' );
godog's avatar
godog committed
134

lucha's avatar
lucha committed
135 136
	$doing_cron = wp_doing_cron();

godog's avatar
godog committed
137
	$options = array(
lucha's avatar
lucha committed
138
		'timeout' => $doing_cron ? 30 : 3,
godog's avatar
godog committed
139 140 141 142
		'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
		'headers' => array(
			'wp_install' => $wp_install,
			'wp_blog' => home_url( '/' )
lucha's avatar
lucha committed
143 144
		),
		'body' => $post_body,
godog's avatar
godog committed
145 146
	);

lucha's avatar
lucha committed
147 148
	$response = wp_remote_post( $url, $options );
	if ( $ssl && is_wp_error( $response ) ) {
lucha's avatar
lucha committed
149 150 151 152 153 154 155 156
		trigger_error(
			sprintf(
				/* translators: %s: support forums URL */
				__( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
				__( 'https://wordpress.org/support/' )
			) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
			headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
		);
lucha's avatar
lucha committed
157 158
		$response = wp_remote_post( $http_url, $options );
	}
godog's avatar
godog committed
159

lechuck's avatar
lechuck committed
160 161 162
	if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
		return;
	}
godog's avatar
godog committed
163

shammash's avatar
shammash committed
164
	$body = trim( wp_remote_retrieve_body( $response ) );
lucha's avatar
lucha committed
165
	$body = json_decode( $body, true );
lechuck's avatar
lechuck committed
166

lechuck's avatar
lechuck committed
167 168 169
	if ( ! is_array( $body ) || ! isset( $body['offers'] ) ) {
		return;
	}
lechuck's avatar
lechuck committed
170

shammash's avatar
shammash committed
171 172 173 174 175 176
	$offers = $body['offers'];

	foreach ( $offers as &$offer ) {
		foreach ( $offer as $offer_key => $value ) {
			if ( 'packages' == $offer_key )
				$offer['packages'] = (object) array_intersect_key( array_map( 'esc_url', $offer['packages'] ),
lucha's avatar
lucha committed
177
					array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' ) );
shammash's avatar
shammash committed
178 179 180 181 182 183
			elseif ( 'download' == $offer_key )
				$offer['download'] = esc_url( $value );
			else
				$offer[ $offer_key ] = esc_html( $value );
		}
		$offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',
lechuck's avatar
lechuck committed
184
			'packages', 'current', 'version', 'php_version', 'mysql_version', 'new_bundled', 'partial_version', 'notify_email', 'support_email', 'new_files' ), '' ) );
godog's avatar
godog committed
185 186 187
	}

	$updates = new stdClass();
shammash's avatar
shammash committed
188
	$updates->updates = $offers;
godog's avatar
godog committed
189 190
	$updates->last_checked = time();
	$updates->version_checked = $wp_version;
lucha's avatar
lucha committed
191 192 193 194

	if ( isset( $body['translations'] ) )
		$updates->translations = $body['translations'];

lechuck's avatar
lechuck committed
195 196 197 198 199 200 201 202 203 204
	set_site_transient( 'update_core', $updates );

	if ( ! empty( $body['ttl'] ) ) {
		$ttl = (int) $body['ttl'];
		if ( $ttl && ( time() + $ttl < wp_next_scheduled( 'wp_version_check' ) ) ) {
			// Queue an event to re-run the update check in $ttl seconds.
			wp_schedule_single_event( time() + $ttl, 'wp_version_check' );
		}
	}

lechuck's avatar
lechuck committed
205
	// Trigger background updates if running non-interactively, and we weren't called from the update handler.
lucha's avatar
lucha committed
206
	if ( $doing_cron && ! doing_action( 'wp_maybe_auto_update' ) ) {
lechuck's avatar
lechuck committed
207 208
		do_action( 'wp_maybe_auto_update' );
	}
godog's avatar
godog committed
209 210 211 212 213 214 215 216 217 218
}

/**
 * Check plugin versions against the latest versions hosted on WordPress.org.
 *
 * The WordPress version, PHP version, and Locale is sent along with a list of
 * all plugins installed. Checks against the WordPress server at
 * api.wordpress.org. Will only check if WordPress isn't installing.
 *
 * @since 2.3.0
lechuck's avatar
lechuck committed
219
 * @global string $wp_version Used to notify the WordPress version.
godog's avatar
godog committed
220
 *
lechuck's avatar
lechuck committed
221
 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
godog's avatar
godog committed
222
 */
lechuck's avatar
lechuck committed
223
function wp_update_plugins( $extra_stats = array() ) {
lechuck's avatar
lechuck committed
224
	if ( wp_installing() ) {
lechuck's avatar
lechuck committed
225 226
		return;
	}
godog's avatar
godog committed
227

lechuck's avatar
lechuck committed
228 229
	// include an unmodified $wp_version
	include( ABSPATH . WPINC . '/version.php' );
godog's avatar
godog committed
230 231 232 233 234 235

	// If running blog-side, bail unless we've not checked in the last 12 hours
	if ( !function_exists( 'get_plugins' ) )
		require_once( ABSPATH . 'wp-admin/includes/plugin.php' );

	$plugins = get_plugins();
lucha's avatar
lucha committed
236 237
	$translations = wp_get_installed_translations( 'plugins' );

godog's avatar
godog committed
238 239 240 241 242 243 244 245
	$active  = get_option( 'active_plugins', array() );
	$current = get_site_transient( 'update_plugins' );
	if ( ! is_object($current) )
		$current = new stdClass;

	$new_option = new stdClass;
	$new_option->last_checked = time();

lucha's avatar
lucha committed
246 247
	$doing_cron = wp_doing_cron();

lechuck's avatar
lechuck committed
248 249
	// Check for update on a different schedule, depending on the page.
	switch ( current_filter() ) {
lucha's avatar
lucha committed
250 251 252
		case 'upgrader_process_complete' :
			$timeout = 0;
			break;
lechuck's avatar
lechuck committed
253
		case 'load-update-core.php' :
lechuck's avatar
lechuck committed
254
			$timeout = MINUTE_IN_SECONDS;
lechuck's avatar
lechuck committed
255 256 257
			break;
		case 'load-plugins.php' :
		case 'load-update.php' :
lechuck's avatar
lechuck committed
258
			$timeout = HOUR_IN_SECONDS;
lechuck's avatar
lechuck committed
259 260
			break;
		default :
lucha's avatar
lucha committed
261
			if ( $doing_cron ) {
lucha's avatar
lucha committed
262
				$timeout = 2 * HOUR_IN_SECONDS;
lechuck's avatar
lechuck committed
263 264 265
			} else {
				$timeout = 12 * HOUR_IN_SECONDS;
			}
godog's avatar
godog committed
266 267
	}

lechuck's avatar
lechuck committed
268 269
	$time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );

lechuck's avatar
lechuck committed
270
	if ( $time_not_changed && ! $extra_stats ) {
lechuck's avatar
lechuck committed
271 272 273 274 275
		$plugin_changed = false;
		foreach ( $plugins as $file => $p ) {
			$new_option->checked[ $file ] = $p['Version'];

			if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
godog's avatar
godog committed
276
				$plugin_changed = true;
lechuck's avatar
lechuck committed
277 278 279 280 281 282 283 284
		}

		if ( isset ( $current->response ) && is_array( $current->response ) ) {
			foreach ( $current->response as $plugin_file => $update_details ) {
				if ( ! isset($plugins[ $plugin_file ]) ) {
					$plugin_changed = true;
					break;
				}
godog's avatar
godog committed
285 286 287
			}
		}

lechuck's avatar
lechuck committed
288
		// Bail if we've checked recently and if nothing has changed
lechuck's avatar
lechuck committed
289 290 291
		if ( ! $plugin_changed ) {
			return;
		}
lechuck's avatar
lechuck committed
292
	}
godog's avatar
godog committed
293 294 295 296 297

	// Update last_checked for current to prevent multiple blocking requests if request hangs
	$current->last_checked = time();
	set_site_transient( 'update_plugins', $current );

lucha's avatar
lucha committed
298 299
	$to_send = compact( 'plugins', 'active' );

lechuck's avatar
lechuck committed
300 301
	$locales = array_values( get_available_languages() );

lucha's avatar
lucha committed
302
	/**
lucha's avatar
lucha committed
303
	 * Filters the locales requested for plugin translations.
lucha's avatar
lucha committed
304 305
	 *
	 * @since 3.7.0
lechuck's avatar
lechuck committed
306
	 * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
lucha's avatar
lucha committed
307
	 *
lechuck's avatar
lechuck committed
308
	 * @param array $locales Plugin locales. Default is all available locales of the site.
lucha's avatar
lucha committed
309
	 */
lechuck's avatar
lechuck committed
310 311
	$locales = apply_filters( 'plugins_update_check_locales', $locales );
	$locales = array_unique( $locales );
godog's avatar
godog committed
312

lucha's avatar
lucha committed
313
	if ( $doing_cron ) {
lucha's avatar
lucha committed
314 315 316 317 318 319
		$timeout = 30;
	} else {
		// Three seconds, plus one extra second for every 10 plugins
		$timeout = 3 + (int) ( count( $plugins ) / 10 );
	}

godog's avatar
godog committed
320
	$options = array(
lucha's avatar
lucha committed
321
		'timeout' => $timeout,
lucha's avatar
lucha committed
322
		'body' => array(
lechuck's avatar
lechuck committed
323 324 325 326
			'plugins'      => wp_json_encode( $to_send ),
			'translations' => wp_json_encode( $translations ),
			'locale'       => wp_json_encode( $locales ),
			'all'          => wp_json_encode( true ),
lucha's avatar
lucha committed
327
		),
lucha's avatar
lucha committed
328
		'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' )
godog's avatar
godog committed
329 330
	);

lechuck's avatar
lechuck committed
331
	if ( $extra_stats ) {
lechuck's avatar
lechuck committed
332
		$options['body']['update_stats'] = wp_json_encode( $extra_stats );
lechuck's avatar
lechuck committed
333 334
	}

lucha's avatar
lucha committed
335 336 337 338 339 340
	$url = $http_url = 'http://api.wordpress.org/plugins/update-check/1.1/';
	if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
		$url = set_url_scheme( $url, 'https' );

	$raw_response = wp_remote_post( $url, $options );
	if ( $ssl && is_wp_error( $raw_response ) ) {
lucha's avatar
lucha committed
341 342 343 344 345 346 347 348
		trigger_error(
			sprintf(
				/* translators: %s: support forums URL */
				__( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
				__( 'https://wordpress.org/support/' )
			) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
			headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
		);
lucha's avatar
lucha committed
349 350
		$raw_response = wp_remote_post( $http_url, $options );
	}
godog's avatar
godog committed
351

lechuck's avatar
lechuck committed
352 353 354
	if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
		return;
	}
godog's avatar
godog committed
355

lucha's avatar
lucha committed
356 357 358
	$response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
	foreach ( $response['plugins'] as &$plugin ) {
		$plugin = (object) $plugin;
lechuck's avatar
lechuck committed
359 360 361 362 363 364
		if ( isset( $plugin->compatibility ) ) {
			$plugin->compatibility = (object) $plugin->compatibility;
			foreach ( $plugin->compatibility as &$data ) {
				$data = (object) $data;
			}
		}
lucha's avatar
lucha committed
365
	}
lechuck's avatar
lechuck committed
366
	unset( $plugin, $data );
lucha's avatar
lucha committed
367 368 369 370
	foreach ( $response['no_update'] as &$plugin ) {
		$plugin = (object) $plugin;
	}
	unset( $plugin );
godog's avatar
godog committed
371

lucha's avatar
lucha committed
372 373 374
	if ( is_array( $response ) ) {
		$new_option->response = $response['plugins'];
		$new_option->translations = $response['translations'];
lucha's avatar
lucha committed
375 376
		// TODO: Perhaps better to store no_update in a separate transient with an expiry?
		$new_option->no_update = $response['no_update'];
lucha's avatar
lucha committed
377
	} else {
godog's avatar
godog committed
378
		$new_option->response = array();
lucha's avatar
lucha committed
379
		$new_option->translations = array();
lucha's avatar
lucha committed
380
		$new_option->no_update = array();
lucha's avatar
lucha committed
381
	}
godog's avatar
godog committed
382 383 384 385 386 387 388 389 390 391 392 393 394

	set_site_transient( 'update_plugins', $new_option );
}

/**
 * Check theme versions against the latest versions hosted on WordPress.org.
 *
 * A list of all themes installed in sent to WP. Checks against the
 * WordPress server at api.wordpress.org. Will only check if WordPress isn't
 * installing.
 *
 * @since 2.7.0
 *
lechuck's avatar
lechuck committed
395
 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
godog's avatar
godog committed
396
 */
lechuck's avatar
lechuck committed
397
function wp_update_themes( $extra_stats = array() ) {
lechuck's avatar
lechuck committed
398
	if ( wp_installing() ) {
lechuck's avatar
lechuck committed
399 400
		return;
	}
lechuck's avatar
lechuck committed
401

lechuck's avatar
lechuck committed
402 403
	// include an unmodified $wp_version
	include( ABSPATH . WPINC . '/version.php' );
godog's avatar
godog committed
404

lechuck's avatar
lechuck committed
405
	$installed_themes = wp_get_themes();
lucha's avatar
lucha committed
406 407
	$translations = wp_get_installed_translations( 'themes' );

godog's avatar
godog committed
408 409 410 411
	$last_update = get_site_transient( 'update_themes' );
	if ( ! is_object($last_update) )
		$last_update = new stdClass;

lucha's avatar
lucha committed
412
	$themes = $checked = $request = array();
godog's avatar
godog committed
413 414

	// Put slug of current theme into request.
lucha's avatar
lucha committed
415
	$request['active'] = get_option( 'stylesheet' );
godog's avatar
godog committed
416

lechuck's avatar
lechuck committed
417 418 419 420 421 422 423 424 425 426 427 428 429
	foreach ( $installed_themes as $theme ) {
		$checked[ $theme->get_stylesheet() ] = $theme->get('Version');

		$themes[ $theme->get_stylesheet() ] = array(
			'Name'       => $theme->get('Name'),
			'Title'      => $theme->get('Name'),
			'Version'    => $theme->get('Version'),
			'Author'     => $theme->get('Author'),
			'Author URI' => $theme->get('AuthorURI'),
			'Template'   => $theme->get_template(),
			'Stylesheet' => $theme->get_stylesheet(),
		);
	}
godog's avatar
godog committed
430

lucha's avatar
lucha committed
431 432
	$doing_cron = wp_doing_cron();

lechuck's avatar
lechuck committed
433 434
	// Check for update on a different schedule, depending on the page.
	switch ( current_filter() ) {
lucha's avatar
lucha committed
435 436 437
		case 'upgrader_process_complete' :
			$timeout = 0;
			break;
lechuck's avatar
lechuck committed
438
		case 'load-update-core.php' :
lechuck's avatar
lechuck committed
439
			$timeout = MINUTE_IN_SECONDS;
lechuck's avatar
lechuck committed
440 441 442
			break;
		case 'load-themes.php' :
		case 'load-update.php' :
lechuck's avatar
lechuck committed
443
			$timeout = HOUR_IN_SECONDS;
lechuck's avatar
lechuck committed
444 445
			break;
		default :
lucha's avatar
lucha committed
446 447 448 449 450
			if ( $doing_cron ) {
				$timeout = 2 * HOUR_IN_SECONDS;
			} else {
				$timeout = 12 * HOUR_IN_SECONDS;
			}
godog's avatar
godog committed
451 452
	}

lechuck's avatar
lechuck committed
453
	$time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
godog's avatar
godog committed
454

lechuck's avatar
lechuck committed
455
	if ( $time_not_changed && ! $extra_stats ) {
lechuck's avatar
lechuck committed
456 457 458
		$theme_changed = false;
		foreach ( $checked as $slug => $v ) {
			if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
godog's avatar
godog committed
459
				$theme_changed = true;
lechuck's avatar
lechuck committed
460 461 462 463 464 465 466 467
		}

		if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
			foreach ( $last_update->response as $slug => $update_details ) {
				if ( ! isset($checked[ $slug ]) ) {
					$theme_changed = true;
					break;
				}
godog's avatar
godog committed
468 469 470
			}
		}

lechuck's avatar
lechuck committed
471
		// Bail if we've checked recently and if nothing has changed
lechuck's avatar
lechuck committed
472 473 474
		if ( ! $theme_changed ) {
			return;
		}
lechuck's avatar
lechuck committed
475
	}
godog's avatar
godog committed
476 477 478 479 480

	// Update last_checked for current to prevent multiple blocking requests if request hangs
	$last_update->last_checked = time();
	set_site_transient( 'update_themes', $last_update );

lucha's avatar
lucha committed
481 482
	$request['themes'] = $themes;

lechuck's avatar
lechuck committed
483 484
	$locales = array_values( get_available_languages() );

lucha's avatar
lucha committed
485
	/**
lucha's avatar
lucha committed
486
	 * Filters the locales requested for theme translations.
lucha's avatar
lucha committed
487 488
	 *
	 * @since 3.7.0
lechuck's avatar
lechuck committed
489
	 * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
lucha's avatar
lucha committed
490
	 *
lechuck's avatar
lechuck committed
491
	 * @param array $locales Theme locales. Default is all available locales of the site.
lucha's avatar
lucha committed
492
	 */
lechuck's avatar
lechuck committed
493 494
	$locales = apply_filters( 'themes_update_check_locales', $locales );
	$locales = array_unique( $locales );
lucha's avatar
lucha committed
495

lucha's avatar
lucha committed
496
	if ( $doing_cron ) {
lucha's avatar
lucha committed
497 498 499 500 501 502
		$timeout = 30;
	} else {
		// Three seconds, plus one extra second for every 10 themes
		$timeout = 3 + (int) ( count( $themes ) / 10 );
	}

godog's avatar
godog committed
503
	$options = array(
lucha's avatar
lucha committed
504
		'timeout' => $timeout,
lucha's avatar
lucha committed
505
		'body' => array(
lechuck's avatar
lechuck committed
506 507 508
			'themes'       => wp_json_encode( $request ),
			'translations' => wp_json_encode( $translations ),
			'locale'       => wp_json_encode( $locales ),
lucha's avatar
lucha committed
509
		),
lucha's avatar
lucha committed
510
		'user-agent'	=> 'WordPress/' . $wp_version . '; ' . home_url( '/' )
godog's avatar
godog committed
511 512
	);

lechuck's avatar
lechuck committed
513
	if ( $extra_stats ) {
lechuck's avatar
lechuck committed
514
		$options['body']['update_stats'] = wp_json_encode( $extra_stats );
lechuck's avatar
lechuck committed
515 516
	}

lucha's avatar
lucha committed
517 518 519 520 521 522
	$url = $http_url = 'http://api.wordpress.org/themes/update-check/1.1/';
	if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
		$url = set_url_scheme( $url, 'https' );

	$raw_response = wp_remote_post( $url, $options );
	if ( $ssl && is_wp_error( $raw_response ) ) {
lucha's avatar
lucha committed
523 524 525 526 527 528 529 530
		trigger_error(
			sprintf(
				/* translators: %s: support forums URL */
				__( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
				__( 'https://wordpress.org/support/' )
			) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
			headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
		);
lucha's avatar
lucha committed
531 532
		$raw_response = wp_remote_post( $http_url, $options );
	}
godog's avatar
godog committed
533

lechuck's avatar
lechuck committed
534 535 536
	if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
		return;
	}
godog's avatar
godog committed
537 538

	$new_update = new stdClass;
lechuck's avatar
lechuck committed
539
	$new_update->last_checked = time();
shammash's avatar
shammash committed
540 541
	$new_update->checked = $checked;

lucha's avatar
lucha committed
542 543 544 545 546 547
	$response = json_decode( wp_remote_retrieve_body( $raw_response ), true );

	if ( is_array( $response ) ) {
		$new_update->response     = $response['themes'];
		$new_update->translations = $response['translations'];
	}
godog's avatar
godog committed
548 549 550 551

	set_site_transient( 'update_themes', $new_update );
}

lucha's avatar
lucha committed
552 553 554 555 556 557
/**
 * Performs WordPress automatic background updates.
 *
 * @since 3.7.0
 */
function wp_maybe_auto_update() {
lucha's avatar
lucha committed
558 559
	include_once( ABSPATH . '/wp-admin/includes/admin.php' );
	include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' );
lucha's avatar
lucha committed
560 561 562 563 564 565 566 567 568

	$upgrader = new WP_Automatic_Updater;
	$upgrader->run();
}

/**
 * Retrieves a list of all language updates available.
 *
 * @since 3.7.0
lechuck's avatar
lechuck committed
569 570
 *
 * @return array
lucha's avatar
lucha committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
 */
function wp_get_translation_updates() {
	$updates = array();
	$transients = array( 'update_core' => 'core', 'update_plugins' => 'plugin', 'update_themes' => 'theme' );
	foreach ( $transients as $transient => $type ) {
		$transient = get_site_transient( $transient );
		if ( empty( $transient->translations ) )
			continue;

		foreach ( $transient->translations as $translation ) {
			$updates[] = (object) $translation;
		}
	}
	return $updates;
}

lucha's avatar
lucha committed
587
/**
root's avatar
root committed
588 589 590 591 592 593 594
 * Collect counts and UI strings for available updates
 *
 * @since 3.3.0
 *
 * @return array
 */
function wp_get_update_data() {
lucha's avatar
lucha committed
595
	$counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0, 'translations' => 0 );
root's avatar
root committed
596

lucha's avatar
lucha committed
597
	if ( $plugins = current_user_can( 'update_plugins' ) ) {
root's avatar
root committed
598 599 600 601 602
		$update_plugins = get_site_transient( 'update_plugins' );
		if ( ! empty( $update_plugins->response ) )
			$counts['plugins'] = count( $update_plugins->response );
	}

lucha's avatar
lucha committed
603
	if ( $themes = current_user_can( 'update_themes' ) ) {
root's avatar
root committed
604 605 606 607 608
		$update_themes = get_site_transient( 'update_themes' );
		if ( ! empty( $update_themes->response ) )
			$counts['themes'] = count( $update_themes->response );
	}

lucha's avatar
lucha committed
609
	if ( ( $core = current_user_can( 'update_core' ) ) && function_exists( 'get_core_updates' ) ) {
root's avatar
root committed
610 611 612 613 614
		$update_wordpress = get_core_updates( array('dismissed' => false) );
		if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') )
			$counts['wordpress'] = 1;
	}

lucha's avatar
lucha committed
615 616 617 618
	if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() )
		$counts['translations'] = 1;

	$counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
lechuck's avatar
lechuck committed
619
	$titles = array();
lucha's avatar
lucha committed
620 621
	if ( $counts['wordpress'] ) {
		/* translators: 1: Number of updates available to WordPress */
lechuck's avatar
lechuck committed
622
		$titles['wordpress'] = sprintf( __( '%d WordPress Update'), $counts['wordpress'] );
lucha's avatar
lucha committed
623 624 625
	}
	if ( $counts['plugins'] ) {
		/* translators: 1: Number of updates available to plugins */
lechuck's avatar
lechuck committed
626
		$titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
lucha's avatar
lucha committed
627 628 629
	}
	if ( $counts['themes'] ) {
		/* translators: 1: Number of updates available to themes */
lechuck's avatar
lechuck committed
630
		$titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
lucha's avatar
lucha committed
631 632
	}
	if ( $counts['translations'] ) {
lucha's avatar
lucha committed
633
		$titles['translations'] = __( 'Translation Updates' );
lucha's avatar
lucha committed
634
	}
root's avatar
root committed
635

lechuck's avatar
lechuck committed
636
	$update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
root's avatar
root committed
637

lucha's avatar
lucha committed
638 639
	$update_data = array( 'counts' => $counts, 'title' => $update_title );
	/**
lucha's avatar
lucha committed
640
	 * Filters the returned array of update data for plugins, themes, and WordPress core.
lucha's avatar
lucha committed
641 642 643 644 645 646 647 648 649 650 651 652
	 *
	 * @since 3.5.0
	 *
	 * @param array $update_data {
	 *     Fetched update data.
	 *
	 *     @type array   $counts       An array of counts for available plugin, theme, and WordPress updates.
	 *     @type string  $update_title Titles of available updates.
	 * }
	 * @param array $titles An array of update counts and UI strings for available updates.
	 */
	return apply_filters( 'wp_get_update_data', $update_data, $titles );
root's avatar
root committed
653 654
}

lechuck's avatar
lechuck committed
655
/**
lechuck's avatar
lechuck committed
656 657 658 659
 * Determines whether core should be updated.
 *
 * @since 2.8.0
 *
lechuck's avatar
lechuck committed
660 661
 * @global string $wp_version
 */
godog's avatar
godog committed
662
function _maybe_update_core() {
lechuck's avatar
lechuck committed
663 664
	// include an unmodified $wp_version
	include( ABSPATH . WPINC . '/version.php' );
godog's avatar
godog committed
665 666 667

	$current = get_site_transient( 'update_core' );

lechuck's avatar
lechuck committed
668
	if ( isset( $current->last_checked, $current->version_checked ) &&
lechuck's avatar
lechuck committed
669
		12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
lechuck's avatar
lechuck committed
670
		$current->version_checked == $wp_version ) {
godog's avatar
godog committed
671
		return;
lechuck's avatar
lechuck committed
672
	}
godog's avatar
godog committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686
	wp_version_check();
}
/**
 * Check the last time plugins were run before checking plugin versions.
 *
 * This might have been backported to WordPress 2.6.1 for performance reasons.
 * This is used for the wp-admin to check only so often instead of every page
 * load.
 *
 * @since 2.7.0
 * @access private
 */
function _maybe_update_plugins() {
	$current = get_site_transient( 'update_plugins' );
lechuck's avatar
lechuck committed
687
	if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
godog's avatar
godog committed
688 689 690 691 692 693 694 695 696 697 698 699 700
		return;
	wp_update_plugins();
}

/**
 * Check themes versions only after a duration of time.
 *
 * This is for performance reasons to make sure that on the theme version
 * checker is not run on every page load.
 *
 * @since 2.7.0
 * @access private
 */
lechuck's avatar
lechuck committed
701
function _maybe_update_themes() {
godog's avatar
godog committed
702
	$current = get_site_transient( 'update_themes' );
lechuck's avatar
lechuck committed
703
	if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
godog's avatar
godog committed
704 705 706 707
		return;
	wp_update_themes();
}

root's avatar
root committed
708 709 710 711 712 713
/**
 * Schedule core, theme, and plugin update checks.
 *
 * @since 3.1.0
 */
function wp_schedule_update_checks() {
lechuck's avatar
lechuck committed
714
	if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() )
root's avatar
root committed
715 716
		wp_schedule_event(time(), 'twicedaily', 'wp_version_check');

lechuck's avatar
lechuck committed
717
	if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() )
root's avatar
root committed
718 719
		wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');

lechuck's avatar
lechuck committed
720
	if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() )
root's avatar
root committed
721 722 723
		wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
}

lechuck's avatar
lechuck committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
/**
 * Clear existing update caches for plugins, themes, and core.
 *
 * @since 4.1.0
 */
function wp_clean_update_cache() {
	if ( function_exists( 'wp_clean_plugins_cache' ) ) {
		wp_clean_plugins_cache();
	} else {
		delete_site_transient( 'update_plugins' );
	}
	wp_clean_themes_cache();
	delete_site_transient( 'update_core' );
}

lucha's avatar
lucha committed
739
if ( ( ! is_main_site() && ! is_network_admin() ) || wp_doing_ajax() ) {
root's avatar
root committed
740
	return;
lechuck's avatar
lechuck committed
741
}
root's avatar
root committed
742

godog's avatar
godog committed
743 744 745 746 747 748 749 750 751 752 753 754 755 756
add_action( 'admin_init', '_maybe_update_core' );
add_action( 'wp_version_check', 'wp_version_check' );

add_action( 'load-plugins.php', 'wp_update_plugins' );
add_action( 'load-update.php', 'wp_update_plugins' );
add_action( 'load-update-core.php', 'wp_update_plugins' );
add_action( 'admin_init', '_maybe_update_plugins' );
add_action( 'wp_update_plugins', 'wp_update_plugins' );

add_action( 'load-themes.php', 'wp_update_themes' );
add_action( 'load-update.php', 'wp_update_themes' );
add_action( 'load-update-core.php', 'wp_update_themes' );
add_action( 'admin_init', '_maybe_update_themes' );
add_action( 'wp_update_themes', 'wp_update_themes' );
lucha's avatar
lucha committed
757

lechuck's avatar
lechuck committed
758 759
add_action( 'update_option_WPLANG', 'wp_clean_update_cache' , 10, 0 );

lucha's avatar
lucha committed
760
add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
godog's avatar
godog committed
761

ale's avatar
ale committed
762
add_action( 'init', 'wp_schedule_update_checks' );