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