PageRenderTime 50ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/update.php

https://github.com/shaiquddin/WordPress
PHP | 309 lines | 229 code | 60 blank | 20 comment | 59 complexity | cbb5aa89651e1b01cca8eb2b996ec050 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  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-dismissed 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 ( !current_user_can('update_core') )
  79. return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
  80. $cur = get_preferred_from_update_core();
  81. if ( ! is_object( $cur ) )
  82. $cur = new stdClass;
  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>.' ), get_bloginfo( 'version', 'display' ), network_admin_url( 'update-core.php' ) );
  92. break;
  93. case 'upgrade' :
  94. return sprintf( '<strong>'.__( '<a href="%1$s">Get Version %2$s</a>' ).'</strong>', network_admin_url( 'update-core.php' ), $cur->current);
  95. break;
  96. case 'latest' :
  97. default :
  98. return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
  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, network_admin_url( '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. }
  117. echo "<div class='update-nag'>$msg</div>";
  118. }
  119. add_action( 'admin_notices', 'update_nag', 3 );
  120. // Called directly from dashboard
  121. function update_right_now_message() {
  122. $msg = sprintf( __( 'You are using <span class="b">WordPress %s</span>.' ), get_bloginfo( 'version', 'display' ) );
  123. if ( current_user_can('update_core') ) {
  124. $cur = get_preferred_from_update_core();
  125. if ( isset( $cur->response ) && $cur->response == 'upgrade' )
  126. $msg .= " <a href='" . network_admin_url( 'update-core.php' ) . "' class='button'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
  127. }
  128. echo "<span id='wp-version-message'>$msg</span>";
  129. }
  130. function get_plugin_updates() {
  131. $all_plugins = get_plugins();
  132. $upgrade_plugins = array();
  133. $current = get_site_transient( 'update_plugins' );
  134. foreach ( (array)$all_plugins as $plugin_file => $plugin_data) {
  135. if ( isset( $current->response[ $plugin_file ] ) ) {
  136. $upgrade_plugins[ $plugin_file ] = (object) $plugin_data;
  137. $upgrade_plugins[ $plugin_file ]->update = $current->response[ $plugin_file ];
  138. }
  139. }
  140. return $upgrade_plugins;
  141. }
  142. function wp_plugin_update_rows() {
  143. if ( !current_user_can('update_plugins' ) )
  144. return;
  145. $plugins = get_site_transient( 'update_plugins' );
  146. if ( isset($plugins->response) && is_array($plugins->response) ) {
  147. $plugins = array_keys( $plugins->response );
  148. foreach( $plugins as $plugin_file ) {
  149. add_action( "after_plugin_row_$plugin_file", 'wp_plugin_update_row', 10, 2 );
  150. }
  151. }
  152. }
  153. add_action( 'admin_init', 'wp_plugin_update_rows' );
  154. function wp_plugin_update_row( $file, $plugin_data ) {
  155. $current = get_site_transient( 'update_plugins' );
  156. if ( !isset( $current->response[ $file ] ) )
  157. return false;
  158. $r = $current->response[ $file ];
  159. $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
  160. $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
  161. $details_url = self_admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&section=changelog&TB_iframe=true&width=600&height=800');
  162. $wp_list_table = _get_list_table('WP_Plugins_List_Table');
  163. if ( is_network_admin() || !is_multisite() ) {
  164. echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
  165. if ( ! current_user_can('update_plugins') )
  166. 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 );
  167. else if ( empty($r->package) )
  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>. <em>Automatic update is unavailable for this plugin.</em>'), $plugin_name, esc_url($details_url), esc_attr($plugin_name), $r->new_version );
  169. else
  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> 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) );
  171. do_action( "in_plugin_update_message-$file", $plugin_data, $r );
  172. echo '</div></td></tr>';
  173. }
  174. }
  175. function wp_update_plugin($plugin, $feedback = '') {
  176. if ( !empty($feedback) )
  177. add_filter('update_feedback', $feedback);
  178. include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  179. $upgrader = new Plugin_Upgrader();
  180. return $upgrader->upgrade($plugin);
  181. }
  182. function get_theme_updates() {
  183. $themes = wp_get_themes();
  184. $current = get_site_transient('update_themes');
  185. if ( ! isset( $current->response ) )
  186. return array();
  187. $update_themes = array();
  188. foreach ( $current->response as $stylesheet => $data ) {
  189. $update_themes[ $stylesheet ] = wp_get_theme( $stylesheet );
  190. $update_themes[ $stylesheet ]->update = $data;
  191. }
  192. return $update_themes;
  193. }
  194. function wp_update_theme($theme, $feedback = '') {
  195. if ( !empty($feedback) )
  196. add_filter('update_feedback', $feedback);
  197. include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  198. $upgrader = new Theme_Upgrader();
  199. return $upgrader->upgrade($theme);
  200. }
  201. function wp_theme_update_rows() {
  202. if ( !current_user_can('update_themes' ) )
  203. return;
  204. $themes = get_site_transient( 'update_themes' );
  205. if ( isset($themes->response) && is_array($themes->response) ) {
  206. $themes = array_keys( $themes->response );
  207. foreach( $themes as $theme ) {
  208. add_action( "after_theme_row_$theme", 'wp_theme_update_row', 10, 2 );
  209. }
  210. }
  211. }
  212. add_action( 'admin_init', 'wp_theme_update_rows' );
  213. function wp_theme_update_row( $theme_key, $theme ) {
  214. $current = get_site_transient( 'update_themes' );
  215. if ( !isset( $current->response[ $theme_key ] ) )
  216. return false;
  217. $r = $current->response[ $theme_key ];
  218. $themes_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
  219. $theme_name = wp_kses( $theme['Name'], $themes_allowedtags );
  220. $details_url = add_query_arg( array( 'TB_iframe' => 'true', 'width' => 1024, 'height' => 800 ), $current->response[ $theme_key ]['url'] );
  221. $wp_list_table = _get_list_table('WP_MS_Themes_List_Table');
  222. echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
  223. if ( ! current_user_can('update_themes') )
  224. 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 );
  225. else if ( empty( $r['package'] ) )
  226. 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'] );
  227. else
  228. 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) );
  229. do_action( "in_theme_update_message-$theme_key", $theme, $r );
  230. echo '</div></td></tr>';
  231. }
  232. function wp_update_core($current, $feedback = '') {
  233. if ( !empty($feedback) )
  234. add_filter('update_feedback', $feedback);
  235. include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  236. $upgrader = new Core_Upgrader();
  237. return $upgrader->upgrade($current);
  238. }
  239. function maintenance_nag() {
  240. global $upgrading;
  241. if ( ! isset( $upgrading ) )
  242. return false;
  243. if ( current_user_can('update_core') )
  244. $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
  245. else
  246. $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
  247. echo "<div class='update-nag'>$msg</div>";
  248. }
  249. add_action( 'admin_notices', 'maintenance_nag' );