3 * A simple set of functions to check our version 1.0 update service.
10 * Check WordPress version against the newest version.
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
17 * @global string $wp_version Used to check against the newest WordPress version.
19 * @global string $wp_local_package
21 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
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.
24 function wp_version_check( $extra_stats = array(), $force_check = false ) {
25 if ( wp_installing() ) {
29 global $wpdb, $wp_local_package;
30 // include an unmodified $wp_version
31 include( ABSPATH . WPINC . '/version.php' );
32 $php_version = phpversion();
34 $current = get_site_transient( 'update_core' );
35 $translations = wp_get_installed_translations( 'core' );
37 // Invalidate the transient when $wp_version changes
38 if ( is_object( $current ) && $wp_version != $current->version_checked )
41 if ( ! is_object($current) ) {
42 $current = new stdClass;
43 $current->updates = array();
44 $current->version_checked = $wp_version;
47 if ( ! empty( $extra_stats ) )
50 // Wait 60 seconds between multiple version check requests
52 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
53 if ( ! $force_check && $time_not_changed ) {
58 * Filters the locale requested for WordPress core translations.
62 * @param string $locale Current locale.
64 $locale = apply_filters( 'core_version_check_locale', get_locale() );
66 // Update last_checked for current to prevent multiple blocking requests if request hangs
67 $current->last_checked = time();
68 set_site_transient( 'update_core', $current );
70 if ( method_exists( $wpdb, 'db_version' ) )
71 $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
73 $mysql_version = 'N/A';
75 if ( is_multisite() ) {
76 $user_count = get_user_count();
77 $num_blogs = get_blog_count();
78 $wp_install = network_site_url();
79 $multisite_enabled = 1;
81 $user_count = count_users();
82 $user_count = $user_count['total_users'];
83 $multisite_enabled = 0;
85 $wp_install = home_url( '/' );
89 'version' => $wp_version,
90 'php' => $php_version,
92 'mysql' => $mysql_version,
93 'local_package' => isset( $wp_local_package ) ? $wp_local_package : '',
94 'blogs' => $num_blogs,
95 'users' => $user_count,
96 'multisite_enabled' => $multisite_enabled,
97 'initial_db_version' => get_site_option( 'initial_db_version' ),
101 'translations' => wp_json_encode( $translations ),
104 if ( is_array( $extra_stats ) )
105 $post_body = array_merge( $post_body, $extra_stats );
107 $url = $http_url = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, null, '&' );
108 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
109 $url = set_url_scheme( $url, 'https' );
112 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
113 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
115 'wp_install' => $wp_install,
116 'wp_blog' => home_url( '/' )
118 'body' => $post_body,
121 $response = wp_remote_post( $url, $options );
122 if ( $ssl && is_wp_error( $response ) ) {
125 /* translators: %s: support forums URL */
126 __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
127 __( 'https://wordpress.org/support/' )
128 ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
129 headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
131 $response = wp_remote_post( $http_url, $options );
134 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
138 $body = trim( wp_remote_retrieve_body( $response ) );
139 $body = json_decode( $body, true );
141 if ( ! is_array( $body ) || ! isset( $body['offers'] ) ) {
145 $offers = $body['offers'];
147 foreach ( $offers as &$offer ) {
148 foreach ( $offer as $offer_key => $value ) {
149 if ( 'packages' == $offer_key )
150 $offer['packages'] = (object) array_intersect_key( array_map( 'esc_url', $offer['packages'] ),
151 array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' ) );
152 elseif ( 'download' == $offer_key )
153 $offer['download'] = esc_url( $value );
155 $offer[ $offer_key ] = esc_html( $value );
157 $offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',
158 'packages', 'current', 'version', 'php_version', 'mysql_version', 'new_bundled', 'partial_version', 'notify_email', 'support_email', 'new_files' ), '' ) );
161 $updates = new stdClass();
162 $updates->updates = $offers;
163 $updates->last_checked = time();
164 $updates->version_checked = $wp_version;
166 if ( isset( $body['translations'] ) )
167 $updates->translations = $body['translations'];
169 set_site_transient( 'update_core', $updates );
171 if ( ! empty( $body['ttl'] ) ) {
172 $ttl = (int) $body['ttl'];
173 if ( $ttl && ( time() + $ttl < wp_next_scheduled( 'wp_version_check' ) ) ) {
174 // Queue an event to re-run the update check in $ttl seconds.
175 wp_schedule_single_event( time() + $ttl, 'wp_version_check' );
179 // Trigger background updates if running non-interactively, and we weren't called from the update handler.
180 if ( defined( 'DOING_CRON' ) && DOING_CRON && ! doing_action( 'wp_maybe_auto_update' ) ) {
181 do_action( 'wp_maybe_auto_update' );
186 * Check plugin versions against the latest versions hosted on WordPress.org.
188 * The WordPress version, PHP version, and Locale is sent along with a list of
189 * all plugins installed. Checks against the WordPress server at
190 * api.wordpress.org. Will only check if WordPress isn't installing.
193 * @global string $wp_version Used to notify the WordPress version.
195 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
197 function wp_update_plugins( $extra_stats = array() ) {
198 if ( wp_installing() ) {
202 // include an unmodified $wp_version
203 include( ABSPATH . WPINC . '/version.php' );
205 // If running blog-side, bail unless we've not checked in the last 12 hours
206 if ( !function_exists( 'get_plugins' ) )
207 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
209 $plugins = get_plugins();
210 $translations = wp_get_installed_translations( 'plugins' );
212 $active = get_option( 'active_plugins', array() );
213 $current = get_site_transient( 'update_plugins' );
214 if ( ! is_object($current) )
215 $current = new stdClass;
217 $new_option = new stdClass;
218 $new_option->last_checked = time();
220 // Check for update on a different schedule, depending on the page.
221 switch ( current_filter() ) {
222 case 'upgrader_process_complete' :
225 case 'load-update-core.php' :
226 $timeout = MINUTE_IN_SECONDS;
228 case 'load-plugins.php' :
229 case 'load-update.php' :
230 $timeout = HOUR_IN_SECONDS;
233 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
236 $timeout = 12 * HOUR_IN_SECONDS;
240 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
242 if ( $time_not_changed && ! $extra_stats ) {
243 $plugin_changed = false;
244 foreach ( $plugins as $file => $p ) {
245 $new_option->checked[ $file ] = $p['Version'];
247 if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
248 $plugin_changed = true;
251 if ( isset ( $current->response ) && is_array( $current->response ) ) {
252 foreach ( $current->response as $plugin_file => $update_details ) {
253 if ( ! isset($plugins[ $plugin_file ]) ) {
254 $plugin_changed = true;
260 // Bail if we've checked recently and if nothing has changed
261 if ( ! $plugin_changed ) {
266 // Update last_checked for current to prevent multiple blocking requests if request hangs
267 $current->last_checked = time();
268 set_site_transient( 'update_plugins', $current );
270 $to_send = compact( 'plugins', 'active' );
272 $locales = array_values( get_available_languages() );
275 * Filters the locales requested for plugin translations.
278 * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
280 * @param array $locales Plugin locales. Default is all available locales of the site.
282 $locales = apply_filters( 'plugins_update_check_locales', $locales );
283 $locales = array_unique( $locales );
285 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
288 // Three seconds, plus one extra second for every 10 plugins
289 $timeout = 3 + (int) ( count( $plugins ) / 10 );
293 'timeout' => $timeout,
295 'plugins' => wp_json_encode( $to_send ),
296 'translations' => wp_json_encode( $translations ),
297 'locale' => wp_json_encode( $locales ),
298 'all' => wp_json_encode( true ),
300 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
303 if ( $extra_stats ) {
304 $options['body']['update_stats'] = wp_json_encode( $extra_stats );
307 $url = $http_url = 'http://api.wordpress.org/plugins/update-check/1.1/';
308 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
309 $url = set_url_scheme( $url, 'https' );
311 $raw_response = wp_remote_post( $url, $options );
312 if ( $ssl && is_wp_error( $raw_response ) ) {
315 /* translators: %s: support forums URL */
316 __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
317 __( 'https://wordpress.org/support/' )
318 ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
319 headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
321 $raw_response = wp_remote_post( $http_url, $options );
324 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
328 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
329 foreach ( $response['plugins'] as &$plugin ) {
330 $plugin = (object) $plugin;
331 if ( isset( $plugin->compatibility ) ) {
332 $plugin->compatibility = (object) $plugin->compatibility;
333 foreach ( $plugin->compatibility as &$data ) {
334 $data = (object) $data;
338 unset( $plugin, $data );
339 foreach ( $response['no_update'] as &$plugin ) {
340 $plugin = (object) $plugin;
344 if ( is_array( $response ) ) {
345 $new_option->response = $response['plugins'];
346 $new_option->translations = $response['translations'];
347 // TODO: Perhaps better to store no_update in a separate transient with an expiry?
348 $new_option->no_update = $response['no_update'];
350 $new_option->response = array();
351 $new_option->translations = array();
352 $new_option->no_update = array();
355 set_site_transient( 'update_plugins', $new_option );
359 * Check theme versions against the latest versions hosted on WordPress.org.
361 * A list of all themes installed in sent to WP. Checks against the
362 * WordPress server at api.wordpress.org. Will only check if WordPress isn't
367 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
369 function wp_update_themes( $extra_stats = array() ) {
370 if ( wp_installing() ) {
374 // include an unmodified $wp_version
375 include( ABSPATH . WPINC . '/version.php' );
377 $installed_themes = wp_get_themes();
378 $translations = wp_get_installed_translations( 'themes' );
380 $last_update = get_site_transient( 'update_themes' );
381 if ( ! is_object($last_update) )
382 $last_update = new stdClass;
384 $themes = $checked = $request = array();
386 // Put slug of current theme into request.
387 $request['active'] = get_option( 'stylesheet' );
389 foreach ( $installed_themes as $theme ) {
390 $checked[ $theme->get_stylesheet() ] = $theme->get('Version');
392 $themes[ $theme->get_stylesheet() ] = array(
393 'Name' => $theme->get('Name'),
394 'Title' => $theme->get('Name'),
395 'Version' => $theme->get('Version'),
396 'Author' => $theme->get('Author'),
397 'Author URI' => $theme->get('AuthorURI'),
398 'Template' => $theme->get_template(),
399 'Stylesheet' => $theme->get_stylesheet(),
403 // Check for update on a different schedule, depending on the page.
404 switch ( current_filter() ) {
405 case 'upgrader_process_complete' :
408 case 'load-update-core.php' :
409 $timeout = MINUTE_IN_SECONDS;
411 case 'load-themes.php' :
412 case 'load-update.php' :
413 $timeout = HOUR_IN_SECONDS;
416 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
419 $timeout = 12 * HOUR_IN_SECONDS;
423 $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
425 if ( $time_not_changed && ! $extra_stats ) {
426 $theme_changed = false;
427 foreach ( $checked as $slug => $v ) {
428 if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
429 $theme_changed = true;
432 if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
433 foreach ( $last_update->response as $slug => $update_details ) {
434 if ( ! isset($checked[ $slug ]) ) {
435 $theme_changed = true;
441 // Bail if we've checked recently and if nothing has changed
442 if ( ! $theme_changed ) {
447 // Update last_checked for current to prevent multiple blocking requests if request hangs
448 $last_update->last_checked = time();
449 set_site_transient( 'update_themes', $last_update );
451 $request['themes'] = $themes;
453 $locales = array_values( get_available_languages() );
456 * Filters the locales requested for theme translations.
459 * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
461 * @param array $locales Theme locales. Default is all available locales of the site.
463 $locales = apply_filters( 'themes_update_check_locales', $locales );
464 $locales = array_unique( $locales );
466 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
469 // Three seconds, plus one extra second for every 10 themes
470 $timeout = 3 + (int) ( count( $themes ) / 10 );
474 'timeout' => $timeout,
476 'themes' => wp_json_encode( $request ),
477 'translations' => wp_json_encode( $translations ),
478 'locale' => wp_json_encode( $locales ),
480 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
483 if ( $extra_stats ) {
484 $options['body']['update_stats'] = wp_json_encode( $extra_stats );
487 $url = $http_url = 'http://api.wordpress.org/themes/update-check/1.1/';
488 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
489 $url = set_url_scheme( $url, 'https' );
491 $raw_response = wp_remote_post( $url, $options );
492 if ( $ssl && is_wp_error( $raw_response ) ) {
495 /* translators: %s: support forums URL */
496 __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
497 __( 'https://wordpress.org/support/' )
498 ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
499 headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
501 $raw_response = wp_remote_post( $http_url, $options );
504 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
508 $new_update = new stdClass;
509 $new_update->last_checked = time();
510 $new_update->checked = $checked;
512 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
514 if ( is_array( $response ) ) {
515 $new_update->response = $response['themes'];
516 $new_update->translations = $response['translations'];
519 set_site_transient( 'update_themes', $new_update );
523 * Performs WordPress automatic background updates.
527 function wp_maybe_auto_update() {
528 include_once( ABSPATH . '/wp-admin/includes/admin.php' );
529 include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' );
531 $upgrader = new WP_Automatic_Updater;
536 * Retrieves a list of all language updates available.
542 function wp_get_translation_updates() {
544 $transients = array( 'update_core' => 'core', 'update_plugins' => 'plugin', 'update_themes' => 'theme' );
545 foreach ( $transients as $transient => $type ) {
546 $transient = get_site_transient( $transient );
547 if ( empty( $transient->translations ) )
550 foreach ( $transient->translations as $translation ) {
551 $updates[] = (object) $translation;
558 * Collect counts and UI strings for available updates
564 function wp_get_update_data() {
565 $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0, 'translations' => 0 );
567 if ( $plugins = current_user_can( 'update_plugins' ) ) {
568 $update_plugins = get_site_transient( 'update_plugins' );
569 if ( ! empty( $update_plugins->response ) )
570 $counts['plugins'] = count( $update_plugins->response );
573 if ( $themes = current_user_can( 'update_themes' ) ) {
574 $update_themes = get_site_transient( 'update_themes' );
575 if ( ! empty( $update_themes->response ) )
576 $counts['themes'] = count( $update_themes->response );
579 if ( ( $core = current_user_can( 'update_core' ) ) && function_exists( 'get_core_updates' ) ) {
580 $update_wordpress = get_core_updates( array('dismissed' => false) );
581 if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') )
582 $counts['wordpress'] = 1;
585 if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() )
586 $counts['translations'] = 1;
588 $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
590 if ( $counts['wordpress'] ) {
591 /* translators: 1: Number of updates available to WordPress */
592 $titles['wordpress'] = sprintf( __( '%d WordPress Update'), $counts['wordpress'] );
594 if ( $counts['plugins'] ) {
595 /* translators: 1: Number of updates available to plugins */
596 $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
598 if ( $counts['themes'] ) {
599 /* translators: 1: Number of updates available to themes */
600 $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
602 if ( $counts['translations'] ) {
603 $titles['translations'] = __( 'Translation Updates' );
606 $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
608 $update_data = array( 'counts' => $counts, 'title' => $update_title );
610 * Filters the returned array of update data for plugins, themes, and WordPress core.
614 * @param array $update_data {
615 * Fetched update data.
617 * @type array $counts An array of counts for available plugin, theme, and WordPress updates.
618 * @type string $update_title Titles of available updates.
620 * @param array $titles An array of update counts and UI strings for available updates.
622 return apply_filters( 'wp_get_update_data', $update_data, $titles );
626 * Determines whether core should be updated.
630 * @global string $wp_version
632 function _maybe_update_core() {
633 // include an unmodified $wp_version
634 include( ABSPATH . WPINC . '/version.php' );
636 $current = get_site_transient( 'update_core' );
638 if ( isset( $current->last_checked, $current->version_checked ) &&
639 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
640 $current->version_checked == $wp_version ) {
646 * Check the last time plugins were run before checking plugin versions.
648 * This might have been backported to WordPress 2.6.1 for performance reasons.
649 * This is used for the wp-admin to check only so often instead of every page
655 function _maybe_update_plugins() {
656 $current = get_site_transient( 'update_plugins' );
657 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
663 * Check themes versions only after a duration of time.
665 * This is for performance reasons to make sure that on the theme version
666 * checker is not run on every page load.
671 function _maybe_update_themes() {
672 $current = get_site_transient( 'update_themes' );
673 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
679 * Schedule core, theme, and plugin update checks.
683 function wp_schedule_update_checks() {
684 if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() )
685 wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
687 if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() )
688 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
690 if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() )
691 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
695 * Clear existing update caches for plugins, themes, and core.
699 function wp_clean_update_cache() {
700 if ( function_exists( 'wp_clean_plugins_cache' ) ) {
701 wp_clean_plugins_cache();
703 delete_site_transient( 'update_plugins' );
705 wp_clean_themes_cache();
706 delete_site_transient( 'update_core' );
709 if ( ( ! is_main_site() && ! is_network_admin() ) || wp_doing_ajax() ) {
713 add_action( 'admin_init', '_maybe_update_core' );
714 add_action( 'wp_version_check', 'wp_version_check' );
716 add_action( 'load-plugins.php', 'wp_update_plugins' );
717 add_action( 'load-update.php', 'wp_update_plugins' );
718 add_action( 'load-update-core.php', 'wp_update_plugins' );
719 add_action( 'admin_init', '_maybe_update_plugins' );
720 add_action( 'wp_update_plugins', 'wp_update_plugins' );
722 add_action( 'load-themes.php', 'wp_update_themes' );
723 add_action( 'load-update.php', 'wp_update_themes' );
724 add_action( 'load-update-core.php', 'wp_update_themes' );
725 add_action( 'admin_init', '_maybe_update_themes' );
726 add_action( 'wp_update_themes', 'wp_update_themes' );
728 add_action( 'update_option_WPLANG', 'wp_clean_update_cache' , 10, 0 );
730 add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
732 add_action( 'init', 'wp_schedule_update_checks' );