Aucune description

update.php 29KB

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