Brak opisu

update.php 29KB

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