From e8cee60072f2d7dd9143d29e299a7d842db7b521 Mon Sep 17 00:00:00 2001
From: Philipp Stracker
Date: Thu, 13 Feb 2025 19:43:24 +0100
Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Introduce=20better=20WC=5F?=
=?UTF-8?q?Gateway=20caching?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../src/Data/PaymentSettings.php | 20 +++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/modules/ppcp-settings/src/Data/PaymentSettings.php b/modules/ppcp-settings/src/Data/PaymentSettings.php
index 891872805..ac80e66fe 100644
--- a/modules/ppcp-settings/src/Data/PaymentSettings.php
+++ b/modules/ppcp-settings/src/Data/PaymentSettings.php
@@ -76,7 +76,7 @@ class PaymentSettings extends AbstractDataModel {
break;
default:
- $gateway = WC()->payment_gateways()->payment_gateways()[ $method_id ] ?? null;
+ $gateway = $this->get_gateway( $method_id );
if ( $gateway ) {
$gateway->enabled = wc_bool_to_string( $is_enabled );
@@ -103,7 +103,7 @@ class PaymentSettings extends AbstractDataModel {
break;
default:
- $gateway = WC()->payment_gateways()->payment_gateways()[ $method_id ] ?? null;
+ $gateway = $this->get_gateway( $method_id );
if ( $gateway ) {
return wc_string_to_bool( $gateway->enabled );
@@ -226,4 +226,20 @@ class PaymentSettings extends AbstractDataModel {
public function set_paylater_enabled( bool $value ) : void {
$this->data['paylater_enabled'] = $value;
}
+
+ /**
+ * Get the gateway object for the given method ID.
+ *
+ * @param string $method_id ID of the payment method.
+ * @return WC_Payment_Gateway|null
+ */
+ private function get_gateway( string $method_id ) : ?WC_Payment_Gateway {
+ if ( isset( $this->unsaved_gateways[ $method_id ] ) ) {
+ return $this->unsaved_gateways[ $method_id ];
+ }
+
+ $gateways = WC()->payment_gateways()->payment_gateways();
+
+ return isset( $gateways[ $method_id ] ) ? $gateways[ $method_id ] : null;
+ }
}