Plugin_Upgrader::upgrade( string $plugin, array $args = array() ): bool|WP_Error
- Since
- 2.8.0, 3.7.0
- Source
wp-admin/includes/class-plugin-upgrader.php:190
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
$pluginstring- Path to the plugin file relative to the plugins directory.
$argsarrayoptional- Other arguments for upgrading a plugin package. Default empty array.Default:
array()$clear_update_cachebooldefault: trueWhether to clear the plugin updates 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 Plugin_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–143
- 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 160.
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 Plugin_Upgrader::upgrade() - cacheobject cache
wp_cache_get()one call below Plugin_Upgrader::upgrade() - serializeserialisation
maybe_unserialize()one call below Plugin_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 Plugin_Upgrader::upgrade() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($plugin) | 34 | wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), ->before(), ->set_result(), ->error(), ->after() |
isset($plugin) && !$parsed_args | 115 | wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), slug(), remove_action() |
isset($plugin) && !$parsed_args && is_wp_error() | 121 | wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), slug(), remove_action(), is_wp_error() |
isset($plugin) && $parsed_args | 121 | wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), add_action(), slug(), remove_action() |
isset($plugin) && $parsed_args && is_wp_error() | 127 | wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), add_action(), slug(), remove_action(), is_wp_error() |
isset($plugin) && !$parsed_args && !is_wp_error() && !isset($past_failure_emails[$plugin]) | 132 | wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), slug(), remove_action(), is_wp_error(), wp_clean_plugins_cache(), get_option() |
isset($plugin) && !$parsed_args && !is_wp_error() && isset($past_failure_emails[$plugin]) | 137 | wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), slug(), remove_action(), is_wp_error(), wp_clean_plugins_cache(), get_option(), update_option() |
isset($plugin) && $parsed_args && !is_wp_error() && !isset($past_failure_emails[$plugin]) | 138 | wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), add_action(), slug(), remove_action(), is_wp_error(), wp_clean_plugins_cache(), get_option() |
isset($plugin) && $parsed_args && !is_wp_error() && isset($past_failure_emails[$plugin]) | 143 | wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), add_action(), slug(), remove_action(), is_wp_error(), wp_clean_plugins_cache(), get_option(), update_option() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 160 | 34–143 | 5 | |
| 8.5 | 160 | 34–143 | 5 | |
| 8.4 | 160 | 34–143 | 5 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 162 | 34–145 | 5 | |
| 8.2 | 162 | 34–145 | 5 | |
| 8.1 | 162 | 34–145 | 5 | |
| 7.4 | 162 | 34–145 | 5 |
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 · 13
- 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.
- 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_plugins_cache()Clears the plugins cache used by get_plugins() and by default, the plugin updates cache.
- get_option()Retrieves an option value based on an option name.
- update_option()Updates the value of an option that was already added.
- Plugin_Upgrader::init()
- Plugin_Upgrader::upgrade_strings()Initializes the upgrade strings.
Show all 13
- Plugin_Upgrader::run()
Source code
public function upgrade( $plugin, $args = array() ) { $defaults = array( 'clear_update_cache' => true, ); $parsed_args = wp_parse_args( $args, $defaults ); $this->init(); $this->upgrade_strings(); $current = get_site_transient( 'update_plugins' ); if ( ! isset( $current->response[ $plugin ] ) ) { $this->skin->before(); $this->skin->set_result( false ); $this->skin->error( 'up_to_date' ); $this->skin->after(); return false; } // 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 ); } $this->run( array( 'package' => $r->package, 'destination' => WP_PLUGIN_DIR, 'clear_destination' => true, 'clear_working' => true, 'hook_extra' => array( 'plugin' => $plugin, 'type' => 'plugin', 'action' => 'update', 'temp_backup' => array( 'slug' => dirname( $plugin ), 'src' => WP_PLUGIN_DIR, 'dir' => 'plugins', ), ), ) ); // 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 ) ) { return $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 ); } 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 plugin update cache optional.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-admin/includes/class-plugin-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.