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 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 );
110 'package' => $package,
111 'destination' => WP_PLUGIN_DIR,
112 'clear_destination' => false, // Do not overwrite files.
113 'clear_working' => true,
114 'hook_extra' => array(
116 'action' => 'install',
120 remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
121 remove_filter('upgrader_source_selection', array($this, 'check_package') );
123 if ( ! $this->result || is_wp_error($this->result) )
124 return $this->result;
126 // Force refresh of plugin update information
127 wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
136 * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
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.
143 * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
146 * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
148 public function upgrade( $plugin, $args = array() ) {
151 'clear_update_cache' => true,
153 $parsed_args = wp_parse_args( $args, $defaults );
156 $this->upgrade_strings();
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();
167 // Get the URL to the zip file
168 $r = $current->response[ $plugin ];
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 );
179 'package' => $r->package,
180 'destination' => WP_PLUGIN_DIR,
181 'clear_destination' => true,
182 'clear_working' => true,
183 'hook_extra' => array(
186 'action' => 'update',
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'));
195 if ( ! $this->result || is_wp_error($this->result) )
196 return $this->result;
198 // Force refresh of plugin update information
199 wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
205 * Bulk upgrade several plugins at once.
208 * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
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.
215 * @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
218 * @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem.
220 public function bulk_upgrade( $plugins, $args = array() ) {
223 'clear_update_cache' => true,
225 $parsed_args = wp_parse_args( $args, $defaults );
229 $this->upgrade_strings();
231 $current = get_site_transient( 'update_plugins' );
233 add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
235 $this->skin->header();
237 // Connect to the Filesystem first.
238 $res = $this->fs_connect( array(WP_CONTENT_DIR, WP_PLUGIN_DIR) );
240 $this->skin->footer();
244 $this->skin->bulk_header();
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.
252 $maintenance = ( is_multisite() && ! empty( $plugins ) );
253 foreach ( $plugins as $plugin )
254 $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
256 $this->maintenance_mode(true);
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);
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;
275 // Get the URL to the zip file.
276 $r = $current->response[ $plugin ];
278 $this->skin->plugin_active = is_plugin_active($plugin);
280 $result = $this->run( array(
281 'package' => $r->package,
282 'destination' => WP_PLUGIN_DIR,
283 'clear_destination' => true,
284 'clear_working' => true,
286 'hook_extra' => array(
291 $results[$plugin] = $this->result;
293 // Prevent credentials auth screen from displaying multiple times
294 if ( false === $result )
296 } //end foreach $plugins
298 $this->maintenance_mode(false);
300 // Force refresh of plugin update information.
301 wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
303 /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
304 do_action( 'upgrader_process_complete', $this, array(
305 'action' => 'update',
308 'plugins' => $plugins,
311 $this->skin->bulk_footer();
313 $this->skin->footer();
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'));
322 * Check a source package to be sure it contains a plugin.
324 * This function is added to the {@see 'upgrader_source_selection'} filter by
325 * Plugin_Upgrader::install().
330 * @global WP_Filesystem_Base $wp_filesystem Subclass
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.
336 public function check_package($source) {
337 global $wp_filesystem;
339 if ( is_wp_error($source) )
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.
346 // Check the folder contains at least 1 valid plugin.
347 $plugins_found = false;
348 $files = glob( $working_directory . '*.php' );
350 foreach ( $files as $file ) {
351 $info = get_plugin_data( $file, false, false );
352 if ( ! empty( $info['Name'] ) ) {
353 $plugins_found = true;
359 if ( ! $plugins_found )
360 return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
366 * Retrieve the path to the file that contains the plugin info.
368 * This isn't used internally in the class, but is called by the skins.
373 * @return string|false The full path to the main plugin file, or false.
375 public function plugin_info() {
376 if ( ! is_array($this->result) )
378 if ( empty($this->result['destination_name']) )
381 $plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
382 if ( empty($plugin) )
385 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
387 return $this->result['destination_name'] . '/' . $pluginfiles[0];
391 * Deactivates a plugin before it is upgraded.
393 * Hooked to the {@see 'upgrader_pre_install'} filter by Plugin_Upgrader::upgrade().
396 * @since 4.1.0 Added a return value.
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.
403 public function deactivate_plugin_before_upgrade($return, $plugin) {
405 if ( is_wp_error($return) ) //Bypass.
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 )
412 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
413 if ( empty($plugin) )
414 return new WP_Error('bad_request', $this->strings['bad_request']);
416 if ( is_plugin_active($plugin) ) {
417 //Deactivate the plugin silently, Prevent deactivation hooks from running.
418 deactivate_plugins($plugin, true);
425 * Delete the old plugin during an upgrade.
427 * Hooked to the {@see 'upgrader_clear_destination'} filter by
428 * Plugin_Upgrader::upgrade() and Plugin_Upgrader::bulk_upgrade().
433 * @global WP_Filesystem_Base $wp_filesystem Subclass
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
441 public function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
442 global $wp_filesystem;
444 if ( is_wp_error($removed) )
445 return $removed; //Pass errors through.
447 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
448 if ( empty($plugin) )
449 return new WP_Error('bad_request', $this->strings['bad_request']);
451 $plugins_dir = $wp_filesystem->wp_plugins_dir();
452 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
454 if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If it's already vanished.
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);
461 $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
464 return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);