PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/update.php

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