3 * WordPress Administration Update API
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 wp_plugin_update_row( $file, $plugin_data ) {
149 $current = get_transient( 'update_plugins' );
150 if ( !isset( $current->response[ $file ] ) )
153 $r = $current->response[ $file ];
155 $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
156 $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
158 $details_url = admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&TB_iframe=true&width=600&height=800');
160 echo '<tr class="plugin-update-tr"><td colspan="3" class="plugin-update"><div class="update-message">';
161 if ( ! current_user_can('update_plugins') )
162 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 );
163 else if ( empty($r->package) )
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> <em>automatic upgrade unavailable for this plugin</em>.'), $plugin_name, esc_url($details_url), esc_attr($plugin_name), $r->new_version );
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> 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) );
168 do_action( "in_plugin_update_message-$file", $plugin_data, $r );
170 echo '</div></td></tr>';
172 add_action( 'after_plugin_row', 'wp_plugin_update_row', 10, 2 );
174 function wp_update_plugin($plugin, $feedback = '') {
176 if ( !empty($feedback) )
177 add_filter('update_feedback', $feedback);
179 include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
180 $upgrader = new Plugin_Upgrader();
181 return $upgrader->upgrade($plugin);
184 function wp_update_theme($theme, $feedback = '') {
186 if ( !empty($feedback) )
187 add_filter('update_feedback', $feedback);
189 include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
190 $upgrader = new Theme_Upgrader();
191 return $upgrader->upgrade($theme);
195 function wp_update_core($current, $feedback = '') {
197 if ( !empty($feedback) )
198 add_filter('update_feedback', $feedback);
200 include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
201 $upgrader = new Core_Upgrader();
202 return $upgrader->upgrade($current);
206 function maintenance_nag() {
208 if ( ! isset( $upgrading ) )
211 if ( current_user_can('manage_options') )
212 $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
214 $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
216 echo "<div id='update-nag'>$msg</div>";
218 add_action( 'admin_notices', 'maintenance_nag' );