PageRenderTime 26ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/update.php

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