]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/update.php
WordPress 3.7.1
[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  * @param array $extra_stats Extra statistics to report to the WordPress.org API.
21  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
22  */
23 function wp_version_check( $extra_stats = array() ) {
24         if ( defined('WP_INSTALLING') )
25                 return;
26
27         global $wpdb, $wp_local_package;
28         include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
29         $php_version = phpversion();
30
31         $current = get_site_transient( 'update_core' );
32         $translations = wp_get_installed_translations( 'core' );
33
34         if ( ! is_object($current) ) {
35                 $current = new stdClass;
36                 $current->updates = array();
37                 $current->version_checked = $wp_version;
38         }
39
40         // Wait 60 seconds between multiple version check requests
41         $timeout = 60;
42         $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
43         if ( $time_not_changed && empty( $extra_stats ) )
44                 return false;
45
46         $locale = get_locale();
47         /**
48          * Filter the locale requested for WordPress core translations.
49          *
50          * @since 2.8.0
51          *
52          * @param string $locale Current locale.
53          */
54         $locale = apply_filters( 'core_version_check_locale', $locale );
55
56         // Update last_checked for current to prevent multiple blocking requests if request hangs
57         $current->last_checked = time();
58         set_site_transient( 'update_core', $current );
59
60         if ( method_exists( $wpdb, 'db_version' ) )
61                 $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
62         else
63                 $mysql_version = 'N/A';
64
65         if ( is_multisite() ) {
66                 $user_count = get_user_count();
67                 $num_blogs = get_blog_count();
68                 $wp_install = network_site_url();
69                 $multisite_enabled = 1;
70         } else {
71                 $user_count = count_users();
72                 $user_count = $user_count['total_users'];
73                 $multisite_enabled = 0;
74                 $num_blogs = 1;
75                 $wp_install = home_url( '/' );
76         }
77
78         $query = array(
79                 'version'           => $wp_version,
80                 'php'               => $php_version,
81                 'locale'            => $locale,
82                 'mysql'             => $mysql_version,
83                 'local_package'     => isset( $wp_local_package ) ? $wp_local_package : '',
84                 'blogs'             => $num_blogs,
85                 'users'             => $user_count,
86                 'multisite_enabled' => $multisite_enabled,
87         );
88
89         $post_body = array(
90                 'translations' => json_encode( $translations ),
91         );
92
93         if ( $extra_stats )
94                 $post_body = array_merge( $post_body, $extra_stats );
95
96         $url = $http_url = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, null, '&' );
97         if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
98                 $url = set_url_scheme( $url, 'https' );
99
100         $options = array(
101                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
102                 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
103                 'headers' => array(
104                         'wp_install' => $wp_install,
105                         'wp_blog' => home_url( '/' )
106                 ),
107                 'body' => $post_body,
108         );
109
110         $response = wp_remote_post( $url, $options );
111         if ( $ssl && is_wp_error( $response ) ) {
112                 trigger_error( __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ) . ' ' . '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)', headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE );
113                 $response = wp_remote_post( $http_url, $options );
114         }
115
116         if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
117                 return false;
118
119         $body = trim( wp_remote_retrieve_body( $response ) );
120         $body = json_decode( $body, true );
121
122         if ( ! is_array( $body ) || ! isset( $body['offers'] ) )
123                 return false;
124
125         $offers = $body['offers'];
126
127         foreach ( $offers as &$offer ) {
128                 foreach ( $offer as $offer_key => $value ) {
129                         if ( 'packages' == $offer_key )
130                                 $offer['packages'] = (object) array_intersect_key( array_map( 'esc_url', $offer['packages'] ),
131                                         array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' ) );
132                         elseif ( 'download' == $offer_key )
133                                 $offer['download'] = esc_url( $value );
134                         else
135                                 $offer[ $offer_key ] = esc_html( $value );
136                 }
137                 $offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',
138                         'packages', 'current', 'version', 'php_version', 'mysql_version', 'new_bundled', 'partial_version', 'notify_email' ), '' ) );
139         }
140
141         $updates = new stdClass();
142         $updates->updates = $offers;
143         $updates->last_checked = time();
144         $updates->version_checked = $wp_version;
145
146         if ( isset( $body['translations'] ) )
147                 $updates->translations = $body['translations'];
148
149         set_site_transient( 'update_core',  $updates);
150 }
151
152 /**
153  * Check plugin versions against the latest versions hosted on WordPress.org.
154  *
155  * The WordPress version, PHP version, and Locale is sent along with a list of
156  * all plugins installed. Checks against the WordPress server at
157  * api.wordpress.org. Will only check if WordPress isn't installing.
158  *
159  * @package WordPress
160  * @since 2.3.0
161  * @uses $wp_version Used to notify the WordPress version.
162  *
163  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
164  */
165 function wp_update_plugins() {
166         include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
167
168         if ( defined('WP_INSTALLING') )
169                 return false;
170
171         // If running blog-side, bail unless we've not checked in the last 12 hours
172         if ( !function_exists( 'get_plugins' ) )
173                 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
174
175         $plugins = get_plugins();
176         $translations = wp_get_installed_translations( 'plugins' );
177
178         $active  = get_option( 'active_plugins', array() );
179         $current = get_site_transient( 'update_plugins' );
180         if ( ! is_object($current) )
181                 $current = new stdClass;
182
183         $new_option = new stdClass;
184         $new_option->last_checked = time();
185
186         // Check for update on a different schedule, depending on the page.
187         switch ( current_filter() ) {
188                 case 'upgrader_process_complete' :
189                         $timeout = 0;
190                         break;
191                 case 'load-update-core.php' :
192                         $timeout = MINUTE_IN_SECONDS;
193                         break;
194                 case 'load-plugins.php' :
195                 case 'load-update.php' :
196                         $timeout = HOUR_IN_SECONDS;
197                         break;
198                 default :
199                         $timeout = 12 * HOUR_IN_SECONDS;
200         }
201
202         $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
203
204         if ( $time_not_changed ) {
205                 $plugin_changed = false;
206                 foreach ( $plugins as $file => $p ) {
207                         $new_option->checked[ $file ] = $p['Version'];
208
209                         if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
210                                 $plugin_changed = true;
211                 }
212
213                 if ( isset ( $current->response ) && is_array( $current->response ) ) {
214                         foreach ( $current->response as $plugin_file => $update_details ) {
215                                 if ( ! isset($plugins[ $plugin_file ]) ) {
216                                         $plugin_changed = true;
217                                         break;
218                                 }
219                         }
220                 }
221
222                 // Bail if we've checked recently and if nothing has changed
223                 if ( ! $plugin_changed )
224                         return false;
225         }
226
227         // Update last_checked for current to prevent multiple blocking requests if request hangs
228         $current->last_checked = time();
229         set_site_transient( 'update_plugins', $current );
230
231         $to_send = compact( 'plugins', 'active' );
232
233         $locales = array( get_locale() );
234         /**
235          * Filter the locales requested for plugin translations.
236          *
237          * @since 3.7.0
238          *
239          * @param array $locales Plugin locale. Default is current locale of the site.
240          */
241         $locales = apply_filters( 'plugins_update_check_locales', $locales );
242
243         $options = array(
244                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
245                 'body' => array(
246                         'plugins'      => json_encode( $to_send ),
247                         'translations' => json_encode( $translations ),
248                         'locale'       => json_encode( $locales ),
249                 ),
250                 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
251         );
252
253         $url = $http_url = 'http://api.wordpress.org/plugins/update-check/1.1/';
254         if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
255                 $url = set_url_scheme( $url, 'https' );
256
257         $raw_response = wp_remote_post( $url, $options );
258         if ( $ssl && is_wp_error( $raw_response ) ) {
259                 trigger_error( __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ) . ' ' . '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)', headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE );
260                 $raw_response = wp_remote_post( $http_url, $options );
261         }
262
263         if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
264                 return false;
265
266         $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
267         foreach ( $response['plugins'] as &$plugin ) {
268                 $plugin = (object) $plugin;
269         }
270         unset( $plugin );
271
272         if ( is_array( $response ) ) {
273                 $new_option->response = $response['plugins'];
274                 $new_option->translations = $response['translations'];
275         } else {
276                 $new_option->response = array();
277                 $new_option->translations = array();
278         }
279
280         set_site_transient( 'update_plugins', $new_option );
281 }
282
283 /**
284  * Check theme versions against the latest versions hosted on WordPress.org.
285  *
286  * A list of all themes installed in sent to WP. Checks against the
287  * WordPress server at api.wordpress.org. Will only check if WordPress isn't
288  * installing.
289  *
290  * @package WordPress
291  * @since 2.7.0
292  * @uses $wp_version Used to notify the WordPress version.
293  *
294  * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
295  */
296 function wp_update_themes() {
297         include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
298
299         if ( defined( 'WP_INSTALLING' ) )
300                 return false;
301
302         $installed_themes = wp_get_themes();
303         $translations = wp_get_installed_translations( 'themes' );
304
305         $last_update = get_site_transient( 'update_themes' );
306         if ( ! is_object($last_update) )
307                 $last_update = new stdClass;
308
309         $themes = $checked = $request = array();
310
311         // Put slug of current theme into request.
312         $request['active'] = get_option( 'stylesheet' );
313
314         foreach ( $installed_themes as $theme ) {
315                 $checked[ $theme->get_stylesheet() ] = $theme->get('Version');
316
317                 $themes[ $theme->get_stylesheet() ] = array(
318                         'Name'       => $theme->get('Name'),
319                         'Title'      => $theme->get('Name'),
320                         'Version'    => $theme->get('Version'),
321                         'Author'     => $theme->get('Author'),
322                         'Author URI' => $theme->get('AuthorURI'),
323                         'Template'   => $theme->get_template(),
324                         'Stylesheet' => $theme->get_stylesheet(),
325                 );
326         }
327
328         // Check for update on a different schedule, depending on the page.
329         switch ( current_filter() ) {
330                 case 'upgrader_process_complete' :
331                         $timeout = 0;
332                         break;
333                 case 'load-update-core.php' :
334                         $timeout = MINUTE_IN_SECONDS;
335                         break;
336                 case 'load-themes.php' :
337                 case 'load-update.php' :
338                         $timeout = HOUR_IN_SECONDS;
339                         break;
340                 default :
341                         $timeout = 12 * HOUR_IN_SECONDS;
342         }
343
344         $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
345
346         if ( $time_not_changed ) {
347                 $theme_changed = false;
348                 foreach ( $checked as $slug => $v ) {
349                         if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
350                                 $theme_changed = true;
351                 }
352
353                 if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
354                         foreach ( $last_update->response as $slug => $update_details ) {
355                                 if ( ! isset($checked[ $slug ]) ) {
356                                         $theme_changed = true;
357                                         break;
358                                 }
359                         }
360                 }
361
362                 // Bail if we've checked recently and if nothing has changed
363                 if ( ! $theme_changed )
364                         return false;
365         }
366
367         // Update last_checked for current to prevent multiple blocking requests if request hangs
368         $last_update->last_checked = time();
369         set_site_transient( 'update_themes', $last_update );
370
371         $request['themes'] = $themes;
372
373         $locales = array( get_locale() );
374         /**
375          * Filter the locales requested for theme translations.
376          *
377          * @since 3.7.0
378          *
379          * @param array $locales Theme locale. Default is current locale of the site.
380          */
381         $locales = apply_filters( 'themes_update_check_locales', $locales );
382
383         $options = array(
384                 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
385                 'body' => array(
386                         'themes'       => json_encode( $request ),
387                         'translations' => json_encode( $translations ),
388                         'locale'       => json_encode( $locales ),
389                 ),
390                 'user-agent'    => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
391         );
392
393         $url = $http_url = 'http://api.wordpress.org/themes/update-check/1.1/';
394         if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
395                 $url = set_url_scheme( $url, 'https' );
396
397         $raw_response = wp_remote_post( $url, $options );
398         if ( $ssl && is_wp_error( $raw_response ) ) {
399                 trigger_error( __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ) . ' ' . '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)', headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE );
400                 $raw_response = wp_remote_post( $http_url, $options );
401         }
402
403         if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
404                 return false;
405
406         $new_update = new stdClass;
407         $new_update->last_checked = time();
408         $new_update->checked = $checked;
409
410         $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
411
412         if ( is_array( $response ) ) {
413                 $new_update->response     = $response['themes'];
414                 $new_update->translations = $response['translations'];
415         }
416
417         set_site_transient( 'update_themes', $new_update );
418 }
419
420 /**
421  * Performs WordPress automatic background updates.
422  *
423  * @since 3.7.0
424  */
425 function wp_maybe_auto_update() {
426         include_once ABSPATH . '/wp-admin/includes/admin.php';
427         include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
428
429         $upgrader = new WP_Automatic_Updater;
430         $upgrader->run();
431 }
432
433 /**
434  * Retrieves a list of all language updates available.
435  *
436  * @since 3.7.0
437  */
438 function wp_get_translation_updates() {
439         $updates = array();
440         $transients = array( 'update_core' => 'core', 'update_plugins' => 'plugin', 'update_themes' => 'theme' );
441         foreach ( $transients as $transient => $type ) {
442
443                 $transient = get_site_transient( $transient );
444                 if ( empty( $transient->translations ) )
445                         continue;
446
447                 foreach ( $transient->translations as $translation ) {
448                         $updates[] = (object) $translation;
449                 }
450         }
451
452         return $updates;
453 }
454
455 /*
456  * Collect counts and UI strings for available updates
457  *
458  * @since 3.3.0
459  *
460  * @return array
461  */
462 function wp_get_update_data() {
463         $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0, 'translations' => 0 );
464
465         if ( $plugins = current_user_can( 'update_plugins' ) ) {
466                 $update_plugins = get_site_transient( 'update_plugins' );
467                 if ( ! empty( $update_plugins->response ) )
468                         $counts['plugins'] = count( $update_plugins->response );
469         }
470
471         if ( $themes = current_user_can( 'update_themes' ) ) {
472                 $update_themes = get_site_transient( 'update_themes' );
473                 if ( ! empty( $update_themes->response ) )
474                         $counts['themes'] = count( $update_themes->response );
475         }
476
477         if ( ( $core = current_user_can( 'update_core' ) ) && function_exists( 'get_core_updates' ) ) {
478                 $update_wordpress = get_core_updates( array('dismissed' => false) );
479                 if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') )
480                         $counts['wordpress'] = 1;
481         }
482
483         if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() )
484                 $counts['translations'] = 1;
485
486         $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
487         $titles = array();
488         if ( $counts['wordpress'] )
489                 $titles['wordpress'] = sprintf( __( '%d WordPress Update'), $counts['wordpress'] );
490         if ( $counts['plugins'] )
491                 $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
492         if ( $counts['themes'] )
493                 $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
494         if ( $counts['translations'] )
495                 $titles['translations'] = __( 'Translation Updates' );
496
497         $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
498
499         $update_data = array( 'counts' => $counts, 'title' => $update_title );
500         /**
501          * Filter the returned array of update data for plugins, themes, and WordPress core.
502          *
503          * @since 3.5.0
504          *
505          * @param array $update_data {
506          *     Fetched update data.
507          *
508          *     @type array   $counts       An array of counts for available plugin, theme, and WordPress updates.
509          *     @type string  $update_title Titles of available updates.
510          * }
511          * @param array $titles An array of update counts and UI strings for available updates.
512          */
513         return apply_filters( 'wp_get_update_data', $update_data, $titles );
514 }
515
516 function _maybe_update_core() {
517         include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
518
519         $current = get_site_transient( 'update_core' );
520
521         if ( isset( $current->last_checked ) &&
522                 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
523                 isset( $current->version_checked ) &&
524                 $current->version_checked == $wp_version )
525                 return;
526
527         wp_version_check();
528 }
529 /**
530  * Check the last time plugins were run before checking plugin versions.
531  *
532  * This might have been backported to WordPress 2.6.1 for performance reasons.
533  * This is used for the wp-admin to check only so often instead of every page
534  * load.
535  *
536  * @since 2.7.0
537  * @access private
538  */
539 function _maybe_update_plugins() {
540         $current = get_site_transient( 'update_plugins' );
541         if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
542                 return;
543         wp_update_plugins();
544 }
545
546 /**
547  * Check themes versions only after a duration of time.
548  *
549  * This is for performance reasons to make sure that on the theme version
550  * checker is not run on every page load.
551  *
552  * @since 2.7.0
553  * @access private
554  */
555 function _maybe_update_themes() {
556         $current = get_site_transient( 'update_themes' );
557         if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
558                 return;
559
560         wp_update_themes();
561 }
562
563 /**
564  * Schedule core, theme, and plugin update checks.
565  *
566  * @since 3.1.0
567  */
568 function wp_schedule_update_checks() {
569         if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
570                 wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
571
572         if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
573                 wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
574
575         if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
576                 wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
577
578         if ( ! wp_next_scheduled( 'wp_maybe_auto_update' ) && ! defined( 'WP_INSTALLING' ) ) {
579                 // Schedule auto updates for 7 a.m. and 7 p.m. in the timezone of the site.
580                 $next = strtotime( 'today 7am' );
581                 $now = time();
582                 // Find the next instance of 7 a.m. or 7 p.m., but skip it if it is within 3 hours from now.
583                 while ( ( $now + 3 * HOUR_IN_SECONDS ) > $next ) {
584                         $next += 12 * HOUR_IN_SECONDS;
585                 }
586                 $next = $next - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
587                 wp_schedule_event( $next, 'twicedaily', 'wp_maybe_auto_update' );
588         }
589 }
590
591 if ( ( ! is_main_site() && ! is_network_admin() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) )
592         return;
593
594 add_action( 'admin_init', '_maybe_update_core' );
595 add_action( 'wp_version_check', 'wp_version_check' );
596 add_action( 'upgrader_process_complete', 'wp_version_check', 10, 0 );
597
598 add_action( 'load-plugins.php', 'wp_update_plugins' );
599 add_action( 'load-update.php', 'wp_update_plugins' );
600 add_action( 'load-update-core.php', 'wp_update_plugins' );
601 add_action( 'admin_init', '_maybe_update_plugins' );
602 add_action( 'wp_update_plugins', 'wp_update_plugins' );
603 add_action( 'upgrader_process_complete', 'wp_update_plugins' );
604
605 add_action( 'load-themes.php', 'wp_update_themes' );
606 add_action( 'load-update.php', 'wp_update_themes' );
607 add_action( 'load-update-core.php', 'wp_update_themes' );
608 add_action( 'admin_init', '_maybe_update_themes' );
609 add_action( 'wp_update_themes', 'wp_update_themes' );
610 add_action( 'upgrader_process_complete', 'wp_update_themes' );
611
612 add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
613
614 add_action('init', 'wp_schedule_update_checks');