Implement the subscription map helper

This commit is contained in:
Narek Zakarian 2025-02-20 13:31:25 +04:00
parent be071decfb
commit 66e52c9ad0
No known key found for this signature in database
GPG key ID: 07AFD7E7A9C164A7

View file

@ -9,7 +9,6 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Compat\Settings;
use WooCommerce\PayPalCommerce\Settings\DTO\LocationStylingDTO;
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
/**
@ -17,14 +16,19 @@ use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
*
* In new settings UI we have to automatically set Subscriptions mode value based on the type of merchant.
* So here we will fake the map and later inject the value of the old subscription setting value based on the type of merchant.
* - Non-vaulting merchants should have it set to PayPal Subscriptions
* - Merchants with vaulting should have it set to PayPal Vaulting
* - Non-vaulting merchants should have it set to PayPal Subscriptions.
* - Merchants with vaulting should have it set to PayPal Vaulting.
* - The disabled subscriptions can be retrieved by using a filter.
*
* @psalm-import-type newSettingsKey from SettingsMap
* @psalm-import-type oldSettingsKey from SettingsMap
*/
class SubscriptionSettingsMapHelper {
public const OLD_SETTINGS_SUBSCRIPTION_MODE_VALUE_VAULTING = 'vaulting_api';
public const OLD_SETTINGS_SUBSCRIPTION_MODE_VALUE_SUBSCRIPTIONS = 'subscriptions_api';
public const OLD_SETTINGS_SUBSCRIPTION_MODE_VALUE_DISABLED = 'disable_paypal_subscriptions';
/**
* The subscription helper.
*
@ -54,17 +58,28 @@ class SubscriptionSettingsMapHelper {
}
/**
* Retrieves the value of a mapped key from the new settings.
* Retrieves the value of a mapped subscriptions_mode key from the new settings.
*
* @param string $old_key The key from the legacy settings.
* @param LocationStylingDTO[] $styling_models The list of location styling models.
* @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).
* @return 'vaulting_api'|'subscriptions_api'|'disable_paypal_subscriptions'|null The value of the mapped subscriptions_mode setting (null if not found).
*/
public function mapped_value( string $old_key, array $styling_models ) {
public function mapped_value( string $old_key, array $settings_model ): ?string {
if ( $old_key !== 'subscriptions_mode' || ! $this->subscription_helper->plugin_is_active() ) {
return null;
}
$vaulting = $settings_model['save_paypal_and_venmo'] ?? false;
$subscription_mode_value = $vaulting ? self::OLD_SETTINGS_SUBSCRIPTION_MODE_VALUE_VAULTING : self::OLD_SETTINGS_SUBSCRIPTION_MODE_VALUE_SUBSCRIPTIONS;
/**
* Allows to disable the subscription mode when using the new settings UI.
*
* @returns bool true if the subscription mode should be disabled, otherwise false (also by default).
*/
$subscription_mode_disabled = ( bool ) apply_filters( 'woocommerce_paypal_payments_subscription_mode_disabled', false );
return $subscription_mode_disabled ? self::OLD_SETTINGS_SUBSCRIPTION_MODE_VALUE_DISABLED : $subscription_mode_value;
}
}