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 $locale = apply_filters( 'core_version_check_locale', get_locale() );
39 // Update last_checked for current to prevent multiple blocking requests if request hangs
40 $current->last_checked = time();
41 set_site_transient( 'update_core', $current );
43 if ( method_exists( $wpdb, 'db_version' ) )
44 $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
46 $mysql_version = 'N/A';
48 if ( is_multisite( ) ) {
49 $user_count = get_user_count( );
50 $num_blogs = get_blog_count( );
51 $wp_install = network_site_url( );
52 $multisite_enabled = 1;
54 $user_count = count_users( );
55 $multisite_enabled = 0;
57 $wp_install = home_url( '/' );
61 'version' => $wp_version,
62 'php' => $php_version,
64 'mysql' => $mysql_version,
65 'local_package' => isset( $wp_local_package ) ? $wp_local_package : '',
66 'blogs' => $num_blogs,
67 'users' => $user_count['total_users'],
68 'multisite_enabled' => $multisite_enabled
71 $url = 'http://api.wordpress.org/core/version-check/1.6/?' . http_build_query( $query, null, '&' );
74 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
75 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
77 'wp_install' => $wp_install,
78 'wp_blog' => home_url( '/' )
82 $response = wp_remote_get($url, $options);
84 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
87 $body = trim( wp_remote_retrieve_body( $response ) );
88 if ( ! $body = maybe_unserialize( $body ) )
90 if ( ! isset( $body['offers'] ) )
92 $offers = $body['offers'];
94 foreach ( $offers as &$offer ) {
95 foreach ( $offer as $offer_key => $value ) {
96 if ( 'packages' == $offer_key )
97 $offer['packages'] = (object) array_intersect_key( array_map( 'esc_url', $offer['packages'] ),
98 array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial' ), '' ) );
99 elseif ( 'download' == $offer_key )
100 $offer['download'] = esc_url( $value );
102 $offer[ $offer_key ] = esc_html( $value );
104 $offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',
105 'packages', 'current', 'php_version', 'mysql_version', 'new_bundled', 'partial_version' ), '' ) );
108 $updates = new stdClass();
109 $updates->updates = $offers;
110 $updates->last_checked = time();
111 $updates->version_checked = $wp_version;
112 set_site_transient( 'update_core', $updates);
116 * Check plugin versions against the latest versions hosted on WordPress.org.
118 * The WordPress version, PHP version, and Locale is sent along with a list of
119 * all plugins installed. Checks against the WordPress server at
120 * api.wordpress.org. Will only check if WordPress isn't installing.
124 * @uses $wp_version Used to notify the WordPress version.
126 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
128 function wp_update_plugins() {
129 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
131 if ( defined('WP_INSTALLING') )
134 // If running blog-side, bail unless we've not checked in the last 12 hours
135 if ( !function_exists( 'get_plugins' ) )
136 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
138 $plugins = get_plugins();
139 $active = get_option( 'active_plugins', array() );
140 $current = get_site_transient( 'update_plugins' );
141 if ( ! is_object($current) )
142 $current = new stdClass;
144 $new_option = new stdClass;
145 $new_option->last_checked = time();
146 // Check for updated every 60 minutes if hitting update pages; else, check every 12 hours.
147 $timeout = in_array( current_filter(), array( 'load-plugins.php', 'load-update.php', 'load-update-core.php' ) ) ? 3600 : 43200;
148 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
150 $plugin_changed = false;
151 foreach ( $plugins as $file => $p ) {
152 $new_option->checked[ $file ] = $p['Version'];
154 if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
155 $plugin_changed = true;
158 if ( isset ( $current->response ) && is_array( $current->response ) ) {
159 foreach ( $current->response as $plugin_file => $update_details ) {
160 if ( ! isset($plugins[ $plugin_file ]) ) {
161 $plugin_changed = true;
167 // Bail if we've checked in the last 12 hours and if nothing has changed
168 if ( $time_not_changed && !$plugin_changed )
171 // Update last_checked for current to prevent multiple blocking requests if request hangs
172 $current->last_checked = time();
173 set_site_transient( 'update_plugins', $current );
175 $to_send = (object) compact('plugins', 'active');
178 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
179 'body' => array( 'plugins' => serialize( $to_send ) ),
180 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
183 $raw_response = wp_remote_post('http://api.wordpress.org/plugins/update-check/1.0/', $options);
185 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
188 $response = unserialize( wp_remote_retrieve_body( $raw_response ) );
190 if ( false !== $response )
191 $new_option->response = $response;
193 $new_option->response = array();
195 set_site_transient( 'update_plugins', $new_option );
199 * Check theme versions against the latest versions hosted on WordPress.org.
201 * A list of all themes installed in sent to WP. Checks against the
202 * WordPress server at api.wordpress.org. Will only check if WordPress isn't
207 * @uses $wp_version Used to notify the WordPress version.
209 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
211 function wp_update_themes() {
212 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
214 if ( defined( 'WP_INSTALLING' ) )
217 if ( !function_exists( 'get_themes' ) )
218 require_once( ABSPATH . 'wp-includes/theme.php' );
220 $installed_themes = get_themes( );
221 $last_update = get_site_transient( 'update_themes' );
222 if ( ! is_object($last_update) )
223 $last_update = new stdClass;
225 // Check for updated every 60 minutes if hitting update pages; else, check every 12 hours.
226 $timeout = in_array( current_filter(), array( 'load-themes.php', 'load-update.php', 'load-update-core.php' ) ) ? 3600 : 43200;
227 $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time( ) - $last_update->last_checked );
231 $exclude_fields = array('Template Files', 'Stylesheet Files', 'Status', 'Theme Root', 'Theme Root URI', 'Template Dir', 'Stylesheet Dir', 'Description', 'Tags', 'Screenshot');
233 // Put slug of current theme into request.
234 $themes['current_theme'] = get_option( 'stylesheet' );
236 foreach ( (array) $installed_themes as $theme_title => $theme ) {
237 $themes[$theme['Stylesheet']] = array();
238 $checked[$theme['Stylesheet']] = $theme['Version'];
240 $themes[$theme['Stylesheet']]['Name'] = $theme['Name'];
241 $themes[$theme['Stylesheet']]['Version'] = $theme['Version'];
243 foreach ( (array) $theme as $key => $value ) {
244 if ( !in_array($key, $exclude_fields) )
245 $themes[$theme['Stylesheet']][$key] = $value;
249 $theme_changed = false;
250 foreach ( $checked as $slug => $v ) {
251 $update_request->checked[ $slug ] = $v;
253 if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
254 $theme_changed = true;
257 if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
258 foreach ( $last_update->response as $slug => $update_details ) {
259 if ( ! isset($checked[ $slug ]) ) {
260 $theme_changed = true;
266 if ( $time_not_changed && !$theme_changed )
269 // Update last_checked for current to prevent multiple blocking requests if request hangs
270 $last_update->last_checked = time();
271 set_site_transient( 'update_themes', $last_update );
274 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
275 'body' => array( 'themes' => serialize( $themes ) ),
276 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
279 $raw_response = wp_remote_post( 'http://api.wordpress.org/themes/update-check/1.0/', $options );
281 if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
284 $new_update = new stdClass;
285 $new_update->last_checked = time( );
286 $new_update->checked = $checked;
288 $response = unserialize( wp_remote_retrieve_body( $raw_response ) );
289 if ( false !== $response )
290 $new_update->response = $response;
292 set_site_transient( 'update_themes', $new_update );
296 * Collect counts and UI strings for available updates
302 function wp_get_update_data() {
303 $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0 );
305 if ( current_user_can( 'update_plugins' ) ) {
306 $update_plugins = get_site_transient( 'update_plugins' );
307 if ( ! empty( $update_plugins->response ) )
308 $counts['plugins'] = count( $update_plugins->response );
311 if ( current_user_can( 'update_themes' ) ) {
312 $update_themes = get_site_transient( 'update_themes' );
313 if ( ! empty( $update_themes->response ) )
314 $counts['themes'] = count( $update_themes->response );
317 if ( function_exists( 'get_core_updates' ) && current_user_can( 'update_core' ) ) {
318 $update_wordpress = get_core_updates( array('dismissed' => false) );
319 if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') )
320 $counts['wordpress'] = 1;
323 $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'];
324 $update_title = array();
325 if ( $counts['wordpress'] )
326 $update_title[] = sprintf(__('%d WordPress Update'), $counts['wordpress']);
327 if ( $counts['plugins'] )
328 $update_title[] = sprintf(_n('%d Plugin Update', '%d Plugin Updates', $counts['plugins']), $counts['plugins']);
329 if ( $counts['themes'] )
330 $update_title[] = sprintf(_n('%d Theme Update', '%d Theme Updates', $counts['themes']), $counts['themes']);
332 $update_title = ! empty( $update_title ) ? esc_attr( implode( ', ', $update_title ) ) : '';
334 return array( 'counts' => $counts, 'title' => $update_title );
337 function _maybe_update_core() {
338 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
340 $current = get_site_transient( 'update_core' );
342 if ( isset( $current->last_checked ) &&
343 43200 > ( time() - $current->last_checked ) &&
344 isset( $current->version_checked ) &&
345 $current->version_checked == $wp_version )
351 * Check the last time plugins were run before checking plugin versions.
353 * This might have been backported to WordPress 2.6.1 for performance reasons.
354 * This is used for the wp-admin to check only so often instead of every page
360 function _maybe_update_plugins() {
361 $current = get_site_transient( 'update_plugins' );
362 if ( isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked ) )
368 * Check themes versions only after a duration of time.
370 * This is for performance reasons to make sure that on the theme version
371 * checker is not run on every page load.
376 function _maybe_update_themes( ) {
377 $current = get_site_transient( 'update_themes' );
378 if ( isset( $current->last_checked ) && 43200 > ( time( ) - $current->last_checked ) )
385 * Schedule core, theme, and plugin update checks.
389 function wp_schedule_update_checks() {
390 if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
391 wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
393 if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
394 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
396 if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
397 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
400 if ( ! is_main_site() && ! is_network_admin() )
403 add_action( 'admin_init', '_maybe_update_core' );
404 add_action( 'wp_version_check', 'wp_version_check' );
406 add_action( 'load-plugins.php', 'wp_update_plugins' );
407 add_action( 'load-update.php', 'wp_update_plugins' );
408 add_action( 'load-update-core.php', 'wp_update_plugins' );
409 add_action( 'admin_init', '_maybe_update_plugins' );
410 add_action( 'wp_update_plugins', 'wp_update_plugins' );
412 add_action( 'load-themes.php', 'wp_update_themes' );
413 add_action( 'load-update.php', 'wp_update_themes' );
414 add_action( 'load-update-core.php', 'wp_update_themes' );
415 add_action( 'admin_init', '_maybe_update_themes' );
416 add_action( 'wp_update_themes', 'wp_update_themes' );
418 add_action('init', 'wp_schedule_update_checks');