From b1fa7c2f85494c5dec2a63bd4cac420f3995fcee Mon Sep 17 00:00:00 2001 From: Moon Date: Tue, 17 Sep 2024 13:46:26 -0700 Subject: [PATCH] Add locale param to jetpack redirect url (#51392) * Add locale from php * Add changefile(s) from automation for the following project(s): woocommerce * Return a full locale with language and region code * Fix style * Fix error * Lint fixes * Lint fixes --------- Co-authored-by: github-actions --- ...92-update-add-locale-param-to-jetpack-auth | 4 ++ .../src/Admin/API/OnboardingPlugins.php | 53 ++++++++++++++++--- 2 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 plugins/woocommerce/changelog/51392-update-add-locale-param-to-jetpack-auth diff --git a/plugins/woocommerce/changelog/51392-update-add-locale-param-to-jetpack-auth b/plugins/woocommerce/changelog/51392-update-add-locale-param-to-jetpack-auth new file mode 100644 index 00000000000..8e1be059b7d --- /dev/null +++ b/plugins/woocommerce/changelog/51392-update-add-locale-param-to-jetpack-auth @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add `locale` param when redirecting to the Jetpack auth page. \ No newline at end of file diff --git a/plugins/woocommerce/src/Admin/API/OnboardingPlugins.php b/plugins/woocommerce/src/Admin/API/OnboardingPlugins.php index 55f55edfe85..e15c616ae9b 100644 --- a/plugins/woocommerce/src/Admin/API/OnboardingPlugins.php +++ b/plugins/woocommerce/src/Admin/API/OnboardingPlugins.php @@ -193,7 +193,7 @@ class OnboardingPlugins extends WC_REST_Data_Controller { $actions = array_filter( PluginsHelper::get_action_data( $actions ), - function( $action ) use ( $job_id ) { + function ( $action ) use ( $job_id ) { return $action['job_id'] === $job_id; } ); @@ -237,7 +237,10 @@ class OnboardingPlugins extends WC_REST_Data_Controller { } $redirect_url = $request->get_param( 'redirect_url' ); - $calypso_env = defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, [ 'development', 'wpcalypso', 'horizon', 'stage' ], true ) ? WOOCOMMERCE_CALYPSO_ENVIRONMENT : 'production'; + $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', $this->get_wpcom_locale(), $authorization_url ); if ( Features::is_enabled( 'use-wp-horizon' ) ) { $calypso_env = 'horizon'; @@ -247,15 +250,53 @@ class OnboardingPlugins extends WC_REST_Data_Controller { 'success' => ! $errors->has_errors(), 'errors' => $errors->get_error_messages(), 'url' => add_query_arg( - [ + array( 'from' => $request->get_param( 'from' ), 'calypso_env' => $calypso_env, - ], - $manager->get_authorization_url( null, $redirect_url ) + ), + $authorization_url, ), ]; } + /** + * Return a locale string for wpcom. + * + * @return string + */ + private function get_wpcom_locale() { + // List of locales that should be used with region code. + $locale_to_lang = array( + 'bre' => 'br', + 'de_AT' => 'de-at', + 'de_CH' => 'de-ch', + 'de' => 'de_formal', + 'el' => 'el-po', + 'en_GB' => 'en-gb', + 'es_CL' => 'es-cl', + 'es_MX' => 'es-mx', + 'fr_BE' => 'fr-be', + 'fr_CA' => 'fr-ca', + 'nl_BE' => 'nl-be', + 'nl' => 'nl_formal', + 'pt_BR' => 'pt-br', + 'sr' => 'sr_latin', + 'zh_CN' => 'zh-cn', + 'zh_HK' => 'zh-hk', + 'zh_SG' => 'zh-sg', + 'zh_TW' => 'zh-tw', + ); + + $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]; + } + /** * Check whether the current user has permission to install plugins * @@ -405,7 +446,7 @@ class OnboardingPlugins extends WC_REST_Data_Controller { ), $slug ), - 'type' => 'plugin_info_api_error', + 'type' => 'plugin_info_api_error', 'slug' => $slug, 'api_version' => $api->version, 'api_download_link' => $api->download_link,