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 * @uses $wp_version Used to check against the newest WordPress version.
19 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
20 * @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.
21 * @return null|false Returns null if update is unsupported. Returns false if check is too soon.
23 function wp_version_check( $extra_stats = array(), $force_check = false ) {
24 if ( defined('WP_INSTALLING') )
27 global $wpdb, $wp_local_package;
28 include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
29 $php_version = phpversion();
31 $current = get_site_transient( 'update_core' );
32 $translations = wp_get_installed_translations( 'core' );
34 // Invalidate the transient when $wp_version changes
35 if ( is_object( $current ) && $wp_version != $current->version_checked )
38 if ( ! is_object($current) ) {
39 $current = new stdClass;
40 $current->updates = array();
41 $current->version_checked = $wp_version;
44 if ( ! empty( $extra_stats ) )
47 // Wait 60 seconds between multiple version check requests
49 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
50 if ( ! $force_check && $time_not_changed )
53 $locale = get_locale();
55 * Filter the locale requested for WordPress core translations.
59 * @param string $locale Current locale.
61 $locale = apply_filters( 'core_version_check_locale', $locale );
63 // Update last_checked for current to prevent multiple blocking requests if request hangs
64 $current->last_checked = time();
65 set_site_transient( 'update_core', $current );
67 if ( method_exists( $wpdb, 'db_version' ) )
68 $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
70 $mysql_version = 'N/A';
72 if ( is_multisite() ) {
73 $user_count = get_user_count();
74 $num_blogs = get_blog_count();
75 $wp_install = network_site_url();
76 $multisite_enabled = 1;
78 $user_count = count_users();
79 $user_count = $user_count['total_users'];
80 $multisite_enabled = 0;
82 $wp_install = home_url( '/' );
86 'version' => $wp_version,
87 'php' => $php_version,
89 'mysql' => $mysql_version,
90 'local_package' => isset( $wp_local_package ) ? $wp_local_package : '',
91 'blogs' => $num_blogs,
92 'users' => $user_count,
93 'multisite_enabled' => $multisite_enabled,
97 'translations' => wp_json_encode( $translations ),
100 if ( is_array( $extra_stats ) )
101 $post_body = array_merge( $post_body, $extra_stats );
103 $url = $http_url = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, null, '&' );
104 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
105 $url = set_url_scheme( $url, 'https' );
108 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
109 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
111 'wp_install' => $wp_install,
112 'wp_blog' => home_url( '/' )
114 'body' => $post_body,
117 $response = wp_remote_post( $url, $options );
118 if ( $ssl && is_wp_error( $response ) ) {
119 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 );
120 $response = wp_remote_post( $http_url, $options );
123 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
126 $body = trim( wp_remote_retrieve_body( $response ) );
127 $body = json_decode( $body, true );
129 if ( ! is_array( $body ) || ! isset( $body['offers'] ) )
132 $offers = $body['offers'];
134 foreach ( $offers as &$offer ) {
135 foreach ( $offer as $offer_key => $value ) {
136 if ( 'packages' == $offer_key )
137 $offer['packages'] = (object) array_intersect_key( array_map( 'esc_url', $offer['packages'] ),
138 array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' ) );
139 elseif ( 'download' == $offer_key )
140 $offer['download'] = esc_url( $value );
142 $offer[ $offer_key ] = esc_html( $value );
144 $offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',
145 'packages', 'current', 'version', 'php_version', 'mysql_version', 'new_bundled', 'partial_version', 'notify_email', 'support_email', 'new_files' ), '' ) );
148 $updates = new stdClass();
149 $updates->updates = $offers;
150 $updates->last_checked = time();
151 $updates->version_checked = $wp_version;
153 if ( isset( $body['translations'] ) )
154 $updates->translations = $body['translations'];
156 set_site_transient( 'update_core', $updates );
158 if ( ! empty( $body['ttl'] ) ) {
159 $ttl = (int) $body['ttl'];
160 if ( $ttl && ( time() + $ttl < wp_next_scheduled( 'wp_version_check' ) ) ) {
161 // Queue an event to re-run the update check in $ttl seconds.
162 wp_schedule_single_event( time() + $ttl, 'wp_version_check' );
166 // Trigger a background updates check if running non-interactively, and we weren't called from the update handler.
167 if ( defined( 'DOING_CRON' ) && DOING_CRON && ! doing_action( 'wp_maybe_auto_update' ) ) {
168 do_action( 'wp_maybe_auto_update' );
173 * Check plugin versions against the latest versions hosted on WordPress.org.
175 * The WordPress version, PHP version, and Locale is sent along with a list of
176 * all plugins installed. Checks against the WordPress server at
177 * api.wordpress.org. Will only check if WordPress isn't installing.
180 * @uses $wp_version Used to notify the WordPress version.
182 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
183 * @return false|null Returns null if update is unsupported. Returns false if check is too soon.
185 function wp_update_plugins( $extra_stats = array() ) {
186 include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
188 if ( defined('WP_INSTALLING') )
191 // If running blog-side, bail unless we've not checked in the last 12 hours
192 if ( !function_exists( 'get_plugins' ) )
193 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
195 $plugins = get_plugins();
196 $translations = wp_get_installed_translations( 'plugins' );
198 $active = get_option( 'active_plugins', array() );
199 $current = get_site_transient( 'update_plugins' );
200 if ( ! is_object($current) )
201 $current = new stdClass;
203 $new_option = new stdClass;
204 $new_option->last_checked = time();
206 // Check for update on a different schedule, depending on the page.
207 switch ( current_filter() ) {
208 case 'upgrader_process_complete' :
211 case 'load-update-core.php' :
212 $timeout = MINUTE_IN_SECONDS;
214 case 'load-plugins.php' :
215 case 'load-update.php' :
216 $timeout = HOUR_IN_SECONDS;
219 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
222 $timeout = 12 * HOUR_IN_SECONDS;
226 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
228 if ( $time_not_changed && ! $extra_stats ) {
229 $plugin_changed = false;
230 foreach ( $plugins as $file => $p ) {
231 $new_option->checked[ $file ] = $p['Version'];
233 if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
234 $plugin_changed = true;
237 if ( isset ( $current->response ) && is_array( $current->response ) ) {
238 foreach ( $current->response as $plugin_file => $update_details ) {
239 if ( ! isset($plugins[ $plugin_file ]) ) {
240 $plugin_changed = true;
246 // Bail if we've checked recently and if nothing has changed
247 if ( ! $plugin_changed )
251 // Update last_checked for current to prevent multiple blocking requests if request hangs
252 $current->last_checked = time();
253 set_site_transient( 'update_plugins', $current );
255 $to_send = compact( 'plugins', 'active' );
257 $locales = array( get_locale() );
259 * Filter the locales requested for plugin translations.
263 * @param array $locales Plugin locale. Default is current locale of the site.
265 $locales = apply_filters( 'plugins_update_check_locales', $locales );
267 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
270 // Three seconds, plus one extra second for every 10 plugins
271 $timeout = 3 + (int) ( count( $plugins ) / 10 );
275 'timeout' => $timeout,
277 'plugins' => wp_json_encode( $to_send ),
278 'translations' => wp_json_encode( $translations ),
279 'locale' => wp_json_encode( $locales ),
280 'all' => wp_json_encode( true ),
282 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
285 if ( $extra_stats ) {
286 $options['body']['update_stats'] = wp_json_encode( $extra_stats );
289 $url = $http_url = 'http://api.wordpress.org/plugins/update-check/1.1/';
290 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
291 $url = set_url_scheme( $url, 'https' );
293 $raw_response = wp_remote_post( $url, $options );
294 if ( $ssl && is_wp_error( $raw_response ) ) {
295 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 );
296 $raw_response = wp_remote_post( $http_url, $options );
299 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
302 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
303 foreach ( $response['plugins'] as &$plugin ) {
304 $plugin = (object) $plugin;
307 foreach ( $response['no_update'] as &$plugin ) {
308 $plugin = (object) $plugin;
312 if ( is_array( $response ) ) {
313 $new_option->response = $response['plugins'];
314 $new_option->translations = $response['translations'];
315 // TODO: Perhaps better to store no_update in a separate transient with an expiry?
316 $new_option->no_update = $response['no_update'];
318 $new_option->response = array();
319 $new_option->translations = array();
320 $new_option->no_update = array();
323 set_site_transient( 'update_plugins', $new_option );
327 * Check theme versions against the latest versions hosted on WordPress.org.
329 * A list of all themes installed in sent to WP. Checks against the
330 * WordPress server at api.wordpress.org. Will only check if WordPress isn't
334 * @uses $wp_version Used to notify the WordPress version.
336 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
337 * @return false|null Returns null if update is unsupported. Returns false if check is too soon.
339 function wp_update_themes( $extra_stats = array() ) {
340 include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
342 if ( defined( 'WP_INSTALLING' ) )
345 $installed_themes = wp_get_themes();
346 $translations = wp_get_installed_translations( 'themes' );
348 $last_update = get_site_transient( 'update_themes' );
349 if ( ! is_object($last_update) )
350 $last_update = new stdClass;
352 $themes = $checked = $request = array();
354 // Put slug of current theme into request.
355 $request['active'] = get_option( 'stylesheet' );
357 foreach ( $installed_themes as $theme ) {
358 $checked[ $theme->get_stylesheet() ] = $theme->get('Version');
360 $themes[ $theme->get_stylesheet() ] = array(
361 'Name' => $theme->get('Name'),
362 'Title' => $theme->get('Name'),
363 'Version' => $theme->get('Version'),
364 'Author' => $theme->get('Author'),
365 'Author URI' => $theme->get('AuthorURI'),
366 'Template' => $theme->get_template(),
367 'Stylesheet' => $theme->get_stylesheet(),
371 // Check for update on a different schedule, depending on the page.
372 switch ( current_filter() ) {
373 case 'upgrader_process_complete' :
376 case 'load-update-core.php' :
377 $timeout = MINUTE_IN_SECONDS;
379 case 'load-themes.php' :
380 case 'load-update.php' :
381 $timeout = HOUR_IN_SECONDS;
384 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
387 $timeout = 12 * HOUR_IN_SECONDS;
391 $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
393 if ( $time_not_changed && ! $extra_stats ) {
394 $theme_changed = false;
395 foreach ( $checked as $slug => $v ) {
396 if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
397 $theme_changed = true;
400 if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
401 foreach ( $last_update->response as $slug => $update_details ) {
402 if ( ! isset($checked[ $slug ]) ) {
403 $theme_changed = true;
409 // Bail if we've checked recently and if nothing has changed
410 if ( ! $theme_changed )
414 // Update last_checked for current to prevent multiple blocking requests if request hangs
415 $last_update->last_checked = time();
416 set_site_transient( 'update_themes', $last_update );
418 $request['themes'] = $themes;
420 $locales = array( get_locale() );
422 * Filter the locales requested for theme translations.
426 * @param array $locales Theme locale. Default is current locale of the site.
428 $locales = apply_filters( 'themes_update_check_locales', $locales );
430 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
433 // Three seconds, plus one extra second for every 10 themes
434 $timeout = 3 + (int) ( count( $themes ) / 10 );
438 'timeout' => $timeout,
440 'themes' => wp_json_encode( $request ),
441 'translations' => wp_json_encode( $translations ),
442 'locale' => wp_json_encode( $locales ),
444 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
447 if ( $extra_stats ) {
448 $options['body']['update_stats'] = wp_json_encode( $extra_stats );
451 $url = $http_url = 'http://api.wordpress.org/themes/update-check/1.1/';
452 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
453 $url = set_url_scheme( $url, 'https' );
455 $raw_response = wp_remote_post( $url, $options );
456 if ( $ssl && is_wp_error( $raw_response ) ) {
457 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 );
458 $raw_response = wp_remote_post( $http_url, $options );
461 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
464 $new_update = new stdClass;
465 $new_update->last_checked = time();
466 $new_update->checked = $checked;
468 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
470 if ( is_array( $response ) ) {
471 $new_update->response = $response['themes'];
472 $new_update->translations = $response['translations'];
475 set_site_transient( 'update_themes', $new_update );
479 * Performs WordPress automatic background updates.
483 function wp_maybe_auto_update() {
484 include_once( ABSPATH . '/wp-admin/includes/admin.php' );
485 include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' );
487 $upgrader = new WP_Automatic_Updater;
492 * Retrieves a list of all language updates available.
496 function wp_get_translation_updates() {
498 $transients = array( 'update_core' => 'core', 'update_plugins' => 'plugin', 'update_themes' => 'theme' );
499 foreach ( $transients as $transient => $type ) {
501 $transient = get_site_transient( $transient );
502 if ( empty( $transient->translations ) )
505 foreach ( $transient->translations as $translation ) {
506 $updates[] = (object) $translation;
514 * Collect counts and UI strings for available updates
520 function wp_get_update_data() {
521 $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0, 'translations' => 0 );
523 if ( $plugins = current_user_can( 'update_plugins' ) ) {
524 $update_plugins = get_site_transient( 'update_plugins' );
525 if ( ! empty( $update_plugins->response ) )
526 $counts['plugins'] = count( $update_plugins->response );
529 if ( $themes = current_user_can( 'update_themes' ) ) {
530 $update_themes = get_site_transient( 'update_themes' );
531 if ( ! empty( $update_themes->response ) )
532 $counts['themes'] = count( $update_themes->response );
535 if ( ( $core = current_user_can( 'update_core' ) ) && function_exists( 'get_core_updates' ) ) {
536 $update_wordpress = get_core_updates( array('dismissed' => false) );
537 if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') )
538 $counts['wordpress'] = 1;
541 if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() )
542 $counts['translations'] = 1;
544 $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
546 if ( $counts['wordpress'] )
547 $titles['wordpress'] = sprintf( __( '%d WordPress Update'), $counts['wordpress'] );
548 if ( $counts['plugins'] )
549 $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
550 if ( $counts['themes'] )
551 $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
552 if ( $counts['translations'] )
553 $titles['translations'] = __( 'Translation Updates' );
555 $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
557 $update_data = array( 'counts' => $counts, 'title' => $update_title );
559 * Filter the returned array of update data for plugins, themes, and WordPress core.
563 * @param array $update_data {
564 * Fetched update data.
566 * @type array $counts An array of counts for available plugin, theme, and WordPress updates.
567 * @type string $update_title Titles of available updates.
569 * @param array $titles An array of update counts and UI strings for available updates.
571 return apply_filters( 'wp_get_update_data', $update_data, $titles );
574 function _maybe_update_core() {
575 include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
577 $current = get_site_transient( 'update_core' );
579 if ( isset( $current->last_checked ) &&
580 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
581 isset( $current->version_checked ) &&
582 $current->version_checked == $wp_version )
588 * Check the last time plugins were run before checking plugin versions.
590 * This might have been backported to WordPress 2.6.1 for performance reasons.
591 * This is used for the wp-admin to check only so often instead of every page
597 function _maybe_update_plugins() {
598 $current = get_site_transient( 'update_plugins' );
599 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
605 * Check themes versions only after a duration of time.
607 * This is for performance reasons to make sure that on the theme version
608 * checker is not run on every page load.
613 function _maybe_update_themes() {
614 $current = get_site_transient( 'update_themes' );
615 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
622 * Schedule core, theme, and plugin update checks.
626 function wp_schedule_update_checks() {
627 if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
628 wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
630 if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
631 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
633 if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
634 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
636 if ( ! wp_next_scheduled( 'wp_maybe_auto_update' ) && ! defined( 'WP_INSTALLING' ) ) {
637 // Schedule auto updates for 7 a.m. and 7 p.m. in the timezone of the site.
638 $next = strtotime( 'today 7am' );
640 // Find the next instance of 7 a.m. or 7 p.m., but skip it if it is within 3 hours from now.
641 while ( ( $now + 3 * HOUR_IN_SECONDS ) > $next ) {
642 $next += 12 * HOUR_IN_SECONDS;
644 $next = $next - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
645 // Add a random number of minutes, so we don't have all sites trying to update exactly on the hour
646 $next = $next + rand( 0, 59 ) * MINUTE_IN_SECONDS;
647 wp_schedule_event( $next, 'twicedaily', 'wp_maybe_auto_update' );
652 * Clear existing update caches for plugins, themes, and core.
656 function wp_clean_update_cache() {
657 if ( function_exists( 'wp_clean_plugins_cache' ) ) {
658 wp_clean_plugins_cache();
660 delete_site_transient( 'update_plugins' );
662 wp_clean_themes_cache();
663 delete_site_transient( 'update_core' );
666 if ( ( ! is_main_site() && ! is_network_admin() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
670 add_action( 'admin_init', '_maybe_update_core' );
671 add_action( 'wp_version_check', 'wp_version_check' );
672 add_action( 'upgrader_process_complete', 'wp_version_check', 10, 0 );
674 add_action( 'load-plugins.php', 'wp_update_plugins' );
675 add_action( 'load-update.php', 'wp_update_plugins' );
676 add_action( 'load-update-core.php', 'wp_update_plugins' );
677 add_action( 'admin_init', '_maybe_update_plugins' );
678 add_action( 'wp_update_plugins', 'wp_update_plugins' );
679 add_action( 'upgrader_process_complete', 'wp_update_plugins', 10, 0 );
681 add_action( 'load-themes.php', 'wp_update_themes' );
682 add_action( 'load-update.php', 'wp_update_themes' );
683 add_action( 'load-update-core.php', 'wp_update_themes' );
684 add_action( 'admin_init', '_maybe_update_themes' );
685 add_action( 'wp_update_themes', 'wp_update_themes' );
686 add_action( 'upgrader_process_complete', 'wp_update_themes', 10, 0 );
688 add_action( 'update_option_WPLANG', 'wp_clean_update_cache' , 10, 0 );
690 add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
692 add_action( 'init', 'wp_schedule_update_checks' );