PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/update.php

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