Enhance/34244 update data source poller for i18n (#34247)

* Update data source poller for i18n

* Update unit test

* Remove unnecessary change_locale hook

* Fix payment-gateway-suggestions unit test
This commit is contained in:
Chi-Hsuan Huang 2022-08-17 12:06:46 +08:00 committed by GitHub
parent cd5ffed61b
commit 64e14ebe7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 43 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: enhancement
Add i18n support for data source poller

View File

@ -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

View File

@ -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();
}

View File

@ -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' ) );
}

View File

@ -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;

View File

@ -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 );
}
}

View File

@ -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 );
}