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_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_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';
46 $local_package = isset( $wp_local_package )? $wp_local_package : '';
47 $url = "http://api.wordpress.org/core/version-check/1.3/?version=$wp_version&php=$php_version&locale=$locale&mysql=$mysql_version&local_package=$local_package";
50 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
51 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
54 $response = wp_remote_get($url, $options);
56 if ( is_wp_error( $response ) )
59 if ( 200 != $response['response']['code'] )
62 $body = trim( $response['body'] );
63 $body = str_replace(array("\r\n", "\r"), "\n", $body);
64 $new_options = array();
65 foreach( explode( "\n\n", $body ) as $entry) {
66 $returns = explode("\n", $entry);
67 $new_option = new stdClass();
68 $new_option->response = esc_attr( $returns[0] );
69 if ( isset( $returns[1] ) )
70 $new_option->url = esc_url( $returns[1] );
71 if ( isset( $returns[2] ) )
72 $new_option->package = esc_url( $returns[2] );
73 if ( isset( $returns[3] ) )
74 $new_option->current = esc_attr( $returns[3] );
75 if ( isset( $returns[4] ) )
76 $new_option->locale = esc_attr( $returns[4] );
77 $new_options[] = $new_option;
80 $updates = new stdClass();
81 $updates->updates = $new_options;
82 $updates->last_checked = time();
83 $updates->version_checked = $wp_version;
84 set_transient( 'update_core', $updates);
88 * Check plugin versions against the latest versions hosted on WordPress.org.
90 * The WordPress version, PHP version, and Locale is sent along with a list of
91 * all plugins installed. Checks against the WordPress server at
92 * api.wordpress.org. Will only check if WordPress isn't installing.
96 * @uses $wp_version Used to notidy the WordPress version.
98 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
100 function wp_update_plugins() {
103 if ( defined('WP_INSTALLING') )
106 // If running blog-side, bail unless we've not checked in the last 12 hours
107 if ( !function_exists( 'get_plugins' ) )
108 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
110 $plugins = get_plugins();
111 $active = get_option( 'active_plugins' );
112 $current = get_transient( 'update_plugins' );
113 if ( ! is_object($current) )
114 $current = new stdClass;
116 $new_option = new stdClass;
117 $new_option->last_checked = time();
118 $timeout = 'load-plugins.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours
119 $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
121 $plugin_changed = false;
122 foreach ( $plugins as $file => $p ) {
123 $new_option->checked[ $file ] = $p['Version'];
125 if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
126 $plugin_changed = true;
129 if ( isset ( $current->response ) && is_array( $current->response ) ) {
130 foreach ( $current->response as $plugin_file => $update_details ) {
131 if ( ! isset($plugins[ $plugin_file ]) ) {
132 $plugin_changed = true;
138 // Bail if we've checked in the last 12 hours and if nothing has changed
139 if ( $time_not_changed && !$plugin_changed )
142 // Update last_checked for current to prevent multiple blocking requests if request hangs
143 $current->last_checked = time();
144 set_transient( 'update_plugins', $current );
146 $to_send = (object)compact('plugins', 'active');
149 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
150 'body' => array( 'plugins' => serialize( $to_send ) ),
151 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
154 $raw_response = wp_remote_post('http://api.wordpress.org/plugins/update-check/1.0/', $options);
156 if ( is_wp_error( $raw_response ) )
159 if( 200 != $raw_response['response']['code'] )
162 $response = unserialize( $raw_response['body'] );
164 if ( false !== $response )
165 $new_option->response = $response;
167 $new_option->response = array();
169 set_transient( 'update_plugins', $new_option );
173 * Check theme versions against the latest versions hosted on WordPress.org.
175 * A list of all themes installed in sent to WP. Checks against the
176 * WordPress server at api.wordpress.org. Will only check if WordPress isn't
181 * @uses $wp_version Used to notidy the WordPress version.
183 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
185 function wp_update_themes( ) {
188 if( defined( 'WP_INSTALLING' ) )
191 if( !function_exists( 'get_themes' ) )
192 require_once( ABSPATH . 'wp-includes/theme.php' );
194 $installed_themes = get_themes( );
195 $current_theme = get_transient( 'update_themes' );
196 if ( ! is_object($current_theme) )
197 $current_theme = new stdClass;
199 $new_option = new stdClass;
200 $new_option->last_checked = time( );
201 $timeout = 'load-themes.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours
202 $time_not_changed = isset( $current_theme->last_checked ) && $timeout > ( time( ) - $current_theme->last_checked );
206 $themes['current_theme'] = (array) $current_theme;
207 foreach( (array) $installed_themes as $theme_title => $theme ) {
208 $themes[$theme['Stylesheet']] = array();
209 $checked[$theme['Stylesheet']] = $theme['Version'];
211 foreach( (array) $theme as $key => $value ) {
212 $themes[$theme['Stylesheet']][$key] = $value;
216 $theme_changed = false;
217 foreach ( $checked as $slug => $v ) {
218 $new_option->checked[ $slug ] = $v;
220 if ( !isset( $current_theme->checked[ $slug ] ) || strval($current_theme->checked[ $slug ]) !== strval($v) )
221 $theme_changed = true;
224 if ( isset ( $current_theme->response ) && is_array( $current_theme->response ) ) {
225 foreach ( $current_theme->response as $slug => $update_details ) {
226 if ( ! isset($checked[ $slug ]) ) {
227 $theme_changed = true;
233 if( $time_not_changed && !$theme_changed )
236 // Update last_checked for current to prevent multiple blocking requests if request hangs
237 $current_theme->last_checked = time();
238 set_transient( 'update_themes', $current_theme );
240 $current_theme->template = get_option( 'template' );
243 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
244 'body' => array( 'themes' => serialize( $themes ) ),
245 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
248 $raw_response = wp_remote_post( 'http://api.wordpress.org/themes/update-check/1.0/', $options );
250 if( is_wp_error( $raw_response ) )
253 if( 200 != $raw_response['response']['code'] )
256 $response = unserialize( $raw_response['body'] );
258 $new_option->checked = $checked;
259 $new_option->response = $response;
262 set_transient( 'update_themes', $new_option );
265 function _maybe_update_core() {
268 $current = get_transient( 'update_core' );
270 if ( isset( $current->last_checked ) &&
271 43200 > ( time() - $current->last_checked ) &&
272 isset( $current->version_checked ) &&
273 $current->version_checked == $wp_version )
279 * Check the last time plugins were run before checking plugin versions.
281 * This might have been backported to WordPress 2.6.1 for performance reasons.
282 * This is used for the wp-admin to check only so often instead of every page
288 function _maybe_update_plugins() {
289 $current = get_transient( 'update_plugins' );
290 if ( isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked ) )
296 * Check themes versions only after a duration of time.
298 * This is for performance reasons to make sure that on the theme version
299 * checker is not run on every page load.
304 function _maybe_update_themes( ) {
305 $current = get_transient( 'update_themes' );
306 if( isset( $current->last_checked ) && 43200 > ( time( ) - $current->last_checked ) )
312 add_action( 'admin_init', '_maybe_update_core' );
313 add_action( 'wp_version_check', 'wp_version_check' );
315 add_action( 'load-plugins.php', 'wp_update_plugins' );
316 add_action( 'load-update.php', 'wp_update_plugins' );
317 add_action( 'load-update-core.php', 'wp_update_plugins' );
318 add_action( 'admin_init', '_maybe_update_plugins' );
319 add_action( 'wp_update_plugins', 'wp_update_plugins' );
321 add_action( 'load-themes.php', 'wp_update_themes' );
322 add_action( 'load-update.php', 'wp_update_themes' );
323 add_action( 'admin_init', '_maybe_update_themes' );
324 add_action( 'wp_update_themes', 'wp_update_themes' );
326 if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
327 wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
329 if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
330 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
332 if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
333 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');