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
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: trueWhether 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
- Scaling
- Constant
- Instructions
- 34–130
- Plugin surface
- None
- Called by
- 0
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 147.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- transienttransient
get_site_transient()called directly - optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()one call below Theme_Upgrader::upgrade() - cacheobject cache
wp_cache_get()one call below Theme_Upgrader::upgrade() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($theme) | 34 | wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), ->before(), ->set_result(), ->error(), ->after() |
isset($theme) && !$parsed_args | 102 | wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), get_theme_root(), get_theme_root(), slug(), remove_action() |
isset($theme) && !$parsed_args && is_wp_error() | 108 | wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), get_theme_root(), get_theme_root(), slug(), remove_action(), is_wp_error() |
isset($theme) && $parsed_args | 108 | wp_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() | 114 | wp_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]) | 119 | wp_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]) | 124 | wp_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]) | 125 | wp_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]) | 130 | wp_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
- wp_parse_args()Merges user defined arguments into defaults array.
- get_site_transient()Retrieves the value of a site transient.
- add_filter()Adds a callback function to a filter hook.
- add_action()Adds a callback function to an action hook.
- get_theme_root()Retrieves path to themes directory.
- remove_action()Removes a callback function from an action hook.
- remove_filter()Removes a callback function from a filter hook.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_clean_themes_cache()Clears the cache held by get_theme_roots() and WP_Theme.
- get_option()Retrieves an option value based on an option name.
- update_option()Updates the value of an option that was already added.
- Theme_Upgrader::init()
Show all 14
- Theme_Upgrader::upgrade_strings()Initializes the upgrade strings.
- Theme_Upgrader::run()
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; } $upgrade_data = $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' => $upgrade_data['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.
Signature, return type and hooks compared across 5 parsed releases.
$args parameter was added, making clearing the update cache optional.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.