declare( strict_types=1 );
namespace Automattic\WooCommerce\Internal\Jetpack;
use Automattic\Jetpack\Connection\Manager;
use Automattic\WooCommerce\Admin\Features\Features;
* Jetpack Connection wrapper class.
class JetpackConnection {
* Jetpack connection manager.
* Get the Jetpack connection manager.
public static function get_manager() {
if ( ! self::$manager instanceof Manager ) {
self::$manager = new Manager( 'woocommerce' );
* Get the authorization URL for the Jetpack connection.
* @param mixed $redirect_url Redirect URL.
* @param string $from From parameter.
* @type bool $success Whether authorization URL generation succeeded.
* @type array $errors Array of error messages if any.
* @type string $color_scheme User's admin color scheme.
* @type string $url The authorization URL.
public static function get_authorization_url( $redirect_url, $from = '' ) {
$manager = self::get_manager();
$errors = new WP_Error();
// Register the site to wp.com.
if ( ! $manager->is_connected() ) {
$result = $manager->try_registration();
if ( is_wp_error( $result ) ) {
$errors->add( $result->get_error_code(), $result->get_error_message() );
$calypso_env = defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, array( 'development', 'wpcalypso', 'horizon', 'stage' ), true ) ? WOOCOMMERCE_CALYPSO_ENVIRONMENT : 'production';
$authorization_url = $manager->get_authorization_url( null, $redirect_url );
$authorization_url = add_query_arg( 'locale', self::get_wpcom_locale(), $authorization_url );
if ( Features::is_enabled( 'use-wp-horizon' ) ) {
$calypso_env = 'horizon';
$color_scheme = get_user_option( 'admin_color', get_current_user_id() );
// The default Core color schema is 'fresh'.
'success' => ! $errors->has_errors(),
'errors' => $errors->get_error_messages(),
'color_scheme' => $color_scheme,
'calypso_env' => $calypso_env,
* Return a locale string for wpcom.
private static function get_wpcom_locale() {
// List of locales that should be used with region code.
$system_locale = get_locale();
if ( isset( $locale_to_lang[ $system_locale ] ) ) {
// Return the locale with region code if it's in the list.
return $locale_to_lang[ $system_locale ];
// If the locale is not in the list, return the language code only.
return explode( '_', $system_locale )[0];