* Upgrade API: Theme_Upgrader class
* Core class used for upgrading/installing themes.
* It is designed to upgrade/install themes 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 Theme_Upgrader extends WP_Upgrader {
* Result of the theme upgrade offer.
* @var array|WP_Error $result
* @see WP_Upgrader::$result
* Whether multiple themes are being upgraded/installed in bulk.
* @var array $new_theme_data
public $new_theme_data = array();
* Initialize the upgrade strings.
public function upgrade_strings() {
$this->strings['up_to_date'] = __( 'The theme 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">%s</span>' );
$this->strings['unpack_package'] = __( 'Unpacking the update…' );
$this->strings['remove_old'] = __( 'Removing the old version of the theme…' );
$this->strings['remove_old_failed'] = __( 'Could not remove the old theme.' );
$this->strings['process_failed'] = __( 'Theme update failed.' );
$this->strings['process_success'] = __( 'Theme updated successfully.' );
* Initialize 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">%s</span>' );
$this->strings['unpack_package'] = __( 'Unpacking the package…' );
$this->strings['installing_package'] = __( 'Installing the theme…' );
$this->strings['remove_old'] = __( 'Removing the old version of the theme…' );
$this->strings['remove_old_failed'] = __( 'Could not remove the old theme.' );
$this->strings['no_files'] = __( 'The theme contains no files.' );
$this->strings['process_failed'] = __( 'Theme installation failed.' );
$this->strings['process_success'] = __( 'Theme installed successfully.' );
/* translators: 1: Theme name, 2: Theme version. */
$this->strings['process_success_specific'] = __( 'Successfully installed the theme <strong>%1$s %2$s</strong>.' );
$this->strings['parent_theme_search'] = __( 'This theme requires a parent theme. Checking if it is installed…' );
/* translators: 1: Theme name, 2: Theme version. */
$this->strings['parent_theme_prepare_install'] = __( 'Preparing to install <strong>%1$s %2$s</strong>…' );
/* translators: 1: Theme name, 2: Theme version. */
$this->strings['parent_theme_currently_installed'] = __( 'The parent theme, <strong>%1$s %2$s</strong>, is currently installed.' );
/* translators: 1: Theme name, 2: Theme version. */
$this->strings['parent_theme_install_success'] = __( 'Successfully installed the parent theme, <strong>%1$s %2$s</strong>.' );
/* translators: %s: Theme name. */
$this->strings['parent_theme_not_found'] = sprintf( __( '<strong>The parent theme could not be found.</strong> You will need to install the parent theme, %s, before you can use this child theme.' ), '<strong>%s</strong>' );
/* translators: %s: Theme error. */
$this->strings['current_theme_has_errors'] = __( 'The current theme has the following error: "%s".' );
if ( ! empty( $this->skin->overwrite ) ) {
if ( 'update-theme' === $this->skin->overwrite ) {
$this->strings['installing_package'] = __( 'Updating the theme…' );
$this->strings['process_failed'] = __( 'Theme update failed.' );
$this->strings['process_success'] = __( 'Theme updated successfully.' );
if ( 'downgrade-theme' === $this->skin->overwrite ) {
$this->strings['installing_package'] = __( 'Downgrading the theme…' );
$this->strings['process_failed'] = __( 'Theme downgrade failed.' );
$this->strings['process_success'] = __( 'Theme downgraded successfully.' );
* Check if a child theme is being installed and we need to install its parent.
* Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::install().
* @param bool $install_result
* @param array $hook_extra
* @param array $child_result
public function check_parent_theme_filter( $install_result, $hook_extra, $child_result ) {
// Check to see if we need to install a parent theme.
$theme_info = $this->theme_info();
if ( ! $theme_info->parent() ) {
$this->skin->feedback( 'parent_theme_search' );
if ( ! $theme_info->parent()->errors() ) {
$this->skin->feedback( 'parent_theme_currently_installed', $theme_info->parent()->display( 'Name' ), $theme_info->parent()->display( 'Version' ) );
// We already have the theme, fall through.
// We don't have the parent theme, let's install it.
'slug' => $theme_info->get( 'Template' ),
); // Save on a bit of bandwidth.
if ( ! $api || is_wp_error( $api ) ) {
$this->skin->feedback( 'parent_theme_not_found', $theme_info->get( 'Template' ) );
// Don't show activate or preview actions after installation.
add_filter( 'install_theme_complete_actions', array( $this, 'hide_activate_preview_actions' ) );
// Backup required data we're going to override:
$child_api = $this->skin->api;
$child_success_message = $this->strings['process_success'];
$this->strings['process_success_specific'] = $this->strings['parent_theme_install_success'];
$this->skin->feedback( 'parent_theme_prepare_install', $api->name, $api->version );
add_filter( 'install_theme_complete_actions', '__return_false', 999 ); // Don't show any actions after installing the theme.
// Install the parent theme.
$parent_result = $this->run(
'package' => $api->download_link,
'destination' => get_theme_root(),
'clear_destination' => false, // Do not overwrite files.
if ( is_wp_error( $parent_result ) ) {
add_filter( 'install_theme_complete_actions', array( $this, 'hide_activate_preview_actions' ) );
// Start cleaning up after the parent's installation.
remove_filter( 'install_theme_complete_actions', '__return_false', 999 );
// Reset child's result and data.
$this->result = $child_result;
$this->skin->api = $child_api;
$this->strings['process_success'] = $child_success_message;
* Don't display the activate and preview actions to the user.
* Hooked to the {@see 'install_theme_complete_actions'} filter by
* Theme_Upgrader::check_parent_theme_filter() when installing
* a child theme and installing the parent theme fails.
* @param array $actions Preview actions.
public function hide_activate_preview_actions( $actions ) {
unset( $actions['activate'], $actions['preview'] );
* Install a theme package.
* @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
* @param string $package The full local path or URI of the package.
* Optional. Other arguments for installing a theme package. Default empty array.
* @type bool $clear_update_cache Whether to clear the updates cache if successful.
* @return bool|WP_Error True if the installation was successful, false or a WP_Error object 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' ) );
add_filter( 'upgrader_post_install', array( $this, 'check_parent_theme_filter' ), 10, 3 );
if ( $parsed_args['clear_update_cache'] ) {
// Clear cache so wp_update_themes() knows about the new theme.
add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
'destination' => get_theme_root(),
'clear_destination' => $parsed_args['overwrite_package'],
remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
remove_filter( 'upgrader_post_install', array( $this, 'check_parent_theme_filter' ) );
if ( ! $this->result || is_wp_error( $this->result ) ) {
// Refresh the Theme Update information.
wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
if ( $parsed_args['overwrite_package'] ) {
/** This action is documented in wp-admin/includes/class-plugin-upgrader.php */
do_action( 'upgrader_overwrote_package', $package, $this->new_theme_data, 'theme' );
* @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
* @param string $theme The theme slug.
* Optional. Other arguments for upgrading a theme. Default empty array.
* @type bool $clear_update_cache Whether to clear the update cache if successful.
* @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
public function upgrade( $theme, $args = array() ) {
'clear_update_cache' => true,
$parsed_args = wp_parse_args( $args, $defaults );
$this->upgrade_strings();
// Is an update available?
$current = get_site_transient( 'update_themes' );
if ( ! isset( $current->response[ $theme ] ) ) {
$this->skin->set_result( false );
$this->skin->error( 'up_to_date' );
$r = $current->response[ $theme ];
add_filter( 'upgrader_pre_install', array( $this, 'current_before' ), 10, 2 );
add_filter( 'upgrader_post_install', array( $this, 'current_after' ), 10, 2 );
add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ), 10, 4 );
if ( $parsed_args['clear_update_cache'] ) {
// Clear cache so wp_update_themes() knows about the new theme.
add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
'package' => $r['package'],
'destination' => get_theme_root( $theme ),
'clear_destination' => true,
remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
remove_filter( 'upgrader_pre_install', array( $this, 'current_before' ) );
remove_filter( 'upgrader_post_install', array( $this, 'current_after' ) );
remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ) );
if ( ! $this->result || is_wp_error( $this->result ) ) {
wp_clean_themes_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 themes update successfully.
$past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
if ( isset( $past_failure_emails[ $theme ] ) ) {
unset( $past_failure_emails[ $theme ] );
update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
* Upgrade several themes at once.
* @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
* @param string[] $themes Array of the theme slugs.
* Optional. Other arguments for upgrading several themes at once. Default empty array.
* @type bool $clear_update_cache Whether to clear the update cache if successful.
* @return array[]|false An array of results, or false if unable to connect to the filesystem.
public function bulk_upgrade( $themes, $args = array() ) {
'clear_update_cache' => true,
$parsed_args = wp_parse_args( $args, $defaults );
$this->upgrade_strings();
$current = get_site_transient( 'update_themes' );
add_filter( 'upgrader_pre_install', array( $this, 'current_before' ), 10, 2 );
add_filter( 'upgrader_post_install', array( $this, 'current_after' ), 10, 2 );
add_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ), 10, 4 );
// Connect to the filesystem first.
$res = $this->fs_connect( array( WP_CONTENT_DIR ) );
$this->skin->bulk_header();
* Only start maintenance mode if:
* - running Multisite and there are one or more themes specified, OR
* - a theme with an update available is currently in use.
* @todo For multisite, maintenance mode should only kick in for individual sites if at all possible.
$maintenance = ( is_multisite() && ! empty( $themes ) );
foreach ( $themes as $theme ) {
$maintenance = $maintenance || get_stylesheet() === $theme || get_template() === $theme;
$this->maintenance_mode( true );
$this->update_count = count( $themes );
$this->update_current = 0;
foreach ( $themes as $theme ) {
$this->skin->theme_info = $this->theme_info( $theme );
if ( ! isset( $current->response[ $theme ] ) ) {
$this->skin->set_result( true );
$this->skin->feedback( 'up_to_date' );
$results[ $theme ] = true;
// Get the URL to the zip file.
$r = $current->response[ $theme ];
'package' => $r['package'],
'destination' => get_theme_root( $theme ),
'clear_destination' => true,
$results[ $theme ] = $this->result;
// Prevent credentials auth screen from displaying multiple times.
if ( false === $result ) {
} // End foreach $themes.
$this->maintenance_mode( false );
// Refresh the Theme Update information.
wp_clean_themes_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 a upgrade on this connection.
remove_filter( 'upgrader_pre_install', array( $this, 'current_before' ) );
remove_filter( 'upgrader_post_install', array( $this, 'current_after' ) );
remove_filter( 'upgrader_clear_destination', array( $this, 'delete_old_theme' ) );
// Ensure any future auto-update failures trigger a failure email by removing
// the last failure notification from the list when themes update successfully.
$past_failure_emails = get_option( 'auto_plugin_theme_update_emails', array() );
foreach ( $results as $theme => $result ) {
// Maintain last failure notification when themes failed to update manually.
if ( ! $result || is_wp_error( $result ) || ! isset( $past_failure_emails[ $theme ] ) ) {
unset( $past_failure_emails[ $theme ] );
update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );