]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/update.php
Wordpress 3.1.1-scripts
[autoinstalls/wordpress.git] / wp-includes / update.php
1 <?php
2 /**
3  * A simple set of functions to check our version 1.0 update service.
4  *
5  * @package WordPress
6  * @since 2.3.0
7  */
8
9 /**
10  * Check WordPress version against the newest version.
11  *
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
14  * isn't installing.
15  *
16  * @package WordPress
17  * @since 2.3.0
18  * @uses $wp_version Used to check against the newest WordPress version.
19  *
20  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
21  */
22 function wp_version_check() {
23         if ( defined('WP_INSTALLING') )
24                 return;
25
26         global $wp_version, $wpdb, $wp_local_package;
27         $php_version = phpversion();
28
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;
34         }
35
36         $locale = apply_filters( 'core_version_check_locale', get_locale() );
37
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 );
41
42         if ( method_exists( $wpdb, 'db_version' ) )
43                 $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
44         else
45                 $mysql_version = 'N/A';
46
47         if ( is_multisite( ) ) {
48                 $user_count = get_user_count( );
49                 $num_blogs = get_blog_count( );
50                 $wp_install = network_site_url( );
51                 $multisite_enabled = 1;
52         } else {
53                 $user_count = count_users( );
54                 $multisite_enabled = 0;
55                 $num_blogs = 1;
56                 $wp_install = home_url( '/' );
57         }
58
59         $local_package = isset( $wp_local_package )? $wp_local_package : '';
60         $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
62         $options = array(
63                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
64                 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
65                 'headers' => array(
66                         'wp_install' => $wp_install,
67                         'wp_blog' => home_url( '/' )
68                 )
69         );
70
71         $response = wp_remote_get($url, $options);
72
73         if ( is_wp_error( $response ) )
74                 return false;
75
76         if ( 200 != $response['response']['code'] )
77                 return false;
78
79         $body = trim( $response['body'] );
80         $body = str_replace(array("\r\n", "\r"), "\n", $body);
81         $new_options = array();
82         foreach ( explode( "\n\n", $body ) as $entry ) {
83                 $returns = explode("\n", $entry);
84                 $new_option = new stdClass();
85                 $new_option->response = esc_attr( $returns[0] );
86                 if ( isset( $returns[1] ) )
87                         $new_option->url = esc_url( $returns[1] );
88                 if ( isset( $returns[2] ) )
89                         $new_option->package = esc_url( $returns[2] );
90                 if ( isset( $returns[3] ) )
91                         $new_option->current = esc_attr( $returns[3] );
92                 if ( isset( $returns[4] ) )
93                         $new_option->locale = esc_attr( $returns[4] );
94                 if ( isset( $returns[5] ) )
95                         $new_option->php_version = esc_attr( $returns[5] );
96                 if ( isset( $returns[6] ) )
97                         $new_option->mysql_version = esc_attr( $returns[6] );
98                 $new_options[] = $new_option;
99         }
100
101         $updates = new stdClass();
102         $updates->updates = $new_options;
103         $updates->last_checked = time();
104         $updates->version_checked = $wp_version;
105         set_site_transient( 'update_core',  $updates);
106 }
107
108 /**
109  * Check plugin versions against the latest versions hosted on WordPress.org.
110  *
111  * The WordPress version, PHP version, and Locale is sent along with a list of
112  * all plugins installed. Checks against the WordPress server at
113  * api.wordpress.org. Will only check if WordPress isn't installing.
114  *
115  * @package WordPress
116  * @since 2.3.0
117  * @uses $wp_version Used to notify the WordPress version.
118  *
119  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
120  */
121 function wp_update_plugins() {
122         global $wp_version;
123
124         if ( defined('WP_INSTALLING') )
125                 return false;
126
127         // If running blog-side, bail unless we've not checked in the last 12 hours
128         if ( !function_exists( 'get_plugins' ) )
129                 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
130
131         $plugins = get_plugins();
132         $active  = get_option( 'active_plugins', array() );
133         $current = get_site_transient( 'update_plugins' );
134         if ( ! is_object($current) )
135                 $current = new stdClass;
136
137         $new_option = new stdClass;
138         $new_option->last_checked = time();
139         $timeout = 'load-plugins.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours
140         $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
141
142         $plugin_changed = false;
143         foreach ( $plugins as $file => $p ) {
144                 $new_option->checked[ $file ] = $p['Version'];
145
146                 if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
147                         $plugin_changed = true;
148         }
149
150         if ( isset ( $current->response ) && is_array( $current->response ) ) {
151                 foreach ( $current->response as $plugin_file => $update_details ) {
152                         if ( ! isset($plugins[ $plugin_file ]) ) {
153                                 $plugin_changed = true;
154                                 break;
155                         }
156                 }
157         }
158
159         // Bail if we've checked in the last 12 hours and if nothing has changed
160         if ( $time_not_changed && !$plugin_changed )
161                 return false;
162
163         // Update last_checked for current to prevent multiple blocking requests if request hangs
164         $current->last_checked = time();
165         set_site_transient( 'update_plugins', $current );
166
167         $to_send = (object) compact('plugins', 'active');
168
169         $options = array(
170                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
171                 'body' => array( 'plugins' => serialize( $to_send ) ),
172                 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
173         );
174
175         $raw_response = wp_remote_post('http://api.wordpress.org/plugins/update-check/1.0/', $options);
176
177         if ( is_wp_error( $raw_response ) )
178                 return false;
179
180         if ( 200 != $raw_response['response']['code'] )
181                 return false;
182
183         $response = unserialize( $raw_response['body'] );
184
185         if ( false !== $response )
186                 $new_option->response = $response;
187         else
188                 $new_option->response = array();
189
190         set_site_transient( 'update_plugins', $new_option );
191 }
192
193 /**
194  * Check theme versions against the latest versions hosted on WordPress.org.
195  *
196  * A list of all themes installed in sent to WP. Checks against the
197  * WordPress server at api.wordpress.org. Will only check if WordPress isn't
198  * installing.
199  *
200  * @package WordPress
201  * @since 2.7.0
202  * @uses $wp_version Used to notify the WordPress version.
203  *
204  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
205  */
206 function wp_update_themes( ) {
207         global $wp_version;
208
209         if ( defined( 'WP_INSTALLING' ) )
210                 return false;
211
212         if ( !function_exists( 'get_themes' ) )
213                 require_once( ABSPATH . 'wp-includes/theme.php' );
214
215         $installed_themes = get_themes( );
216         $last_update = get_site_transient( 'update_themes' );
217         if ( ! is_object($last_update) )
218                 $last_update = new stdClass;
219
220         $timeout = 'load-themes.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours
221         $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time( ) - $last_update->last_checked );
222
223         $themes = array();
224         $checked = array();
225         $exclude_fields = array('Template Files', 'Stylesheet Files', 'Status', 'Theme Root', 'Theme Root URI', 'Template Dir', 'Stylesheet Dir', 'Description', 'Tags', 'Screenshot');
226
227         // Put slug of current theme into request.
228         $themes['current_theme'] = get_option( 'stylesheet' );
229
230         foreach ( (array) $installed_themes as $theme_title => $theme ) {
231                 $themes[$theme['Stylesheet']] = array();
232                 $checked[$theme['Stylesheet']] = $theme['Version'];
233
234                 $themes[$theme['Stylesheet']]['Name'] = $theme['Name'];
235                 $themes[$theme['Stylesheet']]['Version'] = $theme['Version'];
236
237                 foreach ( (array) $theme as $key => $value ) {
238                         if ( !in_array($key, $exclude_fields) )
239                                 $themes[$theme['Stylesheet']][$key] = $value;
240                 }
241         }
242
243         $theme_changed = false;
244         foreach ( $checked as $slug => $v ) {
245                 $update_request->checked[ $slug ] = $v;
246
247                 if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
248                         $theme_changed = true;
249         }
250
251         if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
252                 foreach ( $last_update->response as $slug => $update_details ) {
253                         if ( ! isset($checked[ $slug ]) ) {
254                                 $theme_changed = true;
255                                 break;
256                         }
257                 }
258         }
259
260         if ( $time_not_changed && !$theme_changed )
261                 return false;
262
263         // Update last_checked for current to prevent multiple blocking requests if request hangs
264         $last_update->last_checked = time();
265         set_site_transient( 'update_themes', $last_update );
266
267         $options = array(
268                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
269                 'body'                  => array( 'themes' => serialize( $themes ) ),
270                 'user-agent'    => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
271         );
272
273         $raw_response = wp_remote_post( 'http://api.wordpress.org/themes/update-check/1.0/', $options );
274
275         if ( is_wp_error( $raw_response ) )
276                 return false;
277
278         if ( 200 != $raw_response['response']['code'] )
279                 return false;
280
281         $new_update = new stdClass;
282         $new_update->last_checked = time( );
283         $response = unserialize( $raw_response['body'] );
284         if ( $response ) {
285                 $new_update->checked = $checked;
286                 $new_update->response = $response;
287         }
288
289         set_site_transient( 'update_themes', $new_update );
290 }
291
292 function _maybe_update_core() {
293         global $wp_version;
294
295         $current = get_site_transient( 'update_core' );
296
297         if ( isset( $current->last_checked ) &&
298                 43200 > ( time() - $current->last_checked ) &&
299                 isset( $current->version_checked ) &&
300                 $current->version_checked == $wp_version )
301                 return;
302
303         wp_version_check();
304 }
305 /**
306  * Check the last time plugins were run before checking plugin versions.
307  *
308  * This might have been backported to WordPress 2.6.1 for performance reasons.
309  * This is used for the wp-admin to check only so often instead of every page
310  * load.
311  *
312  * @since 2.7.0
313  * @access private
314  */
315 function _maybe_update_plugins() {
316         $current = get_site_transient( 'update_plugins' );
317         if ( isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked ) )
318                 return;
319         wp_update_plugins();
320 }
321
322 /**
323  * Check themes versions only after a duration of time.
324  *
325  * This is for performance reasons to make sure that on the theme version
326  * checker is not run on every page load.
327  *
328  * @since 2.7.0
329  * @access private
330  */
331 function _maybe_update_themes( ) {
332         $current = get_site_transient( 'update_themes' );
333         if ( isset( $current->last_checked ) && 43200 > ( time( ) - $current->last_checked ) )
334                 return;
335
336         wp_update_themes();
337 }
338
339 /**
340  * Schedule core, theme, and plugin update checks.
341  *
342  * @since 3.1.0
343  */
344 function wp_schedule_update_checks() {
345         if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
346                 wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
347
348         if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
349                 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
350
351         if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
352                 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
353 }
354
355 if ( ! is_main_site() )
356         return;
357
358 add_action( 'admin_init', '_maybe_update_core' );
359 add_action( 'wp_version_check', 'wp_version_check' );
360
361 add_action( 'load-plugins.php', 'wp_update_plugins' );
362 add_action( 'load-update.php', 'wp_update_plugins' );
363 add_action( 'load-update-core.php', 'wp_update_plugins' );
364 add_action( 'admin_init', '_maybe_update_plugins' );
365 add_action( 'wp_update_plugins', 'wp_update_plugins' );
366
367 add_action( 'load-themes.php', 'wp_update_themes' );
368 add_action( 'load-update.php', 'wp_update_themes' );
369 add_action( 'load-update-core.php', 'wp_update_themes' );
370 add_action( 'admin_init', '_maybe_update_themes' );
371 add_action( 'wp_update_themes', 'wp_update_themes' );
372
373 add_action('init', 'wp_schedule_update_checks');
374
375 ?>