3
* Upgrade API: Plugin_Upgrader class
11
* Core class used for upgrading/installing plugins.
13
* It is designed to upgrade/install plugins from a local zip, remote zip URL,
14
* or uploaded zip file.
17
* @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
21
class Plugin_Upgrader extends WP_Upgrader {
24
* Plugin upgrade result.
28
* @var array|WP_Error $result
30
* @see WP_Upgrader::$result
35
* Whether a bulk upgrade/install is being performed.
44
* Initialize the upgrade strings.
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>…');
53
$this->strings['unpack_package'] = __('Unpacking the update…');
54
$this->strings['remove_old'] = __('Removing the old version of the plugin…');
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.');
62
* Initialize the install strings.
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>…');
70
$this->strings['unpack_package'] = __('Unpacking the package…');
71
$this->strings['installing_package'] = __('Installing the plugin…');
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.');
78
* Install a plugin package.
81
* @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
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.
88
* @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
91
* @return bool|WP_Error True if the install was successful, false or a WP_Error otherwise.
93
public function install( $package, $args = array() ) {
96
'clear_update_cache' => true,
98
$parsed_args = wp_parse_args( $args, $defaults );
101
$this->install_strings();
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 );
108
'package' => $package,
109
'destination' => WP_PLUGIN_DIR,
110
'clear_destination' => false, // Do not overwrite files.
111
'clear_working' => true,
112
'hook_extra' => array(
114
'action' => 'install',
118
remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
119
remove_filter('upgrader_source_selection', array($this, 'check_package') );
121
if ( ! $this->result || is_wp_error($this->result) )
122
return $this->result;
124
// Force refresh of plugin update information
125
wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
134
* @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
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.
141
* @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
144
* @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
146
public function upgrade( $plugin, $args = array() ) {
149
'clear_update_cache' => true,
151
$parsed_args = wp_parse_args( $args, $defaults );
154
$this->upgrade_strings();
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();
165
// Get the URL to the zip file
166
$r = $current->response[ $plugin ];
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 );
175
'package' => $r->package,
176
'destination' => WP_PLUGIN_DIR,
177
'clear_destination' => true,
178
'clear_working' => true,
179
'hook_extra' => array(
182
'action' => 'update',
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'));
191
if ( ! $this->result || is_wp_error($this->result) )
192
return $this->result;
194
// Force refresh of plugin update information
195
wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
201
* Bulk upgrade several plugins at once.
204
* @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
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.
211
* @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
214
* @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem.
216
public function bulk_upgrade( $plugins, $args = array() ) {
219
'clear_update_cache' => true,
221
$parsed_args = wp_parse_args( $args, $defaults );
225
$this->upgrade_strings();
227
$current = get_site_transient( 'update_plugins' );
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 );
232
$this->skin->header();
234
// Connect to the Filesystem first.
235
$res = $this->fs_connect( array(WP_CONTENT_DIR, WP_PLUGIN_DIR) );
237
$this->skin->footer();
241
$this->skin->bulk_header();
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.
249
$maintenance = ( is_multisite() && ! empty( $plugins ) );
250
foreach ( $plugins as $plugin )
251
$maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
253
$this->maintenance_mode(true);
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);
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;
272
// Get the URL to the zip file.
273
$r = $current->response[ $plugin ];
275
$this->skin->plugin_active = is_plugin_active($plugin);
277
$result = $this->run( array(
278
'package' => $r->package,
279
'destination' => WP_PLUGIN_DIR,
280
'clear_destination' => true,
281
'clear_working' => true,
283
'hook_extra' => array(
288
$results[$plugin] = $this->result;
290
// Prevent credentials auth screen from displaying multiple times
291
if ( false === $result )
293
} //end foreach $plugins
295
$this->maintenance_mode(false);
297
/** This action is documented in wp-admin/includes/class-wp-upgrader.php */
298
do_action( 'upgrader_process_complete', $this, array(
299
'action' => 'update',
302
'plugins' => $plugins,
305
$this->skin->bulk_footer();
307
$this->skin->footer();
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'));
313
// Force refresh of plugin update information.
314
wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
320
* Check a source package to be sure it contains a plugin.
322
* This function is added to the {@see 'upgrader_source_selection'} filter by
323
* Plugin_Upgrader::install().
328
* @global WP_Filesystem_Base $wp_filesystem Subclass
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.
334
public function check_package($source) {
335
global $wp_filesystem;
337
if ( is_wp_error($source) )
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.
344
// Check the folder contains at least 1 valid plugin.
345
$plugins_found = false;
346
$files = glob( $working_directory . '*.php' );
348
foreach ( $files as $file ) {
349
$info = get_plugin_data( $file, false, false );
350
if ( ! empty( $info['Name'] ) ) {
351
$plugins_found = true;
357
if ( ! $plugins_found )
358
return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
364
* Retrieve the path to the file that contains the plugin info.
366
* This isn't used internally in the class, but is called by the skins.
371
* @return string|false The full path to the main plugin file, or false.
373
public function plugin_info() {
374
if ( ! is_array($this->result) )
376
if ( empty($this->result['destination_name']) )
379
$plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
380
if ( empty($plugin) )
383
$pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
385
return $this->result['destination_name'] . '/' . $pluginfiles[0];
389
* Deactivates a plugin before it is upgraded.
391
* Hooked to the {@see 'upgrader_pre_install'} filter by Plugin_Upgrader::upgrade().
394
* @since 4.1.0 Added a return value.
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.
401
public function deactivate_plugin_before_upgrade($return, $plugin) {
403
if ( is_wp_error($return) ) //Bypass.
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 )
410
$plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
411
if ( empty($plugin) )
412
return new WP_Error('bad_request', $this->strings['bad_request']);
414
if ( is_plugin_active($plugin) ) {
415
//Deactivate the plugin silently, Prevent deactivation hooks from running.
416
deactivate_plugins($plugin, true);
423
* Delete the old plugin during an upgrade.
425
* Hooked to the {@see 'upgrader_clear_destination'} filter by
426
* Plugin_Upgrader::upgrade() and Plugin_Upgrader::bulk_upgrade().
431
* @global WP_Filesystem_Base $wp_filesystem Subclass
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
439
public function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
440
global $wp_filesystem;
442
if ( is_wp_error($removed) )
443
return $removed; //Pass errors through.
445
$plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
446
if ( empty($plugin) )
447
return new WP_Error('bad_request', $this->strings['bad_request']);
449
$plugins_dir = $wp_filesystem->wp_plugins_dir();
450
$this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
452
if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If it's already vanished.
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);
459
$deleted = $wp_filesystem->delete($plugins_dir . $plugin);
462
return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);