diff --git a/plugins/woocommerce/changelog/enhance-34244-data-source-poller-for-i18n b/plugins/woocommerce/changelog/enhance-34244-data-source-poller-for-i18n new file mode 100644 index 00000000000..a2f0b02a1c2 --- /dev/null +++ b/plugins/woocommerce/changelog/enhance-34244-data-source-poller-for-i18n @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +Add i18n support for data source poller diff --git a/plugins/woocommerce/src/Admin/DataSourcePoller.php b/plugins/woocommerce/src/Admin/DataSourcePoller.php index cbd1aa173c0..8b75fab911e 100644 --- a/plugins/woocommerce/src/Admin/DataSourcePoller.php +++ b/plugins/woocommerce/src/Admin/DataSourcePoller.php @@ -104,14 +104,17 @@ abstract class DataSourcePoller { * @return array list of specs. */ public function get_specs_from_data_sources() { - $specs = get_transient( $this->args['transient_name'] ); + $locale = get_user_locale(); + $specs_group = get_transient( $this->args['transient_name'] ) ?? array(); + $specs = isset( $specs_group[ $locale ] ) ? $specs_group[ $locale ] : array(); - if ( false === $specs || ! is_array( $specs ) || 0 === count( $specs ) ) { + if ( ! is_array( $specs ) || empty( $specs ) ) { $this->read_specs_from_data_sources(); - $specs = get_transient( $this->args['transient_name'] ); + $specs_group = get_transient( $this->args['transient_name'] ); + $specs = isset( $specs_group[ $locale ] ) ? $specs_group[ $locale ] : array(); } $specs = apply_filters( self::FILTER_NAME_SPECS, $specs, $this->id ); - return false !== $specs ? $specs : array(); + return $specs !== false ? $specs : array(); } /** @@ -130,14 +133,16 @@ abstract class DataSourcePoller { $this->merge_specs( $specs_from_data_source, $specs, $url ); } + $specs_group = get_transient( $this->args['transient_name'] ) ?? array(); + $locale = get_user_locale(); + $specs_group[ $locale ] = $specs; // Persist the specs as a transient. set_transient( $this->args['transient_name'], - $specs, + $specs_group, $this->args['transient_expiry'] ); - - return 0 !== count( $specs ); + return count( $specs ) !== 0; } /** @@ -161,7 +166,7 @@ abstract class DataSourcePoller { $logger = self::get_logger(); $response = wp_remote_get( add_query_arg( - '_locale', + 'locale', get_user_locale(), $url ) @@ -181,7 +186,7 @@ abstract class DataSourcePoller { $body = $response['body']; $specs = json_decode( $body ); - if ( null === $specs ) { + if ( $specs === null ) { $logger->error( 'Empty response in data feed', $logger_context diff --git a/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/Init.php b/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/Init.php index 48b748cf334..926543b05ad 100644 --- a/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/Init.php +++ b/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/Init.php @@ -7,7 +7,6 @@ namespace Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions; defined( 'ABSPATH' ) || exit; -use Automattic\WooCommerce\Admin\RemoteInboxNotifications\SpecRunner; use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\DefaultPaymentGateways; use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\PaymentGatewaysController; @@ -25,7 +24,6 @@ class Init { * Constructor. */ public function __construct() { - add_action( 'change_locale', array( __CLASS__, 'delete_specs_transient' ) ); PaymentGatewaysController::init(); } diff --git a/plugins/woocommerce/src/Internal/Admin/RemoteFreeExtensions/Init.php b/plugins/woocommerce/src/Internal/Admin/RemoteFreeExtensions/Init.php index 12a28bdec7b..2baad0a2995 100644 --- a/plugins/woocommerce/src/Internal/Admin/RemoteFreeExtensions/Init.php +++ b/plugins/woocommerce/src/Internal/Admin/RemoteFreeExtensions/Init.php @@ -19,7 +19,6 @@ class Init { * Constructor. */ public function __construct() { - add_action( 'change_locale', array( __CLASS__, 'delete_specs_transient' ) ); add_action( 'woocommerce_updated', array( __CLASS__, 'delete_specs_transient' ) ); } diff --git a/plugins/woocommerce/src/Internal/Admin/WCPayPromotion/Init.php b/plugins/woocommerce/src/Internal/Admin/WCPayPromotion/Init.php index fc1bc10da38..b0e6873f70d 100644 --- a/plugins/woocommerce/src/Internal/Admin/WCPayPromotion/Init.php +++ b/plugins/woocommerce/src/Internal/Admin/WCPayPromotion/Init.php @@ -24,8 +24,6 @@ class Init { public function __construct() { include_once __DIR__ . '/WCPaymentGatewayPreInstallWCPayPromotion.php'; - add_action( 'change_locale', array( __CLASS__, 'delete_specs_transient' ) ); - $is_payments_page = isset( $_GET['page'] ) && 'wc-settings' === $_GET['page'] && isset( $_GET['tab'] ) && 'checkout' === $_GET['tab']; // phpcs:ignore WordPress.Security.NonceVerification if ( ! wp_is_json_request() && ! $is_payments_page ) { return; diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/payment-gateway-suggestions/data-source-poller.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/payment-gateway-suggestions/data-source-poller.php index 1fbdf74d0e0..80a6a74d37e 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/payment-gateway-suggestions/data-source-poller.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/payment-gateway-suggestions/data-source-poller.php @@ -5,7 +5,6 @@ * @package WooCommerce\Admin\Tests\PaymentGatewaySuggestions */ -use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\Init as PaymentGatewaySuggestions; use Automattic\WooCommerce\Admin\DataSourcePoller; use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\PaymentGatewaySuggestionsDataSourcePoller; @@ -33,7 +32,7 @@ class WC_Admin_Tests_PaymentGatewaySuggestions_DataSourcePoller extends WC_Unit_ function( $pre, $parsed_args, $url ) { $locale = get_locale(); - if ( 'payment-gateway-suggestions-data-source.json?_locale=' . $locale === $url ) { + if ( $url === 'payment-gateway-suggestions-data-source.json?locale=' . $locale ) { return array( 'body' => wp_json_encode( array( @@ -51,7 +50,7 @@ class WC_Admin_Tests_PaymentGatewaySuggestions_DataSourcePoller extends WC_Unit_ ); } - if ( 'payment-gateway-suggestions-data-source2.json?_locale=' . $locale === $url ) { + if ( $url === 'payment-gateway-suggestions-data-source2.json?locale=' . $locale ) { return array( 'body' => wp_json_encode( array( @@ -161,7 +160,12 @@ class WC_Admin_Tests_PaymentGatewaySuggestions_DataSourcePoller extends WC_Unit_ $data = $data_source_poller->get_specs_from_data_sources(); $this->assertCount( 2, $data ); - $data = get_transient( 'woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs' ); + $data = get_transient( 'woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs' ); + $locale = get_locale(); + $this->assertArrayHasKey( $locale, $data ); + + $data = $data[ $locale ]; $this->assertCount( 2, $data ); + } } diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/payment-gateway-suggestions/payment-gateway-suggestions.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/payment-gateway-suggestions/payment-gateway-suggestions.php index 15e6fe1be2c..98e7491890b 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/payment-gateway-suggestions/payment-gateway-suggestions.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/payment-gateway-suggestions/payment-gateway-suggestions.php @@ -52,12 +52,14 @@ class WC_Admin_Tests_PaymentGatewaySuggestions_Init extends WC_Unit_Test_Case { */ public function get_mock_specs() { return array( - array( - 'id' => 'mock-gateway', - 'is_visible' => (object) array( - 'type' => 'base_location_country', - 'value' => 'ZA', - 'operation' => '=', + 'en_US' => array( + array( + 'id' => 'mock-gateway', + 'is_visible' => (object) array( + 'type' => 'base_location_country', + 'value' => 'ZA', + 'operation' => '=', + ), ), ), ); @@ -87,11 +89,13 @@ class WC_Admin_Tests_PaymentGatewaySuggestions_Init extends WC_Unit_Test_Case { set_transient( 'woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs', array( - array( - 'id' => 'mock-gateway1', - ), - array( - 'id' => 'mock-gateway2', + 'en_US' => array( + array( + 'id' => 'mock-gateway1', + ), + array( + 'id' => 'mock-gateway2', + ), ), ) ); @@ -126,33 +130,33 @@ class WC_Admin_Tests_PaymentGatewaySuggestions_Init extends WC_Unit_Test_Case { } /** - * Test that the transient is deleted on locale change. + * Test that matched locale specs are read from cache. */ - public function test_delete_transient_on_locale_change() { + public function test_specs_locale_transient() { set_transient( 'woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs', array( - array( - 'id' => 'mock-gateway', + 'en_US' => array( + array( + 'id' => 'mock-gateway', + ), + ), + 'zh_TW' => array( + array( + 'id' => 'default-gateway', + ), ), ) ); add_filter( - 'get_available_languages', - function( $languages ) { - $languages[] = 'zh_TW'; - return $languages; + 'locale', + function( $_locale ) { + return 'zh_TW'; } ); - $wp_locale_switcher = new WP_Locale_switcher(); - $wp_locale_switcher->switch_to_locale( 'zh_TW' ); - $suggestions = PaymentGatewaySuggestions::get_suggestions(); - - $wp_locale_switcher->switch_to_locale( 'en_US' ); - $this->assertEquals( 'default-gateway', $suggestions[0]->id ); }