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