PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/update.php

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