Refactor field page checking

Reduce code duplication and support more than 2 pages.
Also remove 'all'
This commit is contained in:
Alex P 2021-09-16 12:40:00 +03:00
parent b2904504f4
commit 620f25d88d
5 changed files with 74 additions and 15 deletions

View file

@ -700,7 +700,7 @@ return array(
State::STATE_ONBOARDED,
),
'requirements' => array(),
'gateway' => 'all',
'gateway' => array( 'paypal', 'dcc' ),
),
'logging_enabled' => array(
'title' => __( 'Logging', 'woocommerce-paypal-payments' ),

View file

@ -0,0 +1,49 @@
<?php
/**
* PageMatcherTrait.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Settings
*/
declare(strict_types=1);
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\State;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use Woocommerce\PayPalCommerce\WcGateway\Helper\DccProductStatus;
use Woocommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
use WooCommerce\PayPalCommerce\Webhooks\Status\WebhooksStatusPage;
/**
* Class PageMatcherTrait.
*/
trait PageMatcherTrait {
/**
* Checks whether the field config matches the current page (can be rendered here).
*
* @param array $field_config The field config (from wcgateway.settings.fields).
* @param string $current_page_id ID of the current PPCP gateway settings page.
* @return bool
*/
protected function field_matches_page( array $field_config, string $current_page_id ): bool {
$allowed_gateways = $field_config['gateway'];
if ( ! is_array( $allowed_gateways ) ) {
$allowed_gateways = array( $allowed_gateways );
}
$gateway_page_id_map = array(
PayPalGateway::ID => 'paypal',
CreditCardGateway::ID => 'dcc', // TODO: consider using just the gateway ID for PayPal and DCC too.
WebhooksStatusPage::ID => WebhooksStatusPage::ID,
);
return array_key_exists( $current_page_id, $gateway_page_id_map )
&& in_array( $gateway_page_id_map[ $current_page_id ], $allowed_gateways, true );
}
}

View file

@ -23,6 +23,8 @@ use WooCommerce\PayPalCommerce\Webhooks\WebhookRegistrar;
*/
class SettingsListener {
use PageMatcherTrait;
const NONCE = 'ppcp-settings';
private const CREDENTIALS_ADDED = 'credentials_added';
@ -360,16 +362,7 @@ class SettingsListener {
if ( ! in_array( $this->state->current_state(), $config['screens'], true ) ) {
continue;
}
if (
'dcc' === $config['gateway']
&& CreditCardGateway::ID !== $this->page_id
) {
continue;
}
if (
'paypal' === $config['gateway']
&& PayPalGateway::ID !== $this->page_id
) {
if ( ! $this->field_matches_page( $config, $this->page_id ) ) {
continue;
}
switch ( $config['type'] ) {

View file

@ -24,6 +24,8 @@ use Woocommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
*/
class SettingsRenderer {
use PageMatcherTrait;
/**
* The Settings status helper.
*
@ -325,10 +327,7 @@ class SettingsRenderer {
if ( ! in_array( $this->state->current_state(), $config['screens'], true ) ) {
continue;
}
if ( $is_dcc && ! in_array( $config['gateway'], array( 'all', 'dcc' ), true ) ) {
continue;
}
if ( ! $is_dcc && ! in_array( $config['gateway'], array( 'all', 'paypal' ), true ) ) {
if ( ! $this->field_matches_page( $config, $this->page_id ) ) {
continue;
}
if (