root/trunk/wp-admin/includes/update.php

Revision 8060, 8.9 kB (checked in by ryan, 1 month ago)

Add delete_plugins and update_plugins caps. Props DD32. fixes #7096

  • Property svn:eol-style set to native
Line 
1 <?php
2
3 // The admin side of our 1.1 update system
4
5 function core_update_footer( $msg = '' ) {
6     if ( !current_user_can('manage_options') )
7         return sprintf( '| '.__( 'Version %s' ), $GLOBALS['wp_version'] );
8
9     $cur = get_option( 'update_core' );
10
11     switch ( $cur->response ) {
12     case 'development' :
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 );
14     break;
15
16     case 'upgrade' :
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 );
19             break;
20         }
21
22     case 'latest' :
23     default :
24         return sprintf( '| '.__( 'Version %s' ), $GLOBALS['wp_version'], $cur->url, $cur->current );
25     break;
26     }
27 }
28 add_filter( 'update_footer', 'core_update_footer' );
29
30 function update_nag() {
31     $cur = get_option( 'update_core' );
32
33     if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
34         return false;
35
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 );
38     else
39         $msg = sprintf( __('WordPress %2$s is available! Please notify the site administrator.'), $cur->url, $cur->current );
40
41     echo "<div id='update-nag'>$msg</div>";
42 }
43 add_action( 'admin_notices', 'update_nag', 3 );
44
45 // Called directly from dashboard
46 function update_right_now_message() {
47     $cur = get_option( 'update_core' );
48
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>';
52
53     echo "<span id='wp-version-message'>$msg</span>";
54 }
55
56 function wp_update_plugins() {
57     global $wp_version;
58
59     if ( !function_exists('fsockopen') )
60         return false;
61
62     $plugins = get_plugins();
63     $active  = get_option( 'active_plugins' );
64     $current = get_option( 'update_plugins' );
65
66     $new_option = '';
67     $new_option->last_checked = time();
68
69     $plugin_changed = false;
70     foreach ( $plugins as $file => $p ) {
71         $new_option->checked[ $file ] = $p['Version'];
72
73         if ( !isset( $current->checked[ $file ] ) ) {
74             $plugin_changed = true;
75             continue;
76         }
77
78         if ( strval($current->checked[ $file ]) !== strval($p['Version']) )
79             $plugin_changed = true;
80     }
81
82     if (
83         isset( $current->last_checked ) &&
84         43200 > ( time() - $current->last_checked ) &&
85         !$plugin_changed
86     )
87         return false;
88
89     $to_send->plugins = $plugins;
90     $to_send->active = $active;
91     $send = serialize( $to_send );
92
93     $request = 'plugins=' . urlencode( $send );
94     $http_request  = "POST /plugins/update-check/1.0/ HTTP/1.0\r\n";
95     $http_request .= "Host: api.wordpress.org\r\n";
96     $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=" . get_option('blog_charset') . "\r\n";
97     $http_request .= "Content-Length: " . strlen($request) . "\r\n";
98     $http_request .= 'User-Agent: WordPress/' . $wp_version . '; ' . get_bloginfo('url') . "\r\n";
99     $http_request .= "\r\n";
100     $http_request .= $request;
101
102     $response = '';
103     if( false != ( $fs = @fsockopen( 'api.wordpress.org', 80, $errno, $errstr, 3) ) && is_resource($fs) ) {
104         fwrite($fs, $http_request);
105
106         while ( !feof($fs) )
107             $response .= fgets($fs, 1160); // One TCP-IP packet
108         fclose($fs);
109         $response = explode("\r\n\r\n", $response, 2);
110     }
111
112     $response = unserialize( $response[1] );
113
114     if ( $response )
115         $new_option->response = $response;
116
117     update_option( 'update_plugins', $new_option );
118 }
119 add_action( 'load-plugins.php', 'wp_update_plugins' );
120
121 function wp_plugin_update_row( $file, $plugin_data ) {
122     $current = get_option( 'update_plugins' );
123     if ( !isset( $current->response[ $file ] ) )
124         return false;
125
126     $r = $current->response[ $file ];
127
128     echo '<tr><td colspan="5" class="plugin-update">';
129     if ( ! current_user_can('update_plugins') )
130         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);
131     else if ( empty($r->package) )
132         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);
133     else
134         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&amp;plugin=' . $file, 'upgrade-plugin_' . $file) );
135     
136     echo '</td></tr>';
137 }
138 add_action( 'after_plugin_row', 'wp_plugin_update_row', 10, 2 );
139
140 function wp_update_plugin($plugin, $feedback = '') {
141     global $wp_filesystem;
142
143     if ( !empty($feedback) )
144         add_filter('update_feedback', $feedback);
145
146     // Is an update available?
147     $current = get_option( 'update_plugins' );
148     if ( !isset( $current->response[ $plugin ] ) )
149         return new WP_Error('up_to_date', __('The plugin is at the latest version.'));
150
151     // Is a filesystem accessor setup?
152     if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
153         WP_Filesystem();
154
155     if ( ! is_object($wp_filesystem) )
156         return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
157
158     if ( $wp_filesystem->errors->get_error_code() )
159         return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
160
161     //Get the base plugin folder
162     $plugins_dir = $wp_filesystem->wp_plugins_dir();
163     if ( empty($plugins_dir) )
164         return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
165
166     //And the same for the Content directory.
167     $content_dir = $wp_filesystem->wp_content_dir();
168     if( empty($content_dir) )
169         return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
170     
171     $plugins_dir = trailingslashit( $plugins_dir );
172     $content_dir = trailingslashit( $content_dir );
173
174     // Get the URL to the zip file
175     $r = $current->response[ $plugin ];
176
177     if ( empty($r->package) )
178         return new WP_Error('no_package', __('Upgrade package not available.'));
179
180     // Download the package
181     $package = $r->package;
182     apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
183     $download_file = download_url($package);
184
185     if ( is_wp_error($download_file) )
186         return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
187
188     $working_dir = $content_dir . 'upgrade/' . basename($plugin, '.php');
189
190     // Clean up working directory
191     if ( $wp_filesystem->is_dir($working_dir) )
192         $wp_filesystem->delete($working_dir, true);
193
194     apply_filters('update_feedback', __('Unpacking the update'));
195     // Unzip package to working directory
196     $result = unzip_file($download_file, $working_dir);
197     
198     // Once extracted, delete the package
199     unlink($download_file);
200     
201     if ( is_wp_error($result) ) {
202         $wp_filesystem->delete($working_dir, true);
203         return $result;
204     }
205
206     if ( is_plugin_active($plugin) ) {
207         //Deactivate the plugin silently, Prevent deactivation hooks from running.
208         apply_filters('update_feedback', __('Deactivating the plugin'));
209         deactivate_plugins($plugin, true);
210     }
211
212     // Remove the existing plugin.
213     apply_filters('update_feedback', __('Removing the old version of the plugin'));
214     $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
215     
216     // If plugin is in its own directory, recursively delete the directory.
217     if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
218         $deleted = $wp_filesystem->delete($this_plugin_dir, true);
219     else
220         $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
221
222     if ( ! $deleted ) {
223         $wp_filesystem->delete($working_dir, true);
224         return new WP_Error('delete_failed', __('Could not remove the old plugin'));
225     }
226
227     apply_filters('update_feedback', __('Installing the latest version'));
228     // Copy new version of plugin into place.
229     $result = copy_dir($working_dir, $plugins_dir);
230     if ( is_wp_error($result) ) {
231         //$wp_filesystem->delete($working_dir, true); //TODO: Uncomment? This DOES mean that the new files are available in the upgrade folder if it fails.
232         return $result;
233     }
234
235     //Get a list of the directories in the working directory before we delete it, We need to know the new folder for the plugin
236     $filelist = array_keys( $wp_filesystem->dirlist($working_dir) );
237
238     // Remove working directory
239     $wp_filesystem->delete($working_dir, true);
240
241     // Force refresh of plugin update information
242     delete_option('update_plugins');
243     
244     if( empty($filelist) )
245         return false; //We couldnt find any files in the working dir, therefor no plugin installed? Failsafe backup.
246     
247     $folder = $filelist[0];
248     $plugin = get_plugins('/' . $folder); //Ensure to pass with leading slash
249     $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
250
251     return  $folder . '/' . $pluginfiles[0];
252 }
253
254 ?>
255
Note: See TracBrowser for help on using the browser.