PageRenderTime 44ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/update.php

https://bitbucket.org/dkrzos/phc
PHP | 449 lines | 286 code | 77 blank | 86 comment | 77 complexity | 2f3b9e14fcc05ad89d1bcc6b32339f33 MD5 | raw file
Possible License(s): GPL-2.0
  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. * @package WordPress
  16. * @since 2.3.0
  17. * @uses $wp_version Used to check against the newest WordPress version.
  18. *
  19. * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
  20. */
  21. function wp_version_check() {
  22. if ( defined('WP_INSTALLING') )
  23. return;
  24. global $wpdb, $wp_local_package;
  25. include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
  26. $php_version = phpversion();
  27. $current = get_site_transient( 'update_core' );
  28. if ( ! is_object($current) ) {
  29. $current = new stdClass;
  30. $current->updates = array();
  31. $current->version_checked = $wp_version;
  32. }
  33. // Wait 60 seconds between multiple version check requests
  34. $timeout = 60;
  35. $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
  36. if ( $time_not_changed )
  37. return false;
  38. $locale = apply_filters( 'core_version_check_locale', get_locale() );
  39. // Update last_checked for current to prevent multiple blocking requests if request hangs
  40. $current->last_checked = time();
  41. set_site_transient( 'update_core', $current );
  42. if ( method_exists( $wpdb, 'db_version' ) )
  43. $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
  44. else
  45. $mysql_version = 'N/A';
  46. if ( is_multisite( ) ) {
  47. $user_count = get_user_count( );
  48. $num_blogs = get_blog_count( );
  49. $wp_install = network_site_url( );
  50. $multisite_enabled = 1;
  51. } else {
  52. $user_count = count_users( );
  53. $user_count = $user_count['total_users'];
  54. $multisite_enabled = 0;
  55. $num_blogs = 1;
  56. $wp_install = home_url( '/' );
  57. }
  58. $query = array(
  59. 'version' => $wp_version,
  60. 'php' => $php_version,
  61. 'locale' => $locale,
  62. 'mysql' => $mysql_version,
  63. 'local_package' => isset( $wp_local_package ) ? $wp_local_package : '',
  64. 'blogs' => $num_blogs,
  65. 'users' => $user_count,
  66. 'multisite_enabled' => $multisite_enabled
  67. );
  68. $url = 'http://api.wordpress.org/core/version-check/1.6/?' . http_build_query( $query, null, '&' );
  69. $options = array(
  70. 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
  71. 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
  72. 'headers' => array(
  73. 'wp_install' => $wp_install,
  74. 'wp_blog' => home_url( '/' )
  75. )
  76. );
  77. $response = wp_remote_get($url, $options);
  78. if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
  79. return false;
  80. $body = trim( wp_remote_retrieve_body( $response ) );
  81. $body = maybe_unserialize( $body );
  82. if ( ! is_array( $body ) || ! isset( $body['offers'] ) )
  83. return false;
  84. $offers = $body['offers'];
  85. foreach ( $offers as &$offer ) {
  86. foreach ( $offer as $offer_key => $value ) {
  87. if ( 'packages' == $offer_key )
  88. $offer['packages'] = (object) array_intersect_key( array_map( 'esc_url', $offer['packages'] ),
  89. array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial' ), '' ) );
  90. elseif ( 'download' == $offer_key )
  91. $offer['download'] = esc_url( $value );
  92. else
  93. $offer[ $offer_key ] = esc_html( $value );
  94. }
  95. $offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',
  96. 'packages', 'current', 'php_version', 'mysql_version', 'new_bundled', 'partial_version' ), '' ) );
  97. }
  98. $updates = new stdClass();
  99. $updates->updates = $offers;
  100. $updates->last_checked = time();
  101. $updates->version_checked = $wp_version;
  102. set_site_transient( 'update_core', $updates);
  103. }
  104. /**
  105. * Check plugin versions against the latest versions hosted on WordPress.org.
  106. *
  107. * The WordPress version, PHP version, and Locale is sent along with a list of
  108. * all plugins installed. Checks against the WordPress server at
  109. * api.wordpress.org. Will only check if WordPress isn't installing.
  110. *
  111. * @package WordPress
  112. * @since 2.3.0
  113. * @uses $wp_version Used to notify the WordPress version.
  114. *
  115. * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
  116. */
  117. function wp_update_plugins() {
  118. include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
  119. if ( defined('WP_INSTALLING') )
  120. return false;
  121. // If running blog-side, bail unless we've not checked in the last 12 hours
  122. if ( !function_exists( 'get_plugins' ) )
  123. require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  124. $plugins = get_plugins();
  125. $active = get_option( 'active_plugins', array() );
  126. $current = get_site_transient( 'update_plugins' );
  127. if ( ! is_object($current) )
  128. $current = new stdClass;
  129. $new_option = new stdClass;
  130. $new_option->last_checked = time();
  131. // Check for update on a different schedule, depending on the page.
  132. switch ( current_filter() ) {
  133. case 'load-update-core.php' :
  134. $timeout = MINUTE_IN_SECONDS;
  135. break;
  136. case 'load-plugins.php' :
  137. case 'load-update.php' :
  138. $timeout = HOUR_IN_SECONDS;
  139. break;
  140. default :
  141. $timeout = 12 * HOUR_IN_SECONDS;
  142. }
  143. $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
  144. if ( $time_not_changed ) {
  145. $plugin_changed = false;
  146. foreach ( $plugins as $file => $p ) {
  147. $new_option->checked[ $file ] = $p['Version'];
  148. if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
  149. $plugin_changed = true;
  150. }
  151. if ( isset ( $current->response ) && is_array( $current->response ) ) {
  152. foreach ( $current->response as $plugin_file => $update_details ) {
  153. if ( ! isset($plugins[ $plugin_file ]) ) {
  154. $plugin_changed = true;
  155. break;
  156. }
  157. }
  158. }
  159. // Bail if we've checked recently and if nothing has changed
  160. if ( ! $plugin_changed )
  161. return false;
  162. }
  163. // Update last_checked for current to prevent multiple blocking requests if request hangs
  164. $current->last_checked = time();
  165. set_site_transient( 'update_plugins', $current );
  166. $to_send = (object) compact('plugins', 'active');
  167. $options = array(
  168. 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
  169. 'body' => array( 'plugins' => serialize( $to_send ) ),
  170. 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
  171. );
  172. $raw_response = wp_remote_post('http://api.wordpress.org/plugins/update-check/1.0/', $options);
  173. if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
  174. return false;
  175. $response = maybe_unserialize( wp_remote_retrieve_body( $raw_response ) );
  176. if ( is_array( $response ) )
  177. $new_option->response = $response;
  178. else
  179. $new_option->response = array();
  180. set_site_transient( 'update_plugins', $new_option );
  181. }
  182. /**
  183. * Check theme versions against the latest versions hosted on WordPress.org.
  184. *
  185. * A list of all themes installed in sent to WP. Checks against the
  186. * WordPress server at api.wordpress.org. Will only check if WordPress isn't
  187. * installing.
  188. *
  189. * @package WordPress
  190. * @since 2.7.0
  191. * @uses $wp_version Used to notify the WordPress version.
  192. *
  193. * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
  194. */
  195. function wp_update_themes() {
  196. include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
  197. if ( defined( 'WP_INSTALLING' ) )
  198. return false;
  199. $installed_themes = wp_get_themes();
  200. $last_update = get_site_transient( 'update_themes' );
  201. if ( ! is_object($last_update) )
  202. $last_update = new stdClass;
  203. $themes = array();
  204. $checked = array();
  205. // Put slug of current theme into request.
  206. $themes['current_theme'] = get_option( 'stylesheet' );
  207. foreach ( $installed_themes as $theme ) {
  208. $checked[ $theme->get_stylesheet() ] = $theme->get('Version');
  209. $themes[ $theme->get_stylesheet() ] = array(
  210. 'Name' => $theme->get('Name'),
  211. 'Title' => $theme->get('Name'),
  212. 'Version' => $theme->get('Version'),
  213. 'Author' => $theme->get('Author'),
  214. 'Author URI' => $theme->get('AuthorURI'),
  215. 'Template' => $theme->get_template(),
  216. 'Stylesheet' => $theme->get_stylesheet(),
  217. );
  218. }
  219. // Check for update on a different schedule, depending on the page.
  220. switch ( current_filter() ) {
  221. case 'load-update-core.php' :
  222. $timeout = MINUTE_IN_SECONDS;
  223. break;
  224. case 'load-themes.php' :
  225. case 'load-update.php' :
  226. $timeout = HOUR_IN_SECONDS;
  227. break;
  228. default :
  229. $timeout = 12 * HOUR_IN_SECONDS;
  230. }
  231. $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time( ) - $last_update->last_checked );
  232. if ( $time_not_changed ) {
  233. $theme_changed = false;
  234. foreach ( $checked as $slug => $v ) {
  235. if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) )
  236. $theme_changed = true;
  237. }
  238. if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) {
  239. foreach ( $last_update->response as $slug => $update_details ) {
  240. if ( ! isset($checked[ $slug ]) ) {
  241. $theme_changed = true;
  242. break;
  243. }
  244. }
  245. }
  246. // Bail if we've checked recently and if nothing has changed
  247. if ( ! $theme_changed )
  248. return false;
  249. }
  250. // Update last_checked for current to prevent multiple blocking requests if request hangs
  251. $last_update->last_checked = time();
  252. set_site_transient( 'update_themes', $last_update );
  253. $options = array(
  254. 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
  255. 'body' => array( 'themes' => serialize( $themes ) ),
  256. 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
  257. );
  258. $raw_response = wp_remote_post( 'http://api.wordpress.org/themes/update-check/1.0/', $options );
  259. if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) )
  260. return false;
  261. $new_update = new stdClass;
  262. $new_update->last_checked = time( );
  263. $new_update->checked = $checked;
  264. $response = maybe_unserialize( wp_remote_retrieve_body( $raw_response ) );
  265. if ( is_array( $response ) )
  266. $new_update->response = $response;
  267. set_site_transient( 'update_themes', $new_update );
  268. }
  269. /*
  270. * Collect counts and UI strings for available updates
  271. *
  272. * @since 3.3.0
  273. *
  274. * @return array
  275. */
  276. function wp_get_update_data() {
  277. $counts = array( 'plugins' => 0, 'themes' => 0, 'wordpress' => 0 );
  278. if ( current_user_can( 'update_plugins' ) ) {
  279. $update_plugins = get_site_transient( 'update_plugins' );
  280. if ( ! empty( $update_plugins->response ) )
  281. $counts['plugins'] = count( $update_plugins->response );
  282. }
  283. if ( current_user_can( 'update_themes' ) ) {
  284. $update_themes = get_site_transient( 'update_themes' );
  285. if ( ! empty( $update_themes->response ) )
  286. $counts['themes'] = count( $update_themes->response );
  287. }
  288. if ( function_exists( 'get_core_updates' ) && current_user_can( 'update_core' ) ) {
  289. $update_wordpress = get_core_updates( array('dismissed' => false) );
  290. if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array('development', 'latest') ) && current_user_can('update_core') )
  291. $counts['wordpress'] = 1;
  292. }
  293. $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'];
  294. $titles = array();
  295. if ( $counts['wordpress'] )
  296. $titles['wordpress'] = sprintf( __( '%d WordPress Update'), $counts['wordpress'] );
  297. if ( $counts['plugins'] )
  298. $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
  299. if ( $counts['themes'] )
  300. $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
  301. $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
  302. return apply_filters( 'wp_get_update_data', array( 'counts' => $counts, 'title' => $update_title ), $titles );
  303. }
  304. function _maybe_update_core() {
  305. include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
  306. $current = get_site_transient( 'update_core' );
  307. if ( isset( $current->last_checked ) &&
  308. 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
  309. isset( $current->version_checked ) &&
  310. $current->version_checked == $wp_version )
  311. return;
  312. wp_version_check();
  313. }
  314. /**
  315. * Check the last time plugins were run before checking plugin versions.
  316. *
  317. * This might have been backported to WordPress 2.6.1 for performance reasons.
  318. * This is used for the wp-admin to check only so often instead of every page
  319. * load.
  320. *
  321. * @since 2.7.0
  322. * @access private
  323. */
  324. function _maybe_update_plugins() {
  325. $current = get_site_transient( 'update_plugins' );
  326. if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) )
  327. return;
  328. wp_update_plugins();
  329. }
  330. /**
  331. * Check themes versions only after a duration of time.
  332. *
  333. * This is for performance reasons to make sure that on the theme version
  334. * checker is not run on every page load.
  335. *
  336. * @since 2.7.0
  337. * @access private
  338. */
  339. function _maybe_update_themes( ) {
  340. $current = get_site_transient( 'update_themes' );
  341. if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time( ) - $current->last_checked ) )
  342. return;
  343. wp_update_themes();
  344. }
  345. /**
  346. * Schedule core, theme, and plugin update checks.
  347. *
  348. * @since 3.1.0
  349. */
  350. function wp_schedule_update_checks() {
  351. if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
  352. wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
  353. if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
  354. wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
  355. if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
  356. wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
  357. }
  358. if ( ( ! is_main_site() && ! is_network_admin() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) )
  359. return;
  360. add_action( 'admin_init', '_maybe_update_core' );
  361. add_action( 'wp_version_check', 'wp_version_check' );
  362. add_action( 'load-plugins.php', 'wp_update_plugins' );
  363. add_action( 'load-update.php', 'wp_update_plugins' );
  364. add_action( 'load-update-core.php', 'wp_update_plugins' );
  365. add_action( 'admin_init', '_maybe_update_plugins' );
  366. add_action( 'wp_update_plugins', 'wp_update_plugins' );
  367. add_action( 'load-themes.php', 'wp_update_themes' );
  368. add_action( 'load-update.php', 'wp_update_themes' );
  369. add_action( 'load-update-core.php', 'wp_update_themes' );
  370. add_action( 'admin_init', '_maybe_update_themes' );
  371. add_action( 'wp_update_themes', 'wp_update_themes' );
  372. add_action('init', 'wp_schedule_update_checks');