PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-includes/update.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 836 lines | 537 code | 110 blank | 189 comment | 121 complexity | e10854893a5b038bc74b2f369ab41341 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. * @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 Locale code of the 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. require 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 1 minute between multiple version check requests.
  46. $timeout = MINUTE_IN_SECONDS;
  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 The WordPress version string.
  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. require 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. * @global string $wp_version The WordPress version string.
  380. *
  381. * @param array $extra_stats Extra statistics to report to the WordPress.org API.
  382. */
  383. function wp_update_themes( $extra_stats = array() ) {
  384. if ( wp_installing() ) {
  385. return;
  386. }
  387. // Include an unmodified $wp_version.
  388. require ABSPATH . WPINC . '/version.php';
  389. $installed_themes = wp_get_themes();
  390. $translations = wp_get_installed_translations( 'themes' );
  391. $last_update = get_site_transient( 'update_themes' );
  392. if ( ! is_object( $last_update ) ) {
  393. $last_update = new stdClass;
  394. }
  395. $themes = array();
  396. $checked = array();
  397. $request = array();
  398. // Put slug of current theme into request.
  399. $request['active'] = get_option( 'stylesheet' );
  400. foreach ( $installed_themes as $theme ) {
  401. $checked[ $theme->get_stylesheet() ] = $theme->get( 'Version' );
  402. $themes[ $theme->get_stylesheet() ] = array(
  403. 'Name' => $theme->get( 'Name' ),
  404. 'Title' => $theme->get( 'Name' ),
  405. 'Version' => $theme->get( 'Version' ),
  406. 'Author' => $theme->get( 'Author' ),
  407. 'Author URI' => $theme->get( 'AuthorURI' ),
  408. 'Template' => $theme->get_template(),
  409. 'Stylesheet' => $theme->get_stylesheet(),
  410. );
  411. }
  412. $doing_cron = wp_doing_cron();
  413. // Check for update on a different schedule, depending on the page.
  414. switch ( current_filter() ) {
  415. case 'upgrader_process_complete':
  416. $timeout = 0;
  417. break;
  418. case 'load-update-core.php':
  419. $timeout = MINUTE_IN_SECONDS;
  420. break;
  421. case 'load-themes.php':
  422. case 'load-update.php':
  423. $timeout = HOUR_IN_SECONDS;
  424. break;
  425. default:
  426. if ( $doing_cron ) {
  427. $timeout = 2 * HOUR_IN_SECONDS;
  428. } else {
  429. $timeout = 12 * HOUR_IN_SECONDS;
  430. }
  431. }
  432. $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
  433. if ( $time_not_changed && ! $extra_stats ) {
  434. $theme_changed = false;
  435. foreach ( $checked as $slug => $v ) {
  436. if ( ! isset( $last_update->checked[ $slug ] ) || strval( $last_update->checked[ $slug ] ) !== strval( $v ) ) {
  437. $theme_changed = true;
  438. }
  439. }
  440. if ( isset( $last_update->response ) && is_array( $last_update->response ) ) {
  441. foreach ( $last_update->response as $slug => $update_details ) {
  442. if ( ! isset( $checked[ $slug ] ) ) {
  443. $theme_changed = true;
  444. break;
  445. }
  446. }
  447. }
  448. // Bail if we've checked recently and if nothing has changed.
  449. if ( ! $theme_changed ) {
  450. return;
  451. }
  452. }
  453. // Update last_checked for current to prevent multiple blocking requests if request hangs.
  454. $last_update->last_checked = time();
  455. set_site_transient( 'update_themes', $last_update );
  456. $request['themes'] = $themes;
  457. $locales = array_values( get_available_languages() );
  458. /**
  459. * Filters the locales requested for theme translations.
  460. *
  461. * @since 3.7.0
  462. * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
  463. *
  464. * @param array $locales Theme locales. Default is all available locales of the site.
  465. */
  466. $locales = apply_filters( 'themes_update_check_locales', $locales );
  467. $locales = array_unique( $locales );
  468. if ( $doing_cron ) {
  469. $timeout = 30;
  470. } else {
  471. // Three seconds, plus one extra second for every 10 themes.
  472. $timeout = 3 + (int) ( count( $themes ) / 10 );
  473. }
  474. $options = array(
  475. 'timeout' => $timeout,
  476. 'body' => array(
  477. 'themes' => wp_json_encode( $request ),
  478. 'translations' => wp_json_encode( $translations ),
  479. 'locale' => wp_json_encode( $locales ),
  480. ),
  481. 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
  482. );
  483. if ( $extra_stats ) {
  484. $options['body']['update_stats'] = wp_json_encode( $extra_stats );
  485. }
  486. $url = 'http://api.wordpress.org/themes/update-check/1.1/';
  487. $http_url = $url;
  488. $ssl = wp_http_supports( array( 'ssl' ) );
  489. if ( $ssl ) {
  490. $url = set_url_scheme( $url, 'https' );
  491. }
  492. $raw_response = wp_remote_post( $url, $options );
  493. if ( $ssl && is_wp_error( $raw_response ) ) {
  494. trigger_error(
  495. sprintf(
  496. /* translators: %s: Support forums URL. */
  497. __( '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>.' ),
  498. __( 'https://wordpress.org/support/forums/' )
  499. ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
  500. headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
  501. );
  502. $raw_response = wp_remote_post( $http_url, $options );
  503. }
  504. if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
  505. return;
  506. }
  507. $new_update = new stdClass;
  508. $new_update->last_checked = time();
  509. $new_update->checked = $checked;
  510. $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
  511. if ( is_array( $response ) ) {
  512. $new_update->response = $response['themes'];
  513. $new_update->translations = $response['translations'];
  514. }
  515. set_site_transient( 'update_themes', $new_update );
  516. }
  517. /**
  518. * Performs WordPress automatic background updates.
  519. *
  520. * @since 3.7.0
  521. */
  522. function wp_maybe_auto_update() {
  523. include_once ABSPATH . 'wp-admin/includes/admin.php';
  524. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  525. $upgrader = new WP_Automatic_Updater;
  526. $upgrader->run();
  527. }
  528. /**
  529. * Retrieves a list of all language updates available.
  530. *
  531. * @since 3.7.0
  532. *
  533. * @return object[] Array of translation objects that have available updates.
  534. */
  535. function wp_get_translation_updates() {
  536. $updates = array();
  537. $transients = array(
  538. 'update_core' => 'core',
  539. 'update_plugins' => 'plugin',
  540. 'update_themes' => 'theme',
  541. );
  542. foreach ( $transients as $transient => $type ) {
  543. $transient = get_site_transient( $transient );
  544. if ( empty( $transient->translations ) ) {
  545. continue;
  546. }
  547. foreach ( $transient->translations as $translation ) {
  548. $updates[] = (object) $translation;
  549. }
  550. }
  551. return $updates;
  552. }
  553. /**
  554. * Collect counts and UI strings for available updates
  555. *
  556. * @since 3.3.0
  557. *
  558. * @return array
  559. */
  560. function wp_get_update_data() {
  561. $counts = array(
  562. 'plugins' => 0,
  563. 'themes' => 0,
  564. 'wordpress' => 0,
  565. 'translations' => 0,
  566. );
  567. $plugins = current_user_can( 'update_plugins' );
  568. if ( $plugins ) {
  569. $update_plugins = get_site_transient( 'update_plugins' );
  570. if ( ! empty( $update_plugins->response ) ) {
  571. $counts['plugins'] = count( $update_plugins->response );
  572. }
  573. }
  574. $themes = current_user_can( 'update_themes' );
  575. if ( $themes ) {
  576. $update_themes = get_site_transient( 'update_themes' );
  577. if ( ! empty( $update_themes->response ) ) {
  578. $counts['themes'] = count( $update_themes->response );
  579. }
  580. }
  581. $core = current_user_can( 'update_core' );
  582. if ( $core && function_exists( 'get_core_updates' ) ) {
  583. $update_wordpress = get_core_updates( array( 'dismissed' => false ) );
  584. if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array( 'development', 'latest' ) ) && current_user_can( 'update_core' ) ) {
  585. $counts['wordpress'] = 1;
  586. }
  587. }
  588. if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() ) {
  589. $counts['translations'] = 1;
  590. }
  591. $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
  592. $titles = array();
  593. if ( $counts['wordpress'] ) {
  594. /* translators: %d: Number of available WordPress updates. */
  595. $titles['wordpress'] = sprintf( __( '%d WordPress Update' ), $counts['wordpress'] );
  596. }
  597. if ( $counts['plugins'] ) {
  598. /* translators: %d: Number of available plugin updates. */
  599. $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
  600. }
  601. if ( $counts['themes'] ) {
  602. /* translators: %d: Number of available theme updates. */
  603. $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
  604. }
  605. if ( $counts['translations'] ) {
  606. $titles['translations'] = __( 'Translation Updates' );
  607. }
  608. $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
  609. $update_data = array(
  610. 'counts' => $counts,
  611. 'title' => $update_title,
  612. );
  613. /**
  614. * Filters the returned array of update data for plugins, themes, and WordPress core.
  615. *
  616. * @since 3.5.0
  617. *
  618. * @param array $update_data {
  619. * Fetched update data.
  620. *
  621. * @type array $counts An array of counts for available plugin, theme, and WordPress updates.
  622. * @type string $update_title Titles of available updates.
  623. * }
  624. * @param array $titles An array of update counts and UI strings for available updates.
  625. */
  626. return apply_filters( 'wp_get_update_data', $update_data, $titles );
  627. }
  628. /**
  629. * Determines whether core should be updated.
  630. *
  631. * @since 2.8.0
  632. *
  633. * @global string $wp_version The WordPress version string.
  634. */
  635. function _maybe_update_core() {
  636. // Include an unmodified $wp_version.
  637. require ABSPATH . WPINC . '/version.php';
  638. $current = get_site_transient( 'update_core' );
  639. if ( isset( $current->last_checked, $current->version_checked ) &&
  640. 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
  641. $current->version_checked == $wp_version ) {
  642. return;
  643. }
  644. wp_version_check();
  645. }
  646. /**
  647. * Check the last time plugins were run before checking plugin versions.
  648. *
  649. * This might have been backported to WordPress 2.6.1 for performance reasons.
  650. * This is used for the wp-admin to check only so often instead of every page
  651. * load.
  652. *
  653. * @since 2.7.0
  654. * @access private
  655. */
  656. function _maybe_update_plugins() {
  657. $current = get_site_transient( 'update_plugins' );
  658. if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) ) {
  659. return;
  660. }
  661. wp_update_plugins();
  662. }
  663. /**
  664. * Check themes versions only after a duration of time.
  665. *
  666. * This is for performance reasons to make sure that on the theme version
  667. * checker is not run on every page load.
  668. *
  669. * @since 2.7.0
  670. * @access private
  671. */
  672. function _maybe_update_themes() {
  673. $current = get_site_transient( 'update_themes' );
  674. if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) ) {
  675. return;
  676. }
  677. wp_update_themes();
  678. }
  679. /**
  680. * Schedule core, theme, and plugin update checks.
  681. *
  682. * @since 3.1.0
  683. */
  684. function wp_schedule_update_checks() {
  685. if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() ) {
  686. wp_schedule_event( time(), 'twicedaily', 'wp_version_check' );
  687. }
  688. if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() ) {
  689. wp_schedule_event( time(), 'twicedaily', 'wp_update_plugins' );
  690. }
  691. if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() ) {
  692. wp_schedule_event( time(), 'twicedaily', 'wp_update_themes' );
  693. }
  694. }
  695. /**
  696. * Clear existing update caches for plugins, themes, and core.
  697. *
  698. * @since 4.1.0
  699. */
  700. function wp_clean_update_cache() {
  701. if ( function_exists( 'wp_clean_plugins_cache' ) ) {
  702. wp_clean_plugins_cache();
  703. } else {
  704. delete_site_transient( 'update_plugins' );
  705. }
  706. wp_clean_themes_cache();
  707. delete_site_transient( 'update_core' );
  708. }
  709. if ( ( ! is_main_site() && ! is_network_admin() ) || wp_doing_ajax() ) {
  710. return;
  711. }
  712. add_action( 'admin_init', '_maybe_update_core' );
  713. add_action( 'wp_version_check', 'wp_version_check' );
  714. add_action( 'load-plugins.php', 'wp_update_plugins' );
  715. add_action( 'load-update.php', 'wp_update_plugins' );
  716. add_action( 'load-update-core.php', 'wp_update_plugins' );
  717. add_action( 'admin_init', '_maybe_update_plugins' );
  718. add_action( 'wp_update_plugins', 'wp_update_plugins' );
  719. add_action( 'load-themes.php', 'wp_update_themes' );
  720. add_action( 'load-update.php', 'wp_update_themes' );
  721. add_action( 'load-update-core.php', 'wp_update_themes' );
  722. add_action( 'admin_init', '_maybe_update_themes' );
  723. add_action( 'wp_update_themes', 'wp_update_themes' );
  724. add_action( 'update_option_WPLANG', 'wp_clean_update_cache', 10, 0 );
  725. add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
  726. add_action( 'init', 'wp_schedule_update_checks' );