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