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 <github-actions@github.com>
This commit is contained in:
Moon 2024-09-17 13:46:26 -07:00 committed by GitHub
parent e03c51c5ed
commit 5fe53a2e3f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 6 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Add `locale` param when redirecting to the Jetpack auth page.

View File

@ -193,7 +193,7 @@ class OnboardingPlugins extends WC_REST_Data_Controller {
$actions = array_filter( $actions = array_filter(
PluginsHelper::get_action_data( $actions ), PluginsHelper::get_action_data( $actions ),
function( $action ) use ( $job_id ) { function ( $action ) use ( $job_id ) {
return $action['job_id'] === $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' ); $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' ) ) { if ( Features::is_enabled( 'use-wp-horizon' ) ) {
$calypso_env = 'horizon'; $calypso_env = 'horizon';
@ -247,15 +250,53 @@ class OnboardingPlugins extends WC_REST_Data_Controller {
'success' => ! $errors->has_errors(), 'success' => ! $errors->has_errors(),
'errors' => $errors->get_error_messages(), 'errors' => $errors->get_error_messages(),
'url' => add_query_arg( 'url' => add_query_arg(
[ array(
'from' => $request->get_param( 'from' ), 'from' => $request->get_param( 'from' ),
'calypso_env' => $calypso_env, '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 * Check whether the current user has permission to install plugins
* *
@ -405,7 +446,7 @@ class OnboardingPlugins extends WC_REST_Data_Controller {
), ),
$slug $slug
), ),
'type' => 'plugin_info_api_error', 'type' => 'plugin_info_api_error',
'slug' => $slug, 'slug' => $slug,
'api_version' => $api->version, 'api_version' => $api->version,
'api_download_link' => $api->download_link, 'api_download_link' => $api->download_link,