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