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
18 * @uses $wp_version Used to check against the newest WordPress version.
20 * @param array $extra_stats Extra statistics to report to the WordPress.org API.
21 * @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.
22 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
24 function wp_version_check( $extra_stats = array(), $force_check = false ) {
25 if ( defined('WP_INSTALLING') )
28 global $wpdb, $wp_local_package;
29 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
30 $php_version = phpversion();
32 $current = get_site_transient( 'update_core' );
33 $translations = wp_get_installed_translations( 'core' );
35 // Invalidate the transient when $wp_version changes
36 if ( is_object( $current ) && $wp_version != $current->version_checked )
39 if ( ! is_object($current) ) {
40 $current = new stdClass;
41 $current->updates = array();
42 $current->version_checked = $wp_version;
45 if ( ! empty( $extra_stats ) )
48 // Wait 60 seconds between multiple version check requests
50 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
51 if ( ! $force_check && $time_not_changed )
54 $locale = get_locale();
56 * Filter the locale requested for WordPress core translations.
60 * @param string $locale Current locale.
62 $locale = apply_filters( 'core_version_check_locale', $locale );
64 // Update last_checked for current to prevent multiple blocking requests if request hangs
65 $current->last_checked = time();
66 set_site_transient( 'update_core', $current );
68 if ( method_exists( $wpdb, 'db_version' ) )
69 $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
71 $mysql_version = 'N/A';
73 if ( is_multisite() ) {
74 $user_count = get_user_count();
75 $num_blogs = get_blog_count();
76 $wp_install = network_site_url();
77 $multisite_enabled = 1;
79 $user_count = count_users();
80 $user_count = $user_count['total_users'];
81 $multisite_enabled = 0;
83 $wp_install = home_url( '/' );
87 'version' => $wp_version,
88 'php' => $php_version,
90 'mysql' => $mysql_version,
91 'local_package' => isset( $wp_local_package ) ? $wp_local_package : '',
92 'blogs' => $num_blogs,
93 'users' => $user_count,
94 'multisite_enabled' => $multisite_enabled,
98 'translations' => json_encode( $translations ),
101 if ( is_array( $extra_stats ) )
102 $post_body = array_merge( $post_body, $extra_stats );
104 $url = $http_url = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, null, '&' );
105 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
106 $url = set_url_scheme( $url, 'https' );
109 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
110 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
112 'wp_install' => $wp_install,
113 'wp_blog' => home_url( '/' )
115 'body' => $post_body,
118 $response = wp_remote_post( $url, $options );
119 if ( $ssl && is_wp_error( $response ) ) {
120 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="http://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 );
121 $response = wp_remote_post( $http_url, $options );
124 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
127 $body = trim( wp_remote_retrieve_body( $response ) );
128 $body = json_decode( $body, true );
130 if ( ! is_array( $body ) || ! isset( $body['offers'] ) )
133 $offers = $body['offers'];
135 foreach ( $offers as &$offer ) {
136 foreach ( $offer as $offer_key => $value ) {
137 if ( 'packages' == $offer_key )
138 $offer['packages'] = (object) array_intersect_key( array_map( 'esc_url', $offer['packages'] ),
139 array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' ) );
140 elseif ( 'download' == $offer_key )
141 $offer['download'] = esc_url( $value );
143 $offer[ $offer_key ] = esc_html( $value );
145 $offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',
146 'packages', 'current', 'version', 'php_version', 'mysql_version', 'new_bundled', 'partial_version', 'notify_email' ), '' ) );
149 $updates = new stdClass();
150 $updates->updates = $offers;
151 $updates->last_checked = time();
152 $updates->version_checked = $wp_version;
154 if ( isset( $body['translations'] ) )
155 $updates->translations = $body['translations'];
157 set_site_transient( 'update_core', $updates);
161 * Check plugin versions against the latest versions hosted on WordPress.org.
163 * The WordPress version, PHP version, and Locale is sent along with a list of
164 * all plugins installed. Checks against the WordPress server at
165 * api.wordpress.org. Will only check if WordPress isn't installing.
169 * @uses $wp_version Used to notify the WordPress version.
171 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
173 function wp_update_plugins() {
174 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
176 if ( defined('WP_INSTALLING') )
179 // If running blog-side, bail unless we've not checked in the last 12 hours
180 if ( !function_exists( 'get_plugins' ) )
181 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
183 $plugins = get_plugins();
184 $translations = wp_get_installed_translations( 'plugins' );
186 $active = get_option( 'active_plugins', array() );
187 $current = get_site_transient( 'update_plugins' );
188 if ( ! is_object($current) )
189 $current = new stdClass;
191 $new_option = new stdClass;
192 $new_option->last_checked = time();
194 // Check for update on a different schedule, depending on the page.
195 switch ( current_filter() ) {
196 case 'upgrader_process_complete' :
199 case 'load-update-core.php' :
200 $timeout = MINUTE_IN_SECONDS;
202 case 'load-plugins.php' :
203 case 'load-update.php' :
204 $timeout = HOUR_IN_SECONDS;
207 $timeout = 12 * HOUR_IN_SECONDS;
210 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
212 if ( $time_not_changed ) {
213 $plugin_changed = false;
214 foreach ( $plugins as $file => $p ) {
215 $new_option->checked[ $file ] = $p['Version'];
217 if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
218 $plugin_changed = true;
221 if ( isset ( $current->response ) && is_array( $current->response ) ) {
222 foreach ( $current->response as $plugin_file => $update_details ) {
223 if ( ! isset($plugins[ $plugin_file ]) ) {
224 $plugin_changed = true;
230 // Bail if we've checked recently and if nothing has changed
231 if ( ! $plugin_changed )
235 // Update last_checked for current to prevent multiple blocking requests if request hangs
236 $current->last_checked = time();
237 set_site_transient( 'update_plugins', $current );
239 $to_send = compact( 'plugins', 'active' );
241 $locales = array( get_locale() );
243 * Filter the locales requested for plugin translations.
247 * @param array $locales Plugin locale. Default is current locale of the site.
249 $locales = apply_filters( 'plugins_update_check_locales', $locales );
252 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
254 'plugins' => json_encode( $to_send ),
255 'translations' => json_encode( $translations ),
256 'locale' => json_encode( $locales ),
258 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
261 $url = $http_url = 'http://api.wordpress.org/plugins/update-check/1.1/';
262 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
263 $url = set_url_scheme( $url, 'https' );
265 $raw_response = wp_remote_post( $url, $options );
266 if ( $ssl && is_wp_error( $raw_response ) ) {
267 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="http://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 );
268 $raw_response = wp_remote_post( $http_url, $options );
271 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
274 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
275 foreach ( $response['plugins'] as &$plugin ) {
276 $plugin = (object) $plugin;
280 if ( is_array( $response ) ) {
281 $new_option->response = $response['plugins'];
282 $new_option->translations = $response['translations'];
284 $new_option->response = array();
285 $new_option->translations = array();
288 set_site_transient( 'update_plugins', $new_option );
292 * Check theme versions against the latest versions hosted on WordPress.org.
294 * A list of all themes installed in sent to WP. Checks against the
295 * WordPress server at api.wordpress.org. Will only check if WordPress isn't
300 * @uses $wp_version Used to notify the WordPress version.
302 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
304 function wp_update_themes() {
305 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
307 if ( defined( 'WP_INSTALLING' ) )
310 $installed_themes = wp_get_themes();
311 $translations = wp_get_installed_translations( 'themes' );
313 $last_update = get_site_transient( 'update_themes' );
314 if ( ! is_object($last_update) )
315 $last_update = new stdClass;
317 $themes = $checked = $request = array();
319 // Put slug of current theme into request.
320 $request['active'] = get_option( 'stylesheet' );
322 foreach ( $installed_themes as $theme ) {
323 $checked[ $theme->get_stylesheet() ] = $theme->get('Version');
325 $themes[ $theme->get_stylesheet() ] = array(
326 'Name' => $theme->get('Name'),
327 'Title' => $theme->get('Name'),
328 'Version' => $theme->get('Version'),
329 'Author' => $theme->get('Author'),
330 'Author URI' => $theme->get('AuthorURI'),
331 'Template' => $theme->get_template(),
332 'Stylesheet' => $theme->get_stylesheet(),
336 // Check for update on a different schedule, depending on the page.
337 switch ( current_filter() ) {
338 case 'upgrader_process_complete' :
341 case 'load-update-core.php' :
342 $timeout = MINUTE_IN_SECONDS;
344 case 'load-themes.php' :
345 case 'load-update.php' :
346 $timeout = HOUR_IN_SECONDS;
349 $timeout = 12 * HOUR_IN_SECONDS;
352 $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
354 if ( $time_not_changed ) {
355 $theme_changed = false;
356 foreach ( $checked as $slug => $v ) {
357 if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
358 $theme_changed = true;
361 if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
362 foreach ( $last_update->response as $slug => $update_details ) {
363 if ( ! isset($checked[ $slug ]) ) {
364 $theme_changed = true;
370 // Bail if we've checked recently and if nothing has changed
371 if ( ! $theme_changed )
375 // Update last_checked for current to prevent multiple blocking requests if request hangs
376 $last_update->last_checked = time();
377 set_site_transient( 'update_themes', $last_update );
379 $request['themes'] = $themes;
381 $locales = array( get_locale() );
383 * Filter the locales requested for theme translations.
387 * @param array $locales Theme locale. Default is current locale of the site.
389 $locales = apply_filters( 'themes_update_check_locales', $locales );
392 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
394 'themes' => json_encode( $request ),
395 'translations' => json_encode( $translations ),
396 'locale' => json_encode( $locales ),
398 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
401 $url = $http_url = 'http://api.wordpress.org/themes/update-check/1.1/';
402 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
403 $url = set_url_scheme( $url, 'https' );
405 $raw_response = wp_remote_post( $url, $options );
406 if ( $ssl && is_wp_error( $raw_response ) ) {
407 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="http://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 );
408 $raw_response = wp_remote_post( $http_url, $options );
411 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
414 $new_update = new stdClass;
415 $new_update->last_checked = time();
416 $new_update->checked = $checked;
418 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
420 if ( is_array( $response ) ) {
421 $new_update->response = $response['themes'];
422 $new_update->translations = $response['translations'];
425 set_site_transient( 'update_themes', $new_update );
429 * Performs WordPress automatic background updates.
433 function wp_maybe_auto_update() {
434 include_once ABSPATH . '/wp-admin/includes/admin.php';
435 include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
437 $upgrader = new WP_Automatic_Updater;
442 * Retrieves a list of all language updates available.
446 function wp_get_translation_updates() {
448 $transients = array( 'update_core' => 'core', 'update_plugins' => 'plugin', 'update_themes' => 'theme' );
449 foreach ( $transients as $transient => $type ) {
451 $transient = get_site_transient( $transient );
452 if ( empty( $transient->translations ) )
455 foreach ( $transient->translations as $translation ) {
456 $updates[] = (object) $translation;
464 * Collect counts and UI strings for available updates
470 function wp_get_update_data() {
471 $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0, 'translations' => 0 );
473 if ( $plugins = current_user_can( 'update_plugins' ) ) {
474 $update_plugins = get_site_transient( 'update_plugins' );
475 if ( ! empty( $update_plugins->response ) )
476 $counts['plugins'] = count( $update_plugins->response );
479 if ( $themes = current_user_can( 'update_themes' ) ) {
480 $update_themes = get_site_transient( 'update_themes' );
481 if ( ! empty( $update_themes->response ) )
482 $counts['themes'] = count( $update_themes->response );
485 if ( ( $core = current_user_can( 'update_core' ) ) && function_exists( 'get_core_updates' ) ) {
486 $update_wordpress = get_core_updates( array('dismissed' => false) );
487 if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') )
488 $counts['wordpress'] = 1;
491 if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() )
492 $counts['translations'] = 1;
494 $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
496 if ( $counts['wordpress'] )
497 $titles['wordpress'] = sprintf( __( '%d WordPress Update'), $counts['wordpress'] );
498 if ( $counts['plugins'] )
499 $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
500 if ( $counts['themes'] )
501 $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
502 if ( $counts['translations'] )
503 $titles['translations'] = __( 'Translation Updates' );
505 $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
507 $update_data = array( 'counts' => $counts, 'title' => $update_title );
509 * Filter the returned array of update data for plugins, themes, and WordPress core.
513 * @param array $update_data {
514 * Fetched update data.
516 * @type array $counts An array of counts for available plugin, theme, and WordPress updates.
517 * @type string $update_title Titles of available updates.
519 * @param array $titles An array of update counts and UI strings for available updates.
521 return apply_filters( 'wp_get_update_data', $update_data, $titles );
524 function _maybe_update_core() {
525 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
527 $current = get_site_transient( 'update_core' );
529 if ( isset( $current->last_checked ) &&
530 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
531 isset( $current->version_checked ) &&
532 $current->version_checked == $wp_version )
538 * Check the last time plugins were run before checking plugin versions.
540 * This might have been backported to WordPress 2.6.1 for performance reasons.
541 * This is used for the wp-admin to check only so often instead of every page
547 function _maybe_update_plugins() {
548 $current = get_site_transient( 'update_plugins' );
549 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
555 * Check themes versions only after a duration of time.
557 * This is for performance reasons to make sure that on the theme version
558 * checker is not run on every page load.
563 function _maybe_update_themes() {
564 $current = get_site_transient( 'update_themes' );
565 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
572 * Schedule core, theme, and plugin update checks.
576 function wp_schedule_update_checks() {
577 if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
578 wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
580 if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
581 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
583 if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
584 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
586 if ( ! wp_next_scheduled( 'wp_maybe_auto_update' ) && ! defined( 'WP_INSTALLING' ) ) {
587 // Schedule auto updates for 7 a.m. and 7 p.m. in the timezone of the site.
588 $next = strtotime( 'today 7am' );
590 // Find the next instance of 7 a.m. or 7 p.m., but skip it if it is within 3 hours from now.
591 while ( ( $now + 3 * HOUR_IN_SECONDS ) > $next ) {
592 $next += 12 * HOUR_IN_SECONDS;
594 $next = $next - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
595 // Add a random number of minutes, so we don't have all sites trying to update exactly on the hour
596 $next = $next + rand( 0, 59 ) * MINUTE_IN_SECONDS;
597 wp_schedule_event( $next, 'twicedaily', 'wp_maybe_auto_update' );
601 if ( ( ! is_main_site() && ! is_network_admin() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) )
604 add_action( 'admin_init', '_maybe_update_core' );
605 add_action( 'wp_version_check', 'wp_version_check' );
606 add_action( 'upgrader_process_complete', 'wp_version_check', 10, 0 );
608 add_action( 'load-plugins.php', 'wp_update_plugins' );
609 add_action( 'load-update.php', 'wp_update_plugins' );
610 add_action( 'load-update-core.php', 'wp_update_plugins' );
611 add_action( 'admin_init', '_maybe_update_plugins' );
612 add_action( 'wp_update_plugins', 'wp_update_plugins' );
613 add_action( 'upgrader_process_complete', 'wp_update_plugins' );
615 add_action( 'load-themes.php', 'wp_update_themes' );
616 add_action( 'load-update.php', 'wp_update_themes' );
617 add_action( 'load-update-core.php', 'wp_update_themes' );
618 add_action( 'admin_init', '_maybe_update_themes' );
619 add_action( 'wp_update_themes', 'wp_update_themes' );
620 add_action( 'upgrader_process_complete', 'wp_update_themes' );
622 add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
624 add_action('init', 'wp_schedule_update_checks');