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 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
22 function wp_version_check() {
23 if ( defined('WP_INSTALLING') )
26 global $wpdb, $wp_local_package;
27 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
28 $php_version = phpversion();
30 $current = get_site_transient( 'update_core' );
31 if ( ! is_object($current) ) {
32 $current = new stdClass;
33 $current->updates = array();
34 $current->version_checked = $wp_version;
37 // Wait 60 seconds between multiple version check requests
39 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
40 if ( $time_not_changed )
43 $locale = apply_filters( 'core_version_check_locale', get_locale() );
45 // Update last_checked for current to prevent multiple blocking requests if request hangs
46 $current->last_checked = time();
47 set_site_transient( 'update_core', $current );
49 if ( method_exists( $wpdb, 'db_version' ) )
50 $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
52 $mysql_version = 'N/A';
54 if ( is_multisite( ) ) {
55 $user_count = get_user_count( );
56 $num_blogs = get_blog_count( );
57 $wp_install = network_site_url( );
58 $multisite_enabled = 1;
60 $user_count = count_users( );
61 $user_count = $user_count['total_users'];
62 $multisite_enabled = 0;
64 $wp_install = home_url( '/' );
68 'version' => $wp_version,
69 'php' => $php_version,
71 'mysql' => $mysql_version,
72 'local_package' => isset( $wp_local_package ) ? $wp_local_package : '',
73 'blogs' => $num_blogs,
74 'users' => $user_count,
75 'multisite_enabled' => $multisite_enabled
78 $url = 'http://api.wordpress.org/core/version-check/1.6/?' . http_build_query( $query, null, '&' );
81 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
82 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
84 'wp_install' => $wp_install,
85 'wp_blog' => home_url( '/' )
89 $response = wp_remote_get($url, $options);
91 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
94 $body = trim( wp_remote_retrieve_body( $response ) );
95 $body = maybe_unserialize( $body );
97 if ( ! is_array( $body ) || ! isset( $body['offers'] ) )
100 $offers = $body['offers'];
102 foreach ( $offers as &$offer ) {
103 foreach ( $offer as $offer_key => $value ) {
104 if ( 'packages' == $offer_key )
105 $offer['packages'] = (object) array_intersect_key( array_map( 'esc_url', $offer['packages'] ),
106 array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial' ), '' ) );
107 elseif ( 'download' == $offer_key )
108 $offer['download'] = esc_url( $value );
110 $offer[ $offer_key ] = esc_html( $value );
112 $offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',
113 'packages', 'current', 'php_version', 'mysql_version', 'new_bundled', 'partial_version' ), '' ) );
116 $updates = new stdClass();
117 $updates->updates = $offers;
118 $updates->last_checked = time();
119 $updates->version_checked = $wp_version;
120 set_site_transient( 'update_core', $updates);
124 * Check plugin versions against the latest versions hosted on WordPress.org.
126 * The WordPress version, PHP version, and Locale is sent along with a list of
127 * all plugins installed. Checks against the WordPress server at
128 * api.wordpress.org. Will only check if WordPress isn't installing.
132 * @uses $wp_version Used to notify the WordPress version.
134 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
136 function wp_update_plugins() {
137 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
139 if ( defined('WP_INSTALLING') )
142 // If running blog-side, bail unless we've not checked in the last 12 hours
143 if ( !function_exists( 'get_plugins' ) )
144 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
146 $plugins = get_plugins();
147 $active = get_option( 'active_plugins', array() );
148 $current = get_site_transient( 'update_plugins' );
149 if ( ! is_object($current) )
150 $current = new stdClass;
152 $new_option = new stdClass;
153 $new_option->last_checked = time();
155 // Check for update on a different schedule, depending on the page.
156 switch ( current_filter() ) {
157 case 'load-update-core.php' :
158 $timeout = MINUTE_IN_SECONDS;
160 case 'load-plugins.php' :
161 case 'load-update.php' :
162 $timeout = HOUR_IN_SECONDS;
165 $timeout = 12 * HOUR_IN_SECONDS;
168 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
170 if ( $time_not_changed ) {
171 $plugin_changed = false;
172 foreach ( $plugins as $file => $p ) {
173 $new_option->checked[ $file ] = $p['Version'];
175 if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
176 $plugin_changed = true;
179 if ( isset ( $current->response ) && is_array( $current->response ) ) {
180 foreach ( $current->response as $plugin_file => $update_details ) {
181 if ( ! isset($plugins[ $plugin_file ]) ) {
182 $plugin_changed = true;
188 // Bail if we've checked recently and if nothing has changed
189 if ( ! $plugin_changed )
193 // Update last_checked for current to prevent multiple blocking requests if request hangs
194 $current->last_checked = time();
195 set_site_transient( 'update_plugins', $current );
197 $to_send = (object) compact('plugins', 'active');
200 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
201 'body' => array( 'plugins' => serialize( $to_send ) ),
202 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
205 $raw_response = wp_remote_post('http://api.wordpress.org/plugins/update-check/1.0/', $options);
207 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
210 $response = maybe_unserialize( wp_remote_retrieve_body( $raw_response ) );
212 if ( is_array( $response ) )
213 $new_option->response = $response;
215 $new_option->response = array();
217 set_site_transient( 'update_plugins', $new_option );
221 * Check theme versions against the latest versions hosted on WordPress.org.
223 * A list of all themes installed in sent to WP. Checks against the
224 * WordPress server at api.wordpress.org. Will only check if WordPress isn't
229 * @uses $wp_version Used to notify the WordPress version.
231 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
233 function wp_update_themes() {
234 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
236 if ( defined( 'WP_INSTALLING' ) )
239 $installed_themes = wp_get_themes();
240 $last_update = get_site_transient( 'update_themes' );
241 if ( ! is_object($last_update) )
242 $last_update = new stdClass;
247 // Put slug of current theme into request.
248 $themes['current_theme'] = get_option( 'stylesheet' );
250 foreach ( $installed_themes as $theme ) {
251 $checked[ $theme->get_stylesheet() ] = $theme->get('Version');
253 $themes[ $theme->get_stylesheet() ] = array(
254 'Name' => $theme->get('Name'),
255 'Title' => $theme->get('Name'),
256 'Version' => $theme->get('Version'),
257 'Author' => $theme->get('Author'),
258 'Author URI' => $theme->get('AuthorURI'),
259 'Template' => $theme->get_template(),
260 'Stylesheet' => $theme->get_stylesheet(),
264 // Check for update on a different schedule, depending on the page.
265 switch ( current_filter() ) {
266 case 'load-update-core.php' :
267 $timeout = MINUTE_IN_SECONDS;
269 case 'load-themes.php' :
270 case 'load-update.php' :
271 $timeout = HOUR_IN_SECONDS;
274 $timeout = 12 * HOUR_IN_SECONDS;
277 $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time( ) - $last_update->last_checked );
279 if ( $time_not_changed ) {
280 $theme_changed = false;
281 foreach ( $checked as $slug => $v ) {
282 if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
283 $theme_changed = true;
286 if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
287 foreach ( $last_update->response as $slug => $update_details ) {
288 if ( ! isset($checked[ $slug ]) ) {
289 $theme_changed = true;
295 // Bail if we've checked recently and if nothing has changed
296 if ( ! $theme_changed )
300 // Update last_checked for current to prevent multiple blocking requests if request hangs
301 $last_update->last_checked = time();
302 set_site_transient( 'update_themes', $last_update );
305 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
306 'body' => array( 'themes' => serialize( $themes ) ),
307 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
310 $raw_response = wp_remote_post( 'http://api.wordpress.org/themes/update-check/1.0/', $options );
312 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
315 $new_update = new stdClass;
316 $new_update->last_checked = time( );
317 $new_update->checked = $checked;
319 $response = maybe_unserialize( wp_remote_retrieve_body( $raw_response ) );
320 if ( is_array( $response ) )
321 $new_update->response = $response;
323 set_site_transient( 'update_themes', $new_update );
327 * Collect counts and UI strings for available updates
333 function wp_get_update_data() {
334 $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0 );
336 if ( current_user_can( 'update_plugins' ) ) {
337 $update_plugins = get_site_transient( 'update_plugins' );
338 if ( ! empty( $update_plugins->response ) )
339 $counts['plugins'] = count( $update_plugins->response );
342 if ( current_user_can( 'update_themes' ) ) {
343 $update_themes = get_site_transient( 'update_themes' );
344 if ( ! empty( $update_themes->response ) )
345 $counts['themes'] = count( $update_themes->response );
348 if ( function_exists( 'get_core_updates' ) && current_user_can( 'update_core' ) ) {
349 $update_wordpress = get_core_updates( array('dismissed' => false) );
350 if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') )
351 $counts['wordpress'] = 1;
354 $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'];
356 if ( $counts['wordpress'] )
357 $titles['wordpress'] = sprintf( __( '%d WordPress Update'), $counts['wordpress'] );
358 if ( $counts['plugins'] )
359 $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
360 if ( $counts['themes'] )
361 $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
363 $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
365 return apply_filters( 'wp_get_update_data', array( 'counts' => $counts, 'title' => $update_title ), $titles );
368 function _maybe_update_core() {
369 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
371 $current = get_site_transient( 'update_core' );
373 if ( isset( $current->last_checked ) &&
374 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
375 isset( $current->version_checked ) &&
376 $current->version_checked == $wp_version )
382 * Check the last time plugins were run before checking plugin versions.
384 * This might have been backported to WordPress 2.6.1 for performance reasons.
385 * This is used for the wp-admin to check only so often instead of every page
391 function _maybe_update_plugins() {
392 $current = get_site_transient( 'update_plugins' );
393 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
399 * Check themes versions only after a duration of time.
401 * This is for performance reasons to make sure that on the theme version
402 * checker is not run on every page load.
407 function _maybe_update_themes( ) {
408 $current = get_site_transient( 'update_themes' );
409 if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time( ) - $current->last_checked ) )
416 * Schedule core, theme, and plugin update checks.
420 function wp_schedule_update_checks() {
421 if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
422 wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
424 if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
425 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
427 if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
428 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
431 if ( ( ! is_main_site() && ! is_network_admin() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) )
434 add_action( 'admin_init', '_maybe_update_core' );
435 add_action( 'wp_version_check', 'wp_version_check' );
437 add_action( 'load-plugins.php', 'wp_update_plugins' );
438 add_action( 'load-update.php', 'wp_update_plugins' );
439 add_action( 'load-update-core.php', 'wp_update_plugins' );
440 add_action( 'admin_init', '_maybe_update_plugins' );
441 add_action( 'wp_update_plugins', 'wp_update_plugins' );
443 add_action( 'load-themes.php', 'wp_update_themes' );
444 add_action( 'load-update.php', 'wp_update_themes' );
445 add_action( 'load-update-core.php', 'wp_update_themes' );
446 add_action( 'admin_init', '_maybe_update_themes' );
447 add_action( 'wp_update_themes', 'wp_update_themes' );
449 add_action('init', 'wp_schedule_update_checks');