From 4ae0b4f7913c6e7e657ae4895561237ab5bc5acf Mon Sep 17 00:00:00 2001
From: Philipp Stracker
Date: Mon, 24 Feb 2025 15:53:26 +0100
Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Extract=20REST=20detection=20to?=
=?UTF-8?q?=20helper=20method?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../LocalAlternativePaymentMethodsModule.php | 21 ++++++++++++-------
1 file changed, 14 insertions(+), 7 deletions(-)
diff --git a/modules/ppcp-local-alternative-payment-methods/src/LocalAlternativePaymentMethodsModule.php b/modules/ppcp-local-alternative-payment-methods/src/LocalAlternativePaymentMethodsModule.php
index 5e635d01a..cc58eb791 100644
--- a/modules/ppcp-local-alternative-payment-methods/src/LocalAlternativePaymentMethodsModule.php
+++ b/modules/ppcp-local-alternative-payment-methods/src/LocalAlternativePaymentMethodsModule.php
@@ -233,7 +233,7 @@ class LocalAlternativePaymentMethodsModule implements ServiceModule, ExtendingMo
* @return bool
*/
private function should_add_local_apm_gateways( ContainerInterface $container ) : bool {
- // Merchant onboarding must be completed.
+ // APMs are only available after merchant onboarding is completed.
$is_connected = $container->get( 'settings.flag.is-connected' );
if ( ! $is_connected ) {
/**
@@ -243,13 +243,8 @@ class LocalAlternativePaymentMethodsModule implements ServiceModule, ExtendingMo
* During the authentication process (which happens via a REST call)
* the gateways need to be present, so they can be correctly
* pre-configured for new merchants.
- *
- * TODO is there a cleaner solution for this?
*/
- // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
- $request_uri = wp_unslash( $_SERVER['REQUEST_URI'] ?? '' );
-
- return str_contains( $request_uri, '/wp-json/wc/' );
+ return $this->is_rest_request();
}
// The general plugin functionality must be enabled.
@@ -263,4 +258,16 @@ class LocalAlternativePaymentMethodsModule implements ServiceModule, ExtendingMo
return $settings->has( 'allow_local_apm_gateways' )
&& $settings->get( 'allow_local_apm_gateways' ) === true;
}
+
+ /**
+ * Checks, whether the current request is trying to access a WooCommerce REST endpoint.
+ *
+ * @return bool True, if the request path matches the WC-Rest namespace.
+ */
+ private function is_rest_request(): bool {
+ // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
+ $request_uri = wp_unslash( $_SERVER['REQUEST_URI'] ?? '' );
+
+ return str_contains( $request_uri, '/wp-json/wc/' );
+ }
}