Automattic\WooCommerce\Admin\API

Plugins::finish_wccom_connect()publicWC 1.0

Finishes connecting to Woo.com.

Method of the class: Plugins{}

Hooks from the method

Return

WP_Error|Array. Contains success status.

Usage

$Plugins = new Plugins();
$Plugins->finish_wccom_connect( $rest_request );
$rest_request(object) (required)
Request details.

Plugins::finish_wccom_connect() code WC 8.6.1

public function finish_wccom_connect( $rest_request ) {
	include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper.php';
	include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-api.php';
	include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-updater.php';
	include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-options.php';
	if ( ! class_exists( 'WC_Helper_API' ) ) {
		return new \WP_Error( 'woocommerce_rest_helper_not_active', __( 'There was an error loading the Woo.com Helper API.', 'woocommerce' ), 404 );
	}

	// Obtain an access token.
	$request = \WC_Helper_API::post(
		'oauth/access_token',
		array(
			'body' => array(
				'request_token' => wp_unslash( $rest_request['request_token'] ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
				'home_url'      => home_url(),
			),
		)
	);

	$code = wp_remote_retrieve_response_code( $request );
	if ( 200 !== $code ) {
		return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to Woo.com. Please try again.', 'woocommerce' ), 500 );
	}

	$access_token = json_decode( wp_remote_retrieve_body( $request ), true );
	if ( ! $access_token ) {
		return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to Woo.com. Please try again.', 'woocommerce' ), 500 );
	}

	\WC_Helper_Options::update(
		'auth',
		array(
			'access_token'        => $access_token['access_token'],
			'access_token_secret' => $access_token['access_token_secret'],
			'site_id'             => $access_token['site_id'],
			'user_id'             => get_current_user_id(),
			'updated'             => time(),
		)
	);

	if ( ! \WC_Helper::_flush_authentication_cache() ) {
		\WC_Helper_Options::update( 'auth', array() );
		return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to Woo.com. Please try again.', 'woocommerce' ), 500 );
	}

	delete_transient( '_woocommerce_helper_subscriptions' );
	\WC_Helper_Updater::flush_updates_cache();

	do_action( 'woocommerce_helper_connected' );

	return array(
		'success' => true,
	);
}