Plugin_Upgrader::upgrade

Advertisement

Summery Summery

Upgrade a plugin.

Syntax Syntax

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

Parameters Parameters

$plugin

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

$args

(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()

Return Return

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

Source Source

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

		remove_action( 'upgrader_process_complete', 'wp_clean_plugins_cache', 9 );
		remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );

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

		if ( $parsed_args['overwrite_package'] ) {
			/**
			 * Fires when the upgrader has successfully overwritten a currently installed
			 * plugin or theme with an uploaded zip package.
			 *
			 * @since 5.5.0
			 *
			 * @param string  $package          The package file.
			 * @param array   $new_plugin_data  The new plugin data.
			 * @param string  $package_type     The package type (plugin or theme).
			 */
			do_action( 'upgrader_overwrote_package', $package, $this->new_plugin_data, 'plugin' );
		}

		return true;
	}

	/**
	 * Upgrade a plugin.
	 *
	 * @since 2.8.0
	 * @since 3.7.0 The `$args` parameter was added, making clearing the plugin update cache optional.
	 *
	 * @param string $plugin Path to the plugin file relative to the plugins directory.
	 * @param array  $args {
	 *     Optional. Other arguments for upgrading a plugin package. Default empty array.
	 *
	 *     @type bool $clear_update_cache Whether to clear the plugin updates cache if successful.
	 *                                    Default true.
	 * }
	 * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
	 */
	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 ];

Advertisement

Changelog Changelog

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

Advertisement

Leave a Reply