diff --git a/modules/ppcp-compat/services.php b/modules/ppcp-compat/services.php index 142bec40d..b69d4fc0d 100644 --- a/modules/ppcp-compat/services.php +++ b/modules/ppcp-compat/services.php @@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\Compat; use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets; use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMap; use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMapHelper; +use WooCommerce\PayPalCommerce\Compat\Settings\SettingsTabMapHelper; use WooCommerce\PayPalCommerce\Compat\Settings\StylingSettingsMapHelper; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; @@ -133,6 +134,9 @@ return array( $styling_settings_map_helper = $container->get( 'compat.settings.styling_map_helper' ); assert( $styling_settings_map_helper instanceof StylingSettingsMapHelper ); + $settings_tab_map_helper = $container->get( 'compat.settings.settings_tab_map_helper' ); + assert( $settings_tab_map_helper instanceof SettingsTabMapHelper ); + return array( new SettingsMap( $container->get( 'settings.data.general' ), @@ -160,9 +164,7 @@ return array( ), new SettingsMap( $container->get( 'settings.data.settings' ), - array( - 'disable_cards' => 'disabled_cards', - ) + $settings_tab_map_helper->map() ), new SettingsMap( $container->get( 'settings.data.styling' ), @@ -183,10 +185,14 @@ return array( 'compat.settings.settings_map_helper' => static function( ContainerInterface $container ) : SettingsMapHelper { return new SettingsMapHelper( $container->get( 'compat.setting.new-to-old-map' ), - $container->get( 'compat.settings.styling_map_helper' ) + $container->get( 'compat.settings.styling_map_helper' ), + $container->get( 'compat.settings.settings_tab_map_helper' ) ); }, 'compat.settings.styling_map_helper' => static function() : StylingSettingsMapHelper { return new StylingSettingsMapHelper(); }, + 'compat.settings.settings_tab_map_helper' => static function() : SettingsTabMapHelper { + return new SettingsTabMapHelper(); + }, ); diff --git a/modules/ppcp-compat/src/Settings/SettingsMapHelper.php b/modules/ppcp-compat/src/Settings/SettingsMapHelper.php index a18f2ceda..678644897 100644 --- a/modules/ppcp-compat/src/Settings/SettingsMapHelper.php +++ b/modules/ppcp-compat/src/Settings/SettingsMapHelper.php @@ -10,6 +10,7 @@ declare( strict_types = 1 ); namespace WooCommerce\PayPalCommerce\Compat\Settings; use RuntimeException; +use WooCommerce\PayPalCommerce\Settings\Data\SettingsModel; use WooCommerce\PayPalCommerce\Settings\Data\StylingSettings; /** @@ -49,17 +50,30 @@ class SettingsMapHelper { */ protected StylingSettingsMapHelper $styling_settings_map_helper; + /** + * A helper for mapping the old/new settings tab settings. + * + * @var SettingsTabMapHelper + */ + protected SettingsTabMapHelper $settings_tab_map_helper; + /** * Constructor. * * @param SettingsMap[] $settings_map A list of settings maps containing key definitions. * @param StylingSettingsMapHelper $styling_settings_map_helper A helper for mapping the old/new styling settings. + * @param SettingsTabMapHelper $settings_tab_map_helper A helper for mapping the old/new settings tab settings. * @throws RuntimeException When an old key has multiple mappings. */ - public function __construct( array $settings_map, StylingSettingsMapHelper $styling_settings_map_helper ) { + public function __construct( + array $settings_map, + StylingSettingsMapHelper $styling_settings_map_helper, + SettingsTabMapHelper $settings_tab_map_helper + ) { $this->validate_settings_map( $settings_map ); $this->settings_map = $settings_map; $this->styling_settings_map_helper = $styling_settings_map_helper; + $this->settings_tab_map_helper = $settings_tab_map_helper; } /** @@ -135,6 +149,10 @@ class SettingsMapHelper { switch ( true ) { case $model instanceof StylingSettings: return $this->styling_settings_map_helper->mapped_value( $old_key, $this->model_cache[ $model_id ] ); + + case $model instanceof SettingsModel: + return $this->settings_tab_map_helper->mapped_value( $old_key, $this->model_cache[ $model_id ] ); + default: return $this->model_cache[ $model_id ][ $new_key ] ?? null; } diff --git a/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php b/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php new file mode 100644 index 000000000..d3ce17290 --- /dev/null +++ b/modules/ppcp-compat/src/Settings/SettingsTabMapHelper.php @@ -0,0 +1,101 @@ + + */ + public function map(): array { + return array( + 'disable_cards' => 'disabled_cards', + 'brand_name' => 'brand_name', + 'soft_descriptor' => 'soft_descriptor', + 'payee_preferred' => 'instant_payments_only', + 'subtotal_mismatch_behavior' => 'subtotal_adjustment', + 'landing_page' => 'landing_page', + 'smart_button_language' => 'button_language', + ); + } + + /** + * Retrieves the value of a mapped key from the new settings. + * + * @param string $old_key The key from the legacy settings. + * @param array $settings_model The new settings model data as an array. + * @return mixed The value of the mapped setting, (null if not found). + */ + public function mapped_value( string $old_key, array $settings_model ) { + $settings_map = $this->map(); + $new_key = $settings_map[ $old_key ] ?? false; + switch ( $old_key ) { + case 'subtotal_mismatch_behavior': + return $this->mapped_mismatch_behavior_value( $settings_model ); + + case 'landing_page': + return $this->mapped_landing_page_value( $settings_model ); + + default: + return $settings_model[ $new_key ] ?? null; + } + } + + /** + * Retrieves the mapped value for the 'mismatch_behavior' from the new settings. + * + * @param array $settings_model The new settings model data as an array. + * @return 'extra_line'|'ditch'|null The mapped 'mismatch_behavior' setting value. + */ + protected function mapped_mismatch_behavior_value( array $settings_model ): ?string { + $subtotal_adjustment = $settings_model['subtotal_adjustment'] ?? false; + + if ( ! $subtotal_adjustment ) { + return null; + } + + return $subtotal_adjustment === 'correction' ? PurchaseUnitSanitizer::MODE_EXTRA_LINE : PurchaseUnitSanitizer::MODE_DITCH; + } + + /** + * Retrieves the mapped value for the 'landing_page' from the new settings. + * + * @param array $settings_model The new settings model data as an array. + * @return 'LOGIN'|'BILLING'|'NO_PREFERENCE'|null The mapped 'landing_page' setting value. + */ + protected function mapped_landing_page_value( array $settings_model ): ?string { + $landing_page = $settings_model['landing_page'] ?? false; + + if ( ! $landing_page ) { + return null; + } + + return $landing_page === 'login' + ? ApplicationContext::LANDING_PAGE_LOGIN + : ( $landing_page === 'guest_checkout' + ? ApplicationContext::LANDING_PAGE_BILLING + : ApplicationContext::LANDING_PAGE_NO_PREFERENCE + ); + } +} diff --git a/modules/ppcp-settings/resources/js/Components/Screens/Settings/Components/Settings/Blocks/PaypalSettings.js b/modules/ppcp-settings/resources/js/Components/Screens/Settings/Components/Settings/Blocks/PaypalSettings.js index 62240b35b..86a0c81af 100644 --- a/modules/ppcp-settings/resources/js/Components/Screens/Settings/Components/Settings/Blocks/PaypalSettings.js +++ b/modules/ppcp-settings/resources/js/Components/Screens/Settings/Components/Settings/Blocks/PaypalSettings.js @@ -142,10 +142,10 @@ const PaypalSettings = () => { }; const languagesExample = [ - { value: 'en', label: 'English' }, - { value: 'de', label: 'German' }, - { value: 'es', label: 'Spanish' }, - { value: 'it', label: 'Italian' }, + { value: 'en_US', label: 'English' }, + { value: 'de_DE', label: 'German' }, + { value: 'es_ES', label: 'Spanish' }, + { value: 'it_IT', label: 'Italian' }, ]; const subtotalAdjustmentChoices = [ diff --git a/modules/ppcp-settings/src/Data/SettingsModel.php b/modules/ppcp-settings/src/Data/SettingsModel.php index b88a899d3..0e9e8e706 100644 --- a/modules/ppcp-settings/src/Data/SettingsModel.php +++ b/modules/ppcp-settings/src/Data/SettingsModel.php @@ -73,7 +73,7 @@ class SettingsModel extends AbstractDataModel { // Enum-type string values. 'subtotal_adjustment' => 'skip_details', // Options: [correction|no_details]. 'landing_page' => 'any', // Options: [any|login|guest_checkout]. - 'button_language' => '', // empty or a 2-letter language code. + 'button_language' => '', // empty or a language locale code. // Boolean flags. 'authorize_only' => false,