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 $wp_version, $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 * Filter 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 ) ) {
123 trigger_error( __( '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="https://wordpress.org/support/">support forums</a>.' ) . ' ' . __( '(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 );
124 $response = wp_remote_post( $http_url, $options );
127 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
131 $body = trim( wp_remote_retrieve_body( $response ) );
132 $body = json_decode( $body, true );
134 if ( ! is_array( $body ) || ! isset( $body['offers'] ) ) {
138 $offers = $body['offers'];
140 foreach ( $offers as &$offer ) {
141 foreach ( $offer as $offer_key => $value ) {
142 if ( 'packages' == $offer_key )
143 $offer['packages'] = (object) array_intersect_key( array_map( 'esc_url', $offer['packages'] ),
144 array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' ) );
145 elseif ( 'download' == $offer_key )
146 $offer['download'] = esc_url( $value );
148 $offer[ $offer_key ] = esc_html( $value );
150 $offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',
151 'packages', 'current', 'version', 'php_version', 'mysql_version', 'new_bundled', 'partial_version', 'notify_email', 'support_email', 'new_files' ), '' ) );
154 $updates = new stdClass();
155 $updates->updates = $offers;
156 $updates->last_checked = time();
157 $updates->version_checked = $wp_version;
159 if ( isset( $body['translations'] ) )
160 $updates->translations = $body['translations'];
162 set_site_transient( 'update_core', $updates );
164 if ( ! empty( $body['ttl'] ) ) {
165 $ttl = (int) $body['ttl'];
166 if ( $ttl && ( time() + $ttl < wp_next_scheduled( 'wp_version_check' ) ) ) {
167 // Queue an event to re-run the update check in $ttl seconds.
168 wp_schedule_single_event( time() + $ttl, 'wp_version_check' );
172 // Trigger a background updates check if running non-interactively, and we weren't called from the update handler.
173 if ( defined( 'DOING_CRON' ) && DOING_CRON && ! doing_action( 'wp_maybe_auto_update' ) ) {
174 do_action( 'wp_maybe_auto_update' );
179 * Check plugin versions against the latest versions hosted on WordPress.org.
181 * The WordPress version, PHP version, and Locale is sent along with a list of
182 * all plugins installed. Checks against the WordPress server at
183 * api.wordpress.org. Will only check if WordPress isn't installing.
186 * @global string $wp_version Used to notify the WordPress version.
188 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
190 function wp_update_plugins( $extra_stats = array() ) {
191 if ( wp_installing() ) {
196 // include an unmodified $wp_version
197 include( ABSPATH . WPINC . '/version.php' );
199 // If running blog-side, bail unless we've not checked in the last 12 hours
200 if ( !function_exists( 'get_plugins' ) )
201 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
203 $plugins = get_plugins();
204 $translations = wp_get_installed_translations( 'plugins' );
206 $active = get_option( 'active_plugins', array() );
207 $current = get_site_transient( 'update_plugins' );
208 if ( ! is_object($current) )
209 $current = new stdClass;
211 $new_option = new stdClass;
212 $new_option->last_checked = time();
214 // Check for update on a different schedule, depending on the page.
215 switch ( current_filter() ) {
216 case 'upgrader_process_complete' :
219 case 'load-update-core.php' :
220 $timeout = MINUTE_IN_SECONDS;
222 case 'load-plugins.php' :
223 case 'load-update.php' :
224 $timeout = HOUR_IN_SECONDS;
227 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
230 $timeout = 12 * HOUR_IN_SECONDS;
234 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
236 if ( $time_not_changed && ! $extra_stats ) {
237 $plugin_changed = false;
238 foreach ( $plugins as $file => $p ) {
239 $new_option->checked[ $file ] = $p['Version'];
241 if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
242 $plugin_changed = true;
245 if ( isset ( $current->response ) && is_array( $current->response ) ) {
246 foreach ( $current->response as $plugin_file => $update_details ) {
247 if ( ! isset($plugins[ $plugin_file ]) ) {
248 $plugin_changed = true;
254 // Bail if we've checked recently and if nothing has changed
255 if ( ! $plugin_changed ) {
260 // Update last_checked for current to prevent multiple blocking requests if request hangs
261 $current->last_checked = time();
262 set_site_transient( 'update_plugins', $current );
264 $to_send = compact( 'plugins', 'active' );
267 * Filter the locales requested for plugin translations.
271 * @param array $locales Plugin locale. Default is current locale of the site.
273 $locales = apply_filters( 'plugins_update_check_locales', array( get_locale() ) );
275 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
278 // Three seconds, plus one extra second for every 10 plugins
279 $timeout = 3 + (int) ( count( $plugins ) / 10 );
283 'timeout' => $timeout,
285 'plugins' => wp_json_encode( $to_send ),
286 'translations' => wp_json_encode( $translations ),
287 'locale' => wp_json_encode( $locales ),
288 'all' => wp_json_encode( true ),
290 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
293 if ( $extra_stats ) {
294 $options['body']['update_stats'] = wp_json_encode( $extra_stats );
297 $url = $http_url = 'http://api.wordpress.org/plugins/update-check/1.1/';
298 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
299 $url = set_url_scheme( $url, 'https' );
301 $raw_response = wp_remote_post( $url, $options );
302 if ( $ssl && is_wp_error( $raw_response ) ) {
303 trigger_error( __( '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="https://wordpress.org/support/">support forums</a>.' ) . ' ' . __( '(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 );
304 $raw_response = wp_remote_post( $http_url, $options );
307 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
311 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
312 foreach ( $response['plugins'] as &$plugin ) {
313 $plugin = (object) $plugin;
316 foreach ( $response['no_update'] as &$plugin ) {
317 $plugin = (object) $plugin;
321 if ( is_array( $response ) ) {
322 $new_option->response = $response['plugins'];
323 $new_option->translations = $response['translations'];
324 // TODO: Perhaps better to store no_update in a separate transient with an expiry?
325 $new_option->no_update = $response['no_update'];
327 $new_option->response = array();
328 $new_option->translations = array();
329 $new_option->no_update = array();
332 set_site_transient( 'update_plugins', $new_option );
336 * Check theme versions against the latest versions hosted on WordPress.org.
338 * A list of all themes installed in sent to WP. Checks against the
339 * WordPress server at api.wordpress.org. Will only check if WordPress isn't
343 * @uses $wp_version Used to notify the WordPress version.
345 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
347 function wp_update_themes( $extra_stats = array() ) {
348 if ( wp_installing() ) {
352 // include an unmodified $wp_version
353 include( ABSPATH . WPINC . '/version.php' );
355 $installed_themes = wp_get_themes();
356 $translations = wp_get_installed_translations( 'themes' );
358 $last_update = get_site_transient( 'update_themes' );
359 if ( ! is_object($last_update) )
360 $last_update = new stdClass;
362 $themes = $checked = $request = array();
364 // Put slug of current theme into request.
365 $request['active'] = get_option( 'stylesheet' );
367 foreach ( $installed_themes as $theme ) {
368 $checked[ $theme->get_stylesheet() ] = $theme->get('Version');
370 $themes[ $theme->get_stylesheet() ] = array(
371 'Name' => $theme->get('Name'),
372 'Title' => $theme->get('Name'),
373 'Version' => $theme->get('Version'),
374 'Author' => $theme->get('Author'),
375 'Author URI' => $theme->get('AuthorURI'),
376 'Template' => $theme->get_template(),
377 'Stylesheet' => $theme->get_stylesheet(),
381 // Check for update on a different schedule, depending on the page.
382 switch ( current_filter() ) {
383 case 'upgrader_process_complete' :
386 case 'load-update-core.php' :
387 $timeout = MINUTE_IN_SECONDS;
389 case 'load-themes.php' :
390 case 'load-update.php' :
391 $timeout = HOUR_IN_SECONDS;
394 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
397 $timeout = 12 * HOUR_IN_SECONDS;
401 $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
403 if ( $time_not_changed && ! $extra_stats ) {
404 $theme_changed = false;
405 foreach ( $checked as $slug => $v ) {
406 if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
407 $theme_changed = true;
410 if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
411 foreach ( $last_update->response as $slug => $update_details ) {
412 if ( ! isset($checked[ $slug ]) ) {
413 $theme_changed = true;
419 // Bail if we've checked recently and if nothing has changed
420 if ( ! $theme_changed ) {
425 // Update last_checked for current to prevent multiple blocking requests if request hangs
426 $last_update->last_checked = time();
427 set_site_transient( 'update_themes', $last_update );
429 $request['themes'] = $themes;
432 * Filter the locales requested for theme translations.
436 * @param array $locales Theme locale. Default is current locale of the site.
438 $locales = apply_filters( 'themes_update_check_locales', array( get_locale() ) );
440 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
443 // Three seconds, plus one extra second for every 10 themes
444 $timeout = 3 + (int) ( count( $themes ) / 10 );
448 'timeout' => $timeout,
450 'themes' => wp_json_encode( $request ),
451 'translations' => wp_json_encode( $translations ),
452 'locale' => wp_json_encode( $locales ),
454 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
457 if ( $extra_stats ) {
458 $options['body']['update_stats'] = wp_json_encode( $extra_stats );
461 $url = $http_url = 'http://api.wordpress.org/themes/update-check/1.1/';
462 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
463 $url = set_url_scheme( $url, 'https' );
465 $raw_response = wp_remote_post( $url, $options );
466 if ( $ssl && is_wp_error( $raw_response ) ) {
467 trigger_error( __( '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="https://wordpress.org/support/">support forums</a>.' ) . ' ' . __( '(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 );
468 $raw_response = wp_remote_post( $http_url, $options );
471 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
475 $new_update = new stdClass;
476 $new_update->last_checked = time();
477 $new_update->checked = $checked;
479 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
481 if ( is_array( $response ) ) {
482 $new_update->response = $response['themes'];
483 $new_update->translations = $response['translations'];
486 set_site_transient( 'update_themes', $new_update );
490 * Performs WordPress automatic background updates.
494 function wp_maybe_auto_update() {
495 include_once( ABSPATH . '/wp-admin/includes/admin.php' );
496 include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' );
498 $upgrader = new WP_Automatic_Updater;
503 * Retrieves a list of all language updates available.
509 function wp_get_translation_updates() {
511 $transients = array( 'update_core' => 'core', 'update_plugins' => 'plugin', 'update_themes' => 'theme' );
512 foreach ( $transients as $transient => $type ) {
513 $transient = get_site_transient( $transient );
514 if ( empty( $transient->translations ) )
517 foreach ( $transient->translations as $translation ) {
518 $updates[] = (object) $translation;
525 * Collect counts and UI strings for available updates
531 function wp_get_update_data() {
532 $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0, 'translations' => 0 );
534 if ( $plugins = current_user_can( 'update_plugins' ) ) {
535 $update_plugins = get_site_transient( 'update_plugins' );
536 if ( ! empty( $update_plugins->response ) )
537 $counts['plugins'] = count( $update_plugins->response );
540 if ( $themes = current_user_can( 'update_themes' ) ) {
541 $update_themes = get_site_transient( 'update_themes' );
542 if ( ! empty( $update_themes->response ) )
543 $counts['themes'] = count( $update_themes->response );
546 if ( ( $core = current_user_can( 'update_core' ) ) && function_exists( 'get_core_updates' ) ) {
547 $update_wordpress = get_core_updates( array('dismissed' => false) );
548 if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') )
549 $counts['wordpress'] = 1;
552 if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() )
553 $counts['translations'] = 1;
555 $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
557 if ( $counts['wordpress'] )
558 $titles['wordpress'] = sprintf( __( '%d WordPress Update'), $counts['wordpress'] );
559 if ( $counts['plugins'] )
560 $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
561 if ( $counts['themes'] )
562 $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
563 if ( $counts['translations'] )
564 $titles['translations'] = __( 'Translation Updates' );
566 $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
568 $update_data = array( 'counts' => $counts, 'title' => $update_title );
570 * Filter the returned array of update data for plugins, themes, and WordPress core.
574 * @param array $update_data {
575 * Fetched update data.
577 * @type array $counts An array of counts for available plugin, theme, and WordPress updates.
578 * @type string $update_title Titles of available updates.
580 * @param array $titles An array of update counts and UI strings for available updates.
582 return apply_filters( 'wp_get_update_data', $update_data, $titles );
586 * @global string $wp_version
588 function _maybe_update_core() {
590 include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
592 $current = get_site_transient( 'update_core' );
594 if ( isset( $current->last_checked, $current->version_checked ) &&
595 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
596 $current->version_checked == $wp_version ) {
602 * Check the last time plugins were run before checking plugin versions.
604 * This might have been backported to WordPress 2.6.1 for performance reasons.
605 * This is used for the wp-admin to check only so often instead of every page
611 function _maybe_update_plugins() {
612 $current = get_site_transient( 'update_plugins' );
613 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
619 * Check themes versions only after a duration of time.
621 * This is for performance reasons to make sure that on the theme version
622 * checker is not run on every page load.
627 function _maybe_update_themes() {
628 $current = get_site_transient( 'update_themes' );
629 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
635 * Schedule core, theme, and plugin update checks.
639 function wp_schedule_update_checks() {
640 if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() )
641 wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
643 if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() )
644 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
646 if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() )
647 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
649 if ( ( wp_next_scheduled( 'wp_maybe_auto_update' ) > ( time() + HOUR_IN_SECONDS ) ) && ! wp_installing() )
650 wp_clear_scheduled_hook( 'wp_maybe_auto_update' );
654 * Clear existing update caches for plugins, themes, and core.
658 function wp_clean_update_cache() {
659 if ( function_exists( 'wp_clean_plugins_cache' ) ) {
660 wp_clean_plugins_cache();
662 delete_site_transient( 'update_plugins' );
664 wp_clean_themes_cache();
665 delete_site_transient( 'update_core' );
668 if ( ( ! is_main_site() && ! is_network_admin() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
672 add_action( 'admin_init', '_maybe_update_core' );
673 add_action( 'wp_version_check', 'wp_version_check' );
674 add_action( 'upgrader_process_complete', 'wp_version_check', 10, 0 );
676 add_action( 'load-plugins.php', 'wp_update_plugins' );
677 add_action( 'load-update.php', 'wp_update_plugins' );
678 add_action( 'load-update-core.php', 'wp_update_plugins' );
679 add_action( 'admin_init', '_maybe_update_plugins' );
680 add_action( 'wp_update_plugins', 'wp_update_plugins' );
681 add_action( 'upgrader_process_complete', 'wp_update_plugins', 10, 0 );
683 add_action( 'load-themes.php', 'wp_update_themes' );
684 add_action( 'load-update.php', 'wp_update_themes' );
685 add_action( 'load-update-core.php', 'wp_update_themes' );
686 add_action( 'admin_init', '_maybe_update_themes' );
687 add_action( 'wp_update_themes', 'wp_update_themes' );
688 add_action( 'upgrader_process_complete', 'wp_update_themes', 10, 0 );
690 add_action( 'update_option_WPLANG', 'wp_clean_update_cache' , 10, 0 );
692 add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
694 add_action( 'init', 'wp_schedule_update_checks' );