PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/update.php

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