From 66e52c9ad064b24186d7ccd68cc3975771ea3ce0 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 20 Feb 2025 13:31:25 +0400 Subject: [PATCH] Implement the subscription map helper --- .../SubscriptionSettingsMapHelper.php | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/modules/ppcp-compat/src/Settings/SubscriptionSettingsMapHelper.php b/modules/ppcp-compat/src/Settings/SubscriptionSettingsMapHelper.php index 5d962f823..ed1b78d8e 100644 --- a/modules/ppcp-compat/src/Settings/SubscriptionSettingsMapHelper.php +++ b/modules/ppcp-compat/src/Settings/SubscriptionSettingsMapHelper.php @@ -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 $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; } }