Automattic\WooCommerce\Admin

PluginsHelper::activate_plugins()public staticWC 1.0

Activate the requested plugins.

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

Return

WP_Error|Array. Plugin Status

Usage

$result = PluginsHelper::activate_plugins( $plugins );
$plugins(array) (required)
Plugins.

Code of PluginsHelper::activate_plugins() WC 7.1.0

public static function activate_plugins( $plugins ) {
	if ( empty( $plugins ) || ! is_array( $plugins ) ) {
		return new \WP_Error( 'woocommerce_plugins_invalid_plugins', __( 'Plugins must be a non-empty array.', 'woocommerce' ), 404 );
	}

	require_once ABSPATH . 'wp-admin/includes/plugin.php';

	// the mollie-payments-for-woocommerce plugin calls `WP_Filesystem()` during it's activation hook, which crashes without this include.
	require_once ABSPATH . 'wp-admin/includes/file.php';

	/**
	 * Filter the list of plugins to activate.
	 *
	 * @param array $plugins A list of the plugins to activate.
	 */
	$plugins = apply_filters( 'woocommerce_admin_plugins_pre_activate', $plugins );

	$plugin_paths      = self::get_installed_plugins_paths();
	$errors            = new \WP_Error();
	$activated_plugins = array();

	foreach ( $plugins as $plugin ) {
		$slug = $plugin;
		$path = isset( $plugin_paths[ $slug ] ) ? $plugin_paths[ $slug ] : false;

		if ( ! $path ) {
			$errors->add(
				$plugin,
				/* translators: %s: plugin slug (example: woocommerce-services) */
				sprintf( __( 'The requested plugin `%s`. is not yet installed.', 'woocommerce' ), $slug )
			);
			continue;
		}

		$result = activate_plugin( $path );
		if ( ! is_null( $result ) ) {
			do_action( 'woocommerce_plugins_activate_error', $slug, $result );

			$errors->add(
				$plugin,
				/* translators: %s: plugin slug (example: woocommerce-services) */
				sprintf( __( 'The requested plugin `%s` could not be activated.', 'woocommerce' ), $slug )
			);
			continue;
		}

		$activated_plugins[] = $plugin;
	}

	$data = array(
		'activated' => $activated_plugins,
		'active'    => self::get_active_plugin_slugs(),
		'errors'    => $errors,
	);

	return $data;
}