Plugin_Upgrader::upgrade() WordPress Method

The Plugin_Upgrader::upgrade() method is used to upgrade a plugin. It takes two arguments: the plugin to be upgraded (which can be a plugin slug, a plugin path, or a plugin object) and an array of options. The most important option is the 'version' option, which should be set to the version number of the plugin to be upgraded. Other options include 'force_update' (which will force the upgrade even if the plugin is already up-to-date), 'clear_update_cache' (which will clear the cached information about plugin updates), and 'source_type' (which can be used to specify the type of source for the upgrade, e.g. 'svn' or 'git'). Once the options are set, the upgrade() method will return a WP_Error object on failure or true on success.

Plugin_Upgrader::upgrade( string $plugin, array $args = array() ) #

Upgrade a plugin.


Parameters

$plugin

(string)(Required)Path to the plugin file relative to the plugins directory.

$args

(array)(Optional)Other arguments for upgrading a plugin package.

  • 'clear_update_cache'
    (bool) Whether to clear the plugin updates cache if successful. Default true.

Default value: array()


Top ↑

Return

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


Top ↑

Source

File: wp-admin/includes/class-plugin-upgrader.php

	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',
				),
			)
		);

		// Cleanup our hooks, in case something else does a 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;
	}


Top ↑

Changelog

Changelog
VersionDescription
3.7.0The $args parameter was added, making clearing the plugin update cache optional.
2.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.