PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/update.php

http://cartonbank.googlecode.com/
PHP | 271 lines | 198 code | 50 blank | 23 comment | 54 complexity | f69810ed3bf43dc618f52eee80d7df87 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. * 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. //is_super_admin()
  117. if ( current_user_can('edit_files'))
  118. {
  119. //echo "<div class='update-nag'>$msg</div>";
  120. }
  121. }
  122. if (current_user_can('edit_users'))
  123. add_action( 'admin_notices', 'update_nag', 3 );
  124. // Called directly from dashboard
  125. function update_right_now_message() {
  126. if ( is_multisite() && !current_user_can('update_core') )
  127. return false;
  128. $cur = get_preferred_from_update_core();
  129. $msg = sprintf( __('You are using <span class="b">WordPress %s</span>.'), $GLOBALS['wp_version'] );
  130. if ( isset( $cur->response ) && $cur->response == 'upgrade' && current_user_can('update_core') )
  131. $msg .= " <a href='update-core.php' class='button'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
  132. echo "<span id='wp-version-message'>$msg</span>";
  133. }
  134. function get_plugin_updates() {
  135. $all_plugins = get_plugins();
  136. $upgrade_plugins = array();
  137. $current = get_site_transient( 'update_plugins' );
  138. foreach ( (array)$all_plugins as $plugin_file => $plugin_data) {
  139. if ( isset( $current->response[ $plugin_file ] ) ) {
  140. $upgrade_plugins[ $plugin_file ] = (object) $plugin_data;
  141. $upgrade_plugins[ $plugin_file ]->update = $current->response[ $plugin_file ];
  142. }
  143. }
  144. return $upgrade_plugins;
  145. }
  146. function wp_plugin_update_rows() {
  147. if ( !current_user_can('update_plugins' ) )
  148. return;
  149. $plugins = get_site_transient( 'update_plugins' );
  150. if ( isset($plugins->response) && is_array($plugins->response) ) {
  151. $plugins = array_keys( $plugins->response );
  152. foreach( $plugins as $plugin_file ) {
  153. add_action( "after_plugin_row_$plugin_file", 'wp_plugin_update_row', 10, 2 );
  154. }
  155. }
  156. }
  157. add_action( 'admin_init', 'wp_plugin_update_rows' );
  158. function wp_plugin_update_row( $file, $plugin_data ) {
  159. $current = get_site_transient( 'update_plugins' );
  160. if ( !isset( $current->response[ $file ] ) )
  161. return false;
  162. $r = $current->response[ $file ];
  163. $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
  164. $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
  165. $details_url = admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&TB_iframe=true&width=600&height=800');
  166. echo '<tr class="plugin-update-tr"><td colspan="3" class="plugin-update"><div class="update-message">';
  167. if ( ! current_user_can('update_plugins') )
  168. 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 );
  169. else if ( empty($r->package) )
  170. 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 );
  171. else
  172. 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) );
  173. do_action( "in_plugin_update_message-$file", $plugin_data, $r );
  174. echo '</div></td></tr>';
  175. }
  176. function wp_update_plugin($plugin, $feedback = '') {
  177. if ( !empty($feedback) )
  178. add_filter('update_feedback', $feedback);
  179. include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  180. $upgrader = new Plugin_Upgrader();
  181. return $upgrader->upgrade($plugin);
  182. }
  183. function get_theme_updates() {
  184. $themes = get_themes();
  185. $current = get_site_transient('update_themes');
  186. $update_themes = array();
  187. foreach ( $themes as $theme ) {
  188. $theme = (object) $theme;
  189. if ( isset($current->response[ $theme->Stylesheet ]) ) {
  190. $update_themes[$theme->Stylesheet] = $theme;
  191. $update_themes[$theme->Stylesheet]->update = $current->response[ $theme->Stylesheet ];
  192. }
  193. }
  194. return $update_themes;
  195. }
  196. function wp_update_theme($theme, $feedback = '') {
  197. if ( !empty($feedback) )
  198. add_filter('update_feedback', $feedback);
  199. include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  200. $upgrader = new Theme_Upgrader();
  201. return $upgrader->upgrade($theme);
  202. }
  203. function wp_update_core($current, $feedback = '') {
  204. if ( !empty($feedback) )
  205. add_filter('update_feedback', $feedback);
  206. include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  207. $upgrader = new Core_Upgrader();
  208. return $upgrader->upgrade($current);
  209. }
  210. function maintenance_nag() {
  211. global $upgrading;
  212. if ( ! isset( $upgrading ) )
  213. return false;
  214. if ( current_user_can('update_core') )
  215. $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
  216. else
  217. $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
  218. //echo "<div class='update-nag'>$msg</div>";
  219. }
  220. add_action( 'admin_notices', 'maintenance_nag' );
  221. ?>