update.php 25 KB

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