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_option( 'dismissed_update_core' );
35 if ( !is_array( $dismissed ) ) $dismissed = array();
36 $from_api = get_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_option( 'dismissed_update_core' );
61 $dismissed[ $update->current.'|'.$update->locale ] = true;
62 return update_option( 'dismissed_update_core', $dismissed );
65 function undismiss_core_update( $version, $locale ) {
66 $dismissed = get_option( 'dismissed_update_core' );
67 $key = $version.'|'.$locale;
68 if ( !isset( $dismissed[$key] ) ) return false;
69 unset( $dismissed[$key] );
70 return update_option( 'dismissed_update_core', $dismissed );
73 function find_core_update( $version, $locale ) {
74 $from_api = get_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 ( !current_user_can('manage_options') )
86 return sprintf( __( 'Version %s' ), $GLOBALS['wp_version'] );
88 $cur = get_preferred_from_update_core();
89 if ( ! isset( $cur->current ) )
92 if ( ! isset( $cur->url ) )
95 if ( ! isset( $cur->response ) )
98 switch ( $cur->response ) {
100 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');
104 if ( current_user_can('manage_options') ) {
105 return sprintf( '<strong>'.__( '<a href="%1$s">Get Version %2$s</a>' ).'</strong>', 'update-core.php', $cur->current);
111 return sprintf( __( 'Version %s' ), $GLOBALS['wp_version'] );
115 add_filter( 'update_footer', 'core_update_footer' );
117 function update_nag() {
120 if ( 'update-core.php' == $pagenow )
123 $cur = get_preferred_from_update_core();
125 if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
128 if ( current_user_can('manage_options') )
129 $msg = sprintf( __('WordPress %1$s is available! <a href="%2$s">Please update now</a>.'), $cur->current, 'update-core.php' );
131 $msg = sprintf( __('WordPress %1$s is available! Please notify the site administrator.'), $cur->current );
133 echo "<div id='update-nag'>$msg</div>";
135 add_action( 'admin_notices', 'update_nag', 3 );
137 // Called directly from dashboard
138 function update_right_now_message() {
139 $cur = get_preferred_from_update_core();
141 $msg = sprintf( __('You are using <span class="b">WordPress %s</span>.'), $GLOBALS['wp_version'] );
142 if ( isset( $cur->response ) && $cur->response == 'upgrade' && current_user_can('manage_options') )
143 $msg .= " <a href='update-core.php' class='button'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
145 echo "<span id='wp-version-message'>$msg</span>";
148 function get_plugin_updates() {
149 $all_plugins = get_plugins();
150 $upgrade_plugins = array();
151 $current = get_transient( 'update_plugins' );
152 foreach ( (array)$all_plugins as $plugin_file => $plugin_data) {
153 if ( isset( $current->response[ $plugin_file ] ) ) {
154 $upgrade_plugins[ $plugin_file ] = (object) $plugin_data;
155 $upgrade_plugins[ $plugin_file ]->update = $current->response[ $plugin_file ];
159 return $upgrade_plugins;
162 function wp_plugin_update_rows() {
163 $plugins = get_transient( 'update_plugins' );
164 if ( isset($plugins->response) && is_array($plugins->response) ) {
165 $plugins = array_keys( $plugins->response );
166 foreach( $plugins as $plugin_file ) {
167 add_action( "after_plugin_row_$plugin_file", 'wp_plugin_update_row', 10, 2 );
171 add_action( 'admin_init', 'wp_plugin_update_rows' );
173 function wp_plugin_update_row( $file, $plugin_data ) {
174 $current = get_transient( 'update_plugins' );
175 if ( !isset( $current->response[ $file ] ) )
178 $r = $current->response[ $file ];
180 $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
181 $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
183 $details_url = admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&TB_iframe=true&width=600&height=800');
185 echo '<tr class="plugin-update-tr"><td colspan="3" class="plugin-update"><div class="update-message">';
186 if ( ! current_user_can('update_plugins') )
187 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 );
188 else if ( empty($r->package) )
189 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 );
191 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) );
193 do_action( "in_plugin_update_message-$file", $plugin_data, $r );
195 echo '</div></td></tr>';
198 function wp_update_plugin($plugin, $feedback = '') {
200 if ( !empty($feedback) )
201 add_filter('update_feedback', $feedback);
203 include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
204 $upgrader = new Plugin_Upgrader();
205 return $upgrader->upgrade($plugin);
208 function get_theme_updates() {
209 $themes = get_themes();
210 $current = get_transient('update_themes');
211 $update_themes = array();
213 foreach ( $themes as $theme ) {
214 $theme = (object) $theme;
215 if ( isset($current->response[ $theme->Stylesheet ]) ) {
216 $update_themes[$theme->Stylesheet] = $theme;
217 $update_themes[$theme->Stylesheet]->update = $current->response[ $theme->Stylesheet ];
221 return $update_themes;
224 function wp_update_theme($theme, $feedback = '') {
226 if ( !empty($feedback) )
227 add_filter('update_feedback', $feedback);
229 include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
230 $upgrader = new Theme_Upgrader();
231 return $upgrader->upgrade($theme);
235 function wp_update_core($current, $feedback = '') {
237 if ( !empty($feedback) )
238 add_filter('update_feedback', $feedback);
240 include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
241 $upgrader = new Core_Upgrader();
242 return $upgrader->upgrade($current);
246 function maintenance_nag() {
248 if ( ! isset( $upgrading ) )
251 if ( current_user_can('manage_options') )
252 $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
254 $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
256 echo "<div id='update-nag'>$msg</div>";
258 add_action( 'admin_notices', 'maintenance_nag' );