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

/wp-includes/update.php

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