PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/www/blog/wp-admin/includes/update.php

http://timoseven.googlecode.com/
PHP | 267 lines | 196 code | 51 blank | 20 comment | 52 complexity | 80fbb0b28eb8537d95b4d040b30411d7 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. * WordPress Administration Update API
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. // The admin side of our 1.1 update system
  9. /**
  10. * Selects the first update version from the update_core option
  11. *
  12. * @return object the response from the API
  13. */
  14. function get_preferred_from_update_core() {
  15. $updates = get_core_updates();
  16. if ( !is_array( $updates ) )
  17. return false;
  18. if ( empty( $updates ) )
  19. return (object)array('response' => 'latest');
  20. return $updates[0];
  21. }
  22. /**
  23. * Get available core updates
  24. *
  25. * @param array $options Set $options['dismissed'] to true to show dismissed upgrades too,
  26. * set $options['available'] to false to skip not-dimissed updates.
  27. * @return array Array of the update objects
  28. */
  29. function get_core_updates( $options = array() ) {
  30. $options = array_merge( array('available' => true, 'dismissed' => false ), $options );
  31. $dismissed = get_site_option( 'dismissed_update_core' );
  32. if ( !is_array( $dismissed ) ) $dismissed = array();
  33. $from_api = get_site_transient( 'update_core' );
  34. if ( empty($from_api) )
  35. return false;
  36. if ( !isset( $from_api->updates ) || !is_array( $from_api->updates ) ) return false;
  37. $updates = $from_api->updates;
  38. if ( !is_array( $updates ) ) return false;
  39. $result = array();
  40. foreach($updates as $update) {
  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. function dismiss_core_update( $update ) {
  56. $dismissed = get_site_option( 'dismissed_update_core' );
  57. $dismissed[ $update->current.'|'.$update->locale ] = true;
  58. return update_site_option( 'dismissed_update_core', $dismissed );
  59. }
  60. function undismiss_core_update( $version, $locale ) {
  61. $dismissed = get_site_option( 'dismissed_update_core' );
  62. $key = $version.'|'.$locale;
  63. if ( !isset( $dismissed[$key] ) ) return false;
  64. unset( $dismissed[$key] );
  65. return update_site_option( 'dismissed_update_core', $dismissed );
  66. }
  67. function find_core_update( $version, $locale ) {
  68. $from_api = get_site_transient( 'update_core' );
  69. if ( !is_array( $from_api->updates ) ) return false;
  70. $updates = $from_api->updates;
  71. foreach($updates as $update) {
  72. if ( $update->current == $version && $update->locale == $locale )
  73. return $update;
  74. }
  75. return false;
  76. }
  77. function core_update_footer( $msg = '' ) {
  78. if ( is_multisite() && !current_user_can('update_core') )
  79. return false;
  80. if ( !current_user_can('update_core') )
  81. return sprintf( __( 'Version %s' ), $GLOBALS['wp_version'] );
  82. $cur = get_preferred_from_update_core();
  83. if ( ! isset( $cur->current ) )
  84. $cur->current = '';
  85. if ( ! isset( $cur->url ) )
  86. $cur->url = '';
  87. if ( ! isset( $cur->response ) )
  88. $cur->response = '';
  89. switch ( $cur->response ) {
  90. case 'development' :
  91. return sprintf( __( 'You are using a development version (%1$s). Cool! Please <a href="%2$s">stay updated</a>.' ), $GLOBALS['wp_version'], 'update-core.php');
  92. break;
  93. case 'upgrade' :
  94. return sprintf( '<strong>'.__( '<a href="%1$s">Get Version %2$s</a>' ).'</strong>', 'update-core.php', $cur->current);
  95. break;
  96. case 'latest' :
  97. default :
  98. return sprintf( __( 'Version %s' ), $GLOBALS['wp_version'] );
  99. break;
  100. }
  101. }
  102. add_filter( 'update_footer', 'core_update_footer' );
  103. function update_nag() {
  104. if ( is_multisite() && !current_user_can('update_core') )
  105. return false;
  106. global $pagenow;
  107. if ( 'update-core.php' == $pagenow )
  108. return;
  109. $cur = get_preferred_from_update_core();
  110. if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
  111. return false;
  112. if ( current_user_can('update_core') )
  113. $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, 'update-core.php' );
  114. else
  115. $msg = sprintf( __('<a href="http://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! Please notify the site administrator.'), $cur->current );
  116. echo "<div class='update-nag'>$msg</div>";
  117. }
  118. add_action( 'admin_notices', 'update_nag', 3 );
  119. // Called directly from dashboard
  120. function update_right_now_message() {
  121. if ( is_multisite() && !current_user_can('update_core') )
  122. return false;
  123. $cur = get_preferred_from_update_core();
  124. $msg = sprintf( __('You are using <span class="b">WordPress %s</span>.'), $GLOBALS['wp_version'] );
  125. if ( isset( $cur->response ) && $cur->response == 'upgrade' && current_user_can('update_core') )
  126. $msg .= " <a href='update-core.php' class='button'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
  127. echo "<span id='wp-version-message'>$msg</span>";
  128. }
  129. function get_plugin_updates() {
  130. $all_plugins = get_plugins();
  131. $upgrade_plugins = array();
  132. $current = get_site_transient( 'update_plugins' );
  133. foreach ( (array)$all_plugins as $plugin_file => $plugin_data) {
  134. if ( isset( $current->response[ $plugin_file ] ) ) {
  135. $upgrade_plugins[ $plugin_file ] = (object) $plugin_data;
  136. $upgrade_plugins[ $plugin_file ]->update = $current->response[ $plugin_file ];
  137. }
  138. }
  139. return $upgrade_plugins;
  140. }
  141. function wp_plugin_update_rows() {
  142. if ( !current_user_can('update_plugins' ) )
  143. return;
  144. $plugins = get_site_transient( 'update_plugins' );
  145. if ( isset($plugins->response) && is_array($plugins->response) ) {
  146. $plugins = array_keys( $plugins->response );
  147. foreach( $plugins as $plugin_file ) {
  148. add_action( "after_plugin_row_$plugin_file", 'wp_plugin_update_row', 10, 2 );
  149. }
  150. }
  151. }
  152. add_action( 'admin_init', 'wp_plugin_update_rows' );
  153. function wp_plugin_update_row( $file, $plugin_data ) {
  154. $current = get_site_transient( 'update_plugins' );
  155. if ( !isset( $current->response[ $file ] ) )
  156. return false;
  157. $r = $current->response[ $file ];
  158. $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
  159. $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
  160. $details_url = admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&TB_iframe=true&width=600&height=800');
  161. echo '<tr class="plugin-update-tr"><td colspan="3" class="plugin-update"><div class="update-message">';
  162. if ( ! current_user_can('update_plugins') )
  163. 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 );
  164. else if ( empty($r->package) )
  165. 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 upgrade unavailable for this plugin</em>.'), $plugin_name, esc_url($details_url), esc_attr($plugin_name), $r->new_version );
  166. else
  167. 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">upgrade automatically</a>.'), $plugin_name, esc_url($details_url), esc_attr($plugin_name), $r->new_version, wp_nonce_url('update.php?action=upgrade-plugin&plugin=' . $file, 'upgrade-plugin_' . $file) );
  168. do_action( "in_plugin_update_message-$file", $plugin_data, $r );
  169. echo '</div></td></tr>';
  170. }
  171. function wp_update_plugin($plugin, $feedback = '') {
  172. if ( !empty($feedback) )
  173. add_filter('update_feedback', $feedback);
  174. include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  175. $upgrader = new Plugin_Upgrader();
  176. return $upgrader->upgrade($plugin);
  177. }
  178. function get_theme_updates() {
  179. $themes = get_themes();
  180. $current = get_site_transient('update_themes');
  181. $update_themes = array();
  182. foreach ( $themes as $theme ) {
  183. $theme = (object) $theme;
  184. if ( isset($current->response[ $theme->Stylesheet ]) ) {
  185. $update_themes[$theme->Stylesheet] = $theme;
  186. $update_themes[$theme->Stylesheet]->update = $current->response[ $theme->Stylesheet ];
  187. }
  188. }
  189. return $update_themes;
  190. }
  191. function wp_update_theme($theme, $feedback = '') {
  192. if ( !empty($feedback) )
  193. add_filter('update_feedback', $feedback);
  194. include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  195. $upgrader = new Theme_Upgrader();
  196. return $upgrader->upgrade($theme);
  197. }
  198. function wp_update_core($current, $feedback = '') {
  199. if ( !empty($feedback) )
  200. add_filter('update_feedback', $feedback);
  201. include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  202. $upgrader = new Core_Upgrader();
  203. return $upgrader->upgrade($current);
  204. }
  205. function maintenance_nag() {
  206. global $upgrading;
  207. if ( ! isset( $upgrading ) )
  208. return false;
  209. if ( current_user_can('update_core') )
  210. $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
  211. else
  212. $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
  213. echo "<div class='update-nag'>$msg</div>";
  214. }
  215. add_action( 'admin_notices', 'maintenance_nag' );
  216. ?>