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://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, null, '&' );
97 if ( 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 );
112 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
115 $body = trim( wp_remote_retrieve_body( $response ) );
116 $body = json_decode( $body, true );
118 if ( ! is_array( $body ) || ! isset( $body['offers'] ) )
121 $offers = $body['offers'];
123 foreach ( $offers as &$offer ) {
124 foreach ( $offer as $offer_key => $value ) {
125 if ( 'packages' == $offer_key )
126 $offer['packages'] = (object) array_intersect_key( array_map( 'esc_url', $offer['packages'] ),
127 array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' ) );
128 elseif ( 'download' == $offer_key )
129 $offer['download'] = esc_url( $value );
131 $offer[ $offer_key ] = esc_html( $value );
133 $offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',
134 'packages', 'current', 'version', 'php_version', 'mysql_version', 'new_bundled', 'partial_version', 'notify_email' ), '' ) );
137 $updates = new stdClass();
138 $updates->updates = $offers;
139 $updates->last_checked = time();
140 $updates->version_checked = $wp_version;
142 if ( isset( $body['translations'] ) )
143 $updates->translations = $body['translations'];
145 set_site_transient( 'update_core', $updates);
149 * Check plugin versions against the latest versions hosted on WordPress.org.
151 * The WordPress version, PHP version, and Locale is sent along with a list of
152 * all plugins installed. Checks against the WordPress server at
153 * api.wordpress.org. Will only check if WordPress isn't installing.
157 * @uses $wp_version Used to notify the WordPress version.
159 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
161 function wp_update_plugins() {
162 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
164 if ( defined('WP_INSTALLING') )
167 // If running blog-side, bail unless we've not checked in the last 12 hours
168 if ( !function_exists( 'get_plugins' ) )
169 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
171 $plugins = get_plugins();
172 $translations = wp_get_installed_translations( 'plugins' );
174 $active = get_option( 'active_plugins', array() );
175 $current = get_site_transient( 'update_plugins' );
176 if ( ! is_object($current) )
177 $current = new stdClass;
179 $new_option = new stdClass;
180 $new_option->last_checked = time();
182 // Check for update on a different schedule, depending on the page.
183 switch ( current_filter() ) {
184 case 'upgrader_process_complete' :
187 case 'load-update-core.php' :
188 $timeout = MINUTE_IN_SECONDS;
190 case 'load-plugins.php' :
191 case 'load-update.php' :
192 $timeout = HOUR_IN_SECONDS;
195 $timeout = 12 * HOUR_IN_SECONDS;
198 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
200 if ( $time_not_changed ) {
201 $plugin_changed = false;
202 foreach ( $plugins as $file => $p ) {
203 $new_option->checked[ $file ] = $p['Version'];
205 if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
206 $plugin_changed = true;
209 if ( isset ( $current->response ) && is_array( $current->response ) ) {
210 foreach ( $current->response as $plugin_file => $update_details ) {
211 if ( ! isset($plugins[ $plugin_file ]) ) {
212 $plugin_changed = true;
218 // Bail if we've checked recently and if nothing has changed
219 if ( ! $plugin_changed )
223 // Update last_checked for current to prevent multiple blocking requests if request hangs
224 $current->last_checked = time();
225 set_site_transient( 'update_plugins', $current );
227 $to_send = compact( 'plugins', 'active' );
229 $locales = array( get_locale() );
231 * Filter the locales requested for plugin translations.
235 * @param array $locales Plugin locale. Default is current locale of the site.
237 $locales = apply_filters( 'plugins_update_check_locales', $locales );
240 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
242 'plugins' => json_encode( $to_send ),
243 'translations' => json_encode( $translations ),
244 'locale' => json_encode( $locales ),
246 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
249 $url = 'http://api.wordpress.org/plugins/update-check/1.1/';
250 if ( wp_http_supports( array( 'ssl' ) ) )
251 $url = set_url_scheme( $url, 'https' );
253 $raw_response = wp_remote_post( $url, $options );
255 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
258 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
259 foreach ( $response['plugins'] as &$plugin ) {
260 $plugin = (object) $plugin;
264 if ( is_array( $response ) ) {
265 $new_option->response = $response['plugins'];
266 $new_option->translations = $response['translations'];
268 $new_option->response = array();
269 $new_option->translations = array();
272 set_site_transient( 'update_plugins', $new_option );
276 * Check theme versions against the latest versions hosted on WordPress.org.
278 * A list of all themes installed in sent to WP. Checks against the
279 * WordPress server at api.wordpress.org. Will only check if WordPress isn't
284 * @uses $wp_version Used to notify the WordPress version.
286 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
288 function wp_update_themes() {
289 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
291 if ( defined( 'WP_INSTALLING' ) )
294 $installed_themes = wp_get_themes();
295 $translations = wp_get_installed_translations( 'themes' );
297 $last_update = get_site_transient( 'update_themes' );
298 if ( ! is_object($last_update) )
299 $last_update = new stdClass;
301 $themes = $checked = $request = array();
303 // Put slug of current theme into request.
304 $request['active'] = get_option( 'stylesheet' );
306 foreach ( $installed_themes as $theme ) {
307 $checked[ $theme->get_stylesheet() ] = $theme->get('Version');
309 $themes[ $theme->get_stylesheet() ] = array(
310 'Name' => $theme->get('Name'),
311 'Title' => $theme->get('Name'),
312 'Version' => $theme->get('Version'),
313 'Author' => $theme->get('Author'),
314 'Author URI' => $theme->get('AuthorURI'),
315 'Template' => $theme->get_template(),
316 'Stylesheet' => $theme->get_stylesheet(),
320 // Check for update on a different schedule, depending on the page.
321 switch ( current_filter() ) {
322 case 'upgrader_process_complete' :
325 case 'load-update-core.php' :
326 $timeout = MINUTE_IN_SECONDS;
328 case 'load-themes.php' :
329 case 'load-update.php' :
330 $timeout = HOUR_IN_SECONDS;
333 $timeout = 12 * HOUR_IN_SECONDS;
336 $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
338 if ( $time_not_changed ) {
339 $theme_changed = false;
340 foreach ( $checked as $slug => $v ) {
341 if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
342 $theme_changed = true;
345 if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
346 foreach ( $last_update->response as $slug => $update_details ) {
347 if ( ! isset($checked[ $slug ]) ) {
348 $theme_changed = true;
354 // Bail if we've checked recently and if nothing has changed
355 if ( ! $theme_changed )
359 // Update last_checked for current to prevent multiple blocking requests if request hangs
360 $last_update->last_checked = time();
361 set_site_transient( 'update_themes', $last_update );
363 $request['themes'] = $themes;
365 $locales = array( get_locale() );
367 * Filter the locales requested for theme translations.
371 * @param array $locales Theme locale. Default is current locale of the site.
373 $locales = apply_filters( 'themes_update_check_locales', $locales );
376 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
378 'themes' => json_encode( $request ),
379 'translations' => json_encode( $translations ),
380 'locale' => json_encode( $locales ),
382 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
385 $url = 'http://api.wordpress.org/themes/update-check/1.1/';
386 if ( wp_http_supports( array( 'ssl' ) ) )
387 $url = set_url_scheme( $url, 'https' );
389 $raw_response = wp_remote_post( $url, $options );
391 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
394 $new_update = new stdClass;
395 $new_update->last_checked = time();
396 $new_update->checked = $checked;
398 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
400 if ( is_array( $response ) ) {
401 $new_update->response = $response['themes'];
402 $new_update->translations = $response['translations'];
405 set_site_transient( 'update_themes', $new_update );
409 * Performs WordPress automatic background updates.
413 function wp_maybe_auto_update() {
414 include_once ABSPATH . '/wp-admin/includes/admin.php';
415 include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
417 $upgrader = new WP_Automatic_Updater;
422 * Retrieves a list of all language updates available.
426 function wp_get_translation_updates() {
428 $transients = array( 'update_core' => 'core', 'update_plugins' => 'plugin', 'update_themes' => 'theme' );
429 foreach ( $transients as $transient => $type ) {
431 $transient = get_site_transient( $transient );
432 if ( empty( $transient->translations ) )
435 foreach ( $transient->translations as $translation ) {
436 $updates[] = (object) $translation;
444 * Collect counts and UI strings for available updates
450 function wp_get_update_data() {
451 $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0, 'translations' => 0 );
453 if ( $plugins = current_user_can( 'update_plugins' ) ) {
454 $update_plugins = get_site_transient( 'update_plugins' );
455 if ( ! empty( $update_plugins->response ) )
456 $counts['plugins'] = count( $update_plugins->response );
459 if ( $themes = current_user_can( 'update_themes' ) ) {
460 $update_themes = get_site_transient( 'update_themes' );
461 if ( ! empty( $update_themes->response ) )
462 $counts['themes'] = count( $update_themes->response );
465 if ( ( $core = current_user_can( 'update_core' ) ) && function_exists( 'get_core_updates' ) ) {
466 $update_wordpress = get_core_updates( array('dismissed' => false) );
467 if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') )
468 $counts['wordpress'] = 1;
471 if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() )
472 $counts['translations'] = 1;
474 $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
476 if ( $counts['wordpress'] )
477 $titles['wordpress'] = sprintf( __( '%d WordPress Update'), $counts['wordpress'] );
478 if ( $counts['plugins'] )
479 $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
480 if ( $counts['themes'] )
481 $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
482 if ( $counts['translations'] )
483 $titles['translations'] = __( 'Translation Updates' );
485 $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
487 $update_data = array( 'counts' => $counts, 'title' => $update_title );
489 * Filter the returned array of update data for plugins, themes, and WordPress core.
493 * @param array $update_data {
494 * Fetched update data.
496 * @type array $counts An array of counts for available plugin, theme, and WordPress updates.
497 * @type string $update_title Titles of available updates.
499 * @param array $titles An array of update counts and UI strings for available updates.
501 return apply_filters( 'wp_get_update_data', $update_data, $titles );
504 function _maybe_update_core() {
505 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
507 $current = get_site_transient( 'update_core' );
509 if ( isset( $current->last_checked ) &&
510 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
511 isset( $current->version_checked ) &&
512 $current->version_checked == $wp_version )
518 * Check the last time plugins were run before checking plugin versions.
520 * This might have been backported to WordPress 2.6.1 for performance reasons.
521 * This is used for the wp-admin to check only so often instead of every page
527 function _maybe_update_plugins() {
528 $current = get_site_transient( 'update_plugins' );
529 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
535 * Check themes versions only after a duration of time.
537 * This is for performance reasons to make sure that on the theme version
538 * checker is not run on every page load.
543 function _maybe_update_themes() {
544 $current = get_site_transient( 'update_themes' );
545 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
552 * Schedule core, theme, and plugin update checks.
556 function wp_schedule_update_checks() {
557 if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
558 wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
560 if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
561 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
563 if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
564 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
566 if ( ! wp_next_scheduled( 'wp_maybe_auto_update' ) && ! defined( 'WP_INSTALLING' ) ) {
567 // Schedule auto updates for 7 a.m. and 7 p.m. in the timezone of the site.
568 $next = strtotime( 'today 7am' );
570 // Find the next instance of 7 a.m. or 7 p.m., but skip it if it is within 3 hours from now.
571 while ( ( $now + 3 * HOUR_IN_SECONDS ) > $next ) {
572 $next += 12 * HOUR_IN_SECONDS;
574 $next = $next - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
575 wp_schedule_event( $next, 'twicedaily', 'wp_maybe_auto_update' );
579 if ( ( ! is_main_site() && ! is_network_admin() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) )
582 add_action( 'admin_init', '_maybe_update_core' );
583 add_action( 'wp_version_check', 'wp_version_check' );
584 add_action( 'upgrader_process_complete', 'wp_version_check', 10, 0 );
586 add_action( 'load-plugins.php', 'wp_update_plugins' );
587 add_action( 'load-update.php', 'wp_update_plugins' );
588 add_action( 'load-update-core.php', 'wp_update_plugins' );
589 add_action( 'admin_init', '_maybe_update_plugins' );
590 add_action( 'wp_update_plugins', 'wp_update_plugins' );
591 add_action( 'upgrader_process_complete', 'wp_update_plugins' );
593 add_action( 'load-themes.php', 'wp_update_themes' );
594 add_action( 'load-update.php', 'wp_update_themes' );
595 add_action( 'load-update-core.php', 'wp_update_themes' );
596 add_action( 'admin_init', '_maybe_update_themes' );
597 add_action( 'wp_update_themes', 'wp_update_themes' );
598 add_action( 'upgrader_process_complete', 'wp_update_themes' );
600 add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
602 add_action('init', 'wp_schedule_update_checks');