🔀 Merge branch 'trunk'

# Conflicts:
#	modules/ppcp-settings/src/Endpoint/PaymentRestEndpoint.php
This commit is contained in:
Philipp Stracker 2025-02-14 12:55:57 +01:00
commit f958696c5f
No known key found for this signature in database
5 changed files with 135 additions and 10 deletions

View file

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

View file

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

View file

@ -0,0 +1,101 @@
<?php
/**
* A helper for mapping the old/new settings tab settings.
*
* @package WooCommerce\PayPalCommerce\Compat\Settings
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Compat\Settings;
use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext;
use WooCommerce\PayPalCommerce\ApiClient\Helper\PurchaseUnitSanitizer;
use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
/**
* A map of old to new styling settings.
*
* @psalm-import-type newSettingsKey from SettingsMap
* @psalm-import-type oldSettingsKey from SettingsMap
*/
class SettingsTabMapHelper {
use ContextTrait;
/**
* Maps old setting keys to new setting keys.
*
* @psalm-return array<oldSettingsKey, newSettingsKey>
*/
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<string, scalar|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<string, scalar|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<string, scalar|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
);
}
}

View file

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

View file

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