♻️ Improve readability and code structure

This commit is contained in:
Philipp Stracker 2025-03-12 11:59:10 +01:00
parent 1938f6b6ed
commit 6da8988e0a
No known key found for this signature in database

View file

@ -58,52 +58,67 @@ class DisabledFundingSources {
? $this->settings->get( 'disable_funding' )
: array();
$is_dcc_enabled = $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' );
$is_checkout = is_checkout();
$is_block_context = in_array( $context, array( 'checkout-block', 'cart-block' ), true );
// Determine context flags.
$is_checkout_page = is_checkout();
$is_block_context = in_array( $context, array( 'checkout-block', 'cart-block' ), true );
$is_classic_checkout = $is_checkout_page && ! $is_block_context;
if (
! $is_checkout
|| ( $is_dcc_enabled && $is_block_context )
) {
$disable_funding[] = 'card';
}
// Determine payment method availability flags.
$is_dcc_enabled = $this->settings->has( 'dcc_enabled' ) && $this->settings->get( 'dcc_enabled' );
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
$is_card_gateway_enabled = isset( $available_gateways[ CardButtonGateway::ID ] );
$is_using_cards = $is_dcc_enabled || $is_card_gateway_enabled;
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
$is_separate_card_enabled = isset( $available_gateways[ CardButtonGateway::ID ] );
// Check if card is already in disabled funding (from settings value).
$card_key = array_search( 'card', $disable_funding, true );
$is_card_disabled = ( $card_key !== false );
if (
(
is_checkout()
&& ! in_array( $context, array( 'checkout-block', 'cart-block' ), true )
)
&& (
$is_dcc_enabled
|| $is_separate_card_enabled
)
) {
$key = array_search( 'card', $disable_funding, true );
if ( false !== $key ) {
unset( $disable_funding[ $key ] );
if ( $is_block_context && $is_dcc_enabled ) {
// Rule 1: Block checkout with DCC - do not load ACDC.
if ( ! $is_card_disabled ) {
$disable_funding[] = 'card';
}
} elseif ( ! $is_checkout_page ) {
// Rule 2: Non-checkout pages - do not load ACDC.
if ( ! $is_card_disabled ) {
$disable_funding[] = 'card';
}
} elseif ( $is_classic_checkout && $is_using_cards ) {
// Rule 3: Standard checkout with card methods - load ACDC.
if ( $is_card_disabled ) {
unset( $disable_funding[ $card_key ] );
}
}
if ( in_array( $context, array( 'checkout-block', 'cart-block' ), true ) ) {
/**
* Block checkout only supports the following funding methods:
* - PayPal
* - PayLater
* - Venmo
* - ACDC ("card", conditionally)
*/
if ( $is_block_context ) {
$allowed_in_blocks = array( 'venmo', 'paylater', 'paypal', 'card' );
$disable_funding = array_merge(
$disable_funding,
array_diff(
array_keys( $this->all_funding_sources ),
array( 'venmo', 'paylater', 'paypal', 'card' )
)
array_diff( array_keys( $this->all_funding_sources ), $allowed_in_blocks )
);
}
/**
* Free trials only support the funding method ACDC ("card").
*/
if ( $this->is_free_trial_cart() ) {
$all_sources = array_keys( $this->all_funding_sources );
if ( $is_dcc_enabled || $is_separate_card_enabled ) {
$all_sources = array_diff( $all_sources, array( 'card' ) );
$disable_funding = array_keys( $this->all_funding_sources );
if ( $is_using_cards ) {
$card_key = array_search( 'card', $disable_funding, true );
if ( false !== $card_key ) {
unset( $disable_funding[ $card_key ] );
}
}
$disable_funding = $all_sources;
}
return apply_filters( 'woocommerce_paypal_payments_disabled_funding_sources', $disable_funding );