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