From e9d27c894958bcd8880d0b8a7b037a36f8f56d97 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Thu, 18 Aug 2022 16:51:36 +0800 Subject: [PATCH] Update in-app marketplace to display localized strings (#34356) * Update in-app marketplace to display localized strings * Add changelog --- .../update-34353-in-app-marketplace-i18n | 4 + .../includes/admin/class-wc-admin-addons.php | 74 ++++++++++++++++--- 2 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 plugins/woocommerce/changelog/update-34353-in-app-marketplace-i18n diff --git a/plugins/woocommerce/changelog/update-34353-in-app-marketplace-i18n b/plugins/woocommerce/changelog/update-34353-in-app-marketplace-i18n new file mode 100644 index 00000000000..072dbcc34b8 --- /dev/null +++ b/plugins/woocommerce/changelog/update-34353-in-app-marketplace-i18n @@ -0,0 +1,4 @@ +Significance: patch +Type: update + +Update in-app marketplace to display localized strings diff --git a/plugins/woocommerce/includes/admin/class-wc-admin-addons.php b/plugins/woocommerce/includes/admin/class-wc-admin-addons.php index b47f5ecd3e1..5e7b312fae3 100644 --- a/plugins/woocommerce/includes/admin/class-wc-admin-addons.php +++ b/plugins/woocommerce/includes/admin/class-wc-admin-addons.php @@ -26,7 +26,8 @@ class WC_Admin_Addons { * @return array of objects */ 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 ) { $headers = array(); $auth = WC_Helper_Options::get( 'auth' ); @@ -45,7 +46,7 @@ class WC_Admin_Addons { if ( ! is_wp_error( $raw_featured ) ) { $featured = json_decode( wp_remote_retrieve_body( $raw_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 */ 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 ) { $headers = array(); $auth = WC_Helper_Options::get( 'auth' ); @@ -71,10 +73,10 @@ class WC_Admin_Addons { $headers['Authorization'] = 'Bearer ' . $auth['access_token']; } - $parameter_string = ''; + $parameter_string = '?' . http_build_query( array( 'locale' => get_user_locale() ) ); $country = WC()->countries->get_base_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. @@ -118,7 +120,7 @@ class WC_Admin_Addons { 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 ) ) { do_action( 'woocommerce_page_wc-addons_connection_error', 'Empty or malformed response' ); $message = __( 'Our request to the featured API got a malformed response.', 'woocommerce' ); @@ -128,7 +130,7 @@ class WC_Admin_Addons { } 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, 'term' => $term, 'country' => $country, + 'locale' => get_user_locale(), ); 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' ); return new WP_Error( 'error', __( 'Our request to the search API got a malformed response.', 'woocommerce' ) ); } - return $addons; } @@ -226,15 +228,17 @@ class WC_Admin_Addons { * @return array of objects */ 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 ) ) { - $raw_sections = wp_safe_remote_get( - 'https://woocommerce.com/wp-json/wccom-extensions/1.0/categories' + $parameter_string = '?' . http_build_query( array( 'locale' => get_user_locale() ) ); + $raw_sections = wp_safe_remote_get( + 'https://woocommerce.com/wp-json/wccom-extensions/1.0/categories' . $parameter_string ); if ( ! is_wp_error( $raw_sections ) ) { $addon_sections = json_decode( wp_remote_retrieve_body( $raw_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 { ..., + * '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 ); + } }