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