PageRenderTime 61ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/update.php

https://github.com/visualzach/belfastguitarfest.com
PHP | 382 lines | 259 code | 76 blank | 47 comment | 77 complexity | d8e04530c27e3ccac838718164379fdf MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * WordPress Administration Update API
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Selects the first update version from the update_core option
  10. *
  11. * @return object the response from the API
  12. */
  13. function get_preferred_from_update_core() {
  14. $updates = get_core_updates();
  15. if ( ! is_array( $updates ) )
  16. return false;
  17. if ( empty( $updates ) )
  18. return (object) array( 'response' => 'latest' );
  19. return $updates[0];
  20. }
  21. /**
  22. * Get available core updates
  23. *
  24. * @param array $options Set $options['dismissed'] to true to show dismissed upgrades too,
  25. * set $options['available'] to false to skip not-dismissed updates.
  26. * @return array Array of the update objects
  27. */
  28. function get_core_updates( $options = array() ) {
  29. $options = array_merge( array( 'available' => true, 'dismissed' => false ), $options );
  30. $dismissed = get_site_option( 'dismissed_update_core' );
  31. if ( ! is_array( $dismissed ) )
  32. $dismissed = array();
  33. $from_api = get_site_transient( 'update_core' );
  34. if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) )
  35. return false;
  36. $updates = $from_api->updates;
  37. $result = array();
  38. foreach ( $updates as $update ) {
  39. if ( $update->response == 'autoupdate' )
  40. continue;
  41. if ( array_key_exists( $update->current . '|' . $update->locale, $dismissed ) ) {
  42. if ( $options['dismissed'] ) {
  43. $update->dismissed = true;
  44. $result[] = $update;
  45. }
  46. } else {
  47. if ( $options['available'] ) {
  48. $update->dismissed = false;
  49. $result[] = $update;
  50. }
  51. }
  52. }
  53. return $result;
  54. }
  55. /**
  56. * Gets the best available (and enabled) Auto-Update for WordPress Core.
  57. *
  58. * If there's 1.2.3 and 1.3 on offer, it'll choose 1.3 if the install allows it, else, 1.2.3
  59. *
  60. * @since 3.7.0
  61. *
  62. * @return bool|array False on failure, otherwise the core update offering.
  63. */
  64. function find_core_auto_update() {
  65. $updates = get_site_transient( 'update_core' );
  66. if ( ! $updates || empty( $updates->updates ) )
  67. return false;
  68. include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  69. $auto_update = false;
  70. $upgrader = new WP_Automatic_Updater;
  71. foreach ( $updates->updates as $update ) {
  72. if ( 'autoupdate' != $update->response )
  73. continue;
  74. if ( ! $upgrader->should_update( 'core', $update, ABSPATH ) )
  75. continue;
  76. if ( ! $auto_update || version_compare( $update->current, $auto_update->current, '>' ) )
  77. $auto_update = $update;
  78. }
  79. return $auto_update;
  80. }
  81. /**
  82. * Gets and caches the checksums for the given version of WordPress.
  83. *
  84. * @since 3.7.0
  85. *
  86. * @param string $version Version string to query.
  87. * @param string $locale Locale to query.
  88. * @return bool|array False on failure. An array of checksums on success.
  89. */
  90. function get_core_checksums( $version, $locale ) {
  91. $return = array();
  92. $url = $http_url = 'http://api.wordpress.org/core/checksums/1.0/?' . http_build_query( compact( 'version', 'locale' ), null, '&' );
  93. if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
  94. $url = set_url_scheme( $url, 'https' );
  95. $options = array(
  96. 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
  97. );
  98. $response = wp_remote_get( $url, $options );
  99. if ( $ssl && is_wp_error( $response ) ) {
  100. 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="http://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 );
  101. $response = wp_remote_get( $http_url, $options );
  102. }
  103. if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
  104. return false;
  105. $body = trim( wp_remote_retrieve_body( $response ) );
  106. $body = json_decode( $body, true );
  107. if ( ! is_array( $body ) || ! isset( $body['checksums'] ) || ! is_array( $body['checksums'] ) )
  108. return false;
  109. return $body['checksums'];
  110. }
  111. function dismiss_core_update( $update ) {
  112. $dismissed = get_site_option( 'dismissed_update_core' );
  113. $dismissed[ $update->current . '|' . $update->locale ] = true;
  114. return update_site_option( 'dismissed_update_core', $dismissed );
  115. }
  116. function undismiss_core_update( $version, $locale ) {
  117. $dismissed = get_site_option( 'dismissed_update_core' );
  118. $key = $version . '|' . $locale;
  119. if ( ! isset( $dismissed[$key] ) )
  120. return false;
  121. unset( $dismissed[$key] );
  122. return update_site_option( 'dismissed_update_core', $dismissed );
  123. }
  124. function find_core_update( $version, $locale ) {
  125. $from_api = get_site_transient( 'update_core' );
  126. if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) )
  127. return false;
  128. $updates = $from_api->updates;
  129. foreach ( $updates as $update ) {
  130. if ( $update->current == $version && $update->locale == $locale )
  131. return $update;
  132. }
  133. return false;
  134. }
  135. function core_update_footer( $msg = '' ) {
  136. if ( !current_user_can('update_core') )
  137. return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
  138. $cur = get_preferred_from_update_core();
  139. if ( ! is_object( $cur ) )
  140. $cur = new stdClass;
  141. if ( ! isset( $cur->current ) )
  142. $cur->current = '';
  143. if ( ! isset( $cur->url ) )
  144. $cur->url = '';
  145. if ( ! isset( $cur->response ) )
  146. $cur->response = '';
  147. switch ( $cur->response ) {
  148. case 'development' :
  149. return sprintf( __( 'You are using a development version (%1$s). Cool! Please <a href="%2$s">stay updated</a>.' ), get_bloginfo( 'version', 'display' ), network_admin_url( 'update-core.php' ) );
  150. break;
  151. case 'upgrade' :
  152. return sprintf( '<strong>'.__( '<a href="%1$s">Get Version %2$s</a>' ).'</strong>', network_admin_url( 'update-core.php' ), $cur->current);
  153. break;
  154. case 'latest' :
  155. default :
  156. return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
  157. break;
  158. }
  159. }
  160. add_filter( 'update_footer', 'core_update_footer' );
  161. function update_nag() {
  162. if ( is_multisite() && !current_user_can('update_core') )
  163. return false;
  164. global $pagenow;
  165. if ( 'update-core.php' == $pagenow )
  166. return;
  167. $cur = get_preferred_from_update_core();
  168. if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
  169. return false;
  170. if ( current_user_can('update_core') ) {
  171. $msg = sprintf( __('<a href="http://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! <a href="%2$s">Please update now</a>.'), $cur->current, network_admin_url( 'update-core.php' ) );
  172. } else {
  173. $msg = sprintf( __('<a href="http://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! Please notify the site administrator.'), $cur->current );
  174. }
  175. echo "<div class='update-nag'>$msg</div>";
  176. }
  177. add_action( 'admin_notices', 'update_nag', 3 );
  178. add_action( 'network_admin_notices', 'update_nag', 3 );
  179. // Called directly from dashboard
  180. function update_right_now_message() {
  181. $msg = sprintf( __( 'You are using <span class="b">WordPress %s</span>.' ), get_bloginfo( 'version', 'display' ) );
  182. if ( current_user_can('update_core') ) {
  183. $cur = get_preferred_from_update_core();
  184. if ( isset( $cur->response ) && $cur->response == 'upgrade' )
  185. $msg .= " <a href='" . network_admin_url( 'update-core.php' ) . "' class='button'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
  186. }
  187. echo "<span id='wp-version-message'>$msg</span>";
  188. }
  189. function get_plugin_updates() {
  190. $all_plugins = get_plugins();
  191. $upgrade_plugins = array();
  192. $current = get_site_transient( 'update_plugins' );
  193. foreach ( (array)$all_plugins as $plugin_file => $plugin_data) {
  194. if ( isset( $current->response[ $plugin_file ] ) ) {
  195. $upgrade_plugins[ $plugin_file ] = (object) $plugin_data;
  196. $upgrade_plugins[ $plugin_file ]->update = $current->response[ $plugin_file ];
  197. }
  198. }
  199. return $upgrade_plugins;
  200. }
  201. function wp_plugin_update_rows() {
  202. if ( !current_user_can('update_plugins' ) )
  203. return;
  204. $plugins = get_site_transient( 'update_plugins' );
  205. if ( isset($plugins->response) && is_array($plugins->response) ) {
  206. $plugins = array_keys( $plugins->response );
  207. foreach( $plugins as $plugin_file ) {
  208. add_action( "after_plugin_row_$plugin_file", 'wp_plugin_update_row', 10, 2 );
  209. }
  210. }
  211. }
  212. add_action( 'admin_init', 'wp_plugin_update_rows' );
  213. function wp_plugin_update_row( $file, $plugin_data ) {
  214. $current = get_site_transient( 'update_plugins' );
  215. if ( !isset( $current->response[ $file ] ) )
  216. return false;
  217. $r = $current->response[ $file ];
  218. $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
  219. $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
  220. $details_url = self_admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&section=changelog&TB_iframe=true&width=600&height=800');
  221. $wp_list_table = _get_list_table('WP_Plugins_List_Table');
  222. if ( is_network_admin() || !is_multisite() ) {
  223. echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
  224. if ( ! current_user_can('update_plugins') )
  225. printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>.'), $plugin_name, esc_url($details_url), esc_attr($plugin_name), $r->new_version );
  226. else if ( empty($r->package) )
  227. printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>. <em>Automatic update is unavailable for this plugin.</em>'), $plugin_name, esc_url($details_url), esc_attr($plugin_name), $r->new_version );
  228. else
  229. printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a> or <a href="%5$s">update now</a>.'), $plugin_name, esc_url($details_url), esc_attr($plugin_name), $r->new_version, wp_nonce_url( self_admin_url('update.php?action=upgrade-plugin&plugin=') . $file, 'upgrade-plugin_' . $file) );
  230. do_action( "in_plugin_update_message-$file", $plugin_data, $r );
  231. echo '</div></td></tr>';
  232. }
  233. }
  234. function get_theme_updates() {
  235. $themes = wp_get_themes();
  236. $current = get_site_transient('update_themes');
  237. if ( ! isset( $current->response ) )
  238. return array();
  239. $update_themes = array();
  240. foreach ( $current->response as $stylesheet => $data ) {
  241. $update_themes[ $stylesheet ] = wp_get_theme( $stylesheet );
  242. $update_themes[ $stylesheet ]->update = $data;
  243. }
  244. return $update_themes;
  245. }
  246. function wp_theme_update_rows() {
  247. if ( !current_user_can('update_themes' ) )
  248. return;
  249. $themes = get_site_transient( 'update_themes' );
  250. if ( isset($themes->response) && is_array($themes->response) ) {
  251. $themes = array_keys( $themes->response );
  252. foreach( $themes as $theme ) {
  253. add_action( "after_theme_row_$theme", 'wp_theme_update_row', 10, 2 );
  254. }
  255. }
  256. }
  257. add_action( 'admin_init', 'wp_theme_update_rows' );
  258. function wp_theme_update_row( $theme_key, $theme ) {
  259. $current = get_site_transient( 'update_themes' );
  260. if ( !isset( $current->response[ $theme_key ] ) )
  261. return false;
  262. $r = $current->response[ $theme_key ];
  263. $themes_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
  264. $theme_name = wp_kses( $theme['Name'], $themes_allowedtags );
  265. $details_url = add_query_arg( array( 'TB_iframe' => 'true', 'width' => 1024, 'height' => 800 ), $current->response[ $theme_key ]['url'] );
  266. $wp_list_table = _get_list_table('WP_MS_Themes_List_Table');
  267. echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
  268. if ( ! current_user_can('update_themes') )
  269. printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>.'), $theme['Name'], esc_url($details_url), esc_attr($theme['Name']), $r->new_version );
  270. else if ( empty( $r['package'] ) )
  271. printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>. <em>Automatic update is unavailable for this theme.</em>'), $theme['Name'], esc_url($details_url), esc_attr($theme['Name']), $r['new_version'] );
  272. else
  273. printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a> or <a href="%5$s">update now</a>.'), $theme['Name'], esc_url($details_url), esc_attr($theme['Name']), $r['new_version'], wp_nonce_url( self_admin_url('update.php?action=upgrade-theme&theme=') . $theme_key, 'upgrade-theme_' . $theme_key) );
  274. do_action( "in_theme_update_message-$theme_key", $theme, $r );
  275. echo '</div></td></tr>';
  276. }
  277. function maintenance_nag() {
  278. include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
  279. global $upgrading;
  280. $nag = isset( $upgrading );
  281. if ( ! $nag ) {
  282. $failed = get_site_option( 'auto_core_update_failed' );
  283. /*
  284. * If an update failed critically, we may have copied over version.php but not other files.
  285. * In that case, if the install claims we're running the version we attempted, nag.
  286. * This is serious enough to err on the side of nagging.
  287. *
  288. * If we simply failed to update before we tried to copy any files, then assume things are
  289. * OK if they are now running the latest.
  290. *
  291. * This flag is cleared whenever a successful update occurs using Core_Upgrader.
  292. */
  293. $comparison = ! empty( $failed['critical'] ) ? '>=' : '>';
  294. if ( version_compare( $failed['attempted'], $wp_version, $comparison ) )
  295. $nag = true;
  296. }
  297. if ( ! $nag )
  298. return false;
  299. if ( current_user_can('update_core') )
  300. $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
  301. else
  302. $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
  303. echo "<div class='update-nag'>$msg</div>";
  304. }
  305. add_action( 'admin_notices', 'maintenance_nag' );
  306. add_action( 'network_admin_notices', 'maintenance_nag' );