Wordpress 4.6
[autoinstalls/wordpress.git] / wp-admin / includes / class-plugin-upgrader.php
1 <?php
2 /**
3  * Upgrade API: Plugin_Upgrader class
4  *
5  * @package WordPress
6  * @subpackage Upgrader
7  * @since 4.6.0
8  */
9
10 /**
11  * Core class used for upgrading/installing plugins.
12  *
13  * It is designed to upgrade/install plugins from a local zip, remote zip URL,
14  * or uploaded zip file.
15  *
16  * @since 2.8.0
17  * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
18  *
19  * @see WP_Upgrader
20  */
21 class Plugin_Upgrader extends WP_Upgrader {
22
23         /**
24          * Plugin upgrade result.
25          *
26          * @since 2.8.0
27          * @access public
28          * @var array|WP_Error $result
29          *
30          * @see WP_Upgrader::$result
31          */
32         public $result;
33
34         /**
35          * Whether a bulk upgrade/install is being performed.
36          *
37          * @since 2.9.0
38          * @access public
39          * @var bool $bulk
40          */
41         public $bulk = false;
42
43         /**
44          * Initialize the upgrade strings.
45          *
46          * @since 2.8.0
47          * @access public
48          */
49         public function upgrade_strings() {
50                 $this->strings['up_to_date'] = __('The plugin is at the latest version.');
51                 $this->strings['no_package'] = __('Update package not available.');
52                 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>&#8230;');
53                 $this->strings['unpack_package'] = __('Unpacking the update&#8230;');
54                 $this->strings['remove_old'] = __('Removing the old version of the plugin&#8230;');
55                 $this->strings['remove_old_failed'] = __('Could not remove the old plugin.');
56                 $this->strings['process_failed'] = __('Plugin update failed.');
57                 $this->strings['process_success'] = __('Plugin updated successfully.');
58                 $this->strings['process_bulk_success'] = __('Plugins updated successfully.');
59         }
60
61         /**
62          * Initialize the install strings.
63          *
64          * @since 2.8.0
65          * @access public
66          */
67         public function install_strings() {
68                 $this->strings['no_package'] = __('Install package not available.');
69                 $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>&#8230;');
70                 $this->strings['unpack_package'] = __('Unpacking the package&#8230;');
71                 $this->strings['installing_package'] = __('Installing the plugin&#8230;');
72                 $this->strings['no_files'] = __('The plugin contains no files.');
73                 $this->strings['process_failed'] = __('Plugin install failed.');
74                 $this->strings['process_success'] = __('Plugin installed successfully.');
75         }
76
77         /**
78          * Install a plugin package.
79          *
80          * @since 2.8.0
81          * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
82          * @access public
83          *
84          * @param string $package The full local path or URI of the package.
85          * @param array  $args {
86          *     Optional. Other arguments for installing a plugin package. Default empty array.
87          *
88          *     @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
89          *                                    Default true.
90          * }
91          * @return bool|WP_Error True if the install was successful, false or a WP_Error otherwise.
92          */
93         public function install( $package, $args = array() ) {
94
95                 $defaults = array(
96                         'clear_update_cache' => true,
97                 );
98                 $parsed_args = wp_parse_args( $args, $defaults );
99
100                 $this->init();
101                 $this->install_strings();
102
103                 add_filter('upgrader_source_selection', array($this, 'check_package') );
104                 // Clear cache so wp_update_plugins() knows about the new plugin.
105                 add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
106
107                 $this->run( array(
108                         'package' => $package,
109                         'destination' => WP_PLUGIN_DIR,
110                         'clear_destination' => false, // Do not overwrite files.
111                         'clear_working' => true,
112                         'hook_extra' => array(
113                                 'type' => 'plugin',
114                                 'action' => 'install',
115                         )
116                 ) );
117
118                 remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
119                 remove_filter('upgrader_source_selection', array($this, 'check_package') );
120
121                 if ( ! $this->result || is_wp_error($this->result) )
122                         return $this->result;
123
124                 // Force refresh of plugin update information
125                 wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
126
127                 return true;
128         }
129
130         /**
131          * Upgrade a plugin.
132          *
133          * @since 2.8.0
134          * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
135          * @access public
136          *
137          * @param string $plugin The basename path to the main plugin file.
138          * @param array  $args {
139          *     Optional. Other arguments for upgrading a plugin package. Default empty array.
140          *
141          *     @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
142          *                                    Default true.
143          * }
144          * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
145          */
146         public function upgrade( $plugin, $args = array() ) {
147
148                 $defaults = array(
149                         'clear_update_cache' => true,
150                 );
151                 $parsed_args = wp_parse_args( $args, $defaults );
152
153                 $this->init();
154                 $this->upgrade_strings();
155
156                 $current = get_site_transient( 'update_plugins' );
157                 if ( !isset( $current->response[ $plugin ] ) ) {
158                         $this->skin->before();
159                         $this->skin->set_result(false);
160                         $this->skin->error('up_to_date');
161                         $this->skin->after();
162                         return false;
163                 }
164
165                 // Get the URL to the zip file
166                 $r = $current->response[ $plugin ];
167
168                 add_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'), 10, 2);
169                 add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
170                 //'source_selection' => array($this, 'source_selection'), //there's a trac ticket to move up the directory for zip's which are made a bit differently, useful for non-.org plugins.
171                 // Clear cache so wp_update_plugins() knows about the new plugin.
172                 add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
173
174                 $this->run( array(
175                         'package' => $r->package,
176                         'destination' => WP_PLUGIN_DIR,
177                         'clear_destination' => true,
178                         'clear_working' => true,
179                         'hook_extra' => array(
180                                 'plugin' => $plugin,
181                                 'type' => 'plugin',
182                                 'action' => 'update',
183                         ),
184                 ) );
185
186                 // Cleanup our hooks, in case something else does a upgrade on this connection.
187                 remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
188                 remove_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'));
189                 remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
190
191                 if ( ! $this->result || is_wp_error($this->result) )
192                         return $this->result;
193
194                 // Force refresh of plugin update information
195                 wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
196
197                 return true;
198         }
199
200         /**
201          * Bulk upgrade several plugins at once.
202          *
203          * @since 2.8.0
204          * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
205          * @access public
206          *
207          * @param array $plugins Array of the basename paths of the plugins' main files.
208          * @param array $args {
209          *     Optional. Other arguments for upgrading several plugins at once. Default empty array.
210          *
211          *     @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
212          *                                    Default true.
213          * }
214          * @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem.
215          */
216         public function bulk_upgrade( $plugins, $args = array() ) {
217
218                 $defaults = array(
219                         'clear_update_cache' => true,
220                 );
221                 $parsed_args = wp_parse_args( $args, $defaults );
222
223                 $this->init();
224                 $this->bulk = true;
225                 $this->upgrade_strings();
226
227                 $current = get_site_transient( 'update_plugins' );
228
229                 add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
230                 add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
231
232                 $this->skin->header();
233
234                 // Connect to the Filesystem first.
235                 $res = $this->fs_connect( array(WP_CONTENT_DIR, WP_PLUGIN_DIR) );
236                 if ( ! $res ) {
237                         $this->skin->footer();
238                         return false;
239                 }
240
241                 $this->skin->bulk_header();
242
243                 /*
244                  * Only start maintenance mode if:
245                  * - running Multisite and there are one or more plugins specified, OR
246                  * - a plugin with an update available is currently active.
247                  * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
248                  */
249                 $maintenance = ( is_multisite() && ! empty( $plugins ) );
250                 foreach ( $plugins as $plugin )
251                         $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
252                 if ( $maintenance )
253                         $this->maintenance_mode(true);
254
255                 $results = array();
256
257                 $this->update_count = count($plugins);
258                 $this->update_current = 0;
259                 foreach ( $plugins as $plugin ) {
260                         $this->update_current++;
261                         $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
262
263                         if ( !isset( $current->response[ $plugin ] ) ) {
264                                 $this->skin->set_result('up_to_date');
265                                 $this->skin->before();
266                                 $this->skin->feedback('up_to_date');
267                                 $this->skin->after();
268                                 $results[$plugin] = true;
269                                 continue;
270                         }
271
272                         // Get the URL to the zip file.
273                         $r = $current->response[ $plugin ];
274
275                         $this->skin->plugin_active = is_plugin_active($plugin);
276
277                         $result = $this->run( array(
278                                 'package' => $r->package,
279                                 'destination' => WP_PLUGIN_DIR,
280                                 'clear_destination' => true,
281                                 'clear_working' => true,
282                                 'is_multi' => true,
283                                 'hook_extra' => array(
284                                         'plugin' => $plugin
285                                 )
286                         ) );
287
288                         $results[$plugin] = $this->result;
289
290                         // Prevent credentials auth screen from displaying multiple times
291                         if ( false === $result )
292                                 break;
293                 } //end foreach $plugins
294
295                 $this->maintenance_mode(false);
296
297                 /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
298                 do_action( 'upgrader_process_complete', $this, array(
299                         'action' => 'update',
300                         'type' => 'plugin',
301                         'bulk' => true,
302                         'plugins' => $plugins,
303                 ) );
304
305                 $this->skin->bulk_footer();
306
307                 $this->skin->footer();
308
309                 // Cleanup our hooks, in case something else does a upgrade on this connection.
310                 remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
311                 remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
312
313                 // Force refresh of plugin update information.
314                 wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
315
316                 return $results;
317         }
318
319         /**
320          * Check a source package to be sure it contains a plugin.
321          *
322          * This function is added to the {@see 'upgrader_source_selection'} filter by
323          * Plugin_Upgrader::install().
324          *
325          * @since 3.3.0
326          * @access public
327          *
328          * @global WP_Filesystem_Base $wp_filesystem Subclass
329          *
330          * @param string $source The path to the downloaded package source.
331          * @return string|WP_Error The source as passed, or a WP_Error object
332          *                         if no plugins were found.
333          */
334         public function check_package($source) {
335                 global $wp_filesystem;
336
337                 if ( is_wp_error($source) )
338                         return $source;
339
340                 $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
341                 if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, let's not prevent installation.
342                         return $source;
343
344                 // Check the folder contains at least 1 valid plugin.
345                 $plugins_found = false;
346                 $files = glob( $working_directory . '*.php' );
347                 if ( $files ) {
348                         foreach ( $files as $file ) {
349                                 $info = get_plugin_data( $file, false, false );
350                                 if ( ! empty( $info['Name'] ) ) {
351                                         $plugins_found = true;
352                                         break;
353                                 }
354                         }
355                 }
356
357                 if ( ! $plugins_found )
358                         return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
359
360                 return $source;
361         }
362
363         /**
364          * Retrieve the path to the file that contains the plugin info.
365          *
366          * This isn't used internally in the class, but is called by the skins.
367          *
368          * @since 2.8.0
369          * @access public
370          *
371          * @return string|false The full path to the main plugin file, or false.
372          */
373         public function plugin_info() {
374                 if ( ! is_array($this->result) )
375                         return false;
376                 if ( empty($this->result['destination_name']) )
377                         return false;
378
379                 $plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
380                 if ( empty($plugin) )
381                         return false;
382
383                 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
384
385                 return $this->result['destination_name'] . '/' . $pluginfiles[0];
386         }
387
388         /**
389          * Deactivates a plugin before it is upgraded.
390          *
391          * Hooked to the {@see 'upgrader_pre_install'} filter by Plugin_Upgrader::upgrade().
392          *
393          * @since 2.8.0
394          * @since 4.1.0 Added a return value.
395          * @access public
396          *
397          * @param bool|WP_Error  $return Upgrade offer return.
398          * @param array          $plugin Plugin package arguments.
399          * @return bool|WP_Error The passed in $return param or WP_Error.
400          */
401         public function deactivate_plugin_before_upgrade($return, $plugin) {
402
403                 if ( is_wp_error($return) ) //Bypass.
404                         return $return;
405
406                 // When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
407                 if ( defined( 'DOING_CRON' ) && DOING_CRON )
408                         return $return;
409
410                 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
411                 if ( empty($plugin) )
412                         return new WP_Error('bad_request', $this->strings['bad_request']);
413
414                 if ( is_plugin_active($plugin) ) {
415                         //Deactivate the plugin silently, Prevent deactivation hooks from running.
416                         deactivate_plugins($plugin, true);
417                 }
418
419                 return $return;
420         }
421
422         /**
423          * Delete the old plugin during an upgrade.
424          *
425          * Hooked to the {@see 'upgrader_clear_destination'} filter by
426          * Plugin_Upgrader::upgrade() and Plugin_Upgrader::bulk_upgrade().
427          *
428          * @since 2.8.0
429          * @access public
430          *
431          * @global WP_Filesystem_Base $wp_filesystem Subclass
432      *
433          * @param bool|WP_Error $removed
434          * @param string        $local_destination
435          * @param string        $remote_destination
436          * @param array         $plugin
437          * @return WP_Error|bool
438          */
439         public function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
440                 global $wp_filesystem;
441
442                 if ( is_wp_error($removed) )
443                         return $removed; //Pass errors through.
444
445                 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
446                 if ( empty($plugin) )
447                         return new WP_Error('bad_request', $this->strings['bad_request']);
448
449                 $plugins_dir = $wp_filesystem->wp_plugins_dir();
450                 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
451
452                 if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If it's already vanished.
453                         return $removed;
454
455                 // If plugin is in its own directory, recursively delete the directory.
456                 if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory separator AND that it's not the root plugin folder
457                         $deleted = $wp_filesystem->delete($this_plugin_dir, true);
458                 else
459                         $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
460
461                 if ( ! $deleted )
462                         return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
463
464                 return true;
465         }
466 }