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_option( 'update_core' );
37 if ( empty($from_api) )
39 if ( !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_option( '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_option( 'update_plugins' );
150 if ( !isset( $current->response[ $file ] ) )
153 $r = $current->response[ $file ];
155 $details_url = admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&TB_iframe=true&width=600&height=800');
157 echo '<tr><td colspan="5" class="plugin-update">';
158 if ( ! current_user_can('update_plugins') )
159 printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s Details</a>.'), $plugin_data['Name'], $details_url, $r->new_version);
160 else if ( empty($r->package) )
161 printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s Details</a> <em>automatic upgrade unavailable for this plugin</em>.'), $plugin_data['Name'], $details_url, $r->new_version);
163 printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%1$s">View version %3$s Details</a> or <a href="%4$s">upgrade automatically</a>.'), $plugin_data['Name'], $details_url, $r->new_version, wp_nonce_url('update.php?action=upgrade-plugin&plugin=' . $file, 'upgrade-plugin_' . $file) );
167 add_action( 'after_plugin_row', 'wp_plugin_update_row', 10, 2 );
169 function wp_update_plugin($plugin, $feedback = '') {
170 global $wp_filesystem;
172 if ( !empty($feedback) )
173 add_filter('update_feedback', $feedback);
175 // Is an update available?
176 $current = get_option( 'update_plugins' );
177 if ( !isset( $current->response[ $plugin ] ) )
178 return new WP_Error('up_to_date', __('The plugin is at the latest version.'));
180 // Is a filesystem accessor setup?
181 if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
184 if ( ! is_object($wp_filesystem) )
185 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
187 if ( $wp_filesystem->errors->get_error_code() )
188 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
190 //Get the base plugin folder
191 $plugins_dir = $wp_filesystem->wp_plugins_dir();
192 if ( empty($plugins_dir) )
193 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
195 //And the same for the Content directory.
196 $content_dir = $wp_filesystem->wp_content_dir();
197 if( empty($content_dir) )
198 return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
200 $plugins_dir = trailingslashit( $plugins_dir );
201 $content_dir = trailingslashit( $content_dir );
203 // Get the URL to the zip file
204 $r = $current->response[ $plugin ];
206 if ( empty($r->package) )
207 return new WP_Error('no_package', __('Upgrade package not available.'));
209 // Download the package
210 $package = $r->package;
211 apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
212 $download_file = download_url($package);
214 if ( is_wp_error($download_file) )
215 return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
217 $working_dir = $content_dir . 'upgrade/' . basename($plugin, '.php');
219 // Clean up working directory
220 if ( $wp_filesystem->is_dir($working_dir) )
221 $wp_filesystem->delete($working_dir, true);
223 apply_filters('update_feedback', __('Unpacking the update'));
224 // Unzip package to working directory
225 $result = unzip_file($download_file, $working_dir);
227 // Once extracted, delete the package
228 unlink($download_file);
230 if ( is_wp_error($result) ) {
231 $wp_filesystem->delete($working_dir, true);
235 if ( is_plugin_active($plugin) ) {
236 //Deactivate the plugin silently, Prevent deactivation hooks from running.
237 apply_filters('update_feedback', __('Deactivating the plugin'));
238 deactivate_plugins($plugin, true);
241 // Remove the existing plugin.
242 apply_filters('update_feedback', __('Removing the old version of the plugin'));
243 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
245 // If plugin is in its own directory, recursively delete the directory.
246 if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
247 $deleted = $wp_filesystem->delete($this_plugin_dir, true);
249 $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
252 $wp_filesystem->delete($working_dir, true);
253 return new WP_Error('delete_failed', __('Could not remove the old plugin'));
256 apply_filters('update_feedback', __('Installing the latest version'));
257 // Copy new version of plugin into place.
258 $result = copy_dir($working_dir, $plugins_dir);
259 if ( is_wp_error($result) ) {
260 $wp_filesystem->delete($working_dir, true);
264 //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
265 $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
267 // Remove working directory
268 $wp_filesystem->delete($working_dir, true);
270 // Force refresh of plugin update information
271 delete_option('update_plugins');
273 if( empty($filelist) )
274 return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
276 $folder = $filelist[0];
277 $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
278 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
280 return $folder . '/' . $pluginfiles[0];
283 function wp_update_theme($theme, $feedback = '') {
284 global $wp_filesystem;
286 if ( !empty($feedback) )
287 add_filter('update_feedback', $feedback);
289 // Is an update available?
290 $current = get_option( 'update_themes' );
291 if ( !isset( $current->response[ $theme ] ) )
292 return new WP_Error('up_to_date', __('The theme is at the latest version.'));
294 $r = $current->response[ $theme ];
296 $themes = get_themes();
297 foreach ( (array) $themes as $this_theme ) {
298 if ( $this_theme['Stylesheet'] == $theme ) {
299 $theme_directory = preg_replace('!^/themes/!i', '', $this_theme['Stylesheet Dir']);
305 if ( empty($theme_directory) )
306 return new WP_Error('theme_non_existant', __('Theme does not exist.'));
308 // Is a filesystem accessor setup?
309 if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
312 if ( ! is_object($wp_filesystem) )
313 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
315 if ( $wp_filesystem->errors->get_error_code() )
316 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
318 //Get the base plugin folder
319 $themes_dir = $wp_filesystem->wp_themes_dir();
320 if ( empty($themes_dir) )
321 return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress Theme directory.'));
323 //And the same for the Content directory.
324 $content_dir = $wp_filesystem->wp_content_dir();
325 if( empty($content_dir) )
326 return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
328 $themes_dir = trailingslashit( $themes_dir );
329 $content_dir = trailingslashit( $content_dir );
331 if ( empty($r->package) )
332 return new WP_Error('no_package', __('Upgrade package not available.'));
334 // Download the package
335 apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $r['package']));
336 $download_file = download_url($r['package']);
338 if ( is_wp_error($download_file) )
339 return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
341 $working_dir = $content_dir . 'upgrade/' . basename($theme_directory);
343 // Clean up working directory
344 if ( $wp_filesystem->is_dir($working_dir) )
345 $wp_filesystem->delete($working_dir, true);
347 apply_filters('update_feedback', __('Unpacking the update'));
348 // Unzip package to working directory
349 $result = unzip_file($download_file, $working_dir);
351 // Once extracted, delete the package
352 unlink($download_file);
354 if ( is_wp_error($result) ) {
355 $wp_filesystem->delete($working_dir, true);
359 //TODO: Is theme currently active? If so, set default theme
361 if ( is_plugin_active($plugin) ) {
362 //Deactivate the plugin silently, Prevent deactivation hooks from running.
363 apply_filters('update_feedback', __('Deactivating the plugin'));
364 deactivate_plugins($plugin, true);
367 // Remove the existing plugin.
368 apply_filters('update_feedback', __('Removing the old version of the theme'));
369 $deleted = $wp_filesystem->delete($themes_dir . $theme_directory, true);
372 $wp_filesystem->delete($working_dir, true);
373 return new WP_Error('delete_failed', __('Could not remove the old plugin'));
376 apply_filters('update_feedback', __('Installing the latest version'));
377 // Copy new version of plugin into place.
378 $result = copy_dir($working_dir, $themes_dir);
379 if ( is_wp_error($result) ) {
380 $wp_filesystem->delete($working_dir, true);
384 //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
385 //$filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
387 // Remove working directory
388 $wp_filesystem->delete($working_dir, true);
390 // Force refresh of plugin update information
391 delete_option('update_themes');
393 /*if( empty($filelist) )
394 return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
396 $folder = $filelist[0];
397 $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
398 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
400 return $folder . '/' . $pluginfiles[0];*/
404 function wp_update_core($current, $feedback = '') {
405 global $wp_filesystem;
407 @set_time_limit( 300 );
409 if ( !empty($feedback) )
410 add_filter('update_feedback', $feedback);
412 // Is an update available?
413 if ( !isset( $current->response ) || $current->response == 'latest' )
414 return new WP_Error('up_to_date', __('WordPress is at the latest version.'));
416 // Is a filesystem accessor setup?
417 if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
420 if ( ! is_object($wp_filesystem) )
421 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
423 if ( $wp_filesystem->errors->get_error_code() )
424 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
426 // Get the base WP folder
427 $wp_dir = $wp_filesystem->abspath();
428 if ( empty($wp_dir) )
429 return new WP_Error('fs_no_wp_dir', __('Unable to locate WordPress directory.'));
431 // And the same for the Content directory.
432 $content_dir = $wp_filesystem->wp_content_dir();
433 if( empty($content_dir) )
434 return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
436 $wp_dir = trailingslashit( $wp_dir );
437 $content_dir = trailingslashit( $content_dir );
439 // Get the URL to the zip file
440 $package = $current->package;
442 // Download the package
443 apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
444 $download_file = download_url($package);
446 if ( is_wp_error($download_file) )
447 return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
449 $working_dir = $content_dir . 'upgrade/core';
450 // Clean up working directory
451 if ( $wp_filesystem->is_dir($working_dir) ) {
452 $wp_filesystem->delete($working_dir, true);
455 apply_filters('update_feedback', __('Unpacking the core update'));
456 // Unzip package to working directory
457 $result = unzip_file($download_file, $working_dir);
458 // Once extracted, delete the package
459 unlink($download_file);
461 if ( is_wp_error($result) ) {
462 $wp_filesystem->delete($working_dir, true);
466 // Copy update-core.php from the new version into place.
467 if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
468 $wp_filesystem->delete($working_dir, true);
469 return new WP_Error('copy_failed', __('Could not copy files'));
471 $wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
473 require(ABSPATH . 'wp-admin/includes/update-core.php');
475 return update_core($working_dir, $wp_dir);
478 function maintenance_nag() {
480 if ( ! isset( $upgrading ) )
483 if ( current_user_can('manage_options') )
484 $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
486 $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
488 echo "<div id='update-nag'>$msg</div>";
490 add_action( 'admin_notices', 'maintenance_nag' );