wppaste
WordPress

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
Upgrades a plugin.

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: true

    Whether 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

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–143

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

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 Plugin_Upgrader::upgrade()
  • cacheobject cachewp_cache_get()one call below Plugin_Upgrader::upgrade()
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
!isset($plugin)34wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), ->before(), ->set_result(), ->error(), ->after()
isset($plugin) && !$parsed_args115wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), slug(), remove_action()
isset($plugin) && !$parsed_args && is_wp_error()121wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), slug(), remove_action(), is_wp_error()
isset($plugin) && $parsed_args121wp_parse_args(), ->init(), ->upgrade_strings(), get_site_transient(), add_action(), slug(), remove_action()
isset($plugin) && $parsed_args && is_wp_error()127wp_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])132wp_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])137wp_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])138wp_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])143wp_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

PHPCompiledExecutedBranchesNotes
8.6-dev16034–1435
8.516034–1435
8.416034–14352 fewer instructions than PHP 8.3
8.316234–1455
8.216234–1455
8.116234–1455
7.416234–1455

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

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.		$upgrade_data = $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'           => $upgrade_data->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.

  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 plugin update cache optional.from the docblock
2.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.