WC_Helper::activated_plugin()public staticWC 1.0

Runs when any plugin is activated.

Depending on the activated plugin attempts to look through available subscriptions and auto-activate one if possible, so the user does not need to visit the Helper UI at all after installing a new extension.

Method of the class: WC_Helper{}

Return

null. Nothing (null).

Usage

$result = WC_Helper::activated_plugin( $filename );
$filename(string) (required)
The filename of the activated plugin.

WC_Helper::activated_plugin() code WC 8.6.1

public static function activated_plugin( $filename ) {
	$plugins = self::get_local_woo_plugins();

	// Not a local woo plugin.
	if ( empty( $plugins[ $filename ] ) ) {
		return;
	}

	// Make sure we have a connection.
	$auth = WC_Helper_Options::get( 'auth' );
	if ( empty( $auth ) ) {
		return;
	}

	$plugin        = $plugins[ $filename ];
	$product_id    = $plugin['_product_id'];
	$subscriptions = self::_get_subscriptions_from_product_id( $product_id, false );

	// No valid subscriptions for this product.
	if ( empty( $subscriptions ) ) {
		return;
	}

	$subscription = null;
	foreach ( $subscriptions as $_sub ) {

		// Don't attempt to activate expired subscriptions.
		if ( $_sub['expired'] ) {
			continue;
		}

		// No more sites available in this subscription.
		if ( $_sub['sites_max'] && $_sub['sites_active'] >= $_sub['sites_max'] ) {
			continue;
		}

		// Looks good.
		$subscription = $_sub;
		break;
	}

	// No valid subscription found.
	if ( ! $subscription ) {
		return;
	}

	$product_key         = $subscription['product_key'];
	$activation_response = WC_Helper_API::post(
		'activate',
		array(
			'authenticated' => true,
			'body'          => wp_json_encode(
				array(
					'product_key' => $product_key,
				)
			),
		)
	);

	$activated = wp_remote_retrieve_response_code( $activation_response ) === 200;
	$body      = json_decode( wp_remote_retrieve_body( $activation_response ), true );

	if ( ! $activated && ! empty( $body['code'] ) && 'already_connected' === $body['code'] ) {
		$activated = true;
	}

	if ( $activated ) {
		self::log( 'Auto-activated a subscription for ' . $filename );
		/**
		 * Fires when the Helper activates a product successfully.
		 *
		 * @param int    $product_id Product ID being activated.
		 * @param string $product_key Subscription product key.
		 * @param array  $activation_response The response object from wp_safe_remote_request().
		 */
		do_action( 'woocommerce_helper_subscription_activate_success', $product_id, $product_key, $activation_response );
	} else {
		self::log( 'Could not activate a subscription upon plugin activation: ' . $filename );

		/**
		 * Fires when the Helper fails to activate a product.
		 *
		 * @param int    $product_id Product ID being activated.
		 * @param string $product_key Subscription product key.
		 * @param array  $activation_response The response object from wp_safe_remote_request().
		 */
		do_action( 'woocommerce_helper_subscription_activate_error', $product_id, $product_key, $activation_response );
	}

	self::_flush_subscriptions_cache();
	self::_flush_updates_cache();
}