Refactor settings element state requirements checking

This commit is contained in:
Alex P 2022-02-03 14:28:51 +02:00
parent 72c9bd1bef
commit 70ef7dd367
3 changed files with 32 additions and 15 deletions

View file

@ -48,6 +48,22 @@ class State {
$this->settings = $settings;
}
/**
* Returns the state of the specified environment (or the active environment if null).
*
* @param string|null $environment 'sandbox', 'production'.
* @return int
*/
public function environment_state( ?string $environment = null ): int {
switch ( $environment ) {
case Environment::PRODUCTION:
return $this->production_state();
case Environment::SANDBOX:
return $this->sandbox_state();
}
return $this->current_state();
}
/**
* Returns the current active onboarding state.
*

View file

@ -255,6 +255,8 @@ return array(
'wcgateway.settings.fields' => static function ( ContainerInterface $container ): array {
$state = $container->get( 'onboarding.state' );
assert( $state instanceof State );
$messages_disclaimers = $container->get( 'button.helper.messages-disclaimers' );
$dcc_applies = $container->get( 'api.helpers.dccapplies' );
@ -338,7 +340,8 @@ return array(
'screens' => array(
State::STATE_START,
),
'env' => 'production',
'state_from' => Environment::PRODUCTION,
'env' => Environment::PRODUCTION,
'products' => array( 'PPCP' ),
'requirements' => array(),
'gateway' => 'paypal',
@ -348,7 +351,8 @@ return array(
'screens' => array(
State::STATE_START,
),
'env' => 'production',
'state_from' => Environment::PRODUCTION,
'env' => Environment::PRODUCTION,
'products' => array( 'EXPRESS_CHECKOUT' ),
'requirements' => array(),
'gateway' => 'paypal',
@ -358,7 +362,8 @@ return array(
'screens' => array(
State::STATE_START,
),
'env' => 'sandbox',
'state_from' => Environment::SANDBOX,
'env' => Environment::SANDBOX,
'products' => array( 'PPCP' ),
'requirements' => array(),
'gateway' => 'paypal',
@ -369,7 +374,8 @@ return array(
'screens' => array(
State::STATE_START,
),
'env' => 'sandbox',
'state_from' => Environment::SANDBOX,
'env' => Environment::SANDBOX,
'products' => array( 'EXPRESS_CHECKOUT' ),
'requirements' => array(),
'gateway' => 'paypal',
@ -383,7 +389,8 @@ return array(
'screens' => array(
State::STATE_ONBOARDED,
),
'env' => 'production',
'state_from' => Environment::PRODUCTION,
'env' => Environment::PRODUCTION,
'requirements' => array(),
'gateway' => 'paypal',
'description' => __( 'Click to reset current credentials and use another account.', 'woocommerce-paypal-payments' ),
@ -395,7 +402,8 @@ return array(
'screens' => array(
State::STATE_ONBOARDED,
),
'env' => 'production',
'state_from' => Environment::SANDBOX,
'env' => Environment::SANDBOX,
'requirements' => array(),
'gateway' => 'paypal',
'description' => __( 'Click to reset current credentials and use another account.', 'woocommerce-paypal-payments' ),
@ -1909,14 +1917,6 @@ return array(
unset( $fields['vault_enabled'] );
}
if ( State::STATE_ONBOARDED === $state->production_state() ) {
unset( $fields['ppcp_disconnect_sandbox'] );
unset( $fields['credentials_sandbox_heading'] );
} elseif ( State::STATE_ONBOARDED === $state->sandbox_state() ) {
unset( $fields['ppcp_disconnect_production'] );
unset( $fields['credentials_production_heading'] );
}
/**
* Depending on your store location, some credit cards can't be used.
* Here, we filter them out.

View file

@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Settings;
use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use Psr\Container\ContainerInterface;
@ -388,7 +389,7 @@ $data_rows_html
<?php
foreach ( $this->fields as $field => $config ) :
if ( ! in_array( $this->state->current_state(), $config['screens'], true ) ) {
if ( ! in_array( $this->state->environment_state( $config['state_from'] ?? null ), $config['screens'], true ) ) {
continue;
}
if ( ! $this->field_matches_page( $config, $this->page_id ) ) {