* Upgrade API: Plugin_Upgrader class
* Core class used for upgrading/installing plugins.
* It is designed to upgrade/install plugins from a local zip, remote zip URL,
* @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
class Plugin_Upgrader extends WP_Upgrader {
* @var array|WP_Error $result
* @see WP_Upgrader::$result
* Whether a bulk upgrade/installation is being performed.
* @var array $new_plugin_data
public $new_plugin_data = array();
* Initializes the upgrade strings.
public function upgrade_strings() {
$this->strings['up_to_date'] = __( 'The plugin is at the latest version.' );
$this->strings['no_package'] = __( 'Update package not available.' );
/* translators: %s: Package URL. */
$this->strings['downloading_package'] = sprintf( __( 'Downloading update from %s…' ), '<span class="code pre">%s</span>' );
$this->strings['unpack_package'] = __( 'Unpacking the update…' );
$this->strings['remove_old'] = __( 'Removing the old version of the plugin…' );
$this->strings['remove_old_failed'] = __( 'Could not remove the old plugin.' );
$this->strings['process_failed'] = __( 'Plugin update failed.' );
$this->strings['process_success'] = __( 'Plugin updated successfully.' );
$this->strings['process_bulk_success'] = __( 'Plugins updated successfully.' );
* Initializes the installation strings.
public function install_strings() {
$this->strings['no_package'] = __( 'Installation package not available.' );
/* translators: %s: Package URL. */
$this->strings['downloading_package'] = sprintf( __( 'Downloading installation package from %s…' ), '<span class="code pre">%s</span>' );
$this->strings['unpack_package'] = __( 'Unpacking the package…' );
$this->strings['installing_package'] = __( 'Installing the plugin…' );
$this->strings['remove_old'] = __( 'Removing the current plugin…' );
$this->strings['remove_old_failed'] = __( 'Could not remove the current plugin.' );
$this->strings['no_files'] = __( 'The plugin contains no files.' );
$this->strings['process_failed'] = __( 'Plugin installation failed.' );
$this->strings['process_success'] = __( 'Plugin installed successfully.' );
/* translators: 1: Plugin name, 2: Plugin version. */
$this->strings['process_success_specific'] = __( 'Successfully installed the plugin <strong>%1$s %2$s</strong>.' );
if ( ! empty( $this->skin->overwrite ) ) {
if ( 'update-plugin' === $this->skin->overwrite ) {
$this->strings['installing_package'] = __( 'Updating the plugin…' );
$this->strings['process_failed'] = __( 'Plugin update failed.' );
$this->strings['process_success'] = __( 'Plugin updated successfully.' );
if ( 'downgrade-plugin' === $this->skin->overwrite ) {
$this->strings['installing_package'] = __( 'Downgrading the plugin…' );
$this->strings['process_failed'] = __( 'Plugin downgrade failed.' );
$this->strings['process_success'] = __( 'Plugin downgraded successfully.' );
* Install a plugin package.
* @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
* @param string $package The full local path or URI of the package.
* Optional. Other arguments for installing a plugin package. Default empty array.
* @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
* @return bool|WP_Error True if the installation was successful, false or a WP_Error otherwise.
public function install( $package, $args = array() ) {
'clear_update_cache' => true,
'overwrite_package' => false, // Do not overwrite files.
$parsed_args = wp_parse_args( $args, $defaults );
$this->install_strings();
add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
if ( $parsed_args['clear_update_cache'] ) {
// Clear cache so wp_update_plugins() knows about the new plugin.
add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
'destination' => WP_PLUGIN_DIR,
'clear_destination' => $parsed_args['overwrite_package'],
remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
if ( ! $this->result || is_wp_error( $this->result ) ) {
// Force refresh of plugin update information.
wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
if ( $parsed_args['overwrite_package'] ) {
* Fires when the upgrader has successfully overwritten a currently installed
* plugin or theme with an uploaded zip package.
* @param string $package The package file.
* @param array $data The new plugin or theme data.
* @param string $package_type The package type ('plugin' or 'theme').
do_action( 'upgrader_overwrote_package', $package, $this->new_plugin_data, 'plugin' );
* @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
* @param string $plugin Path to the plugin file relative to the plugins directory.
* Optional. Other arguments for upgrading a plugin package. Default empty array.
* @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
* @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
public function upgrade( $plugin, $args = array() ) {
'clear_update_cache' => true,
$parsed_args = wp_parse_args( $args, $defaults );
$this->upgrade_strings();
$current = get_site_transient( 'update_plugins' );
if ( ! isset( $current->response[ $plugin ] ) ) {
$this->skin->set_result( false );
$this->skin->error( 'up_to_date' );
// Get the URL to the zip file.
$r = $current->response[ $plugin ];
add_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ), 10, 2 );
add_filter( 'upgrader_pre_install', array( $this, 'active_before' ), 10, 2 );
add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
add_filter( 'upgrader_post_install', array( $this, 'active_after' ), 10, 2 );
* There's a Trac ticket to move up the directory for zips which are made a bit differently, useful for non-.org plugins.
* 'source_selection' => array( $this, 'source_selection' ),
if ( $parsed_args['clear_update_cache'] ) {
// Clear cache so wp_update_plugins() knows about the new plugin.
add_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9, 0 );
'package' => $r->package,
'destination' => WP_PLUGIN_DIR,
'clear_destination' => true,
'slug' => dirname( $plugin ),
// Cleanup our hooks, in case something else does an upgrade on this connection.
remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
remove_filter( 'upgrader_pre_install', array( $this, 'deactivate_plugin_before_upgrade' ) );
remove_filter( 'upgrader_pre_install', array( $this, 'active_before' ) );
remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
remove_filter( 'upgrader_post_install', array( $this, 'active_after' ) );
if ( ! $this->result || is_wp_error( $this->result ) ) {
// Force refresh of plugin update information.
wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
* Ensure any future auto-update failures trigger a failure email by removing
* the last failure notification from the list when plugins update successfully.
$past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
if ( isset( $past_failure_emails[ $plugin ] ) ) {
unset( $past_failure_emails[ $plugin ] );
update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
* Upgrades several plugins at once.
* @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
* @global string $wp_version The WordPress version string.
* @param string[] $plugins Array of paths to plugin files relative to the plugins directory.
* Optional. Other arguments for upgrading several plugins at once.
* @type bool $clear_update_cache Whether to clear the plugin updates cache if successful. Default true.
* @return array|false An array of results indexed by plugin file, or false if unable to connect to the filesystem.
public function bulk_upgrade( $plugins, $args = array() ) {
'clear_update_cache' => true,
$parsed_args = wp_parse_args( $args, $defaults );
$this->upgrade_strings();
$current = get_site_transient( 'update_plugins' );
add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ), 10, 4 );
// Connect to the filesystem first.
$res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
$this->skin->bulk_header();
* Only start maintenance mode if:
* - running Multisite and there are one or more plugins specified, OR
* - a plugin with an update available is currently active.
* @todo For multisite, maintenance mode should only kick in for individual sites if at all possible.
$maintenance = ( is_multisite() && ! empty( $plugins ) );
foreach ( $plugins as $plugin ) {
$maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin ] ) );
$this->maintenance_mode( true );
$this->update_count = count( $plugins );
$this->update_current = 0;
foreach ( $plugins as $plugin ) {
$this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true );
if ( ! isset( $current->response[ $plugin ] ) ) {
$this->skin->set_result( 'up_to_date' );
$this->skin->feedback( 'up_to_date' );
$results[ $plugin ] = true;
// Get the URL to the zip file.
$r = $current->response[ $plugin ];
$this->skin->plugin_active = is_plugin_active( $plugin );
if ( isset( $r->requires ) && ! is_wp_version_compatible( $r->requires ) ) {
'incompatible_wp_required_version',
/* translators: 1: Current WordPress version, 2: WordPress version required by the new plugin version. */
__( 'Your WordPress version is %1$s, however the new plugin version requires %2$s.' ),
$this->skin->before( $result );
$this->skin->error( $result );
} elseif ( isset( $r->requires_php ) && ! is_php_version_compatible( $r->requires_php ) ) {
'incompatible_php_required_version',
/* translators: 1: Current PHP version, 2: PHP version required by the new plugin version. */
__( 'The PHP version on your server is %1$s, however the new plugin version requires %2$s.' ),
$this->skin->before( $result );
$this->skin->error( $result );
add_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
'package' => $r->package,
'destination' => WP_PLUGIN_DIR,
'clear_destination' => true,
'slug' => dirname( $plugin ),
remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
$results[ $plugin ] = $result;
// Prevent credentials auth screen from displaying multiple times.
if ( false === $result ) {
} // End foreach $plugins.
$this->maintenance_mode( false );
// Force refresh of plugin update information.
wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
/** This action is documented in wp-admin/includes/class-wp-upgrader.php */
'upgrader_process_complete',
$this->skin->bulk_footer();
// Cleanup our hooks, in case something else does an upgrade on this connection.
remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_plugin' ) );
* Ensure any future auto-update failures trigger a failure email by removing
* the last failure notification from the list when plugins update successfully.
$past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
foreach ( $results as $plugin => $result ) {
// Maintain last failure notification when plugins failed to update manually.
if ( ! $result || is_wp_error( $result ) || ! isset( $past_failure_emails[ $plugin ] ) ) {
unset( $past_failure_emails[ $plugin ] );
update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
* Checks that the source package contains a valid plugin.
* Hooked to the {@see 'upgrader_source_selection'} filter by Plugin_Upgrader::install().
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
* @global string $wp_version The WordPress version string.
* @param string $source The path to the downloaded package source.
* @return string|WP_Error The source as passed, or a WP_Error object on failure.
public function check_package( $source ) {
global $wp_filesystem, $wp_version;
$this->new_plugin_data = array();
if ( is_wp_error( $source ) ) {
$working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source );
if ( ! is_dir( $working_directory ) ) { // Confidence check, if the above fails, let's not prevent installation.
// Check that the folder contains at least 1 valid plugin.
$files = glob( $working_directory . '*.php' );
foreach ( $files as $file ) {
$info = get_plugin_data( $file, false, false );
if ( ! empty( $info['Name'] ) ) {
$this->new_plugin_data = $info;
if ( empty( $this->new_plugin_data ) ) {
return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
$requires_php = isset( $info['RequiresPHP'] ) ? $info['RequiresPHP'] : null;
$requires_wp = isset( $info['RequiresWP'] ) ? $info['RequiresWP'] : null;
if ( ! is_php_version_compatible( $requires_php ) ) {
/* translators: 1: Current PHP version, 2: Version required by the uploaded plugin. */