You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

733 lines
23 KiB

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