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 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
23 function wp_version_check( $extra_stats = array() ) {
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 if ( ! is_object($current) ) {
35 $current = new stdClass;
36 $current->updates = array();
37 $current->version_checked = $wp_version;
40 // Wait 60 seconds between multiple version check requests
42 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
43 if ( $time_not_changed && empty( $extra_stats ) )
46 $locale = get_locale();
48 * Filter the locale requested for WordPress core translations.
52 * @param string $locale Current locale.
54 $locale = apply_filters( 'core_version_check_locale', $locale );
56 // Update last_checked for current to prevent multiple blocking requests if request hangs
57 $current->last_checked = time();
58 set_site_transient( 'update_core', $current );
60 if ( method_exists( $wpdb, 'db_version' ) )
61 $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
63 $mysql_version = 'N/A';
65 if ( is_multisite() ) {
66 $user_count = get_user_count();
67 $num_blogs = get_blog_count();
68 $wp_install = network_site_url();
69 $multisite_enabled = 1;
71 $user_count = count_users();
72 $user_count = $user_count['total_users'];
73 $multisite_enabled = 0;
75 $wp_install = home_url( '/' );
79 'version' => $wp_version,
80 'php' => $php_version,
82 'mysql' => $mysql_version,
83 'local_package' => isset( $wp_local_package ) ? $wp_local_package : '',
84 'blogs' => $num_blogs,
85 'users' => $user_count,
86 'multisite_enabled' => $multisite_enabled,
90 'translations' => json_encode( $translations ),
94 $post_body = array_merge( $post_body, $extra_stats );
96 $url = $http_url = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, null, '&' );
97 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
98 $url = set_url_scheme( $url, 'https' );
101 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
102 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
104 'wp_install' => $wp_install,
105 'wp_blog' => home_url( '/' )
107 'body' => $post_body,
110 $response = wp_remote_post( $url, $options );
111 if ( $ssl && is_wp_error( $response ) ) {
112 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 );
113 $response = wp_remote_post( $http_url, $options );
116 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
119 $body = trim( wp_remote_retrieve_body( $response ) );
120 $body = json_decode( $body, true );
122 if ( ! is_array( $body ) || ! isset( $body['offers'] ) )
125 $offers = $body['offers'];
127 foreach ( $offers as &$offer ) {
128 foreach ( $offer as $offer_key => $value ) {
129 if ( 'packages' == $offer_key )
130 $offer['packages'] = (object) array_intersect_key( array_map( 'esc_url', $offer['packages'] ),
131 array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' ) );
132 elseif ( 'download' == $offer_key )
133 $offer['download'] = esc_url( $value );
135 $offer[ $offer_key ] = esc_html( $value );
137 $offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',
138 'packages', 'current', 'version', 'php_version', 'mysql_version', 'new_bundled', 'partial_version', 'notify_email' ), '' ) );
141 $updates = new stdClass();
142 $updates->updates = $offers;
143 $updates->last_checked = time();
144 $updates->version_checked = $wp_version;
146 if ( isset( $body['translations'] ) )
147 $updates->translations = $body['translations'];
149 set_site_transient( 'update_core', $updates);
153 * Check plugin versions against the latest versions hosted on WordPress.org.
155 * The WordPress version, PHP version, and Locale is sent along with a list of
156 * all plugins installed. Checks against the WordPress server at
157 * api.wordpress.org. Will only check if WordPress isn't installing.
161 * @uses $wp_version Used to notify the WordPress version.
163 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
165 function wp_update_plugins() {
166 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
168 if ( defined('WP_INSTALLING') )
171 // If running blog-side, bail unless we've not checked in the last 12 hours
172 if ( !function_exists( 'get_plugins' ) )
173 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
175 $plugins = get_plugins();
176 $translations = wp_get_installed_translations( 'plugins' );
178 $active = get_option( 'active_plugins', array() );
179 $current = get_site_transient( 'update_plugins' );
180 if ( ! is_object($current) )
181 $current = new stdClass;
183 $new_option = new stdClass;
184 $new_option->last_checked = time();
186 // Check for update on a different schedule, depending on the page.
187 switch ( current_filter() ) {
188 case 'upgrader_process_complete' :
191 case 'load-update-core.php' :
192 $timeout = MINUTE_IN_SECONDS;
194 case 'load-plugins.php' :
195 case 'load-update.php' :
196 $timeout = HOUR_IN_SECONDS;
199 $timeout = 12 * HOUR_IN_SECONDS;
202 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
204 if ( $time_not_changed ) {
205 $plugin_changed = false;
206 foreach ( $plugins as $file => $p ) {
207 $new_option->checked[ $file ] = $p['Version'];
209 if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
210 $plugin_changed = true;
213 if ( isset ( $current->response ) && is_array( $current->response ) ) {
214 foreach ( $current->response as $plugin_file => $update_details ) {
215 if ( ! isset($plugins[ $plugin_file ]) ) {
216 $plugin_changed = true;
222 // Bail if we've checked recently and if nothing has changed
223 if ( ! $plugin_changed )
227 // Update last_checked for current to prevent multiple blocking requests if request hangs
228 $current->last_checked = time();
229 set_site_transient( 'update_plugins', $current );
231 $to_send = compact( 'plugins', 'active' );
233 $locales = array( get_locale() );
235 * Filter the locales requested for plugin translations.
239 * @param array $locales Plugin locale. Default is current locale of the site.
241 $locales = apply_filters( 'plugins_update_check_locales', $locales );
244 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
246 'plugins' => json_encode( $to_send ),
247 'translations' => json_encode( $translations ),
248 'locale' => json_encode( $locales ),
250 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
253 $url = $http_url = 'http://api.wordpress.org/plugins/update-check/1.1/';
254 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
255 $url = set_url_scheme( $url, 'https' );
257 $raw_response = wp_remote_post( $url, $options );
258 if ( $ssl && is_wp_error( $raw_response ) ) {
259 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 );
260 $raw_response = wp_remote_post( $http_url, $options );
263 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
266 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
267 foreach ( $response['plugins'] as &$plugin ) {
268 $plugin = (object) $plugin;
272 if ( is_array( $response ) ) {
273 $new_option->response = $response['plugins'];
274 $new_option->translations = $response['translations'];
276 $new_option->response = array();
277 $new_option->translations = array();
280 set_site_transient( 'update_plugins', $new_option );
284 * Check theme versions against the latest versions hosted on WordPress.org.
286 * A list of all themes installed in sent to WP. Checks against the
287 * WordPress server at api.wordpress.org. Will only check if WordPress isn't
292 * @uses $wp_version Used to notify the WordPress version.
294 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
296 function wp_update_themes() {
297 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
299 if ( defined( 'WP_INSTALLING' ) )
302 $installed_themes = wp_get_themes();
303 $translations = wp_get_installed_translations( 'themes' );
305 $last_update = get_site_transient( 'update_themes' );
306 if ( ! is_object($last_update) )
307 $last_update = new stdClass;
309 $themes = $checked = $request = array();
311 // Put slug of current theme into request.
312 $request['active'] = get_option( 'stylesheet' );
314 foreach ( $installed_themes as $theme ) {
315 $checked[ $theme->get_stylesheet() ] = $theme->get('Version');
317 $themes[ $theme->get_stylesheet() ] = array(
318 'Name' => $theme->get('Name'),
319 'Title' => $theme->get('Name'),
320 'Version' => $theme->get('Version'),
321 'Author' => $theme->get('Author'),
322 'Author URI' => $theme->get('AuthorURI'),
323 'Template' => $theme->get_template(),
324 'Stylesheet' => $theme->get_stylesheet(),
328 // Check for update on a different schedule, depending on the page.
329 switch ( current_filter() ) {
330 case 'upgrader_process_complete' :
333 case 'load-update-core.php' :
334 $timeout = MINUTE_IN_SECONDS;
336 case 'load-themes.php' :
337 case 'load-update.php' :
338 $timeout = HOUR_IN_SECONDS;
341 $timeout = 12 * HOUR_IN_SECONDS;
344 $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
346 if ( $time_not_changed ) {
347 $theme_changed = false;
348 foreach ( $checked as $slug => $v ) {
349 if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
350 $theme_changed = true;
353 if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
354 foreach ( $last_update->response as $slug => $update_details ) {
355 if ( ! isset($checked[ $slug ]) ) {
356 $theme_changed = true;
362 // Bail if we've checked recently and if nothing has changed
363 if ( ! $theme_changed )
367 // Update last_checked for current to prevent multiple blocking requests if request hangs
368 $last_update->last_checked = time();
369 set_site_transient( 'update_themes', $last_update );
371 $request['themes'] = $themes;
373 $locales = array( get_locale() );
375 * Filter the locales requested for theme translations.
379 * @param array $locales Theme locale. Default is current locale of the site.
381 $locales = apply_filters( 'themes_update_check_locales', $locales );
384 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
386 'themes' => json_encode( $request ),
387 'translations' => json_encode( $translations ),
388 'locale' => json_encode( $locales ),
390 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
393 $url = $http_url = 'http://api.wordpress.org/themes/update-check/1.1/';
394 if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
395 $url = set_url_scheme( $url, 'https' );
397 $raw_response = wp_remote_post( $url, $options );
398 if ( $ssl && is_wp_error( $raw_response ) ) {
399 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 );
400 $raw_response = wp_remote_post( $http_url, $options );
403 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
406 $new_update = new stdClass;
407 $new_update->last_checked = time();
408 $new_update->checked = $checked;
410 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
412 if ( is_array( $response ) ) {
413 $new_update->response = $response['themes'];
414 $new_update->translations = $response['translations'];
417 set_site_transient( 'update_themes', $new_update );
421 * Performs WordPress automatic background updates.
425 function wp_maybe_auto_update() {
426 include_once ABSPATH . '/wp-admin/includes/admin.php';
427 include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
429 $upgrader = new WP_Automatic_Updater;
434 * Retrieves a list of all language updates available.
438 function wp_get_translation_updates() {
440 $transients = array( 'update_core' => 'core', 'update_plugins' => 'plugin', 'update_themes' => 'theme' );
441 foreach ( $transients as $transient => $type ) {
443 $transient = get_site_transient( $transient );
444 if ( empty( $transient->translations ) )
447 foreach ( $transient->translations as $translation ) {
448 $updates[] = (object) $translation;
456 * Collect counts and UI strings for available updates
462 function wp_get_update_data() {
463 $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0, 'translations' => 0 );
465 if ( $plugins = current_user_can( 'update_plugins' ) ) {
466 $update_plugins = get_site_transient( 'update_plugins' );
467 if ( ! empty( $update_plugins->response ) )
468 $counts['plugins'] = count( $update_plugins->response );
471 if ( $themes = current_user_can( 'update_themes' ) ) {
472 $update_themes = get_site_transient( 'update_themes' );
473 if ( ! empty( $update_themes->response ) )
474 $counts['themes'] = count( $update_themes->response );
477 if ( ( $core = current_user_can( 'update_core' ) ) && function_exists( 'get_core_updates' ) ) {
478 $update_wordpress = get_core_updates( array('dismissed' => false) );
479 if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') )
480 $counts['wordpress'] = 1;
483 if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() )
484 $counts['translations'] = 1;
486 $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
488 if ( $counts['wordpress'] )
489 $titles['wordpress'] = sprintf( __( '%d WordPress Update'), $counts['wordpress'] );
490 if ( $counts['plugins'] )
491 $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
492 if ( $counts['themes'] )
493 $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
494 if ( $counts['translations'] )
495 $titles['translations'] = __( 'Translation Updates' );
497 $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
499 $update_data = array( 'counts' => $counts, 'title' => $update_title );
501 * Filter the returned array of update data for plugins, themes, and WordPress core.
505 * @param array $update_data {
506 * Fetched update data.
508 * @type array $counts An array of counts for available plugin, theme, and WordPress updates.
509 * @type string $update_title Titles of available updates.
511 * @param array $titles An array of update counts and UI strings for available updates.
513 return apply_filters( 'wp_get_update_data', $update_data, $titles );
516 function _maybe_update_core() {
517 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
519 $current = get_site_transient( 'update_core' );
521 if ( isset( $current->last_checked ) &&
522 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
523 isset( $current->version_checked ) &&
524 $current->version_checked == $wp_version )
530 * Check the last time plugins were run before checking plugin versions.
532 * This might have been backported to WordPress 2.6.1 for performance reasons.
533 * This is used for the wp-admin to check only so often instead of every page
539 function _maybe_update_plugins() {
540 $current = get_site_transient( 'update_plugins' );
541 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
547 * Check themes versions only after a duration of time.
549 * This is for performance reasons to make sure that on the theme version
550 * checker is not run on every page load.
555 function _maybe_update_themes() {
556 $current = get_site_transient( 'update_themes' );
557 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
564 * Schedule core, theme, and plugin update checks.
568 function wp_schedule_update_checks() {
569 if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
570 wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
572 if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
573 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
575 if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
576 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
578 if ( ! wp_next_scheduled( 'wp_maybe_auto_update' ) && ! defined( 'WP_INSTALLING' ) ) {
579 // Schedule auto updates for 7 a.m. and 7 p.m. in the timezone of the site.
580 $next = strtotime( 'today 7am' );
582 // Find the next instance of 7 a.m. or 7 p.m., but skip it if it is within 3 hours from now.
583 while ( ( $now + 3 * HOUR_IN_SECONDS ) > $next ) {
584 $next += 12 * HOUR_IN_SECONDS;
586 $next = $next - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
587 wp_schedule_event( $next, 'twicedaily', 'wp_maybe_auto_update' );
591 if ( ( ! is_main_site() && ! is_network_admin() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) )
594 add_action( 'admin_init', '_maybe_update_core' );
595 add_action( 'wp_version_check', 'wp_version_check' );
596 add_action( 'upgrader_process_complete', 'wp_version_check', 10, 0 );
598 add_action( 'load-plugins.php', 'wp_update_plugins' );
599 add_action( 'load-update.php', 'wp_update_plugins' );
600 add_action( 'load-update-core.php', 'wp_update_plugins' );
601 add_action( 'admin_init', '_maybe_update_plugins' );
602 add_action( 'wp_update_plugins', 'wp_update_plugins' );
603 add_action( 'upgrader_process_complete', 'wp_update_plugins' );
605 add_action( 'load-themes.php', 'wp_update_themes' );
606 add_action( 'load-update.php', 'wp_update_themes' );
607 add_action( 'load-update-core.php', 'wp_update_themes' );
608 add_action( 'admin_init', '_maybe_update_themes' );
609 add_action( 'wp_update_themes', 'wp_update_themes' );
610 add_action( 'upgrader_process_complete', 'wp_update_themes' );
612 add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
614 add_action('init', 'wp_schedule_update_checks');