From 54652951bbd94c89e77042b0bf6fcb77b7dc14ba Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 19 Aug 2020 05:39:45 +0300 Subject: [PATCH] phpcs --- .../ppcp-button/src/Assets/SmartButton.php | 77 ++++++++++++------- .../Checkout/CheckoutPayPalAddressPreset.php | 32 +++++--- .../src/Checkout/DisableGateways.php | 7 +- .../src/Gateway/CreditCardGateway.php | 9 +-- .../src/Settings/SettingsListener.php | 25 +++++- .../src/Settings/SettingsRenderer.php | 8 +- .../ppcp-wc-gateway/src/WcGatewayModule.php | 28 ++++--- 7 files changed, 121 insertions(+), 65 deletions(-) diff --git a/modules.local/ppcp-button/src/Assets/SmartButton.php b/modules.local/ppcp-button/src/Assets/SmartButton.php index 634a8f5c9..26ab95a51 100644 --- a/modules.local/ppcp-button/src/Assets/SmartButton.php +++ b/modules.local/ppcp-button/src/Assets/SmartButton.php @@ -169,7 +169,8 @@ class SmartButton implements SmartButtonInterface return true; } - public function buttonRenderer() { + public function buttonRenderer() + { $product = wc_get_product(); if ( ! is_checkout() && is_a($product, \WC_Product::class) @@ -185,7 +186,9 @@ class SmartButton implements SmartButtonInterface $this->renderMessage(); } - private function renderMessage() { + //phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong + private function renderMessage() + { $markup = '
'; - $placement = 'product'; $product = wc_get_product(); $amount = (is_a($product, \WC_Product::class)) ? wc_get_price_including_tax($product) : 0; - $layout = $this->settings->has('message_product_layout') ? $this->settings->get('message_product_layout') : 'text'; - $logoType = $this->settings->has('message_product_logo') ? $this->settings->get('message_product_logo') : 'primary'; - $logoPosition = $this->settings->has('message_product_position') ? $this->settings->get('message_product_position') : 'left'; - $textColor = $this->settings->has('message_product_color') ? $this->settings->get('message_product_color') : 'black'; - $styleColor = $this->settings->has('message_product_flex_color') ? $this->settings->get('message_product_flex_color') : 'blue'; - $ratio = $this->settings->has('message_product_flex_ratio') ? $this->settings->get('message_product_flex_ratio') : '1x1'; - $shouldShow = $this->settings->has('message_product_enabled') && $this->settings->get('message_product_enabled'); + $layout = $this->settings->has('message_product_layout') ? + $this->settings->get('message_product_layout') : 'text'; + $logoType = $this->settings->has('message_product_logo') ? + $this->settings->get('message_product_logo') : 'primary'; + $logoPosition = $this->settings->has('message_product_position') ? + $this->settings->get('message_product_position') : 'left'; + $textColor = $this->settings->has('message_product_color') ? + $this->settings->get('message_product_color') : 'black'; + $styleColor = $this->settings->has('message_product_flex_color') ? + $this->settings->get('message_product_flex_color') : 'blue'; + $ratio = $this->settings->has('message_product_flex_ratio') ? + $this->settings->get('message_product_flex_ratio') : '1x1'; + $shouldShow = $this->settings->has('message_product_enabled') + && $this->settings->get('message_product_enabled'); if (is_checkout()) { $placement = 'payment'; $amount = WC()->cart->get_total('raw'); - $layout = $this->settings->has('message_layout') ? $this->settings->get('message_layout') : 'text'; - $logoType = $this->settings->has('message_logo') ? $this->settings->get('message_logo') : 'primary'; - $logoPosition = $this->settings->has('message_position') ? $this->settings->get('message_position') : 'left'; - $textColor = $this->settings->has('message_color') ? $this->settings->get('message_color') : 'black'; - $styleColor = $this->settings->has('message_flex_color') ? $this->settings->get('message_flex_color') : 'blue'; - $ratio = $this->settings->has('message_flex_ratio') ? $this->settings->get('message_flex_ratio') : '1x1'; - $shouldShow = $this->settings->has('message_enabled') && $this->settings->get('message_enabled'); + $layout = $this->settings->has('message_layout') ? + $this->settings->get('message_layout') : 'text'; + $logoType = $this->settings->has('message_logo') ? + $this->settings->get('message_logo') : 'primary'; + $logoPosition = $this->settings->has('message_position') ? + $this->settings->get('message_position') : 'left'; + $textColor = $this->settings->has('message_color') ? + $this->settings->get('message_color') : 'black'; + $styleColor = $this->settings->has('message_flex_color') ? + $this->settings->get('message_flex_color') : 'blue'; + $ratio = $this->settings->has('message_flex_ratio') ? + $this->settings->get('message_flex_ratio') : '1x1'; + $shouldShow = $this->settings->has('message_enabled') + && $this->settings->get('message_enabled'); } if (is_cart()) { $placement = 'cart'; $amount = WC()->cart->get_total('raw'); - $layout = $this->settings->has('message_cart_layout') ? $this->settings->get('message_cart_layout') : 'text'; - $logoType = $this->settings->has('message_cart_logo') ? $this->settings->get('message_cart_logo') : 'primary'; - $logoPosition = $this->settings->has('message_cart_position') ? $this->settings->get('message_cart_position') : 'left'; - $textColor = $this->settings->has('message_cart_color') ? $this->settings->get('message_cart_color') : 'black'; - $styleColor = $this->settings->has('message_cart_flex_color') ? $this->settings->get('message_cart_flex_color') : 'blue'; - $ratio = $this->settings->has('message_cart_flex_ratio') ? $this->settings->get('message_cart_flex_ratio') : '1x1'; - $shouldShow = $this->settings->has('message_cart_enabled') && $this->settings->get('message_cart_enabled'); + $layout = $this->settings->has('message_cart_layout') ? + $this->settings->get('message_cart_layout') : 'text'; + $logoType = $this->settings->has('message_cart_logo') ? + $this->settings->get('message_cart_logo') : 'primary'; + $logoPosition = $this->settings->has('message_cart_position') ? + $this->settings->get('message_cart_position') : 'left'; + $textColor = $this->settings->has('message_cart_color') ? + $this->settings->get('message_cart_color') : 'black'; + $styleColor = $this->settings->has('message_cart_flex_color') ? + $this->settings->get('message_cart_flex_color') : 'blue'; + $ratio = $this->settings->has('message_cart_flex_ratio') ? + $this->settings->get('message_cart_flex_ratio') : '1x1'; + $shouldShow = $this->settings->has('message_cart_enabled') + && $this->settings->get('message_cart_enabled'); } if (! $shouldShow) { @@ -235,7 +258,7 @@ class SmartButton implements SmartButtonInterface 'data-pp-message' => '', 'data-pp-placement' => $placement, 'data-pp-amount' => $amount, - 'data-pp-style-layout' => $layout + 'data-pp-style-layout' => $layout, ]; if ($layout === 'text') { $attributes['data-pp-style-logo-type'] = $logoType; @@ -253,7 +276,6 @@ class SmartButton implements SmartButtonInterface echo '>'; } - // phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong public function dccRenderer() { @@ -433,7 +455,8 @@ class SmartButton implements SmartButtonInterface //ToDo: Update date on releases. 'integration-date' => date('Y-m-d'), 'components' => implode(',', $this->components()), - 'vault' => (is_checkout() && $this->dccIsEnabled()) || $this->canSaveVaultToken() ? 'true' : 'false', + 'vault' => (is_checkout() && $this->dccIsEnabled()) || $this->canSaveVaultToken() ? + 'true' : 'false', 'commit' => is_checkout() ? 'true' : 'false', 'intent' => ($this->settings->has('intent')) ? $this->settings->get('intent') : 'capture', ]; diff --git a/modules.local/ppcp-wc-gateway/src/Checkout/CheckoutPayPalAddressPreset.php b/modules.local/ppcp-wc-gateway/src/Checkout/CheckoutPayPalAddressPreset.php index 9e8cf094c..86f8926d1 100644 --- a/modules.local/ppcp-wc-gateway/src/Checkout/CheckoutPayPalAddressPreset.php +++ b/modules.local/ppcp-wc-gateway/src/Checkout/CheckoutPayPalAddressPreset.php @@ -38,11 +38,11 @@ class CheckoutPayPalAddressPreset */ public function filterCheckoutFiled($defaultValue, $fieldId): ?string { - if(! is_string($defaultValue)) { + if (! is_string($defaultValue)) { $defaultValue = null; } - if(!is_string($fieldId)) { + if (!is_string($fieldId)) { return $defaultValue; } @@ -51,7 +51,8 @@ class CheckoutPayPalAddressPreset private function readPresetForField(string $fieldId): ?string { - if(!$order = $this->sessionHandler->order()) { + $order = $this->sessionHandler->order(); + if (! $order) { return null; } @@ -64,7 +65,7 @@ class CheckoutPayPalAddressPreset 'billing_postcode' => 'postalCode', 'billing_country' => 'countryCode', 'billing_city' => 'adminArea2', - 'billing_state' => 'adminArea1' + 'billing_state' => 'adminArea1', ]; $payerNameMap = [ 'billing_last_name' => 'surname', @@ -77,19 +78,24 @@ class CheckoutPayPalAddressPreset 'billing_phone' => 'nationalNumber', ]; - if(array_key_exists($fieldId, $addressMap) && $shipping) { + if (array_key_exists($fieldId, $addressMap) && $shipping) { return $shipping->address()->{$addressMap[$fieldId]}() ?: null; } - if(array_key_exists($fieldId, $payerNameMap) && $payer) { + if (array_key_exists($fieldId, $payerNameMap) && $payer) { return $payer->name()->{$payerNameMap[$fieldId]}() ?: null; } - if(array_key_exists($fieldId, $payerMap) && $payer) { + if (array_key_exists($fieldId, $payerMap) && $payer) { return $payer->{$payerMap[$fieldId]}() ?: null; } - if(array_key_exists($fieldId, $payerPhoneMap) && $payer && $payer->phone() && $payer->phone()->phone()) { + if ( + array_key_exists($fieldId, $payerPhoneMap) + && $payer + && $payer->phone() + && $payer->phone()->phone() + ) { return $payer->phone()->phone()->{$payerPhoneMap[$fieldId]}() ?: null; } @@ -98,17 +104,19 @@ class CheckoutPayPalAddressPreset private function readShippingFromOrder(): ?Shipping { - if(!$order = $this->sessionHandler->order()) { + $order = $this->sessionHandler->order(); + if (! $order) { return null; } - if(array_key_exists($order->id(), $this->shippingCache)) { + if (array_key_exists($order->id(), $this->shippingCache)) { return $this->shippingCache[$order->id()]; } $shipping = null; - foreach($this->sessionHandler->order()->purchaseUnits() as $unit) { - if($shipping = $unit->shipping()) { + foreach ($this->sessionHandler->order()->purchaseUnits() as $unit) { + $shipping = $unit->shipping(); + if ($shipping) { break; } } diff --git a/modules.local/ppcp-wc-gateway/src/Checkout/DisableGateways.php b/modules.local/ppcp-wc-gateway/src/Checkout/DisableGateways.php index 28b311bb2..29174ad52 100644 --- a/modules.local/ppcp-wc-gateway/src/Checkout/DisableGateways.php +++ b/modules.local/ppcp-wc-gateway/src/Checkout/DisableGateways.php @@ -41,7 +41,6 @@ class DisableGateways unset($methods[CreditCardGateway::ID]); } - if (! $this->needsToDisableGateways()) { return $methods; } @@ -57,13 +56,13 @@ class DisableGateways return $this->sessionHandler->order() !== null; } - private function isCreditCard() : bool + private function isCreditCard(): bool { - $order =$this->sessionHandler->order(); + $order = $this->sessionHandler->order(); if (! $order) { return false; } - if ( ! $order->paymentSource() || ! $order->paymentSource()->card()) { + if (! $order->paymentSource() || ! $order->paymentSource()->card()) { return false; } return true; diff --git a/modules.local/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php b/modules.local/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php index e624a51ad..b14618c46 100644 --- a/modules.local/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php +++ b/modules.local/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php @@ -1,20 +1,21 @@ settings->reset(); $this->settings->persist(); @@ -57,6 +58,7 @@ class SettingsListener } //phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + //phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotValidated /** * Sanitization is done at a later stage. */ @@ -77,21 +79,34 @@ class SettingsListener if ($this->cache->has(PayPalBearer::CACHE_KEY)) { $this->cache->delete(PayPalBearer::CACHE_KEY); } + //phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotValidated + //phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + //phpcs:enable WordPress.Security.NonceVerification.Missing } //phpcs:disable Inpsyde.CodeQuality.NestingLevel.MaxExceeded //phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh + //phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong private function retrieveSettingsFromRawData(array $rawData): array { + if (! isset($_GET['section'])) { + return []; + } $settings = []; foreach ($this->settingFields as $key => $config) { if (! in_array($this->state->currentState(), $config['screens'], true)) { continue; } - if ($config['gateway'] === 'dcc' && wp_unslash(sanitize_text_field($_GET['section'])) !== 'ppcp-credit-card-gateway') { + if ( + $config['gateway'] === 'dcc' + && sanitize_text_field(wp_unslash($_GET['section'])) !== 'ppcp-credit-card-gateway' + ) { continue; } - if ($config['gateway'] === 'paypal' && wp_unslash(sanitize_text_field($_GET['section'])) !== 'ppcp-gateway') { + if ( + $config['gateway'] === 'paypal' + && sanitize_text_field(wp_unslash($_GET['section'])) !== 'ppcp-gateway' + ) { continue; } switch ($config['type']) { @@ -141,7 +156,11 @@ class SettingsListener if ( ! isset($_REQUEST['section']) - || ! in_array(sanitize_text_field(wp_unslash($_REQUEST['section'])), ['ppcp-gateway', 'ppcp-credit-card-gateway'],true) + || ! in_array( + sanitize_text_field(wp_unslash($_REQUEST['section'])), + ['ppcp-gateway', 'ppcp-credit-card-gateway'], + true + ) ) { return false; } diff --git a/modules.local/ppcp-wc-gateway/src/Settings/SettingsRenderer.php b/modules.local/ppcp-wc-gateway/src/Settings/SettingsRenderer.php index ce693cd2a..27c2257c2 100644 --- a/modules.local/ppcp-wc-gateway/src/Settings/SettingsRenderer.php +++ b/modules.local/ppcp-wc-gateway/src/Settings/SettingsRenderer.php @@ -134,10 +134,10 @@ class SettingsRenderer if (! in_array($this->state->currentState(), $config['screens'], true)) { continue; } - if ($isDcc && ! in_array($config['gateway'], ['all', 'dcc'],true)) { + if ($isDcc && ! in_array($config['gateway'], ['all', 'dcc'], true)) { continue; } - if (! $isDcc && ! in_array($config['gateway'], ['all', 'paypal'],true)) { + if (! $isDcc && ! in_array($config['gateway'], ['all', 'paypal'], true)) { continue; } if (in_array('dcc', $config['requirements'], true) && ! $this->dccApplies->forCountryCurrency()) { @@ -164,10 +164,10 @@ class SettingsRenderer endif; ?> - < colspan=""> colspan="">renderText($config) - : woocommerce_form_field($id, $config, $value); ?>> + : woocommerce_form_field($id, $config, $value); ?>> registerOrderFunctionality($container); $this->registerColumns($container); $this->registerCheckoutAddressPreset($container); + $this->ajaxGatewayEnabler($container); add_filter( Repository::NOTICES_FILTER, @@ -58,7 +59,16 @@ class WcGatewayModule implements ModuleInterface return $notices; } ); + add_action( + 'woocommerce-paypal-commerce-gateway.deactivate', + static function () use ($container) { + delete_option(Settings::KEY); + } + ); + } + private function ajaxGatewayEnabler(ContainerInterface $container) + { add_action( 'wp_ajax_woocommerce_toggle_gateway_enabled', static function () use ($container) { @@ -78,7 +88,7 @@ class WcGatewayModule implements ModuleInterface */ $settings = $container->get('wcgateway.settings'); $key = $_POST['gateway_id'] === PayPalGateway::ID ? 'enabled' : ''; - if ($_POST['gateway_id'] === CreditCardGateway::ID ) { + if ($_POST['gateway_id'] === CreditCardGateway::ID) { $key = 'dcc_gateway_enabled'; } if (! $key) { @@ -93,12 +103,6 @@ class WcGatewayModule implements ModuleInterface }, 9 ); - add_action( - 'woocommerce-paypal-commerce-gateway.deactivate', - static function () use ($container) { - delete_option(Settings::KEY); - } - ); } private function registerPaymentGateWay(ContainerInterface $container) @@ -224,9 +228,13 @@ class WcGatewayModule implements ModuleInterface add_filter( 'woocommerce_checkout_get_value', static function (...$args) use ($container) { - // It's important to not instantiate the service to early - // as it depends on SessionHandler and WooCommerce Session - /* @var CheckoutPayPalAddressPreset $service */ + + // Its important to not instantiate the service too early as it + // depends on SessionHandler and WooCommerce Session + + /** + * @var CheckoutPayPalAddressPreset $service + */ $service = $container->get('wcgateway.checkout.address-preset'); return $service->filterCheckoutFiled(...$args);