3 * WordPress Administration Update API
6 * @subpackage Administration
10 * Selects the first update version from the update_core option
12 * @return object the response from the API
14 function get_preferred_from_update_core() {
15 $updates = get_core_updates();
16 if ( ! is_array( $updates ) )
18 if ( empty( $updates ) )
19 return (object) array( 'response' => 'latest' );
24 * Get available core updates
26 * @param array $options Set $options['dismissed'] to true to show dismissed upgrades too,
27 * set $options['available'] to false to skip not-dismissed updates.
28 * @return array Array of the update objects
30 function get_core_updates( $options = array() ) {
31 $options = array_merge( array( 'available' => true, 'dismissed' => false ), $options );
32 $dismissed = get_site_option( 'dismissed_update_core' );
34 if ( ! is_array( $dismissed ) )
37 $from_api = get_site_transient( 'update_core' );
39 if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) )
42 $updates = $from_api->updates;
44 foreach ( $updates as $update ) {
45 if ( $update->response == 'autoupdate' )
48 if ( array_key_exists( $update->current . '|' . $update->locale, $dismissed ) ) {
49 if ( $options['dismissed'] ) {
50 $update->dismissed = true;
54 if ( $options['available'] ) {
55 $update->dismissed = false;
64 * Gets the best available (and enabled) Auto-Update for WordPress Core.
66 * If there's 1.2.3 and 1.3 on offer, it'll choose 1.3 if the install allows it, else, 1.2.3
70 * @return bool|array False on failure, otherwise the core update offering.
72 function find_core_auto_update() {
73 $updates = get_site_transient( 'update_core' );
74 if ( ! $updates || empty( $updates->updates ) )
77 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
80 $upgrader = new WP_Automatic_Updater;
81 foreach ( $updates->updates as $update ) {
82 if ( 'autoupdate' != $update->response )
85 if ( ! $upgrader->should_update( 'core', $update, ABSPATH ) )
88 if ( ! $auto_update || version_compare( $update->current, $auto_update->current, '>' ) )
89 $auto_update = $update;
95 * Gets and caches the checksums for the given version of WordPress.
99 * @param string $version Version string to query.
100 * @param string $locale Locale to query.
101 * @return bool|array False on failure. An array of checksums on success.
103 function get_core_checksums( $version, $locale ) {
106 $url = 'http://api.wordpress.org/core/checksums/1.0/?' . http_build_query( compact( 'version', 'locale' ), null, '&' );
108 if ( wp_http_supports( array( 'ssl' ) ) )
109 $url = set_url_scheme( $url, 'https' );
112 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
115 $response = wp_remote_get( $url, $options );
117 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
120 $body = trim( wp_remote_retrieve_body( $response ) );
121 $body = json_decode( $body, true );
123 if ( ! is_array( $body ) || ! isset( $body['checksums'] ) || ! is_array( $body['checksums'] ) )
126 return $body['checksums'];
129 function dismiss_core_update( $update ) {
130 $dismissed = get_site_option( 'dismissed_update_core' );
131 $dismissed[ $update->current . '|' . $update->locale ] = true;
132 return update_site_option( 'dismissed_update_core', $dismissed );
135 function undismiss_core_update( $version, $locale ) {
136 $dismissed = get_site_option( 'dismissed_update_core' );
137 $key = $version . '|' . $locale;
139 if ( ! isset( $dismissed[$key] ) )
142 unset( $dismissed[$key] );
143 return update_site_option( 'dismissed_update_core', $dismissed );
146 function find_core_update( $version, $locale ) {
147 $from_api = get_site_transient( 'update_core' );
149 if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) )
152 $updates = $from_api->updates;
153 foreach ( $updates as $update ) {
154 if ( $update->current == $version && $update->locale == $locale )
160 function core_update_footer( $msg = '' ) {
161 if ( !current_user_can('update_core') )
162 return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
164 $cur = get_preferred_from_update_core();
165 if ( ! is_object( $cur ) )
168 if ( ! isset( $cur->current ) )
171 if ( ! isset( $cur->url ) )
174 if ( ! isset( $cur->response ) )
177 switch ( $cur->response ) {
179 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' ) );
183 return sprintf( '<strong>'.__( '<a href="%1$s">Get Version %2$s</a>' ).'</strong>', network_admin_url( 'update-core.php' ), $cur->current);
188 return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
192 add_filter( 'update_footer', 'core_update_footer' );
194 function update_nag() {
195 if ( is_multisite() && !current_user_can('update_core') )
200 if ( 'update-core.php' == $pagenow )
203 $cur = get_preferred_from_update_core();
205 if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
208 if ( current_user_can('update_core') ) {
209 $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' ) );
211 $msg = sprintf( __('<a href="http://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! Please notify the site administrator.'), $cur->current );
213 echo "<div class='update-nag'>$msg</div>";
215 add_action( 'admin_notices', 'update_nag', 3 );
216 add_action( 'network_admin_notices', 'update_nag', 3 );
218 // Called directly from dashboard
219 function update_right_now_message() {
220 $msg = sprintf( __( 'You are using <span class="b">WordPress %s</span>.' ), get_bloginfo( 'version', 'display' ) );
222 if ( current_user_can('update_core') ) {
223 $cur = get_preferred_from_update_core();
225 if ( isset( $cur->response ) && $cur->response == 'upgrade' )
226 $msg .= " <a href='" . network_admin_url( 'update-core.php' ) . "' class='button'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
229 echo "<span id='wp-version-message'>$msg</span>";
232 function get_plugin_updates() {
233 $all_plugins = get_plugins();
234 $upgrade_plugins = array();
235 $current = get_site_transient( 'update_plugins' );
236 foreach ( (array)$all_plugins as $plugin_file => $plugin_data) {
237 if ( isset( $current->response[ $plugin_file ] ) ) {
238 $upgrade_plugins[ $plugin_file ] = (object) $plugin_data;
239 $upgrade_plugins[ $plugin_file ]->update = $current->response[ $plugin_file ];
243 return $upgrade_plugins;
246 function wp_plugin_update_rows() {
247 if ( !current_user_can('update_plugins' ) )
250 $plugins = get_site_transient( 'update_plugins' );
251 if ( isset($plugins->response) && is_array($plugins->response) ) {
252 $plugins = array_keys( $plugins->response );
253 foreach( $plugins as $plugin_file ) {
254 add_action( "after_plugin_row_$plugin_file", 'wp_plugin_update_row', 10, 2 );
258 add_action( 'admin_init', 'wp_plugin_update_rows' );
260 function wp_plugin_update_row( $file, $plugin_data ) {
261 $current = get_site_transient( 'update_plugins' );
262 if ( !isset( $current->response[ $file ] ) )
265 $r = $current->response[ $file ];
267 $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
268 $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
270 $details_url = self_admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '§ion=changelog&TB_iframe=true&width=600&height=800');
272 $wp_list_table = _get_list_table('WP_Plugins_List_Table');
274 if ( is_network_admin() || !is_multisite() ) {
275 echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
277 if ( ! current_user_can('update_plugins') )
278 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 );
279 else if ( empty($r->package) )
280 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 );
282 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) );
284 do_action( "in_plugin_update_message-$file", $plugin_data, $r );
286 echo '</div></td></tr>';
290 function get_theme_updates() {
291 $themes = wp_get_themes();
292 $current = get_site_transient('update_themes');
294 if ( ! isset( $current->response ) )
297 $update_themes = array();
298 foreach ( $current->response as $stylesheet => $data ) {
299 $update_themes[ $stylesheet ] = wp_get_theme( $stylesheet );
300 $update_themes[ $stylesheet ]->update = $data;
303 return $update_themes;
306 function wp_theme_update_rows() {
307 if ( !current_user_can('update_themes' ) )
310 $themes = get_site_transient( 'update_themes' );
311 if ( isset($themes->response) && is_array($themes->response) ) {
312 $themes = array_keys( $themes->response );
314 foreach( $themes as $theme ) {
315 add_action( "after_theme_row_$theme", 'wp_theme_update_row', 10, 2 );
319 add_action( 'admin_init', 'wp_theme_update_rows' );
321 function wp_theme_update_row( $theme_key, $theme ) {
322 $current = get_site_transient( 'update_themes' );
323 if ( !isset( $current->response[ $theme_key ] ) )
325 $r = $current->response[ $theme_key ];
326 $themes_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
327 $theme_name = wp_kses( $theme['Name'], $themes_allowedtags );
329 $details_url = add_query_arg( array( 'TB_iframe' => 'true', 'width' => 1024, 'height' => 800 ), $current->response[ $theme_key ]['url'] );
331 $wp_list_table = _get_list_table('WP_MS_Themes_List_Table');
333 echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
334 if ( ! current_user_can('update_themes') )
335 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 );
336 else if ( empty( $r['package'] ) )
337 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'] );
339 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) );
341 do_action( "in_theme_update_message-$theme_key", $theme, $r );
343 echo '</div></td></tr>';
346 function maintenance_nag() {
347 include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
349 $nag = isset( $upgrading );
351 $failed = get_site_option( 'auto_core_update_failed' );
353 * If an update failed critically, we may have copied over version.php but not other files.
354 * In that case, if the install claims we're running the version we attempted, nag.
355 * This is serious enough to err on the side of nagging.
357 * If we simply failed to update before we tried to copy any files, then assume things are
358 * OK if they are now running the latest.
360 * This flag is cleared whenever a successful update occurs using Core_Upgrader.
362 $comparison = ! empty( $failed['critical'] ) ? '>=' : '>';
363 if ( version_compare( $failed['attempted'], $wp_version, '>=' ) )
370 if ( current_user_can('update_core') )
371 $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
373 $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
375 echo "<div class='update-nag'>$msg</div>";
377 add_action( 'admin_notices', 'maintenance_nag' );
378 add_action( 'network_admin_notices', 'maintenance_nag' );