Automattic\WooCommerce\Internal\Utilities

PluginInstaller::install_plugin()publicWC 1.0

Programmatically installs a plugin. Upgrade/reinstall of already existing plugins is not supported. The plugin source must be the WordPress.org plugins directory.

$metadata can contain anything, but the following keys are recognized by the code that renders the notice in the plugins list:

  • 'installed_by': defaults to 'WooCommerce' if not present.
  • 'info_link': if present, a "More information" link will be included in the notice.

If 'installed_by' is supplied and it's not 'WooCommerce' (case-insensitive), an exception will be thrown if the code calling this method is not in a WooCommerce core file (in 'includes' or in 'src').

Information about plugins successfully installed with this method will be kept in an option named 'woocommerce_autoinstalled_plugins'. Keys will be the plugin name and values will be associative arrays with these keys: 'plugin_name', 'version', 'date' and 'metadata' (same meaning as in the returned array).

A log entry will be created with the result of the process and all the installer messages (source: 'plugin_auto_installs'). In multisite this log entry will be created on each site.

The returned array will contain the following (only 'install_ok' and 'messages' if the installation fails):

  • 'install_ok', a boolean.
  • 'messages', all the messages generated by the installer.
  • 'plugin_name', in the form of 'directory/file.php' (taken from the instance of PluginInstaller used).
  • 'version', of the plugin that has been installed.
  • 'date', ISO-formatted installation date.
  • 'metadata', as supplied (except the 'plugin_name' key) and only if not empty.

If the plugin is already in the process of being installed (can happen in multisite), the returned array will contain only one key: 'already_installing', with a value of true.

Метод класса: PluginInstaller{}

Хуков нет.

Возвращает

Массив. Information about the installation result.

Использование

$PluginInstaller = new PluginInstaller();
$PluginInstaller->install_plugin( $plugin_url, $metadata ): array;
$plugin_url(строка) (обязательный)
URL or file path of the plugin to install.
$metadata(массив)
Metadata to store if the installation succeeds.
По умолчанию: array()

Код PluginInstaller::install_plugin() WC 9.7.1

public function install_plugin( string $plugin_url, array $metadata = array() ): array {
	$this->installing_plugin = true;

	$plugins_being_installed = get_site_option( 'woocommerce_autoinstalling_plugins', array() );
	if ( in_array( $plugin_url, $plugins_being_installed, true ) ) {
		return array( 'already_installing' => true );
	}
	$plugins_being_installed[] = $plugin_url;
	update_site_option( 'woocommerce_autoinstalling_plugins', $plugins_being_installed );

	try {
		return $this->install_plugin_core( $plugin_url, $metadata );
	} finally {
		$plugins_being_installed = array_diff( $plugins_being_installed, array( $plugin_url ) );
		if ( empty( $plugins_being_installed ) ) {
			delete_site_option( 'woocommerce_autoinstalling_plugins' );
		} else {
			update_site_option( 'woocommerce_autoinstalling_plugins', $plugins_being_installed );
		}

		$this->installing_plugin = false;
	}
}