update.php 23 KB

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