wppaste
WordPress

Theme_Upgrader::upgrade( string $theme, array $args = array() ): bool|WP_Error

Since
2.8.0, 3.7.0
Source
wp-admin/includes/class-theme-upgrader.php:295
Upgrades a theme.

Compatibility

WordPress
since 3.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$themestring
The theme slug.
$argsarrayoptional
Other arguments for upgrading a theme. Default empty array.Default: array()
  • $clear_update_cachebooldefault: true

    Whether to clear the update cache if successful.

Return value

bool|WP_Error
True if the upgrade was successful, false or a WP_Error object otherwise.

Performance profile

How much work a call to Theme_Upgrader::upgrade() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
34–130

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 147.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • transienttransientget_site_transient()called directly
  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()one call below Theme_Upgrader::upgrade()
  • cacheobject cachewp_cache_get()one call below Theme_Upgrader::upgrade()
  • serializeserialisationmaybe_unserialize()one call below Theme_Upgrader::upgrade()

Further down the call graph this can also reach query. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 9 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost Theme_Upgrader::upgrade() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!isset($theme)34wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), ->before(), ->set_result(), ->error(), ->after()
isset($theme) && !$parsed_args102wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), get_theme_root(), get_theme_root(), slug(), remove_action()
isset($theme) && !$parsed_args && is_wp_error()108wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), get_theme_root(), get_theme_root(), slug(), remove_action(), is_wp_error()
isset($theme) && $parsed_args108wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), add_action(), get_theme_root(), get_theme_root(), slug(), remove_action()
isset($theme) && $parsed_args && is_wp_error()114wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), add_action(), get_theme_root(), get_theme_root(), slug(), remove_action(), is_wp_error()
isset($theme) && !$parsed_args && !is_wp_error() && !isset($past_failure_emails[$theme])119wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), get_theme_root(), get_theme_root(), slug(), remove_action(), is_wp_error(), wp_clean_themes_cache(), get_option()
isset($theme) && !$parsed_args && !is_wp_error() && isset($past_failure_emails[$theme])124wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), get_theme_root(), get_theme_root(), slug(), remove_action(), is_wp_error(), wp_clean_themes_cache(), get_option(), update_option()
isset($theme) && $parsed_args && !is_wp_error() && !isset($past_failure_emails[$theme])125wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), add_action(), get_theme_root(), get_theme_root(), slug(), remove_action(), is_wp_error(), wp_clean_themes_cache(), get_option()
isset($theme) && $parsed_args && !is_wp_error() && isset($past_failure_emails[$theme])130wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), add_action(), get_theme_root(), get_theme_root(), slug(), remove_action(), is_wp_error(), wp_clean_themes_cache(), get_option(), update_option()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 147 instructions, 34–130 executed per call, 5 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 14

Show all 14

Source code

	public function upgrade( $theme, $args = array() ) {		$defaults    = array(			'clear_update_cache' => true,		);		$parsed_args = wp_parse_args( $args, $defaults ); 		$this->init();		$this->upgrade_strings(); 		// Is an update available?		$current = get_site_transient( 'update_themes' );		if ( ! isset( $current->response[ $theme ] ) ) {			$this->skin->before();			$this->skin->set_result( false );			$this->skin->error( 'up_to_date' );			$this->skin->after();			return false;		} 		$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 );		} 		$this->run(			array(				'package'           => $r['package'],				'destination'       => get_theme_root( $theme ),				'clear_destination' => true,				'clear_working'     => true,				'hook_extra'        => array(					'theme'       => $theme,					'type'        => 'theme',					'action'      => 'update',					'temp_backup' => array(						'slug' => $theme,						'src'  => get_theme_root( $theme ),						'dir'  => 'themes',					),				),			)		); 		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 ) ) {			return $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 );		} 		return true;	}

Changelog

Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

3.7.0
The $args parameter was added, making clearing the update cache optional.from the docblock
2.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-admin/includes/class-theme-upgrader.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.