update.php 24 KB

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