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 ) ) {
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 background updates 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() ) {
195 // include an unmodified $wp_version
196 include( ABSPATH . WPINC . '/version.php' );
198 // If running blog-side, bail unless we've not checked in the last 12 hours
199 if ( !function_exists( 'get_plugins' ) )
200 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
202 $plugins = get_plugins();
203 $translations = wp_get_installed_translations( 'plugins' );
205 $active = get_option( 'active_plugins', array() );
206 $current = get_site_transient( 'update_plugins' );
207 if ( ! is_object($current) )
208 $current = new stdClass;
210 $new_option = new stdClass;
211 $new_option->last_checked = time();
213 // Check for update on a different schedule, depending on the page.
214 switch ( current_filter() ) {
215 case 'upgrader_process_complete' :
218 case 'load-update-core.php' :
219 $timeout = MINUTE_IN_SECONDS;
221 case 'load-plugins.php' :
222 case 'load-update.php' :
223 $timeout = HOUR_IN_SECONDS;
226 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
229 $timeout = 12 * HOUR_IN_SECONDS;
233 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
235 if ( $time_not_changed && ! $extra_stats ) {
236 $plugin_changed = false;
237 foreach ( $plugins as $file => $p ) {
238 $new_option->checked[ $file ] = $p['Version'];
240 if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
241 $plugin_changed = true;
244 if ( isset ( $current->response ) && is_array( $current->response ) ) {
245 foreach ( $current->response as $plugin_file => $update_details ) {
246 if ( ! isset($plugins[ $plugin_file ]) ) {
247 $plugin_changed = true;
253 // Bail if we've checked recently and if nothing has changed
254 if ( ! $plugin_changed ) {
259 // Update last_checked for current to prevent multiple blocking requests if request hangs
260 $current->last_checked = time();
261 set_site_transient( 'update_plugins', $current );
263 $to_send = compact( 'plugins', 'active' );
265 $locales = array_values( get_available_languages() );
268 * Filters the locales requested for plugin translations.
271 * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
273 * @param array $locales Plugin locales. Default is all available locales of the site.
275 $locales = apply_filters( 'plugins_update_check_locales', $locales );
276 $locales = array_unique( $locales );
278 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
281 // Three seconds, plus one extra second for every 10 plugins
282 $timeout = 3 + (int) ( count( $plugins ) / 10 );
286 'timeout' => $timeout,
288 'plugins' => wp_json_encode( $to_send ),
289 'translations' => wp_json_encode( $translations ),
290 'locale' => wp_json_encode( $locales ),
291 'all' => wp_json_encode( true ),
293 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
296 if ( $extra_stats ) {
297 $options['body']['update_stats'] = wp_json_encode( $extra_stats );
300 $url = $http_url = 'http://api.wordpress.org/plugins/update-check/1.1/';
301 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
302 $url = set_url_scheme( $url, 'https' );
304 $raw_response = wp_remote_post( $url, $options );
305 if ( $ssl && is_wp_error( $raw_response ) ) {
306 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 );
307 $raw_response = wp_remote_post( $http_url, $options );
310 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
314 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
315 foreach ( $response['plugins'] as &$plugin ) {
316 $plugin = (object) $plugin;
317 if ( isset( $plugin->compatibility ) ) {
318 $plugin->compatibility = (object) $plugin->compatibility;
319 foreach ( $plugin->compatibility as &$data ) {
320 $data = (object) $data;
324 unset( $plugin, $data );
325 foreach ( $response['no_update'] as &$plugin ) {
326 $plugin = (object) $plugin;
330 if ( is_array( $response ) ) {
331 $new_option->response = $response['plugins'];
332 $new_option->translations = $response['translations'];
333 // TODO: Perhaps better to store no_update in a separate transient with an expiry?
334 $new_option->no_update = $response['no_update'];
336 $new_option->response = array();
337 $new_option->translations = array();
338 $new_option->no_update = array();
341 set_site_transient( 'update_plugins', $new_option );
345 * Check theme versions against the latest versions hosted on WordPress.org.
347 * A list of all themes installed in sent to WP. Checks against the
348 * WordPress server at api.wordpress.org. Will only check if WordPress isn't
353 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
355 function wp_update_themes( $extra_stats = array() ) {
356 if ( wp_installing() ) {
360 // include an unmodified $wp_version
361 include( ABSPATH . WPINC . '/version.php' );
363 $installed_themes = wp_get_themes();
364 $translations = wp_get_installed_translations( 'themes' );
366 $last_update = get_site_transient( 'update_themes' );
367 if ( ! is_object($last_update) )
368 $last_update = new stdClass;
370 $themes = $checked = $request = array();
372 // Put slug of current theme into request.
373 $request['active'] = get_option( 'stylesheet' );
375 foreach ( $installed_themes as $theme ) {
376 $checked[ $theme->get_stylesheet() ] = $theme->get('Version');
378 $themes[ $theme->get_stylesheet() ] = array(
379 'Name' => $theme->get('Name'),
380 'Title' => $theme->get('Name'),
381 'Version' => $theme->get('Version'),
382 'Author' => $theme->get('Author'),
383 'Author URI' => $theme->get('AuthorURI'),
384 'Template' => $theme->get_template(),
385 'Stylesheet' => $theme->get_stylesheet(),
389 // Check for update on a different schedule, depending on the page.
390 switch ( current_filter() ) {
391 case 'upgrader_process_complete' :
394 case 'load-update-core.php' :
395 $timeout = MINUTE_IN_SECONDS;
397 case 'load-themes.php' :
398 case 'load-update.php' :
399 $timeout = HOUR_IN_SECONDS;
402 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
405 $timeout = 12 * HOUR_IN_SECONDS;
409 $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
411 if ( $time_not_changed && ! $extra_stats ) {
412 $theme_changed = false;
413 foreach ( $checked as $slug => $v ) {
414 if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
415 $theme_changed = true;
418 if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
419 foreach ( $last_update->response as $slug => $update_details ) {
420 if ( ! isset($checked[ $slug ]) ) {
421 $theme_changed = true;
427 // Bail if we've checked recently and if nothing has changed
428 if ( ! $theme_changed ) {
433 // Update last_checked for current to prevent multiple blocking requests if request hangs
434 $last_update->last_checked = time();
435 set_site_transient( 'update_themes', $last_update );
437 $request['themes'] = $themes;
439 $locales = array_values( get_available_languages() );
442 * Filters the locales requested for theme translations.
445 * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
447 * @param array $locales Theme locales. Default is all available locales of the site.
449 $locales = apply_filters( 'themes_update_check_locales', $locales );
450 $locales = array_unique( $locales );
452 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
455 // Three seconds, plus one extra second for every 10 themes
456 $timeout = 3 + (int) ( count( $themes ) / 10 );
460 'timeout' => $timeout,
462 'themes' => wp_json_encode( $request ),
463 'translations' => wp_json_encode( $translations ),
464 'locale' => wp_json_encode( $locales ),
466 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
469 if ( $extra_stats ) {
470 $options['body']['update_stats'] = wp_json_encode( $extra_stats );
473 $url = $http_url = 'http://api.wordpress.org/themes/update-check/1.1/';
474 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
475 $url = set_url_scheme( $url, 'https' );
477 $raw_response = wp_remote_post( $url, $options );
478 if ( $ssl && is_wp_error( $raw_response ) ) {
479 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 );
480 $raw_response = wp_remote_post( $http_url, $options );
483 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
487 $new_update = new stdClass;
488 $new_update->last_checked = time();
489 $new_update->checked = $checked;
491 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
493 if ( is_array( $response ) ) {
494 $new_update->response = $response['themes'];
495 $new_update->translations = $response['translations'];
498 set_site_transient( 'update_themes', $new_update );
502 * Performs WordPress automatic background updates.
506 function wp_maybe_auto_update() {
507 include_once( ABSPATH . '/wp-admin/includes/admin.php' );
508 include_once( ABSPATH . '/wp-admin/includes/class-wp-upgrader.php' );
510 $upgrader = new WP_Automatic_Updater;
515 * Retrieves a list of all language updates available.
521 function wp_get_translation_updates() {
523 $transients = array( 'update_core' => 'core', 'update_plugins' => 'plugin', 'update_themes' => 'theme' );
524 foreach ( $transients as $transient => $type ) {
525 $transient = get_site_transient( $transient );
526 if ( empty( $transient->translations ) )
529 foreach ( $transient->translations as $translation ) {
530 $updates[] = (object) $translation;
537 * Collect counts and UI strings for available updates
543 function wp_get_update_data() {
544 $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0, 'translations' => 0 );
546 if ( $plugins = current_user_can( 'update_plugins' ) ) {
547 $update_plugins = get_site_transient( 'update_plugins' );
548 if ( ! empty( $update_plugins->response ) )
549 $counts['plugins'] = count( $update_plugins->response );
552 if ( $themes = current_user_can( 'update_themes' ) ) {
553 $update_themes = get_site_transient( 'update_themes' );
554 if ( ! empty( $update_themes->response ) )
555 $counts['themes'] = count( $update_themes->response );
558 if ( ( $core = current_user_can( 'update_core' ) ) && function_exists( 'get_core_updates' ) ) {
559 $update_wordpress = get_core_updates( array('dismissed' => false) );
560 if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') )
561 $counts['wordpress'] = 1;
564 if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() )
565 $counts['translations'] = 1;
567 $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
569 if ( $counts['wordpress'] )
570 $titles['wordpress'] = sprintf( __( '%d WordPress Update'), $counts['wordpress'] );
571 if ( $counts['plugins'] )
572 $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
573 if ( $counts['themes'] )
574 $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
575 if ( $counts['translations'] )
576 $titles['translations'] = __( 'Translation Updates' );
578 $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
580 $update_data = array( 'counts' => $counts, 'title' => $update_title );
582 * Filters the returned array of update data for plugins, themes, and WordPress core.
586 * @param array $update_data {
587 * Fetched update data.
589 * @type array $counts An array of counts for available plugin, theme, and WordPress updates.
590 * @type string $update_title Titles of available updates.
592 * @param array $titles An array of update counts and UI strings for available updates.
594 return apply_filters( 'wp_get_update_data', $update_data, $titles );
598 * Determines whether core should be updated.
602 * @global string $wp_version
604 function _maybe_update_core() {
605 // include an unmodified $wp_version
606 include( ABSPATH . WPINC . '/version.php' );
608 $current = get_site_transient( 'update_core' );
610 if ( isset( $current->last_checked, $current->version_checked ) &&
611 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
612 $current->version_checked == $wp_version ) {
618 * Check the last time plugins were run before checking plugin versions.
620 * This might have been backported to WordPress 2.6.1 for performance reasons.
621 * This is used for the wp-admin to check only so often instead of every page
627 function _maybe_update_plugins() {
628 $current = get_site_transient( 'update_plugins' );
629 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
635 * Check themes versions only after a duration of time.
637 * This is for performance reasons to make sure that on the theme version
638 * checker is not run on every page load.
643 function _maybe_update_themes() {
644 $current = get_site_transient( 'update_themes' );
645 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
651 * Schedule core, theme, and plugin update checks.
655 function wp_schedule_update_checks() {
656 if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() )
657 wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
659 if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() )
660 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
662 if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() )
663 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
667 * Clear existing update caches for plugins, themes, and core.
671 function wp_clean_update_cache() {
672 if ( function_exists( 'wp_clean_plugins_cache' ) ) {
673 wp_clean_plugins_cache();
675 delete_site_transient( 'update_plugins' );
677 wp_clean_themes_cache();
678 delete_site_transient( 'update_core' );
681 if ( ( ! is_main_site() && ! is_network_admin() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
685 add_action( 'admin_init', '_maybe_update_core' );
686 add_action( 'wp_version_check', 'wp_version_check' );
688 add_action( 'load-plugins.php', 'wp_update_plugins' );
689 add_action( 'load-update.php', 'wp_update_plugins' );
690 add_action( 'load-update-core.php', 'wp_update_plugins' );
691 add_action( 'admin_init', '_maybe_update_plugins' );
692 add_action( 'wp_update_plugins', 'wp_update_plugins' );
694 add_action( 'load-themes.php', 'wp_update_themes' );
695 add_action( 'load-update.php', 'wp_update_themes' );
696 add_action( 'load-update-core.php', 'wp_update_themes' );
697 add_action( 'admin_init', '_maybe_update_themes' );
698 add_action( 'wp_update_themes', 'wp_update_themes' );
700 add_action( 'update_option_WPLANG', 'wp_clean_update_cache' , 10, 0 );
702 add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
704 add_action( 'init', 'wp_schedule_update_checks' );