mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Merge pull request #3137 from woocommerce/PCP-4241-subscriptions-mode-setting-automatically-set-based-on-merchant-type
Set the Subscriptions mode setting automatically based on merchant type (4241)
This commit is contained in:
commit
30c5931f55
3 changed files with 115 additions and 6 deletions
|
@ -14,6 +14,7 @@ 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\Compat\Settings\SubscriptionSettingsMapHelper;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
|
||||
return array(
|
||||
|
@ -137,6 +138,9 @@ return array(
|
|||
$settings_tab_map_helper = $container->get( 'compat.settings.settings_tab_map_helper' );
|
||||
assert( $settings_tab_map_helper instanceof SettingsTabMapHelper );
|
||||
|
||||
$subscription_map_helper = $container->get( 'compat.settings.subscription_map_helper' );
|
||||
assert( $subscription_map_helper instanceof SubscriptionSettingsMapHelper );
|
||||
|
||||
return array(
|
||||
new SettingsMap(
|
||||
$container->get( 'settings.data.general' ),
|
||||
|
@ -180,13 +184,18 @@ return array(
|
|||
*/
|
||||
$styling_settings_map_helper->map()
|
||||
),
|
||||
new SettingsMap(
|
||||
$container->get( 'settings.data.settings' ),
|
||||
$subscription_map_helper->map()
|
||||
),
|
||||
);
|
||||
},
|
||||
'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.settings_tab_map_helper' )
|
||||
$container->get( 'compat.settings.settings_tab_map_helper' ),
|
||||
$container->get( 'compat.settings.subscription_map_helper' )
|
||||
);
|
||||
},
|
||||
'compat.settings.styling_map_helper' => static function() : StylingSettingsMapHelper {
|
||||
|
@ -195,4 +204,7 @@ return array(
|
|||
'compat.settings.settings_tab_map_helper' => static function() : SettingsTabMapHelper {
|
||||
return new SettingsTabMapHelper();
|
||||
},
|
||||
'compat.settings.subscription_map_helper' => static function( ContainerInterface $container ) : SubscriptionSettingsMapHelper {
|
||||
return new SubscriptionSettingsMapHelper( $container->get( 'wc-subscriptions.helper' ) );
|
||||
},
|
||||
);
|
||||
|
|
|
@ -57,23 +57,33 @@ class SettingsMapHelper {
|
|||
*/
|
||||
protected SettingsTabMapHelper $settings_tab_map_helper;
|
||||
|
||||
/**
|
||||
* A helper for mapping old and new subscription settings.
|
||||
*
|
||||
* @var SubscriptionSettingsMapHelper
|
||||
*/
|
||||
protected SubscriptionSettingsMapHelper $subscription_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.
|
||||
* @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.
|
||||
* @param SubscriptionSettingsMapHelper $subscription_map_helper A helper for mapping old and new subscription settings.
|
||||
* @throws RuntimeException When an old key has multiple mappings.
|
||||
*/
|
||||
public function __construct(
|
||||
array $settings_map,
|
||||
StylingSettingsMapHelper $styling_settings_map_helper,
|
||||
SettingsTabMapHelper $settings_tab_map_helper
|
||||
SettingsTabMapHelper $settings_tab_map_helper,
|
||||
SubscriptionSettingsMapHelper $subscription_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;
|
||||
$this->subscription_map_helper = $subscription_map_helper;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -151,7 +161,9 @@ class SettingsMapHelper {
|
|||
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 ] );
|
||||
return $old_key === 'subscriptions_mode'
|
||||
? $this->subscription_map_helper->mapped_value( $old_key, $this->model_cache[ $model_id ] )
|
||||
: $this->settings_tab_map_helper->mapped_value( $old_key, $this->model_cache[ $model_id ] );
|
||||
|
||||
default:
|
||||
return $this->model_cache[ $model_id ][ $new_key ] ?? null;
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
/**
|
||||
* A helper for mapping old and new subscription settings.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Compat\Settings
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Compat\Settings;
|
||||
|
||||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
||||
|
||||
/**
|
||||
* Handles mapping between old and new subscription settings.
|
||||
*
|
||||
* In the new settings UI, the Subscriptions mode value is set automatically based on the merchant type.
|
||||
* This class fakes the mapping and injects the appropriate value based on the merchant:
|
||||
* - Non-vaulting merchants will use PayPal Subscriptions.
|
||||
* - Merchants with vaulting will use PayPal Vaulting.
|
||||
* - Disabled subscriptions can be controlled 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.
|
||||
*
|
||||
* @var SubscriptionHelper $subscription_helper
|
||||
*/
|
||||
protected SubscriptionHelper $subscription_helper;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param SubscriptionHelper $subscription_helper The subscription helper.
|
||||
*/
|
||||
public function __construct( SubscriptionHelper $subscription_helper ) {
|
||||
$this->subscription_helper = $subscription_helper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps the old subscription setting key.
|
||||
*
|
||||
* This method creates a placeholder mapping as this setting doesn't exist in the new settings.
|
||||
* The Subscriptions mode value is set automatically based on the merchant type.
|
||||
*
|
||||
* @psalm-return array<oldSettingsKey, newSettingsKey>
|
||||
*/
|
||||
public function map(): array {
|
||||
return array( 'subscriptions_mode' => '' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the mapped value for the subscriptions_mode 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 'vaulting_api'|'subscriptions_api'|'disable_paypal_subscriptions'|null The mapped subscriptions_mode value, or null if not applicable.
|
||||
*/
|
||||
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 disabling the subscription mode when using the new settings UI.
|
||||
*
|
||||
* @returns bool true if the subscription mode should be disabled, false otherwise (default is false).
|
||||
*/
|
||||
$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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue