3 // The admin side of our 1.1 update system
5 function core_update_footer( $msg = '' ) {
6 if ( !current_user_can('manage_options') )
7 return sprintf( '| '.__( 'Version %s' ), $GLOBALS['wp_version'] );
9 $cur = get_option( 'update_core' );
11 switch ( $cur->response ) {
13 return sprintf( '| '.__( 'You are using a development version (%s). Cool! Please <a href="%s">stay updated</a>.' ), $GLOBALS['wp_version'], $cur->url, $cur->current );
17 if ( current_user_can('manage_options') ) {
18 return sprintf( '| <strong>'.__( '<a href="%2$s">Get Version %3$s</a>' ).'</strong>', $GLOBALS['wp_version'], $cur->url, $cur->current );
24 return sprintf( '| '.__( 'Version %s' ), $GLOBALS['wp_version'], $cur->url, $cur->current );
28 add_filter( 'update_footer', 'core_update_footer' );
30 function update_nag() {
31 $cur = get_option( 'update_core' );
33 if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
36 if ( current_user_can('manage_options') )
37 $msg = sprintf( __('WordPress %2$s is available! <a href="%1$s">Please update now</a>.'), $cur->url, $cur->current );
39 $msg = sprintf( __('WordPress %2$s is available! Please notify the site administrator.'), $cur->url, $cur->current );
41 echo "<div id='update-nag'>$msg</div>";
43 add_action( 'admin_notices', 'update_nag', 3 );
45 // Called directly from dashboard
46 function update_right_now_message() {
47 $cur = get_option( 'update_core' );
49 $msg = sprintf( __('This is WordPress version %s.'), $GLOBALS['wp_version'] );
50 if ( isset( $cur->response ) && $cur->response == 'upgrade' && current_user_can('manage_options') )
51 $msg .= " <a href='$cur->url' class='rbutton'>" . sprintf( __('Update to %s'), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a>';
53 echo "<span id='wp-version-message'>$msg</span>";
56 function wp_plugin_update_row( $file, $plugin_data ) {
57 $current = get_option( 'update_plugins' );
58 if ( !isset( $current->response[ $file ] ) )
61 $r = $current->response[ $file ];
63 echo '<tr><td colspan="5" class="plugin-update">';
64 if ( ! current_user_can('update_plugins') )
65 printf( __('There is a new version of %1$s available. <a href="%2$s">Download version %3$s here</a>.'), $plugin_data['Name'], $r->url, $r->new_version);
66 else if ( empty($r->package) )
67 printf( __('There is a new version of %1$s available. <a href="%2$s">Download version %3$s here</a> <em>automatic upgrade unavailable for this plugin</em>.'), $plugin_data['Name'], $r->url, $r->new_version);
69 printf( __('There is a new version of %1$s available. <a href="%2$s">Download version %3$s here</a> or <a href="%4$s">upgrade automatically</a>.'), $plugin_data['Name'], $r->url, $r->new_version, wp_nonce_url('update.php?action=upgrade-plugin&plugin=' . $file, 'upgrade-plugin_' . $file) );
73 add_action( 'after_plugin_row', 'wp_plugin_update_row', 10, 2 );
75 function wp_update_plugin($plugin, $feedback = '') {
76 global $wp_filesystem;
78 if ( !empty($feedback) )
79 add_filter('update_feedback', $feedback);
81 // Is an update available?
82 $current = get_option( 'update_plugins' );
83 if ( !isset( $current->response[ $plugin ] ) )
84 return new WP_Error('up_to_date', __('The plugin is at the latest version.'));
86 // Is a filesystem accessor setup?
87 if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
90 if ( ! is_object($wp_filesystem) )
91 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
93 if ( $wp_filesystem->errors->get_error_code() )
94 return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
96 //Get the base plugin folder
97 $plugins_dir = $wp_filesystem->wp_plugins_dir();
98 if ( empty($plugins_dir) )
99 return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
101 //And the same for the Content directory.
102 $content_dir = $wp_filesystem->wp_content_dir();
103 if( empty($content_dir) )
104 return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
106 $plugins_dir = trailingslashit( $plugins_dir );
107 $content_dir = trailingslashit( $content_dir );
109 // Get the URL to the zip file
110 $r = $current->response[ $plugin ];
112 if ( empty($r->package) )
113 return new WP_Error('no_package', __('Upgrade package not available.'));
115 // Download the package
116 $package = $r->package;
117 apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
118 $download_file = download_url($package);
120 if ( is_wp_error($download_file) )
121 return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
123 $working_dir = $content_dir . 'upgrade/' . basename($plugin, '.php');
125 // Clean up working directory
126 if ( $wp_filesystem->is_dir($working_dir) )
127 $wp_filesystem->delete($working_dir, true);
129 apply_filters('update_feedback', __('Unpacking the update'));
130 // Unzip package to working directory
131 $result = unzip_file($download_file, $working_dir);
133 // Once extracted, delete the package
134 unlink($download_file);
136 if ( is_wp_error($result) ) {
137 $wp_filesystem->delete($working_dir, true);
141 if ( is_plugin_active($plugin) ) {
142 //Deactivate the plugin silently, Prevent deactivation hooks from running.
143 apply_filters('update_feedback', __('Deactivating the plugin'));
144 deactivate_plugins($plugin, true);
147 // Remove the existing plugin.
148 apply_filters('update_feedback', __('Removing the old version of the plugin'));
149 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
151 // If plugin is in its own directory, recursively delete the directory.
152 if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
153 $deleted = $wp_filesystem->delete($this_plugin_dir, true);
155 $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
158 $wp_filesystem->delete($working_dir, true);
159 return new WP_Error('delete_failed', __('Could not remove the old plugin'));
162 apply_filters('update_feedback', __('Installing the latest version'));
163 // Copy new version of plugin into place.
164 $result = copy_dir($working_dir, $plugins_dir);
165 if ( is_wp_error($result) ) {
166 //$wp_filesystem->delete($working_dir, true); //TODO: Uncomment? This DOES mean that the new files are available in the upgrade folder if it fails.
170 //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
171 $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
173 // Remove working directory
174 $wp_filesystem->delete($working_dir, true);
176 // Force refresh of plugin update information
177 delete_option('update_plugins');
179 if( empty($filelist) )
180 return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
182 $folder = $filelist[0];
183 $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
184 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
186 return $folder . '/' . $pluginfiles[0];