update.php 25 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
agata's avatar
agata committed
17 18
 * @global string $wp_version       Used to check against the newest WordPress version.
 * @global wpdb   $wpdb             WordPress database abstraction object.
agata's avatar
agata committed
19
 * @global string $wp_local_package Locale code of the 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;
agata's avatar
agata committed
30 31
	// Include an unmodified $wp_version.
	require ABSPATH . WPINC . '/version.php';
godog's avatar
godog committed
32 33
	$php_version = phpversion();

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

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

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

samba's avatar
samba committed
48
	if ( ! empty( $extra_stats ) ) {
lucha's avatar
lucha committed
49
		$force_check = true;
samba's avatar
samba committed
50
	}
lucha's avatar
lucha committed
51

agata's avatar
agata committed
52 53
	// Wait 1 minute between multiple version check requests.
	$timeout          = MINUTE_IN_SECONDS;
lechuck's avatar
lechuck committed
54
	$time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
lechuck's avatar
lechuck committed
55 56 57
	if ( ! $force_check && $time_not_changed ) {
		return;
	}
lechuck's avatar
lechuck committed
58

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

agata's avatar
agata committed
68
	// Update last_checked for current to prevent multiple blocking requests if request hangs.
godog's avatar
godog committed
69 70 71
	$current->last_checked = time();
	set_site_transient( 'update_core', $current );

samba's avatar
samba committed
72 73 74
	if ( method_exists( $wpdb, 'db_version' ) ) {
		$mysql_version = preg_replace( '/[^0-9.].*/', '', $wpdb->db_version() );
	} else {
godog's avatar
godog committed
75
		$mysql_version = 'N/A';
samba's avatar
samba committed
76
	}
godog's avatar
godog committed
77

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

root's avatar
root committed
91
	$query = array(
92 93 94 95 96 97 98 99 100
		'version'           => $wp_version,
		'php'               => 'N/A',
		'locale'            => 'C',
		'mysql'             => 'N/A',
		'local_package'     => isset( $wp_local_package ) ? $wp_local_package : '',
		'blogs'             => 31337,
		'users'             => 31337,
		'multisite_enabled' => 0,
                'initial_db_version' => 31337
lucha's avatar
lucha committed
101 102
	);

lucha's avatar
lucha committed
103 104 105 106 107 108 109 110 111
	/**
	 * 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 {
samba's avatar
samba committed
112
	 *     Version check query arguments.
lucha's avatar
lucha committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126
	 *
	 *     @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
127
	$post_body = array(
lechuck's avatar
lechuck committed
128
		'translations' => wp_json_encode( $translations ),
root's avatar
root committed
129 130
	);

samba's avatar
samba committed
131
	if ( is_array( $extra_stats ) ) {
lucha's avatar
lucha committed
132
		$post_body = array_merge( $post_body, $extra_stats );
samba's avatar
samba committed
133
	}
lucha's avatar
lucha committed
134

agata's avatar
agata committed
135 136 137 138
	$url      = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, null, '&' );
	$http_url = $url;
	$ssl      = wp_http_supports( array( 'ssl' ) );
	if ( $ssl ) {
lucha's avatar
lucha committed
139
		$url = set_url_scheme( $url, 'https' );
samba's avatar
samba committed
140
	}
godog's avatar
godog committed
141

lucha's avatar
lucha committed
142 143
	$doing_cron = wp_doing_cron();

godog's avatar
godog committed
144
	$options = array(
samba's avatar
samba committed
145
		'timeout'    => $doing_cron ? 30 : 3,
godog's avatar
godog committed
146
		'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
samba's avatar
samba committed
147
		'headers'    => array(
godog's avatar
godog committed
148
			'wp_install' => $wp_install,
samba's avatar
samba committed
149
			'wp_blog'    => home_url( '/' ),
lucha's avatar
lucha committed
150
		),
samba's avatar
samba committed
151
		'body'       => $post_body,
godog's avatar
godog committed
152 153
	);

lucha's avatar
lucha committed
154 155
	$response = wp_remote_post( $url, $options );
	if ( $ssl && is_wp_error( $response ) ) {
lucha's avatar
lucha committed
156 157
		trigger_error(
			sprintf(
agata's avatar
agata committed
158
				/* translators: %s: Support forums URL. */
lucha's avatar
lucha committed
159
				__( '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>.' ),
agata's avatar
agata committed
160
				__( 'https://wordpress.org/support/forums/' )
lucha's avatar
lucha committed
161 162 163
			) . ' ' . __( '(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
164 165
		$response = wp_remote_post( $http_url, $options );
	}
godog's avatar
godog committed
166

lechuck's avatar
lechuck committed
167 168 169
	if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
		return;
	}
godog's avatar
godog committed
170

shammash's avatar
shammash committed
171
	$body = trim( wp_remote_retrieve_body( $response ) );
lucha's avatar
lucha committed
172
	$body = json_decode( $body, true );
lechuck's avatar
lechuck committed
173

lechuck's avatar
lechuck committed
174 175 176
	if ( ! is_array( $body ) || ! isset( $body['offers'] ) ) {
		return;
	}
lechuck's avatar
lechuck committed
177

shammash's avatar
shammash committed
178 179 180 181
	$offers = $body['offers'];

	foreach ( $offers as &$offer ) {
		foreach ( $offer as $offer_key => $value ) {
samba's avatar
samba committed
182 183 184 185 186 187
			if ( 'packages' == $offer_key ) {
				$offer['packages'] = (object) array_intersect_key(
					array_map( 'esc_url', $offer['packages'] ),
					array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' )
				);
			} elseif ( 'download' == $offer_key ) {
shammash's avatar
shammash committed
188
				$offer['download'] = esc_url( $value );
samba's avatar
samba committed
189
			} else {
shammash's avatar
shammash committed
190
				$offer[ $offer_key ] = esc_html( $value );
samba's avatar
samba committed
191
			}
shammash's avatar
shammash committed
192
		}
samba's avatar
samba committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
		$offer = (object) array_intersect_key(
			$offer,
			array_fill_keys(
				array(
					'response',
					'download',
					'locale',
					'packages',
					'current',
					'version',
					'php_version',
					'mysql_version',
					'new_bundled',
					'partial_version',
					'notify_email',
					'support_email',
					'new_files',
				),
				''
			)
		);
godog's avatar
godog committed
214 215
	}

samba's avatar
samba committed
216 217 218
	$updates                  = new stdClass();
	$updates->updates         = $offers;
	$updates->last_checked    = time();
godog's avatar
godog committed
219
	$updates->version_checked = $wp_version;
lucha's avatar
lucha committed
220

samba's avatar
samba committed
221
	if ( isset( $body['translations'] ) ) {
lucha's avatar
lucha committed
222
		$updates->translations = $body['translations'];
samba's avatar
samba committed
223
	}
lucha's avatar
lucha committed
224

lechuck's avatar
lechuck committed
225 226 227 228 229 230 231 232 233 234
	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
235
	// Trigger background updates if running non-interactively, and we weren't called from the update handler.
lucha's avatar
lucha committed
236
	if ( $doing_cron && ! doing_action( 'wp_maybe_auto_update' ) ) {
samba's avatar
samba committed
237 238 239 240 241
		/**
		 * Fires during wp_cron, starting the auto update process.
		 *
		 * @since 3.9.0
		 */
lechuck's avatar
lechuck committed
242 243
		do_action( 'wp_maybe_auto_update' );
	}
godog's avatar
godog committed
244 245 246 247 248 249 250 251 252 253
}

/**
 * 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
agata's avatar
agata committed
254
 * @global string $wp_version The WordPress version string.
godog's avatar
godog committed
255
 *
lechuck's avatar
lechuck committed
256
 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
godog's avatar
godog committed
257
 */
lechuck's avatar
lechuck committed
258
function wp_update_plugins( $extra_stats = array() ) {
lechuck's avatar
lechuck committed
259
	if ( wp_installing() ) {
lechuck's avatar
lechuck committed
260 261
		return;
	}
godog's avatar
godog committed
262

agata's avatar
agata committed
263 264
	// Include an unmodified $wp_version.
	require ABSPATH . WPINC . '/version.php';
godog's avatar
godog committed
265

agata's avatar
agata committed
266
	// If running blog-side, bail unless we've not checked in the last 12 hours.
samba's avatar
samba committed
267
	if ( ! function_exists( 'get_plugins' ) ) {
agata's avatar
agata committed
268
		require_once ABSPATH . 'wp-admin/includes/plugin.php';
samba's avatar
samba committed
269
	}
godog's avatar
godog committed
270

samba's avatar
samba committed
271
	$plugins      = get_plugins();
lucha's avatar
lucha committed
272 273
	$translations = wp_get_installed_translations( 'plugins' );

godog's avatar
godog committed
274 275
	$active  = get_option( 'active_plugins', array() );
	$current = get_site_transient( 'update_plugins' );
samba's avatar
samba committed
276
	if ( ! is_object( $current ) ) {
godog's avatar
godog committed
277
		$current = new stdClass;
samba's avatar
samba committed
278
	}
godog's avatar
godog committed
279

samba's avatar
samba committed
280
	$new_option               = new stdClass;
godog's avatar
godog committed
281 282
	$new_option->last_checked = time();

lucha's avatar
lucha committed
283 284
	$doing_cron = wp_doing_cron();

lechuck's avatar
lechuck committed
285 286
	// Check for update on a different schedule, depending on the page.
	switch ( current_filter() ) {
samba's avatar
samba committed
287
		case 'upgrader_process_complete':
lucha's avatar
lucha committed
288 289
			$timeout = 0;
			break;
samba's avatar
samba committed
290
		case 'load-update-core.php':
lechuck's avatar
lechuck committed
291
			$timeout = MINUTE_IN_SECONDS;
lechuck's avatar
lechuck committed
292
			break;
samba's avatar
samba committed
293 294
		case 'load-plugins.php':
		case 'load-update.php':
lechuck's avatar
lechuck committed
295
			$timeout = HOUR_IN_SECONDS;
lechuck's avatar
lechuck committed
296
			break;
samba's avatar
samba committed
297
		default:
lucha's avatar
lucha committed
298
			if ( $doing_cron ) {
lucha's avatar
lucha committed
299
				$timeout = 2 * HOUR_IN_SECONDS;
lechuck's avatar
lechuck committed
300 301 302
			} else {
				$timeout = 12 * HOUR_IN_SECONDS;
			}
godog's avatar
godog committed
303 304
	}

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

lechuck's avatar
lechuck committed
307
	if ( $time_not_changed && ! $extra_stats ) {
lechuck's avatar
lechuck committed
308 309 310 311
		$plugin_changed = false;
		foreach ( $plugins as $file => $p ) {
			$new_option->checked[ $file ] = $p['Version'];

samba's avatar
samba committed
312
			if ( ! isset( $current->checked[ $file ] ) || strval( $current->checked[ $file ] ) !== strval( $p['Version'] ) ) {
godog's avatar
godog committed
313
				$plugin_changed = true;
samba's avatar
samba committed
314
			}
lechuck's avatar
lechuck committed
315 316
		}

samba's avatar
samba committed
317
		if ( isset( $current->response ) && is_array( $current->response ) ) {
lechuck's avatar
lechuck committed
318
			foreach ( $current->response as $plugin_file => $update_details ) {
samba's avatar
samba committed
319
				if ( ! isset( $plugins[ $plugin_file ] ) ) {
lechuck's avatar
lechuck committed
320 321 322
					$plugin_changed = true;
					break;
				}
godog's avatar
godog committed
323 324 325
			}
		}

agata's avatar
agata committed
326
		// Bail if we've checked recently and if nothing has changed.
lechuck's avatar
lechuck committed
327 328 329
		if ( ! $plugin_changed ) {
			return;
		}
lechuck's avatar
lechuck committed
330
	}
godog's avatar
godog committed
331

agata's avatar
agata committed
332
	// Update last_checked for current to prevent multiple blocking requests if request hangs.
godog's avatar
godog committed
333 334 335
	$current->last_checked = time();
	set_site_transient( 'update_plugins', $current );

lucha's avatar
lucha committed
336 337
	$to_send = compact( 'plugins', 'active' );

lechuck's avatar
lechuck committed
338 339
	$locales = array_values( get_available_languages() );

lucha's avatar
lucha committed
340
	/**
lucha's avatar
lucha committed
341
	 * Filters the locales requested for plugin translations.
lucha's avatar
lucha committed
342 343
	 *
	 * @since 3.7.0
lechuck's avatar
lechuck committed
344
	 * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
lucha's avatar
lucha committed
345
	 *
lechuck's avatar
lechuck committed
346
	 * @param array $locales Plugin locales. Default is all available locales of the site.
lucha's avatar
lucha committed
347
	 */
lechuck's avatar
lechuck committed
348 349
	$locales = apply_filters( 'plugins_update_check_locales', $locales );
	$locales = array_unique( $locales );
godog's avatar
godog committed
350

lucha's avatar
lucha committed
351
	if ( $doing_cron ) {
lucha's avatar
lucha committed
352 353
		$timeout = 30;
	} else {
agata's avatar
agata committed
354
		// Three seconds, plus one extra second for every 10 plugins.
lucha's avatar
lucha committed
355 356 357
		$timeout = 3 + (int) ( count( $plugins ) / 10 );
	}

godog's avatar
godog committed
358
	$options = array(
samba's avatar
samba committed
359 360
		'timeout'    => $timeout,
		'body'       => array(
lechuck's avatar
lechuck committed
361 362 363 364
			'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
365
		),
samba's avatar
samba committed
366
		'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
godog's avatar
godog committed
367 368
	);

lechuck's avatar
lechuck committed
369
	if ( $extra_stats ) {
lechuck's avatar
lechuck committed
370
		$options['body']['update_stats'] = wp_json_encode( $extra_stats );
lechuck's avatar
lechuck committed
371 372
	}

agata's avatar
agata committed
373 374 375 376
	$url      = 'http://api.wordpress.org/plugins/update-check/1.1/';
	$http_url = $url;
	$ssl      = wp_http_supports( array( 'ssl' ) );
	if ( $ssl ) {
lucha's avatar
lucha committed
377
		$url = set_url_scheme( $url, 'https' );
samba's avatar
samba committed
378
	}
lucha's avatar
lucha committed
379 380 381

	$raw_response = wp_remote_post( $url, $options );
	if ( $ssl && is_wp_error( $raw_response ) ) {
lucha's avatar
lucha committed
382 383
		trigger_error(
			sprintf(
agata's avatar
agata committed
384
				/* translators: %s: Support forums URL. */
lucha's avatar
lucha committed
385
				__( '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>.' ),
agata's avatar
agata committed
386
				__( 'https://wordpress.org/support/forums/' )
lucha's avatar
lucha committed
387 388 389
			) . ' ' . __( '(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
390 391
		$raw_response = wp_remote_post( $http_url, $options );
	}
godog's avatar
godog committed
392

lechuck's avatar
lechuck committed
393 394 395
	if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
		return;
	}
godog's avatar
godog committed
396

lucha's avatar
lucha committed
397 398 399
	$response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
	foreach ( $response['plugins'] as &$plugin ) {
		$plugin = (object) $plugin;
lechuck's avatar
lechuck committed
400 401 402 403 404 405
		if ( isset( $plugin->compatibility ) ) {
			$plugin->compatibility = (object) $plugin->compatibility;
			foreach ( $plugin->compatibility as &$data ) {
				$data = (object) $data;
			}
		}
lucha's avatar
lucha committed
406
	}
lechuck's avatar
lechuck committed
407
	unset( $plugin, $data );
lucha's avatar
lucha committed
408 409 410 411
	foreach ( $response['no_update'] as &$plugin ) {
		$plugin = (object) $plugin;
	}
	unset( $plugin );
godog's avatar
godog committed
412

lucha's avatar
lucha committed
413
	if ( is_array( $response ) ) {
samba's avatar
samba committed
414
		$new_option->response     = $response['plugins'];
lucha's avatar
lucha committed
415
		$new_option->translations = $response['translations'];
lucha's avatar
lucha committed
416 417
		// 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
418
	} else {
samba's avatar
samba committed
419
		$new_option->response     = array();
lucha's avatar
lucha committed
420
		$new_option->translations = array();
samba's avatar
samba committed
421
		$new_option->no_update    = array();
lucha's avatar
lucha committed
422
	}
godog's avatar
godog committed
423 424 425 426 427 428 429 430 431 432 433 434

	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
agata's avatar
agata committed
435
 * @global string $wp_version The WordPress version string.
godog's avatar
godog committed
436
 *
lechuck's avatar
lechuck committed
437
 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
godog's avatar
godog committed
438
 */
lechuck's avatar
lechuck committed
439
function wp_update_themes( $extra_stats = array() ) {
lechuck's avatar
lechuck committed
440
	if ( wp_installing() ) {
lechuck's avatar
lechuck committed
441 442
		return;
	}
lechuck's avatar
lechuck committed
443

agata's avatar
agata committed
444 445
	// Include an unmodified $wp_version.
	require ABSPATH . WPINC . '/version.php';
godog's avatar
godog committed
446

lechuck's avatar
lechuck committed
447
	$installed_themes = wp_get_themes();
samba's avatar
samba committed
448
	$translations     = wp_get_installed_translations( 'themes' );
lucha's avatar
lucha committed
449

godog's avatar
godog committed
450
	$last_update = get_site_transient( 'update_themes' );
samba's avatar
samba committed
451
	if ( ! is_object( $last_update ) ) {
godog's avatar
godog committed
452
		$last_update = new stdClass;
samba's avatar
samba committed
453
	}
godog's avatar
godog committed
454

agata's avatar
agata committed
455 456 457
	$themes  = array();
	$checked = array();
	$request = array();
godog's avatar
godog committed
458 459

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

lechuck's avatar
lechuck committed
462
	foreach ( $installed_themes as $theme ) {
samba's avatar
samba committed
463
		$checked[ $theme->get_stylesheet() ] = $theme->get( 'Version' );
lechuck's avatar
lechuck committed
464 465

		$themes[ $theme->get_stylesheet() ] = array(
samba's avatar
samba committed
466 467 468 469 470
			'Name'       => $theme->get( 'Name' ),
			'Title'      => $theme->get( 'Name' ),
			'Version'    => $theme->get( 'Version' ),
			'Author'     => $theme->get( 'Author' ),
			'Author URI' => $theme->get( 'AuthorURI' ),
lechuck's avatar
lechuck committed
471 472 473 474
			'Template'   => $theme->get_template(),
			'Stylesheet' => $theme->get_stylesheet(),
		);
	}
godog's avatar
godog committed
475

lucha's avatar
lucha committed
476 477
	$doing_cron = wp_doing_cron();

lechuck's avatar
lechuck committed
478 479
	// Check for update on a different schedule, depending on the page.
	switch ( current_filter() ) {
samba's avatar
samba committed
480
		case 'upgrader_process_complete':
lucha's avatar
lucha committed
481 482
			$timeout = 0;
			break;
samba's avatar
samba committed
483
		case 'load-update-core.php':
lechuck's avatar
lechuck committed
484
			$timeout = MINUTE_IN_SECONDS;
lechuck's avatar
lechuck committed
485
			break;
samba's avatar
samba committed
486 487
		case 'load-themes.php':
		case 'load-update.php':
lechuck's avatar
lechuck committed
488
			$timeout = HOUR_IN_SECONDS;
lechuck's avatar
lechuck committed
489
			break;
samba's avatar
samba committed
490
		default:
lucha's avatar
lucha committed
491 492 493 494 495
			if ( $doing_cron ) {
				$timeout = 2 * HOUR_IN_SECONDS;
			} else {
				$timeout = 12 * HOUR_IN_SECONDS;
			}
godog's avatar
godog committed
496 497
	}

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

lechuck's avatar
lechuck committed
500
	if ( $time_not_changed && ! $extra_stats ) {
lechuck's avatar
lechuck committed
501 502
		$theme_changed = false;
		foreach ( $checked as $slug => $v ) {
samba's avatar
samba committed
503
			if ( ! isset( $last_update->checked[ $slug ] ) || strval( $last_update->checked[ $slug ] ) !== strval( $v ) ) {
godog's avatar
godog committed
504
				$theme_changed = true;
samba's avatar
samba committed
505
			}
lechuck's avatar
lechuck committed
506 507
		}

samba's avatar
samba committed
508
		if ( isset( $last_update->response ) && is_array( $last_update->response ) ) {
lechuck's avatar
lechuck committed
509
			foreach ( $last_update->response as $slug => $update_details ) {
samba's avatar
samba committed
510
				if ( ! isset( $checked[ $slug ] ) ) {
lechuck's avatar
lechuck committed
511 512 513
					$theme_changed = true;
					break;
				}
godog's avatar
godog committed
514 515 516
			}
		}

agata's avatar
agata committed
517
		// Bail if we've checked recently and if nothing has changed.
lechuck's avatar
lechuck committed
518 519 520
		if ( ! $theme_changed ) {
			return;
		}
lechuck's avatar
lechuck committed
521
	}
godog's avatar
godog committed
522

agata's avatar
agata committed
523
	// Update last_checked for current to prevent multiple blocking requests if request hangs.
godog's avatar
godog committed
524 525 526
	$last_update->last_checked = time();
	set_site_transient( 'update_themes', $last_update );

lucha's avatar
lucha committed
527 528
	$request['themes'] = $themes;

lechuck's avatar
lechuck committed
529 530
	$locales = array_values( get_available_languages() );

lucha's avatar
lucha committed
531
	/**
lucha's avatar
lucha committed
532
	 * Filters the locales requested for theme translations.
lucha's avatar
lucha committed
533 534
	 *
	 * @since 3.7.0
lechuck's avatar
lechuck committed
535
	 * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
lucha's avatar
lucha committed
536
	 *
lechuck's avatar
lechuck committed
537
	 * @param array $locales Theme locales. Default is all available locales of the site.
lucha's avatar
lucha committed
538
	 */
lechuck's avatar
lechuck committed
539 540
	$locales = apply_filters( 'themes_update_check_locales', $locales );
	$locales = array_unique( $locales );
lucha's avatar
lucha committed
541

lucha's avatar
lucha committed
542
	if ( $doing_cron ) {
lucha's avatar
lucha committed
543 544
		$timeout = 30;
	} else {
agata's avatar
agata committed
545
		// Three seconds, plus one extra second for every 10 themes.
lucha's avatar
lucha committed
546 547 548
		$timeout = 3 + (int) ( count( $themes ) / 10 );
	}

godog's avatar
godog committed
549
	$options = array(
samba's avatar
samba committed
550 551
		'timeout'    => $timeout,
		'body'       => array(
lechuck's avatar
lechuck committed
552 553 554
			'themes'       => wp_json_encode( $request ),
			'translations' => wp_json_encode( $translations ),
			'locale'       => wp_json_encode( $locales ),
lucha's avatar
lucha committed
555
		),
samba's avatar
samba committed
556
		'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
godog's avatar
godog committed
557 558
	);

lechuck's avatar
lechuck committed
559
	if ( $extra_stats ) {
lechuck's avatar
lechuck committed
560
		$options['body']['update_stats'] = wp_json_encode( $extra_stats );
lechuck's avatar
lechuck committed
561 562
	}

agata's avatar
agata committed
563 564 565 566
	$url      = 'http://api.wordpress.org/themes/update-check/1.1/';
	$http_url = $url;
	$ssl      = wp_http_supports( array( 'ssl' ) );
	if ( $ssl ) {
lucha's avatar
lucha committed
567
		$url = set_url_scheme( $url, 'https' );
samba's avatar
samba committed
568
	}
lucha's avatar
lucha committed
569 570 571

	$raw_response = wp_remote_post( $url, $options );
	if ( $ssl && is_wp_error( $raw_response ) ) {
lucha's avatar
lucha committed
572 573
		trigger_error(
			sprintf(
agata's avatar
agata committed
574
				/* translators: %s: Support forums URL. */
lucha's avatar
lucha committed
575
				__( '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>.' ),
agata's avatar
agata committed
576
				__( 'https://wordpress.org/support/forums/' )
lucha's avatar
lucha committed
577 578 579
			) . ' ' . __( '(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
580 581
		$raw_response = wp_remote_post( $http_url, $options );
	}
godog's avatar
godog committed
582

lechuck's avatar
lechuck committed
583 584 585
	if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
		return;
	}
godog's avatar
godog committed
586

samba's avatar
samba committed
587
	$new_update               = new stdClass;
lechuck's avatar
lechuck committed
588
	$new_update->last_checked = time();
samba's avatar
samba committed
589
	$new_update->checked      = $checked;
shammash's avatar
shammash committed
590

lucha's avatar
lucha committed
591 592 593 594 595 596
	$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
597 598 599 600

	set_site_transient( 'update_themes', $new_update );
}

lucha's avatar
lucha committed
601 602 603 604 605 606
/**
 * Performs WordPress automatic background updates.
 *
 * @since 3.7.0
 */
function wp_maybe_auto_update() {
agata's avatar
agata committed
607 608
	include_once ABSPATH . 'wp-admin/includes/admin.php';
	require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
lucha's avatar
lucha committed
609 610 611 612 613 614 615 616 617

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

/**
 * Retrieves a list of all language updates available.
 *
 * @since 3.7.0
lechuck's avatar
lechuck committed
618
 *
samba's avatar
samba committed
619
 * @return object[] Array of translation objects that have available updates.
lucha's avatar
lucha committed
620 621
 */
function wp_get_translation_updates() {
samba's avatar
samba committed
622 623 624 625 626 627
	$updates    = array();
	$transients = array(
		'update_core'    => 'core',
		'update_plugins' => 'plugin',
		'update_themes'  => 'theme',
	);
lucha's avatar
lucha committed
628 629
	foreach ( $transients as $transient => $type ) {
		$transient = get_site_transient( $transient );
samba's avatar
samba committed
630
		if ( empty( $transient->translations ) ) {
lucha's avatar
lucha committed
631
			continue;
samba's avatar
samba committed
632
		}
lucha's avatar
lucha committed
633 634 635 636 637 638 639 640

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

lucha's avatar
lucha committed
641
/**
root's avatar
root committed
642 643 644 645 646 647 648
 * Collect counts and UI strings for available updates
 *
 * @since 3.3.0
 *
 * @return array
 */
function wp_get_update_data() {
samba's avatar
samba committed
649 650 651 652 653 654
	$counts = array(
		'plugins'      => 0,
		'themes'       => 0,
		'wordpress'    => 0,
		'translations' => 0,
	);
root's avatar
root committed
655

agata's avatar
agata committed
656 657
	$plugins = current_user_can( 'update_plugins' );
	if ( $plugins ) {
root's avatar
root committed
658
		$update_plugins = get_site_transient( 'update_plugins' );
samba's avatar
samba committed
659
		if ( ! empty( $update_plugins->response ) ) {
root's avatar
root committed
660
			$counts['plugins'] = count( $update_plugins->response );
samba's avatar
samba committed
661
		}
root's avatar
root committed
662 663
	}

agata's avatar
agata committed
664 665
	$themes = current_user_can( 'update_themes' );
	if ( $themes ) {
root's avatar
root committed
666
		$update_themes = get_site_transient( 'update_themes' );
samba's avatar
samba committed
667
		if ( ! empty( $update_themes->response ) ) {
root's avatar
root committed
668
			$counts['themes'] = count( $update_themes->response );
samba's avatar
samba committed
669
		}
root's avatar
root committed
670 671
	}

agata's avatar
agata committed
672 673
	$core = current_user_can( 'update_core' );
	if ( $core && function_exists( 'get_core_updates' ) ) {
samba's avatar
samba committed
674 675
		$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' ) ) {
root's avatar
root committed
676
			$counts['wordpress'] = 1;
samba's avatar
samba committed
677
		}
root's avatar
root committed
678 679
	}

samba's avatar
samba committed
680
	if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() ) {
lucha's avatar
lucha committed
681
		$counts['translations'] = 1;
samba's avatar
samba committed
682
	}
lucha's avatar
lucha committed
683 684

	$counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
samba's avatar
samba committed
685
	$titles          = array();
lucha's avatar
lucha committed
686
	if ( $counts['wordpress'] ) {
agata's avatar
agata committed
687
		/* translators: %d: Number of available WordPress updates. */
samba's avatar
samba committed
688
		$titles['wordpress'] = sprintf( __( '%d WordPress Update' ), $counts['wordpress'] );
lucha's avatar
lucha committed
689 690
	}
	if ( $counts['plugins'] ) {
agata's avatar
agata committed
691
		/* translators: %d: Number of available plugin updates. */
lechuck's avatar
lechuck committed
692
		$titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
lucha's avatar
lucha committed
693 694
	}
	if ( $counts['themes'] ) {
agata's avatar
agata committed
695
		/* translators: %d: Number of available theme updates. */
lechuck's avatar
lechuck committed
696
		$titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
lucha's avatar
lucha committed
697 698
	}
	if ( $counts['translations'] ) {
lucha's avatar
lucha committed
699
		$titles['translations'] = __( 'Translation Updates' );
lucha's avatar
lucha committed
700
	}
root's avatar
root committed
701

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

samba's avatar
samba committed
704 705 706 707
	$update_data = array(
		'counts' => $counts,
		'title'  => $update_title,
	);
lucha's avatar
lucha committed
708
	/**
lucha's avatar
lucha committed
709
	 * Filters the returned array of update data for plugins, themes, and WordPress core.
lucha's avatar
lucha committed
710 711 712 713 714 715 716 717 718 719 720 721
	 *
	 * @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
722 723
}

lechuck's avatar
lechuck committed
724
/**
lechuck's avatar
lechuck committed
725 726 727 728
 * Determines whether core should be updated.
 *
 * @since 2.8.0
 *
agata's avatar
agata committed
729
 * @global string $wp_version The WordPress version string.
lechuck's avatar
lechuck committed
730
 */
godog's avatar
godog committed
731
function _maybe_update_core() {
agata's avatar
agata committed
732 733
	// Include an unmodified $wp_version.
	require ABSPATH . WPINC . '/version.php';
godog's avatar
godog committed
734 735 736

	$current = get_site_transient( 'update_core' );

lechuck's avatar
lechuck committed
737
	if ( isset( $current->last_checked, $current->version_checked ) &&
lechuck's avatar
lechuck committed
738
		12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
lechuck's avatar
lechuck committed
739
		$current->version_checked == $wp_version ) {
godog's avatar
godog committed
740
		return;
lechuck's avatar
lechuck committed
741
	}
godog's avatar
godog committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755
	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' );
samba's avatar
samba committed
756
	if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) ) {
godog's avatar
godog committed
757
		return;
samba's avatar
samba committed
758
	}
godog's avatar
godog committed
759 760 761 762 763 764 765 766 767 768 769 770
	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
771
function _maybe_update_themes() {
godog's avatar
godog committed
772
	$current = get_site_transient( 'update_themes' );
samba's avatar
samba committed
773
	if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) ) {
godog's avatar
godog committed
774
		return;
samba's avatar
samba committed
775
	}
godog's avatar
godog committed
776 777 778
	wp_update_themes();
}

root's avatar
root committed
779 780 781 782 783 784
/**
 * Schedule core, theme, and plugin update checks.
 *
 * @since 3.1.0
 */
function wp_schedule_update_checks() {
samba's avatar
samba committed
785 786 787
	if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() ) {
		wp_schedule_event( time(), 'twicedaily', 'wp_version_check' );
	}
root's avatar
root committed
788

samba's avatar
samba committed
789 790 791
	if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() ) {
		wp_schedule_event( time(), 'twicedaily', 'wp_update_plugins' );
	}
root's avatar
root committed
792

samba's avatar
samba committed
793 794 795
	if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() ) {
		wp_schedule_event( time(), 'twicedaily', 'wp_update_themes' );
	}
root's avatar
root committed
796 797
}

lechuck's avatar
lechuck committed
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
/**
 * 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
813
if ( ( ! is_main_site() && ! is_network_admin() ) || wp_doing_ajax() ) {
root's avatar
root committed
814
	return;
lechuck's avatar
lechuck committed
815
}
root's avatar
root committed
816

godog's avatar
godog committed
817 818 819 820 821 822 823 824 825 826 827 828 829 830
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
831

samba's avatar
samba committed
832
add_action( 'update_option_WPLANG', 'wp_clean_update_cache', 10, 0 );
lechuck's avatar
lechuck committed
833

lucha's avatar
lucha committed
834
add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
godog's avatar
godog committed
835

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