Update in-app marketplace to display localized strings (#34356)

* Update in-app marketplace to display localized strings

* Add changelog
This commit is contained in:
Chi-Hsuan Huang 2022-08-18 16:51:36 +08:00 committed by GitHub
parent 50d4d6774c
commit e9d27c8949
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 12 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
Update in-app marketplace to display localized strings

View File

@ -26,7 +26,8 @@ class WC_Admin_Addons {
* @return array of objects * @return array of objects
*/ */
public static function get_featured() { public static function get_featured() {
$featured = get_transient( 'wc_addons_featured_2' ); $locale = get_user_locale();
$featured = self::get_locale_data_from_transient( 'wc_addons_featured_2', $locale );
if ( false === $featured ) { if ( false === $featured ) {
$headers = array(); $headers = array();
$auth = WC_Helper_Options::get( 'auth' ); $auth = WC_Helper_Options::get( 'auth' );
@ -45,7 +46,7 @@ class WC_Admin_Addons {
if ( ! is_wp_error( $raw_featured ) ) { if ( ! is_wp_error( $raw_featured ) ) {
$featured = json_decode( wp_remote_retrieve_body( $raw_featured ) ); $featured = json_decode( wp_remote_retrieve_body( $raw_featured ) );
if ( $featured ) { if ( $featured ) {
set_transient( 'wc_addons_featured_2', $featured, DAY_IN_SECONDS ); self::set_locale_data_in_transient( 'wc_addons_featured_2', $featured, $locale, DAY_IN_SECONDS );
} }
} }
} }
@ -62,7 +63,8 @@ class WC_Admin_Addons {
* @return void * @return void
*/ */
public static function render_featured() { public static function render_featured() {
$featured = get_transient( 'wc_addons_featured' ); $locale = get_user_locale();
$featured = self::get_locale_data_from_transient( 'wc_addons_featured', $locale );
if ( false === $featured ) { if ( false === $featured ) {
$headers = array(); $headers = array();
$auth = WC_Helper_Options::get( 'auth' ); $auth = WC_Helper_Options::get( 'auth' );
@ -71,10 +73,10 @@ class WC_Admin_Addons {
$headers['Authorization'] = 'Bearer ' . $auth['access_token']; $headers['Authorization'] = 'Bearer ' . $auth['access_token'];
} }
$parameter_string = ''; $parameter_string = '?' . http_build_query( array( 'locale' => get_user_locale() ) );
$country = WC()->countries->get_base_country(); $country = WC()->countries->get_base_country();
if ( ! empty( $country ) ) { if ( ! empty( $country ) ) {
$parameter_string = '?' . http_build_query( array( 'country' => $country ) ); $parameter_string = $parameter_string . '&' . http_build_query( array( 'country' => $country ) );
} }
// Important: WCCOM Extensions API v2.0 is used. // Important: WCCOM Extensions API v2.0 is used.
@ -118,7 +120,7 @@ class WC_Admin_Addons {
return; return;
} }
$featured = json_decode( wp_remote_retrieve_body( $raw_featured ) ); $featured = json_decode( wp_remote_retrieve_body( $raw_featured ) );
if ( empty( $featured ) || ! is_array( $featured ) ) { if ( empty( $featured ) || ! is_array( $featured ) ) {
do_action( 'woocommerce_page_wc-addons_connection_error', 'Empty or malformed response' ); do_action( 'woocommerce_page_wc-addons_connection_error', 'Empty or malformed response' );
$message = __( 'Our request to the featured API got a malformed response.', 'woocommerce' ); $message = __( 'Our request to the featured API got a malformed response.', 'woocommerce' );
@ -128,7 +130,7 @@ class WC_Admin_Addons {
} }
if ( $featured ) { if ( $featured ) {
set_transient( 'wc_addons_featured', $featured, DAY_IN_SECONDS ); self::set_locale_data_in_transient( 'wc_addons_featured', $featured, $locale, DAY_IN_SECONDS );
} }
} }
@ -161,6 +163,7 @@ class WC_Admin_Addons {
'category' => $category, 'category' => $category,
'term' => $term, 'term' => $term,
'country' => $country, 'country' => $country,
'locale' => get_user_locale(),
); );
return '?' . http_build_query( $parameters ); return '?' . http_build_query( $parameters );
@ -216,7 +219,6 @@ class WC_Admin_Addons {
do_action( 'woocommerce_page_wc-addons_connection_error', 'Empty or malformed response' ); do_action( 'woocommerce_page_wc-addons_connection_error', 'Empty or malformed response' );
return new WP_Error( 'error', __( 'Our request to the search API got a malformed response.', 'woocommerce' ) ); return new WP_Error( 'error', __( 'Our request to the search API got a malformed response.', 'woocommerce' ) );
} }
return $addons; return $addons;
} }
@ -226,15 +228,17 @@ class WC_Admin_Addons {
* @return array of objects * @return array of objects
*/ */
public static function get_sections() { public static function get_sections() {
$addon_sections = get_transient( 'wc_addons_sections' ); $locale = get_user_locale();
$addon_sections = self::get_locale_data_from_transient( 'wc_addons_sections', $locale );
if ( false === ( $addon_sections ) ) { if ( false === ( $addon_sections ) ) {
$raw_sections = wp_safe_remote_get( $parameter_string = '?' . http_build_query( array( 'locale' => get_user_locale() ) );
'https://woocommerce.com/wp-json/wccom-extensions/1.0/categories' $raw_sections = wp_safe_remote_get(
'https://woocommerce.com/wp-json/wccom-extensions/1.0/categories' . $parameter_string
); );
if ( ! is_wp_error( $raw_sections ) ) { if ( ! is_wp_error( $raw_sections ) ) {
$addon_sections = json_decode( wp_remote_retrieve_body( $raw_sections ) ); $addon_sections = json_decode( wp_remote_retrieve_body( $raw_sections ) );
if ( $addon_sections ) { if ( $addon_sections ) {
set_transient( 'wc_addons_sections', $addon_sections, WEEK_IN_SECONDS ); self::set_locale_data_in_transient( 'wc_addons_sections', $addon_sections, $locale, WEEK_IN_SECONDS );
} }
} }
} }
@ -1482,4 +1486,50 @@ class WC_Admin_Addons {
<?php <?php
} }
} }
/**
* Retrieves the locale data from a transient.
*
* Transient value is an array of locale data in the following format:
* array(
* 'en_US' => ...,
* 'fr_FR' => ...,
* )
*
* If the transient does not exist, does not have a value, or has expired,
* then the return value will be false.
*
* @param string $transient Transient name. Expected to not be SQL-escaped.
* @param string $locale Locale to retrieve.
* @return mixed Value of transient.
*/
private static function get_locale_data_from_transient( $transient, $locale ) {
$transient_value = get_transient( $transient );
$transient_value = is_array( $transient_value ) ? $transient_value : array();
return $transient_value[ $locale ] ?? false;
}
/**
* Sets the locale data in a transient.
*
* Transient value is an array of locale data in the following format:
* array(
* 'en_US' => ...,
* 'fr_FR' => ...,
* )
*
* @param string $transient Transient name. Expected to not be SQL-escaped.
* Must be 172 characters or fewer in length.
* @param mixed $value Transient value. Must be serializable if non-scalar.
* Expected to not be SQL-escaped.
* @param string $locale Locale to set.
* @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
* @return bool True if the value was set, false otherwise.
*/
private static function set_locale_data_in_transient( $transient, $value, $locale, $expiration = 0 ) {
$transient_value = get_transient( $transient );
$transient_value = is_array( $transient_value ) ? $transient_value : array();
$transient_value[ $locale ] = $value;
return set_transient( $transient, $transient_value, $expiration );
}
} }