JetpackConnection.php
130 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\Jetpack; |
| 5 | |
| 6 | use Automattic\Jetpack\Connection\Manager; |
| 7 | use Automattic\WooCommerce\Admin\Features\Features; |
| 8 | use WP_Error; |
| 9 | |
| 10 | /** |
| 11 | * Jetpack Connection wrapper class. |
| 12 | * |
| 13 | * @since 8.3.0 |
| 14 | */ |
| 15 | class JetpackConnection { |
| 16 | /** |
| 17 | * Jetpack connection manager. |
| 18 | * |
| 19 | * @var Manager |
| 20 | */ |
| 21 | private static $manager; |
| 22 | |
| 23 | /** |
| 24 | * Get the Jetpack connection manager. |
| 25 | * |
| 26 | * @return Manager |
| 27 | */ |
| 28 | public static function get_manager() { |
| 29 | if ( ! self::$manager instanceof Manager ) { |
| 30 | self::$manager = new Manager( 'woocommerce' ); |
| 31 | } |
| 32 | |
| 33 | return self::$manager; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Get the authorization URL for the Jetpack connection. |
| 38 | * |
| 39 | * @param mixed $redirect_url Redirect URL. |
| 40 | * @param string $from From parameter. |
| 41 | * |
| 42 | * @return array { |
| 43 | * Authorization data. |
| 44 | * |
| 45 | * @type bool $success Whether authorization URL generation succeeded. |
| 46 | * @type array $errors Array of error messages if any. |
| 47 | * @type string $color_scheme User's admin color scheme. |
| 48 | * @type string $url The authorization URL. |
| 49 | * } |
| 50 | */ |
| 51 | public static function get_authorization_url( $redirect_url, $from = '' ) { |
| 52 | $manager = self::get_manager(); |
| 53 | $errors = new WP_Error(); |
| 54 | |
| 55 | // Register the site to wp.com. |
| 56 | if ( ! $manager->is_connected() ) { |
| 57 | $result = $manager->try_registration(); |
| 58 | if ( is_wp_error( $result ) ) { |
| 59 | $errors->add( $result->get_error_code(), $result->get_error_message() ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | $calypso_env = defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, array( 'development', 'wpcalypso', 'horizon', 'stage' ), true ) ? WOOCOMMERCE_CALYPSO_ENVIRONMENT : 'production'; |
| 64 | |
| 65 | $authorization_url = $manager->get_authorization_url( null, $redirect_url ); |
| 66 | $authorization_url = add_query_arg( 'locale', self::get_wpcom_locale(), $authorization_url ); |
| 67 | |
| 68 | if ( Features::is_enabled( 'use-wp-horizon' ) ) { |
| 69 | $calypso_env = 'horizon'; |
| 70 | } |
| 71 | |
| 72 | $color_scheme = get_user_option( 'admin_color', get_current_user_id() ); |
| 73 | if ( ! $color_scheme ) { |
| 74 | // The default Core color schema is 'fresh'. |
| 75 | $color_scheme = 'fresh'; |
| 76 | } |
| 77 | |
| 78 | return array( |
| 79 | 'success' => ! $errors->has_errors(), |
| 80 | 'errors' => $errors->get_error_messages(), |
| 81 | 'color_scheme' => $color_scheme, |
| 82 | 'url' => add_query_arg( |
| 83 | array( |
| 84 | 'from' => $from, |
| 85 | 'calypso_env' => $calypso_env, |
| 86 | ), |
| 87 | $authorization_url, |
| 88 | ), |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Return a locale string for wpcom. |
| 94 | * |
| 95 | * @return string |
| 96 | */ |
| 97 | private static function get_wpcom_locale() { |
| 98 | // List of locales that should be used with region code. |
| 99 | $locale_to_lang = array( |
| 100 | 'bre' => 'br', |
| 101 | 'de_AT' => 'de-at', |
| 102 | 'de_CH' => 'de-ch', |
| 103 | 'de' => 'de_formal', |
| 104 | 'el' => 'el-po', |
| 105 | 'en_GB' => 'en-gb', |
| 106 | 'es_CL' => 'es-cl', |
| 107 | 'es_MX' => 'es-mx', |
| 108 | 'fr_BE' => 'fr-be', |
| 109 | 'fr_CA' => 'fr-ca', |
| 110 | 'nl_BE' => 'nl-be', |
| 111 | 'nl' => 'nl_formal', |
| 112 | 'pt_BR' => 'pt-br', |
| 113 | 'sr' => 'sr_latin', |
| 114 | 'zh_CN' => 'zh-cn', |
| 115 | 'zh_HK' => 'zh-hk', |
| 116 | 'zh_SG' => 'zh-sg', |
| 117 | 'zh_TW' => 'zh-tw', |
| 118 | ); |
| 119 | |
| 120 | $system_locale = get_locale(); |
| 121 | if ( isset( $locale_to_lang[ $system_locale ] ) ) { |
| 122 | // Return the locale with region code if it's in the list. |
| 123 | return $locale_to_lang[ $system_locale ]; |
| 124 | } |
| 125 | |
| 126 | // If the locale is not in the list, return the language code only. |
| 127 | return explode( '_', $system_locale )[0]; |
| 128 | } |
| 129 | } |
| 130 |