Skip to content

Commit 5947495

Browse files
mgascamclaudefrosso
authored
Add feature flag and admin UI for cache-optimized multi-currency rendering mode (#11364)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Francesco <frosso@users.noreply.github.com>
1 parent 3c85ef0 commit 5947495

File tree

10 files changed

+124
-7
lines changed

10 files changed

+124
-7
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: add
3+
4+
Add feature flag and admin UI setting for cache-optimized multi-currency rendering mode

includes/class-wc-payments-features.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class WC_Payments_Features {
3131
const WOOPAY_GLOBAL_THEME_SUPPORT_FLAG_NAME = '_wcpay_feature_woopay_global_theme_support';
3232
const WCPAY_DYNAMIC_CHECKOUT_PLACE_ORDER_BUTTON_FLAG_NAME = '_wcpay_feature_dynamic_checkout_place_order_button';
3333
const AMAZON_PAY_FLAG_NAME = '_wcpay_feature_amazon_pay';
34+
const MC_CACHE_OPTIMIZED_FLAG_NAME = '_wcpay_feature_mc_cache_optimized';
3435

3536
/**
3637
* Indicates whether card payments are enabled for this (Stripe) account.
@@ -363,6 +364,15 @@ public static function is_dynamic_checkout_place_order_button_enabled(): bool {
363364
return defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '10.6.0', '>=' );
364365
}
365366

367+
/**
368+
* Checks whether the multi-currency cache-optimized rendering mode is enabled.
369+
*
370+
* @return bool
371+
*/
372+
public static function is_mc_cache_optimized_enabled(): bool {
373+
return '1' === get_option( self::MC_CACHE_OPTIMIZED_FLAG_NAME, '0' );
374+
}
375+
366376
/**
367377
* Checks whether Amazon Pay is enabled.
368378
*

includes/multi-currency/MultiCurrency.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class MultiCurrency {
3030
const CURRENCY_META_KEY = 'wcpay_currency';
3131
const FILTER_PREFIX = 'wcpay_multi_currency_';
3232
const CUSTOMER_CURRENCIES_KEY = 'wcpay_multi_currency_stored_customer_currencies';
33+
const RENDERING_MODE_SPEED = 'speed';
34+
const RENDERING_MODE_CACHE = 'cache';
3335

3436
/**
3537
* The plugin's ID.
@@ -1041,6 +1043,25 @@ public function is_using_storefront_switcher(): bool {
10411043
return 'yes' === get_option( $this->id . '_enable_storefront_switcher', 'no' );
10421044
}
10431045

1046+
/**
1047+
* Gets the rendering mode for multi-currency prices.
1048+
*
1049+
* @return string One of 'speed' or 'cache'.
1050+
*/
1051+
public function get_rendering_mode(): string {
1052+
return get_option( 'wcpay_multi_currency_rendering_mode', self::RENDERING_MODE_SPEED );
1053+
}
1054+
1055+
/**
1056+
* Checks if the cache-optimized rendering mode is active.
1057+
*
1058+
* @return bool
1059+
*/
1060+
public function is_cache_optimized_mode(): bool {
1061+
return \WC_Payments_Features::is_mc_cache_optimized_enabled()
1062+
&& self::RENDERING_MODE_CACHE === $this->get_rendering_mode();
1063+
}
1064+
10441065
/**
10451066
* Gets the store settings.
10461067
*
@@ -1050,6 +1071,8 @@ public function get_settings() {
10501071
return [
10511072
$this->id . '_enable_auto_currency' => $this->is_using_auto_currency_switching(),
10521073
$this->id . '_enable_storefront_switcher' => $this->is_using_storefront_switcher(),
1074+
'wcpay_multi_currency_rendering_mode' => $this->get_rendering_mode(),
1075+
'is_cache_optimized_feature_enabled' => \WC_Payments_Features::is_mc_cache_optimized_enabled(),
10531076
'site_theme' => wp_get_theme()->get( 'Name' ),
10541077
'date_format' => esc_attr( get_option( 'date_format', 'F j, Y' ) ),
10551078
'time_format' => esc_attr( get_option( 'time_format', 'g:i a' ) ),
@@ -1068,12 +1091,23 @@ public function update_settings( $params ) {
10681091
$updateable_options = [
10691092
'wcpay_multi_currency_enable_auto_currency',
10701093
'wcpay_multi_currency_enable_storefront_switcher',
1094+
'wcpay_multi_currency_rendering_mode',
10711095
];
10721096

10731097
foreach ( $updateable_options as $key ) {
1074-
if ( isset( $params[ $key ] ) ) {
1075-
update_option( $key, sanitize_text_field( $params[ $key ] ) );
1098+
if ( ! isset( $params[ $key ] ) ) {
1099+
continue;
10761100
}
1101+
1102+
$value = sanitize_text_field( $params[ $key ] );
1103+
1104+
// Validate rendering mode to only accept known values.
1105+
if ( 'wcpay_multi_currency_rendering_mode' === $key
1106+
&& ! in_array( $value, [ self::RENDERING_MODE_SPEED, self::RENDERING_MODE_CACHE ], true ) ) {
1107+
continue;
1108+
}
1109+
1110+
update_option( $key, $value );
10771111
}
10781112
}
10791113

includes/multi-currency/RestController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function register_routes() {
146146
[
147147
'methods' => \WP_REST_Server::CREATABLE,
148148
'args' => [
149-
'wcpay_multi_currency_enable_auto_currency' => [
149+
'wcpay_multi_currency_enable_auto_currency' => [
150150
'type' => 'string',
151151
'format' => 'text-field',
152152
'required' => true,
@@ -156,6 +156,11 @@ public function register_routes() {
156156
'format' => 'text-field',
157157
'required' => true,
158158
],
159+
'wcpay_multi_currency_rendering_mode' => [
160+
'type' => 'string',
161+
'required' => false,
162+
'enum' => [ 'speed', 'cache' ],
163+
],
159164
],
160165
'callback' => [ $this, 'update_settings' ],
161166
'permission_callback' => [ $this, 'check_permission' ],

includes/multi-currency/client/data/actions.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export function* submitCurrencySettings( currencyCode, settings ) {
117117
export function* submitStoreSettingsUpdate(
118118
isAutoSwitchEnabled,
119119
isStorefrontSwitcherEnabled,
120+
renderingMode = 'speed',
120121
suppressNotices = false
121122
) {
122123
try {
@@ -130,6 +131,7 @@ export function* submitStoreSettingsUpdate(
130131
wcpay_multi_currency_enable_storefront_switcher: isStorefrontSwitcherEnabled
131132
? 'yes'
132133
: 'no',
134+
wcpay_multi_currency_rendering_mode: renderingMode,
133135
},
134136
} );
135137

includes/multi-currency/client/data/reducer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ const receiveMultiCurrencies = (
6363
data.wcpay_multi_currency_enable_auto_currency,
6464
enable_storefront_switcher:
6565
data.wcpay_multi_currency_enable_storefront_switcher,
66+
rendering_mode: data.wcpay_multi_currency_rendering_mode,
67+
is_cache_optimized_feature_enabled:
68+
data.is_cache_optimized_feature_enabled,
6669
site_theme: data.site_theme,
6770
date_format: data.date_format,
6871
time_format: data.time_format,

includes/multi-currency/client/settings/multi-currency/store-settings/__tests__/index.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ describe( 'Multi-Currency store settings', () => {
8080
name: /Save changes/,
8181
} )
8282
);
83-
expect( submitStoreSettingsUpdate ).toBeCalledWith( true, true );
83+
expect( submitStoreSettingsUpdate ).toBeCalledWith(
84+
true,
85+
true,
86+
'speed'
87+
);
8488
} );
8589
} );

includes/multi-currency/client/settings/multi-currency/store-settings/index.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
CardBody,
1515
CheckboxControl,
1616
ExternalLink,
17+
RadioControl,
1718
} from '@wordpress/components';
1819
import { useStoreSettings } from 'multi-currency/data';
1920
import {
@@ -61,6 +62,8 @@ const StoreSettings = () => {
6162
setIsStorefrontSwitcherEnabledValue,
6263
] = useState( false );
6364

65+
const [ renderingModeValue, setRenderingModeValue ] = useState( 'speed' );
66+
6467
const [ isPreviewModalOpen, setPreviewModalOpen ] = useState( false );
6568

6669
const [ isDirty, setIsDirty ] = useState( false );
@@ -73,6 +76,9 @@ const StoreSettings = () => {
7376
setIsAutomaticSwitchEnabledValue(
7477
storeSettings.enable_auto_currency
7578
);
79+
if ( storeSettings.rendering_mode ) {
80+
setRenderingModeValue( storeSettings.rendering_mode );
81+
}
7682
}
7783
}, [
7884
setIsAutomaticSwitchEnabledValue,
@@ -90,11 +96,17 @@ const StoreSettings = () => {
9096
setIsDirty( true );
9197
};
9298

99+
const handleRenderingModeChange = ( value ) => {
100+
setRenderingModeValue( value );
101+
setIsDirty( true );
102+
};
103+
93104
const saveSettings = () => {
94105
setIsSavingSettings( true );
95106
submitStoreSettingsUpdate(
96107
isAutomaticSwitchEnabledValue,
97-
isStorefrontSwitcherEnabledValue
108+
isStorefrontSwitcherEnabledValue,
109+
renderingModeValue
98110
);
99111
setIsSavingSettings( false );
100112
setIsDirty( false );
@@ -137,6 +149,36 @@ const StoreSettings = () => {
137149
) }
138150
__nextHasNoMarginBottom
139151
/>
152+
{ storeSettings.is_cache_optimized_feature_enabled ? (
153+
<RadioControl
154+
label={ __(
155+
'Price rendering mode',
156+
'woocommerce-payments'
157+
) }
158+
help={ __(
159+
'Choose how multi-currency prices are rendered. "Optimized for caching" outputs identical HTML for all visitors and converts prices client-side, allowing hosting providers to cache pages effectively.',
160+
'woocommerce-payments'
161+
) }
162+
selected={ renderingModeValue }
163+
options={ [
164+
{
165+
label: __(
166+
'Optimized for speed (default)',
167+
'woocommerce-payments'
168+
),
169+
value: 'speed',
170+
},
171+
{
172+
label: __(
173+
'Optimized for caching',
174+
'woocommerce-payments'
175+
),
176+
value: 'cache',
177+
},
178+
] }
179+
onChange={ handleRenderingModeChange }
180+
/>
181+
) : null }
140182
{ storeSettings.site_theme === 'Storefront' ? (
141183
<CheckboxControl
142184
checked={ isStorefrontSwitcherEnabledValue }

includes/multi-currency/client/setup/tasks/store-settings-task/__tests__/index.test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ describe( 'Multi-Currency store settings', () => {
118118
expect( submitStoreSettingsUpdate ).toBeCalledWith(
119119
false,
120120
false,
121+
'speed',
121122
true
122123
);
123124

@@ -130,7 +131,12 @@ describe( 'Multi-Currency store settings', () => {
130131
name: /Continue/,
131132
} )
132133
);
133-
expect( submitStoreSettingsUpdate ).toBeCalledWith( true, true, true );
134+
expect( submitStoreSettingsUpdate ).toBeCalledWith(
135+
true,
136+
true,
137+
'speed',
138+
true
139+
);
134140
} );
135141

136142
test( 'store settings are saved with continue button click', () => {
@@ -144,6 +150,7 @@ describe( 'Multi-Currency store settings', () => {
144150
expect( submitStoreSettingsUpdate ).toBeCalledWith(
145151
false,
146152
false,
153+
'speed',
147154
false
148155
);
149156

@@ -156,7 +163,12 @@ describe( 'Multi-Currency store settings', () => {
156163
name: /Continue/,
157164
} )
158165
);
159-
expect( submitStoreSettingsUpdate ).toBeCalledWith( true, true, false );
166+
expect( submitStoreSettingsUpdate ).toBeCalledWith(
167+
true,
168+
true,
169+
'speed',
170+
false
171+
);
160172
} );
161173

162174
test( 'store settings preview should open a modal with an iframe', () => {

includes/multi-currency/client/setup/tasks/store-settings-task/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const StoreSettingsTask = () => {
8888
submitStoreSettingsUpdate(
8989
isAutomaticSwitchEnabledValue,
9090
isStorefrontSwitcherEnabledValue,
91+
'speed',
9192
! isMultiCurrencyEnabled
9293
);
9394

0 commit comments

Comments
 (0)