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

/wp-includes/update.php

http://cartonbank.googlecode.com/
PHP | 354 lines | 221 code | 64 blank | 69 comment | 60 complexity | 8492e56cd386fb66d4fe9769769a9b7d MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1, AGPL-1.0, LGPL-3.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 $wp_version, $wpdb, $wp_local_package;
  25. $php_version = phpversion();
  26. $current = get_site_transient( 'update_core' );
  27. if ( ! is_object($current) ) {
  28. $current = new stdClass;
  29. $current->updates = array();
  30. $current->version_checked = $wp_version;
  31. }
  32. $locale = apply_filters( 'core_version_check_locale', get_locale() );
  33. // Update last_checked for current to prevent multiple blocking requests if request hangs
  34. $current->last_checked = time();
  35. set_site_transient( 'update_core', $current );
  36. if ( method_exists( $wpdb, 'db_version' ) )
  37. $mysql_version = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
  38. else
  39. $mysql_version = 'N/A';
  40. $num_blogs = 1;
  41. $wp_install = home_url( '/' );
  42. $multisite_enabled = 0;
  43. $user_count = count_users( );
  44. if ( is_multisite( ) ) {
  45. $num_blogs = get_blog_count( );
  46. $wp_install = network_site_url( );
  47. $multisite_enabled = 1;
  48. }
  49. $local_package = isset( $wp_local_package )? $wp_local_package : '';
  50. $url = "http://api.wordpress.org/core/version-check/1.5/?version=$wp_version&php=$php_version&locale=$locale&mysql=$mysql_version&local_package=$local_package&blogs=$num_blogs&users={$user_count['total_users']}&multisite_enabled=$multisite_enabled";
  51. $options = array(
  52. 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
  53. 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
  54. 'headers' => array(
  55. 'wp_install' => $wp_install,
  56. 'wp_blog' => home_url( '/' )
  57. )
  58. );
  59. $response = wp_remote_get($url, $options);
  60. if ( is_wp_error( $response ) )
  61. return false;
  62. if ( 200 != $response['response']['code'] )
  63. return false;
  64. $body = trim( $response['body'] );
  65. $body = str_replace(array("\r\n", "\r"), "\n", $body);
  66. $new_options = array();
  67. foreach ( explode( "\n\n", $body ) as $entry ) {
  68. $returns = explode("\n", $entry);
  69. $new_option = new stdClass();
  70. $new_option->response = esc_attr( $returns[0] );
  71. if ( isset( $returns[1] ) )
  72. $new_option->url = esc_url( $returns[1] );
  73. if ( isset( $returns[2] ) )
  74. $new_option->package = esc_url( $returns[2] );
  75. if ( isset( $returns[3] ) )
  76. $new_option->current = esc_attr( $returns[3] );
  77. if ( isset( $returns[4] ) )
  78. $new_option->locale = esc_attr( $returns[4] );
  79. if ( isset( $returns[5] ) )
  80. $new_option->php_version = esc_attr( $returns[5] );
  81. if ( isset( $returns[6] ) )
  82. $new_option->mysql_version = esc_attr( $returns[6] );
  83. $new_options[] = $new_option;
  84. }
  85. $updates = new stdClass();
  86. $updates->updates = $new_options;
  87. $updates->last_checked = time();
  88. $updates->version_checked = $wp_version;
  89. set_site_transient( 'update_core', $updates);
  90. }
  91. /**
  92. * Check plugin versions against the latest versions hosted on WordPress.org.
  93. *
  94. * The WordPress version, PHP version, and Locale is sent along with a list of
  95. * all plugins installed. Checks against the WordPress server at
  96. * api.wordpress.org. Will only check if WordPress isn't installing.
  97. *
  98. * @package WordPress
  99. * @since 2.3.0
  100. * @uses $wp_version Used to notidy the WordPress version.
  101. *
  102. * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
  103. */
  104. function wp_update_plugins() {
  105. global $wp_version;
  106. if ( defined('WP_INSTALLING') )
  107. return false;
  108. // If running blog-side, bail unless we've not checked in the last 12 hours
  109. if ( !function_exists( 'get_plugins' ) )
  110. require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  111. $plugins = get_plugins();
  112. $active = get_option( 'active_plugins', array() );
  113. $current = get_site_transient( 'update_plugins' );
  114. if ( ! is_object($current) )
  115. $current = new stdClass;
  116. $new_option = new stdClass;
  117. $new_option->last_checked = time();
  118. $timeout = 'load-plugins.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours
  119. $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
  120. $plugin_changed = false;
  121. foreach ( $plugins as $file => $p ) {
  122. $new_option->checked[ $file ] = $p['Version'];
  123. if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) )
  124. $plugin_changed = true;
  125. }
  126. if ( isset ( $current->response ) && is_array( $current->response ) ) {
  127. foreach ( $current->response as $plugin_file => $update_details ) {
  128. if ( ! isset($plugins[ $plugin_file ]) ) {
  129. $plugin_changed = true;
  130. break;
  131. }
  132. }
  133. }
  134. // Bail if we've checked in the last 12 hours and if nothing has changed
  135. if ( $time_not_changed && !$plugin_changed )
  136. return false;
  137. // Update last_checked for current to prevent multiple blocking requests if request hangs
  138. $current->last_checked = time();
  139. set_site_transient( 'update_plugins', $current );
  140. $to_send = (object) compact('plugins', 'active');
  141. $options = array(
  142. 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
  143. 'body' => array( 'plugins' => serialize( $to_send ) ),
  144. 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
  145. );
  146. $raw_response = wp_remote_post('http://api.wordpress.org/plugins/update-check/1.0/', $options);
  147. if ( is_wp_error( $raw_response ) )
  148. return false;
  149. if ( 200 != $raw_response['response']['code'] )
  150. return false;
  151. $response = unserialize( $raw_response['body'] );
  152. if ( false !== $response )
  153. $new_option->response = $response;
  154. else
  155. $new_option->response = array();
  156. set_site_transient( 'update_plugins', $new_option );
  157. }
  158. /**
  159. * Check theme versions against the latest versions hosted on WordPress.org.
  160. *
  161. * A list of all themes installed in sent to WP. Checks against the
  162. * WordPress server at api.wordpress.org. Will only check if WordPress isn't
  163. * installing.
  164. *
  165. * @package WordPress
  166. * @since 2.7.0
  167. * @uses $wp_version Used to notidy the WordPress version.
  168. *
  169. * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
  170. */
  171. function wp_update_themes( ) {
  172. global $wp_version;
  173. if ( defined( 'WP_INSTALLING' ) )
  174. return false;
  175. if ( !function_exists( 'get_themes' ) )
  176. require_once( ABSPATH . 'wp-includes/theme.php' );
  177. $installed_themes = get_themes( );
  178. $current_theme = get_site_transient( 'update_themes' );
  179. if ( ! is_object($current_theme) )
  180. $current_theme = new stdClass;
  181. $new_option = new stdClass;
  182. $new_option->last_checked = time( );
  183. $timeout = 'load-themes.php' == current_filter() ? 3600 : 43200; //Check for updated every 60 minutes if hitting the themes page, Else, check every 12 hours
  184. $time_not_changed = isset( $current_theme->last_checked ) && $timeout > ( time( ) - $current_theme->last_checked );
  185. $themes = array();
  186. $checked = array();
  187. $themes['current_theme'] = (array) $current_theme;
  188. foreach ( (array) $installed_themes as $theme_title => $theme ) {
  189. $themes[$theme['Stylesheet']] = array();
  190. $checked[$theme['Stylesheet']] = $theme['Version'];
  191. foreach ( (array) $theme as $key => $value )
  192. $themes[$theme['Stylesheet']][$key] = $value;
  193. }
  194. $theme_changed = false;
  195. foreach ( $checked as $slug => $v ) {
  196. $new_option->checked[ $slug ] = $v;
  197. if ( !isset( $current_theme->checked[ $slug ] ) || strval($current_theme->checked[ $slug ]) !== strval($v) )
  198. $theme_changed = true;
  199. }
  200. if ( isset ( $current_theme->response ) && is_array( $current_theme->response ) ) {
  201. foreach ( $current_theme->response as $slug => $update_details ) {
  202. if ( ! isset($checked[ $slug ]) ) {
  203. $theme_changed = true;
  204. break;
  205. }
  206. }
  207. }
  208. if ( $time_not_changed && !$theme_changed )
  209. return false;
  210. // Update last_checked for current to prevent multiple blocking requests if request hangs
  211. $current_theme->last_checked = time();
  212. set_site_transient( 'update_themes', $current_theme );
  213. $current_theme->template = get_option( 'template' );
  214. $options = array(
  215. 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
  216. 'body' => array( 'themes' => serialize( $themes ) ),
  217. 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
  218. );
  219. $raw_response = wp_remote_post( 'http://api.wordpress.org/themes/update-check/1.0/', $options );
  220. if ( is_wp_error( $raw_response ) )
  221. return false;
  222. if ( 200 != $raw_response['response']['code'] )
  223. return false;
  224. $response = unserialize( $raw_response['body'] );
  225. if ( $response ) {
  226. $new_option->checked = $checked;
  227. $new_option->response = $response;
  228. }
  229. set_site_transient( 'update_themes', $new_option );
  230. }
  231. function _maybe_update_core() {
  232. global $wp_version;
  233. $current = get_site_transient( 'update_core' );
  234. if ( isset( $current->last_checked ) &&
  235. 43200 > ( time() - $current->last_checked ) &&
  236. isset( $current->version_checked ) &&
  237. $current->version_checked == $wp_version )
  238. return;
  239. wp_version_check();
  240. }
  241. /**
  242. * Check the last time plugins were run before checking plugin versions.
  243. *
  244. * This might have been backported to WordPress 2.6.1 for performance reasons.
  245. * This is used for the wp-admin to check only so often instead of every page
  246. * load.
  247. *
  248. * @since 2.7.0
  249. * @access private
  250. */
  251. function _maybe_update_plugins() {
  252. $current = get_site_transient( 'update_plugins' );
  253. if ( isset( $current->last_checked ) && 43200 > ( time() - $current->last_checked ) )
  254. return;
  255. wp_update_plugins();
  256. }
  257. /**
  258. * Check themes versions only after a duration of time.
  259. *
  260. * This is for performance reasons to make sure that on the theme version
  261. * checker is not run on every page load.
  262. *
  263. * @since 2.7.0
  264. * @access private
  265. */
  266. function _maybe_update_themes( ) {
  267. $current = get_site_transient( 'update_themes' );
  268. if ( isset( $current->last_checked ) && 43200 > ( time( ) - $current->last_checked ) )
  269. return;
  270. wp_update_themes();
  271. }
  272. add_action( 'admin_init', '_maybe_update_core' );
  273. add_action( 'wp_version_check', 'wp_version_check' );
  274. add_action( 'load-plugins.php', 'wp_update_plugins' );
  275. add_action( 'load-update.php', 'wp_update_plugins' );
  276. add_action( 'load-update-core.php', 'wp_update_plugins' );
  277. add_action( 'admin_init', '_maybe_update_plugins' );
  278. add_action( 'wp_update_plugins', 'wp_update_plugins' );
  279. add_action( 'load-themes.php', 'wp_update_themes' );
  280. add_action( 'load-update.php', 'wp_update_themes' );
  281. add_action( 'load-update-core.php', 'wp_update_themes' );
  282. add_action( 'admin_init', '_maybe_update_themes' );
  283. add_action( 'wp_update_themes', 'wp_update_themes' );
  284. if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
  285. wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
  286. if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
  287. wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
  288. if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
  289. wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
  290. ?>