3 * WordPress Administration Update API
6 * @subpackage Administration
9 // The admin side of our 1.1 update system
12 * Selects the first update version from the update_core option
14 * @return object the response from the API
16 function get_preferred_from_update_core() {
17 $updates = get_core_updates();
18 if ( !is_array( $updates ) )
20 if ( empty( $updates ) )
21 return (object)array('response' => 'latest');
26 * Get available core updates
28 * @param array $options Set $options['dismissed'] to true to show dismissed upgrades too,
29 * set $options['available'] to false to skip not-dimissed updates.
30 * @return array Array of the update objects
32 function get_core_updates( $options = array() ) {
33 $options = array_merge( array('available' => true, 'dismissed' => false ), $options );
34 $dismissed = get_site_option( 'dismissed_update_core' );
35 if ( !is_array( $dismissed ) ) $dismissed = array();
36 $from_api = get_site_transient( 'update_core' );
37 if ( empty($from_api) )
39 if ( !isset( $from_api->updates ) || !is_array( $from_api->updates ) ) return false;
40 $updates = $from_api->updates;
41 if ( !is_array( $updates ) ) return false;
43 foreach($updates as $update) {
44 if ( array_key_exists( $update->current.'|'.$update->locale, $dismissed ) ) {
45 if ( $options['dismissed'] ) {
46 $update->dismissed = true;
50 if ( $options['available'] ) {
51 $update->dismissed = false;
59 function dismiss_core_update( $update ) {
60 $dismissed = get_site_option( 'dismissed_update_core' );
61 $dismissed[ $update->current.'|'.$update->locale ] = true;
62 return update_site_option( 'dismissed_update_core', $dismissed );
65 function undismiss_core_update( $version, $locale ) {
66 $dismissed = get_site_option( 'dismissed_update_core' );
67 $key = $version.'|'.$locale;
68 if ( !isset( $dismissed[$key] ) ) return false;
69 unset( $dismissed[$key] );
70 return update_site_option( 'dismissed_update_core', $dismissed );
73 function find_core_update( $version, $locale ) {
74 $from_api = get_site_transient( 'update_core' );
75 if ( !is_array( $from_api->updates ) ) return false;
76 $updates = $from_api->updates;
77 foreach($updates as $update) {
78 if ( $update->current == $version && $update->locale == $locale )
84 function core_update_footer( $msg = '' ) {
85 if ( is_multisite() && !current_user_can('update_core') )
88 if ( !current_user_can('update_core') )
89 return sprintf( __( 'Version %s' ), $GLOBALS['wp_version'] );
91 $cur = get_preferred_from_update_core();
92 if ( ! isset( $cur->current ) )
95 if ( ! isset( $cur->url ) )
98 if ( ! isset( $cur->response ) )
101 switch ( $cur->response ) {
103 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');
107 return sprintf( '<strong>'.__( '<a href="%1$s">Get Version %2$s</a>' ).'</strong>', 'update-core.php', $cur->current);
112 return sprintf( __( 'Version %s' ), $GLOBALS['wp_version'] );
116 add_filter( 'update_footer', 'core_update_footer' );
118 function update_nag() {
119 if ( is_multisite() && !current_user_can('update_core') )
124 if ( 'update-core.php' == $pagenow )
127 $cur = get_preferred_from_update_core();
129 if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
132 if ( current_user_can('update_core') )
133 $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' );
135 $msg = sprintf( __('<a href="http://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! Please notify the site administrator.'), $cur->current );
137 echo "<div class='update-nag'>$msg</div>";
139 add_action( 'admin_notices', 'update_nag', 3 );
141 // Called directly from dashboard
142 function update_right_now_message() {
143 if ( is_multisite() && !current_user_can('update_core') )
146 $cur = get_preferred_from_update_core();
148 $msg = sprintf( __('You are using <span class="b">WordPress %s</span>.'), $GLOBALS['wp_version'] );
149 if ( isset( $cur->response ) && $cur->response == 'upgrade' && current_user_can('update_core') )
150 $msg .= " <a href='update-core.php' class='button'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
152 echo "<span id='wp-version-message'>$msg</span>";
155 function get_plugin_updates() {
156 $all_plugins = get_plugins();
157 $upgrade_plugins = array();
158 $current = get_site_transient( 'update_plugins' );
159 foreach ( (array)$all_plugins as $plugin_file => $plugin_data) {
160 if ( isset( $current->response[ $plugin_file ] ) ) {
161 $upgrade_plugins[ $plugin_file ] = (object) $plugin_data;
162 $upgrade_plugins[ $plugin_file ]->update = $current->response[ $plugin_file ];
166 return $upgrade_plugins;
169 function wp_plugin_update_rows() {
170 if ( !current_user_can('update_plugins' ) )
173 $plugins = get_site_transient( 'update_plugins' );
174 if ( isset($plugins->response) && is_array($plugins->response) ) {
175 $plugins = array_keys( $plugins->response );
176 foreach( $plugins as $plugin_file ) {
177 add_action( "after_plugin_row_$plugin_file", 'wp_plugin_update_row', 10, 2 );
181 add_action( 'admin_init', 'wp_plugin_update_rows' );
183 function wp_plugin_update_row( $file, $plugin_data ) {
184 $current = get_site_transient( 'update_plugins' );
185 if ( !isset( $current->response[ $file ] ) )
188 $r = $current->response[ $file ];
190 $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
191 $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
193 $details_url = admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&TB_iframe=true&width=600&height=800');
195 echo '<tr class="plugin-update-tr"><td colspan="3" class="plugin-update"><div class="update-message">';
196 if ( ! current_user_can('update_plugins') )
197 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 );
198 else if ( empty($r->package) )
199 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 );
201 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) );
203 do_action( "in_plugin_update_message-$file", $plugin_data, $r );
205 echo '</div></td></tr>';
208 function wp_update_plugin($plugin, $feedback = '') {
209 if ( !empty($feedback) )
210 add_filter('update_feedback', $feedback);
212 include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
213 $upgrader = new Plugin_Upgrader();
214 return $upgrader->upgrade($plugin);
217 function get_theme_updates() {
218 $themes = get_themes();
219 $current = get_site_transient('update_themes');
220 $update_themes = array();
222 foreach ( $themes as $theme ) {
223 $theme = (object) $theme;
224 if ( isset($current->response[ $theme->Stylesheet ]) ) {
225 $update_themes[$theme->Stylesheet] = $theme;
226 $update_themes[$theme->Stylesheet]->update = $current->response[ $theme->Stylesheet ];
230 return $update_themes;
233 function wp_update_theme($theme, $feedback = '') {
234 if ( !empty($feedback) )
235 add_filter('update_feedback', $feedback);
237 include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
238 $upgrader = new Theme_Upgrader();
239 return $upgrader->upgrade($theme);
243 function wp_update_core($current, $feedback = '') {
244 if ( !empty($feedback) )
245 add_filter('update_feedback', $feedback);
247 include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
248 $upgrader = new Core_Upgrader();
249 return $upgrader->upgrade($current);
253 function maintenance_nag() {
255 if ( ! isset( $upgrading ) )
258 if ( current_user_can('update_core') )
259 $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
261 $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
263 echo "<div class='update-nag'>$msg</div>";
265 add_action( 'admin_notices', 'maintenance_nag' );