]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/update.php
Wordpress 3.3
[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         $locale = apply_filters( 'core_version_check_locale', get_locale() );
38
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 );
42
43         if ( method_exists( $wpdb, 'db_version' ) )
44                 $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
45         else
46                 $mysql_version = 'N/A';
47
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;
53         } else {
54                 $user_count = count_users( );
55                 $multisite_enabled = 0;
56                 $num_blogs = 1;
57                 $wp_install = home_url( '/' );
58         }
59
60         $query = array(
61                 'version'           => $wp_version,
62                 'php'               => $php_version,
63                 'locale'            => $locale,
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
69         );
70
71         $url = 'http://api.wordpress.org/core/version-check/1.6/?' . http_build_query( $query, null, '&' );
72
73         $options = array(
74                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
75                 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
76                 'headers' => array(
77                         'wp_install' => $wp_install,
78                         'wp_blog' => home_url( '/' )
79                 )
80         );
81
82         $response = wp_remote_get($url, $options);
83
84         if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
85                 return false;
86
87         $body = trim( wp_remote_retrieve_body( $response ) );
88         if ( ! $body = maybe_unserialize( $body ) )
89                 return false;
90         if ( ! isset( $body['offers'] ) )
91                 return false;
92         $offers = $body['offers'];
93
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 );
101                         else
102                                 $offer[ $offer_key ] = esc_html( $value );
103                 }
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' ), '' ) );
106         }
107
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);
113 }
114
115 /**
116  * Check plugin versions against the latest versions hosted on WordPress.org.
117  *
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.
121  *
122  * @package WordPress
123  * @since 2.3.0
124  * @uses $wp_version Used to notify the WordPress version.
125  *
126  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
127  */
128 function wp_update_plugins() {
129         include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
130
131         if ( defined('WP_INSTALLING') )
132                 return false;
133
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' );
137
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;
143
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 );
149
150         $plugin_changed = false;
151         foreach ( $plugins as $file => $p ) {
152                 $new_option->checked[ $file ] = $p['Version'];
153
154                 if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
155                         $plugin_changed = true;
156         }
157
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;
162                                 break;
163                         }
164                 }
165         }
166
167         // Bail if we've checked in the last 12 hours and if nothing has changed
168         if ( $time_not_changed && !$plugin_changed )
169                 return false;
170
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 );
174
175         $to_send = (object) compact('plugins', 'active');
176
177         $options = array(
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' )
181         );
182
183         $raw_response = wp_remote_post('http://api.wordpress.org/plugins/update-check/1.0/', $options);
184
185         if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
186                 return false;
187
188         $response = unserialize( wp_remote_retrieve_body( $raw_response ) );
189
190         if ( false !== $response )
191                 $new_option->response = $response;
192         else
193                 $new_option->response = array();
194
195         set_site_transient( 'update_plugins', $new_option );
196 }
197
198 /**
199  * Check theme versions against the latest versions hosted on WordPress.org.
200  *
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
203  * installing.
204  *
205  * @package WordPress
206  * @since 2.7.0
207  * @uses $wp_version Used to notify the WordPress version.
208  *
209  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
210  */
211 function wp_update_themes() {
212         include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
213
214         if ( defined( 'WP_INSTALLING' ) )
215                 return false;
216
217         if ( !function_exists( 'get_themes' ) )
218                 require_once( ABSPATH . 'wp-includes/theme.php' );
219
220         $installed_themes = get_themes( );
221         $last_update = get_site_transient( 'update_themes' );
222         if ( ! is_object($last_update) )
223                 $last_update = new stdClass;
224
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 );
228
229         $themes = array();
230         $checked = array();
231         $exclude_fields = array('Template Files', 'Stylesheet Files', 'Status', 'Theme Root', 'Theme Root URI', 'Template Dir', 'Stylesheet Dir', 'Description', 'Tags', 'Screenshot');
232
233         // Put slug of current theme into request.
234         $themes['current_theme'] = get_option( 'stylesheet' );
235
236         foreach ( (array) $installed_themes as $theme_title => $theme ) {
237                 $themes[$theme['Stylesheet']] = array();
238                 $checked[$theme['Stylesheet']] = $theme['Version'];
239
240                 $themes[$theme['Stylesheet']]['Name'] = $theme['Name'];
241                 $themes[$theme['Stylesheet']]['Version'] = $theme['Version'];
242
243                 foreach ( (array) $theme as $key => $value ) {
244                         if ( !in_array($key, $exclude_fields) )
245                                 $themes[$theme['Stylesheet']][$key] = $value;
246                 }
247         }
248
249         $theme_changed = false;
250         foreach ( $checked as $slug => $v ) {
251                 $update_request->checked[ $slug ] = $v;
252
253                 if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
254                         $theme_changed = true;
255         }
256
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;
261                                 break;
262                         }
263                 }
264         }
265
266         if ( $time_not_changed && !$theme_changed )
267                 return false;
268
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 );
272
273         $options = array(
274                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
275                 'body'                  => array( 'themes' => serialize( $themes ) ),
276                 'user-agent'    => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
277         );
278
279         $raw_response = wp_remote_post( 'http://api.wordpress.org/themes/update-check/1.0/', $options );
280
281         if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
282                 return false;
283
284         $new_update = new stdClass;
285         $new_update->last_checked = time( );
286         $new_update->checked = $checked;
287
288         $response = unserialize( wp_remote_retrieve_body( $raw_response ) );
289         if ( false !== $response )
290                 $new_update->response = $response;
291
292         set_site_transient( 'update_themes', $new_update );
293 }
294
295 /*
296  * Collect counts and UI strings for available updates
297  *
298  * @since 3.3.0
299  *
300  * @return array
301  */
302 function wp_get_update_data() {
303         $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0 );
304
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 );
309         }
310
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 );
315         }
316
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;
321         }
322
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']);
331
332         $update_title = ! empty( $update_title ) ? esc_attr( implode( ', ', $update_title ) ) : '';
333
334         return array( 'counts' => $counts, 'title' => $update_title );
335 }
336
337 function _maybe_update_core() {
338         include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
339
340         $current = get_site_transient( 'update_core' );
341
342         if ( isset( $current->last_checked ) &&
343                 43200 > ( time() - $current->last_checked ) &&
344                 isset( $current->version_checked ) &&
345                 $current->version_checked == $wp_version )
346                 return;
347
348         wp_version_check();
349 }
350 /**
351  * Check the last time plugins were run before checking plugin versions.
352  *
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
355  * load.
356  *
357  * @since 2.7.0
358  * @access private
359  */
360 function _maybe_update_plugins() {
361         $current = get_site_transient( 'update_plugins' );
362         if ( isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked ) )
363                 return;
364         wp_update_plugins();
365 }
366
367 /**
368  * Check themes versions only after a duration of time.
369  *
370  * This is for performance reasons to make sure that on the theme version
371  * checker is not run on every page load.
372  *
373  * @since 2.7.0
374  * @access private
375  */
376 function _maybe_update_themes( ) {
377         $current = get_site_transient( 'update_themes' );
378         if ( isset( $current->last_checked ) && 43200 > ( time( ) - $current->last_checked ) )
379                 return;
380
381         wp_update_themes();
382 }
383
384 /**
385  * Schedule core, theme, and plugin update checks.
386  *
387  * @since 3.1.0
388  */
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');
392
393         if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
394                 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
395
396         if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
397                 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
398 }
399
400 if ( ! is_main_site() && ! is_network_admin() )
401         return;
402
403 add_action( 'admin_init', '_maybe_update_core' );
404 add_action( 'wp_version_check', 'wp_version_check' );
405
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' );
411
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' );
417
418 add_action('init', 'wp_schedule_update_checks');
419
420 ?>