]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/update.php
7fdaede33cdcf8ab4dfbe60722c8dcdf32609754
[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 $wpdb, $wp_local_package;
27         include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
28         $php_version = phpversion();
29
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;
35         }
36
37         // Wait 60 seconds between multiple version check requests
38         $timeout = 60;
39         $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
40         if ( $time_not_changed )
41                 return false;
42
43         $locale = apply_filters( 'core_version_check_locale', get_locale() );
44
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 );
48
49         if ( method_exists( $wpdb, 'db_version' ) )
50                 $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
51         else
52                 $mysql_version = 'N/A';
53
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;
59         } else {
60                 $user_count = count_users( );
61                 $multisite_enabled = 0;
62                 $num_blogs = 1;
63                 $wp_install = home_url( '/' );
64         }
65
66         $query = array(
67                 'version'           => $wp_version,
68                 'php'               => $php_version,
69                 'locale'            => $locale,
70                 'mysql'             => $mysql_version,
71                 'local_package'     => isset( $wp_local_package ) ? $wp_local_package : '',
72                 'blogs'             => $num_blogs,
73                 'users'             => $user_count['total_users'],
74                 'multisite_enabled' => $multisite_enabled
75         );
76
77         $url = 'http://api.wordpress.org/core/version-check/1.6/?' . http_build_query( $query, null, '&' );
78
79         $options = array(
80                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
81                 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
82                 'headers' => array(
83                         'wp_install' => $wp_install,
84                         'wp_blog' => home_url( '/' )
85                 )
86         );
87
88         $response = wp_remote_get($url, $options);
89
90         if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
91                 return false;
92
93         $body = trim( wp_remote_retrieve_body( $response ) );
94         $body = maybe_unserialize( $body );
95
96         if ( ! is_array( $body ) || ! isset( $body['offers'] ) )
97                 return false;
98
99         $offers = $body['offers'];
100
101         foreach ( $offers as &$offer ) {
102                 foreach ( $offer as $offer_key => $value ) {
103                         if ( 'packages' == $offer_key )
104                                 $offer['packages'] = (object) array_intersect_key( array_map( 'esc_url', $offer['packages'] ),
105                                         array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial' ), '' ) );
106                         elseif ( 'download' == $offer_key )
107                                 $offer['download'] = esc_url( $value );
108                         else
109                                 $offer[ $offer_key ] = esc_html( $value );
110                 }
111                 $offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',
112                         'packages', 'current', 'php_version', 'mysql_version', 'new_bundled', 'partial_version' ), '' ) );
113         }
114
115         $updates = new stdClass();
116         $updates->updates = $offers;
117         $updates->last_checked = time();
118         $updates->version_checked = $wp_version;
119         set_site_transient( 'update_core',  $updates);
120 }
121
122 /**
123  * Check plugin versions against the latest versions hosted on WordPress.org.
124  *
125  * The WordPress version, PHP version, and Locale is sent along with a list of
126  * all plugins installed. Checks against the WordPress server at
127  * api.wordpress.org. Will only check if WordPress isn't installing.
128  *
129  * @package WordPress
130  * @since 2.3.0
131  * @uses $wp_version Used to notify the WordPress version.
132  *
133  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
134  */
135 function wp_update_plugins() {
136         include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
137
138         if ( defined('WP_INSTALLING') )
139                 return false;
140
141         // If running blog-side, bail unless we've not checked in the last 12 hours
142         if ( !function_exists( 'get_plugins' ) )
143                 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
144
145         $plugins = get_plugins();
146         $active  = get_option( 'active_plugins', array() );
147         $current = get_site_transient( 'update_plugins' );
148         if ( ! is_object($current) )
149                 $current = new stdClass;
150
151         $new_option = new stdClass;
152         $new_option->last_checked = time();
153
154         // Check for update on a different schedule, depending on the page.
155         switch ( current_filter() ) {
156                 case 'load-update-core.php' :
157                         $timeout = 60; // 1 min
158                         break;
159                 case 'load-plugins.php' :
160                 case 'load-update.php' :
161                         $timeout = 3600; // 1 hour
162                         break;
163                 default :
164                         $timeout = 43200; // 12 hours
165         }
166
167         $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
168
169         if ( $time_not_changed ) {
170                 $plugin_changed = false;
171                 foreach ( $plugins as $file => $p ) {
172                         $new_option->checked[ $file ] = $p['Version'];
173
174                         if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
175                                 $plugin_changed = true;
176                 }
177
178                 if ( isset ( $current->response ) && is_array( $current->response ) ) {
179                         foreach ( $current->response as $plugin_file => $update_details ) {
180                                 if ( ! isset($plugins[ $plugin_file ]) ) {
181                                         $plugin_changed = true;
182                                         break;
183                                 }
184                         }
185                 }
186
187                 // Bail if we've checked recently and if nothing has changed
188                 if ( ! $plugin_changed )
189                         return false;
190         }
191
192         // Update last_checked for current to prevent multiple blocking requests if request hangs
193         $current->last_checked = time();
194         set_site_transient( 'update_plugins', $current );
195
196         $to_send = (object) compact('plugins', 'active');
197
198         $options = array(
199                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
200                 'body' => array( 'plugins' => serialize( $to_send ) ),
201                 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
202         );
203
204         $raw_response = wp_remote_post('http://api.wordpress.org/plugins/update-check/1.0/', $options);
205
206         if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
207                 return false;
208
209         $response = maybe_unserialize( wp_remote_retrieve_body( $raw_response ) );
210
211         if ( is_array( $response ) )
212                 $new_option->response = $response;
213         else
214                 $new_option->response = array();
215
216         set_site_transient( 'update_plugins', $new_option );
217 }
218
219 /**
220  * Check theme versions against the latest versions hosted on WordPress.org.
221  *
222  * A list of all themes installed in sent to WP. Checks against the
223  * WordPress server at api.wordpress.org. Will only check if WordPress isn't
224  * installing.
225  *
226  * @package WordPress
227  * @since 2.7.0
228  * @uses $wp_version Used to notify the WordPress version.
229  *
230  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
231  */
232 function wp_update_themes() {
233         include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
234
235         if ( defined( 'WP_INSTALLING' ) )
236                 return false;
237
238         $installed_themes = wp_get_themes();
239         $last_update = get_site_transient( 'update_themes' );
240         if ( ! is_object($last_update) )
241                 $last_update = new stdClass;
242
243         $themes = array();
244         $checked = array();
245
246         // Put slug of current theme into request.
247         $themes['current_theme'] = get_option( 'stylesheet' );
248
249         foreach ( $installed_themes as $theme ) {
250                 $checked[ $theme->get_stylesheet() ] = $theme->get('Version');
251
252                 $themes[ $theme->get_stylesheet() ] = array(
253                         'Name'       => $theme->get('Name'),
254                         'Title'      => $theme->get('Name'),
255                         'Version'    => $theme->get('Version'),
256                         'Author'     => $theme->get('Author'),
257                         'Author URI' => $theme->get('AuthorURI'),
258                         'Template'   => $theme->get_template(),
259                         'Stylesheet' => $theme->get_stylesheet(),
260                 );
261         }
262
263         // Check for update on a different schedule, depending on the page.
264         switch ( current_filter() ) {
265                 case 'load-update-core.php' :
266                         $timeout = 60; // 1 min
267                         break;
268                 case 'load-themes.php' :
269                 case 'load-update.php' :
270                         $timeout = 3600; // 1 hour
271                         break;
272                 default :
273                         $timeout = 43200; // 12 hours
274         }
275
276         $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time( ) - $last_update->last_checked );
277
278         if ( $time_not_changed ) {
279                 $theme_changed = false;
280                 foreach ( $checked as $slug => $v ) {
281                         $update_request->checked[ $slug ] = $v;
282
283                         if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
284                                 $theme_changed = true;
285                 }
286
287                 if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
288                         foreach ( $last_update->response as $slug => $update_details ) {
289                                 if ( ! isset($checked[ $slug ]) ) {
290                                         $theme_changed = true;
291                                         break;
292                                 }
293                         }
294                 }
295
296                 // Bail if we've checked recently and if nothing has changed
297                 if ( ! $theme_changed )
298                         return false;
299         }
300
301         // Update last_checked for current to prevent multiple blocking requests if request hangs
302         $last_update->last_checked = time();
303         set_site_transient( 'update_themes', $last_update );
304
305         $options = array(
306                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
307                 'body'                  => array( 'themes' => serialize( $themes ) ),
308                 'user-agent'    => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
309         );
310
311         $raw_response = wp_remote_post( 'http://api.wordpress.org/themes/update-check/1.0/', $options );
312
313         if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
314                 return false;
315
316         $new_update = new stdClass;
317         $new_update->last_checked = time( );
318         $new_update->checked = $checked;
319
320         $response = maybe_unserialize( wp_remote_retrieve_body( $raw_response ) );
321         if ( is_array( $response ) )
322                 $new_update->response = $response;
323
324         set_site_transient( 'update_themes', $new_update );
325 }
326
327 /*
328  * Collect counts and UI strings for available updates
329  *
330  * @since 3.3.0
331  *
332  * @return array
333  */
334 function wp_get_update_data() {
335         $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0 );
336
337         if ( current_user_can( 'update_plugins' ) ) {
338                 $update_plugins = get_site_transient( 'update_plugins' );
339                 if ( ! empty( $update_plugins->response ) )
340                         $counts['plugins'] = count( $update_plugins->response );
341         }
342
343         if ( current_user_can( 'update_themes' ) ) {
344                 $update_themes = get_site_transient( 'update_themes' );
345                 if ( ! empty( $update_themes->response ) )
346                         $counts['themes'] = count( $update_themes->response );
347         }
348
349         if ( function_exists( 'get_core_updates' ) && current_user_can( 'update_core' ) ) {
350                 $update_wordpress = get_core_updates( array('dismissed' => false) );
351                 if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') )
352                         $counts['wordpress'] = 1;
353         }
354
355         $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'];
356         $update_title = array();
357         if ( $counts['wordpress'] )
358                 $update_title[] = sprintf(__('%d WordPress Update'), $counts['wordpress']);
359         if ( $counts['plugins'] )
360                 $update_title[] = sprintf(_n('%d Plugin Update', '%d Plugin Updates', $counts['plugins']), $counts['plugins']);
361         if ( $counts['themes'] )
362                 $update_title[] = sprintf(_n('%d Theme Update', '%d Theme Updates', $counts['themes']), $counts['themes']);
363
364         $update_title = ! empty( $update_title ) ? esc_attr( implode( ', ', $update_title ) ) : '';
365
366         return array( 'counts' => $counts, 'title' => $update_title );
367 }
368
369 function _maybe_update_core() {
370         include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
371
372         $current = get_site_transient( 'update_core' );
373
374         if ( isset( $current->last_checked ) &&
375                 43200 > ( time() - $current->last_checked ) &&
376                 isset( $current->version_checked ) &&
377                 $current->version_checked == $wp_version )
378                 return;
379
380         wp_version_check();
381 }
382 /**
383  * Check the last time plugins were run before checking plugin versions.
384  *
385  * This might have been backported to WordPress 2.6.1 for performance reasons.
386  * This is used for the wp-admin to check only so often instead of every page
387  * load.
388  *
389  * @since 2.7.0
390  * @access private
391  */
392 function _maybe_update_plugins() {
393         $current = get_site_transient( 'update_plugins' );
394         if ( isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked ) )
395                 return;
396         wp_update_plugins();
397 }
398
399 /**
400  * Check themes versions only after a duration of time.
401  *
402  * This is for performance reasons to make sure that on the theme version
403  * checker is not run on every page load.
404  *
405  * @since 2.7.0
406  * @access private
407  */
408 function _maybe_update_themes( ) {
409         $current = get_site_transient( 'update_themes' );
410         if ( isset( $current->last_checked ) && 43200 > ( time( ) - $current->last_checked ) )
411                 return;
412
413         wp_update_themes();
414 }
415
416 /**
417  * Schedule core, theme, and plugin update checks.
418  *
419  * @since 3.1.0
420  */
421 function wp_schedule_update_checks() {
422         if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
423                 wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
424
425         if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
426                 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
427
428         if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
429                 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
430 }
431
432 if ( ( ! is_main_site() && ! is_network_admin() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) )
433         return;
434
435 add_action( 'admin_init', '_maybe_update_core' );
436 add_action( 'wp_version_check', 'wp_version_check' );
437
438 add_action( 'load-plugins.php', 'wp_update_plugins' );
439 add_action( 'load-update.php', 'wp_update_plugins' );
440 add_action( 'load-update-core.php', 'wp_update_plugins' );
441 add_action( 'admin_init', '_maybe_update_plugins' );
442 add_action( 'wp_update_plugins', 'wp_update_plugins' );
443
444 add_action( 'load-themes.php', 'wp_update_themes' );
445 add_action( 'load-update.php', 'wp_update_themes' );
446 add_action( 'load-update-core.php', 'wp_update_themes' );
447 add_action( 'admin_init', '_maybe_update_themes' );
448 add_action( 'wp_update_themes', 'wp_update_themes' );
449
450 add_action('init', 'wp_schedule_update_checks');