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 $wp_version, $wpdb, $wp_local_package;
27 $php_version = phpversion();
29 $current = get_site_transient( 'update_core' );
30 if ( ! is_object($current) ) {
31 $current = new stdClass;
32 $current->updates = array();
33 $current->version_checked = $wp_version;
36 $locale = apply_filters( 'core_version_check_locale', get_locale() );
38 // Update last_checked for current to prevent multiple blocking requests if request hangs
39 $current->last_checked = time();
40 set_site_transient( 'update_core', $current );
42 if ( method_exists( $wpdb, 'db_version' ) )
43 $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
45 $mysql_version = 'N/A';
48 $wp_install = home_url( '/' );
49 $multisite_enabled = 0;
50 $user_count = count_users( );
51 if ( is_multisite( ) ) {
52 $num_blogs = get_blog_count( );
53 $wp_install = network_site_url( );
54 $multisite_enabled = 1;
57 $local_package = isset( $wp_local_package )? $wp_local_package : '';
58 $url = "http://api.wordpress.org/core/version-check/1.5/?version=$wp_version&php=$php_version&locale=$locale&mysql=$mysql_version&local_package=$local_package&blogs=$num_blogs&users={$user_count['total_users']}&multisite_enabled=$multisite_enabled";
61 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
62 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
64 'wp_install' => $wp_install,
65 'wp_blog' => home_url( '/' )
69 $response = wp_remote_get($url, $options);
71 if ( is_wp_error( $response ) )
74 if ( 200 != $response['response']['code'] )
77 $body = trim( $response['body'] );
78 $body = str_replace(array("\r\n", "\r"), "\n", $body);
79 $new_options = array();
80 foreach ( explode( "\n\n", $body ) as $entry ) {
81 $returns = explode("\n", $entry);
82 $new_option = new stdClass();
83 $new_option->response = esc_attr( $returns[0] );
84 if ( isset( $returns[1] ) )
85 $new_option->url = esc_url( $returns[1] );
86 if ( isset( $returns[2] ) )
87 $new_option->package = esc_url( $returns[2] );
88 if ( isset( $returns[3] ) )
89 $new_option->current = esc_attr( $returns[3] );
90 if ( isset( $returns[4] ) )
91 $new_option->locale = esc_attr( $returns[4] );
92 if ( isset( $returns[5] ) )
93 $new_option->php_version = esc_attr( $returns[5] );
94 if ( isset( $returns[6] ) )
95 $new_option->mysql_version = esc_attr( $returns[6] );
96 $new_options[] = $new_option;
99 $updates = new stdClass();
100 $updates->updates = $new_options;
101 $updates->last_checked = time();
102 $updates->version_checked = $wp_version;
103 set_site_transient( 'update_core', $updates);
107 * Check plugin versions against the latest versions hosted on WordPress.org.
109 * The WordPress version, PHP version, and Locale is sent along with a list of
110 * all plugins installed. Checks against the WordPress server at
111 * api.wordpress.org. Will only check if WordPress isn't installing.
115 * @uses $wp_version Used to notidy the WordPress version.
117 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
119 function wp_update_plugins() {
122 if ( defined('WP_INSTALLING') )
125 // If running blog-side, bail unless we've not checked in the last 12 hours
126 if ( !function_exists( 'get_plugins' ) )
127 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
129 $plugins = get_plugins();
130 $active = get_option( 'active_plugins', array() );
131 $current = get_site_transient( 'update_plugins' );
132 if ( ! is_object($current) )
133 $current = new stdClass;
135 $new_option = new stdClass;
136 $new_option->last_checked = time();
137 $timeout = 'load-plugins.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours
138 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
140 $plugin_changed = false;
141 foreach ( $plugins as $file => $p ) {
142 $new_option->checked[ $file ] = $p['Version'];
144 if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
145 $plugin_changed = true;
148 if ( isset ( $current->response ) && is_array( $current->response ) ) {
149 foreach ( $current->response as $plugin_file => $update_details ) {
150 if ( ! isset($plugins[ $plugin_file ]) ) {
151 $plugin_changed = true;
157 // Bail if we've checked in the last 12 hours and if nothing has changed
158 if ( $time_not_changed && !$plugin_changed )
161 // Update last_checked for current to prevent multiple blocking requests if request hangs
162 $current->last_checked = time();
163 set_site_transient( 'update_plugins', $current );
165 $to_send = (object) compact('plugins', 'active');
168 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
169 'body' => array( 'plugins' => serialize( $to_send ) ),
170 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
173 $raw_response = wp_remote_post('http://api.wordpress.org/plugins/update-check/1.0/', $options);
175 if ( is_wp_error( $raw_response ) )
178 if ( 200 != $raw_response['response']['code'] )
181 $response = unserialize( $raw_response['body'] );
183 if ( false !== $response )
184 $new_option->response = $response;
186 $new_option->response = array();
188 set_site_transient( 'update_plugins', $new_option );
192 * Check theme versions against the latest versions hosted on WordPress.org.
194 * A list of all themes installed in sent to WP. Checks against the
195 * WordPress server at api.wordpress.org. Will only check if WordPress isn't
200 * @uses $wp_version Used to notidy the WordPress version.
202 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
204 function wp_update_themes( ) {
207 if ( defined( 'WP_INSTALLING' ) )
210 if ( !function_exists( 'get_themes' ) )
211 require_once( ABSPATH . 'wp-includes/theme.php' );
213 $installed_themes = get_themes( );
214 $last_update = get_site_transient( 'update_themes' );
215 if ( ! is_object($last_update) )
216 $last_update = new stdClass;
218 $timeout = 'load-themes.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours
219 $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time( ) - $last_update->last_checked );
223 $exclude_fields = array('Template Files', 'Stylesheet Files', 'Status', 'Theme Root', 'Theme Root URI', 'Template Dir', 'Stylesheet Dir', 'Description', 'Tags', 'Screenshot');
225 // Put slug of current theme into request.
226 $themes['current_theme'] = get_option( 'stylesheet' );
228 foreach ( (array) $installed_themes as $theme_title => $theme ) {
229 $themes[$theme['Stylesheet']] = array();
230 $checked[$theme['Stylesheet']] = $theme['Version'];
232 $themes[$theme['Stylesheet']]['Name'] = $theme['Name'];
233 $themes[$theme['Stylesheet']]['Version'] = $theme['Version'];
235 foreach ( (array) $theme as $key => $value ) {
236 if ( !in_array($key, $exclude_fields) )
237 $themes[$theme['Stylesheet']][$key] = $value;
241 $theme_changed = false;
242 foreach ( $checked as $slug => $v ) {
243 $update_request->checked[ $slug ] = $v;
245 if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
246 $theme_changed = true;
249 if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
250 foreach ( $last_update->response as $slug => $update_details ) {
251 if ( ! isset($checked[ $slug ]) ) {
252 $theme_changed = true;
258 if ( $time_not_changed && !$theme_changed )
261 // Update last_checked for current to prevent multiple blocking requests if request hangs
262 $last_update->last_checked = time();
263 set_site_transient( 'update_themes', $last_update );
266 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
267 'body' => array( 'themes' => serialize( $themes ) ),
268 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
271 $raw_response = wp_remote_post( 'http://api.wordpress.org/themes/update-check/1.0/', $options );
273 if ( is_wp_error( $raw_response ) )
276 if ( 200 != $raw_response['response']['code'] )
279 $new_update = new stdClass;
280 $new_update->last_checked = time( );
281 $response = unserialize( $raw_response['body'] );
283 $new_update->checked = $checked;
284 $new_update->response = $response;
287 set_site_transient( 'update_themes', $new_update );
290 function _maybe_update_core() {
293 $current = get_site_transient( 'update_core' );
295 if ( isset( $current->last_checked ) &&
296 43200 > ( time() - $current->last_checked ) &&
297 isset( $current->version_checked ) &&
298 $current->version_checked == $wp_version )
304 * Check the last time plugins were run before checking plugin versions.
306 * This might have been backported to WordPress 2.6.1 for performance reasons.
307 * This is used for the wp-admin to check only so often instead of every page
313 function _maybe_update_plugins() {
314 $current = get_site_transient( 'update_plugins' );
315 if ( isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked ) )
321 * Check themes versions only after a duration of time.
323 * This is for performance reasons to make sure that on the theme version
324 * checker is not run on every page load.
329 function _maybe_update_themes( ) {
330 $current = get_site_transient( 'update_themes' );
331 if ( isset( $current->last_checked ) && 43200 > ( time( ) - $current->last_checked ) )
337 add_action( 'admin_init', '_maybe_update_core' );
338 add_action( 'wp_version_check', 'wp_version_check' );
340 add_action( 'load-plugins.php', 'wp_update_plugins' );
341 add_action( 'load-update.php', 'wp_update_plugins' );
342 add_action( 'load-update-core.php', 'wp_update_plugins' );
343 add_action( 'admin_init', '_maybe_update_plugins' );
344 add_action( 'wp_update_plugins', 'wp_update_plugins' );
346 add_action( 'load-themes.php', 'wp_update_themes' );
347 add_action( 'load-update.php', 'wp_update_themes' );
348 add_action( 'load-update-core.php', 'wp_update_themes' );
349 add_action( 'admin_init', '_maybe_update_themes' );
350 add_action( 'wp_update_themes', 'wp_update_themes' );
352 if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
353 wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
355 if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
356 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
358 if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
359 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');