update.php 34 KB

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