From bb03a4148070fdf30f86ed6ec95734bed58289db Mon Sep 17 00:00:00 2001
From: Philipp Stracker
Date: Wed, 26 Mar 2025 17:43:57 +0100
Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=8E=A8=20Apply=20phpcs=20rules=20to?=
=?UTF-8?q?=20the=20Cache=20class?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
modules/ppcp-api-client/src/Helper/Cache.php | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/modules/ppcp-api-client/src/Helper/Cache.php b/modules/ppcp-api-client/src/Helper/Cache.php
index 3b831ea00..2ae82a115 100644
--- a/modules/ppcp-api-client/src/Helper/Cache.php
+++ b/modules/ppcp-api-client/src/Helper/Cache.php
@@ -5,7 +5,7 @@
* @package WooCommerce\PayPalCommerce\ApiClient\Helper
*/
-declare( strict_types=1 );
+declare( strict_types = 1 );
namespace WooCommerce\PayPalCommerce\ApiClient\Helper;
@@ -48,8 +48,9 @@ class Cache {
*
* @return bool
*/
- public function has( string $key ): bool {
+ public function has( string $key ) : bool {
$value = $this->get( $key );
+
return false !== $value;
}
@@ -58,20 +59,20 @@ class Cache {
*
* @param string $key The key.
*/
- public function delete( string $key ): void {
+ public function delete( string $key ) : void {
delete_transient( $this->prefix . $key );
}
/**
* Caches a value.
*
- * @param string $key The key under which the value should be cached.
- * @param mixed $value The value to cache.
+ * @param string $key The key under which the value should be cached.
+ * @param mixed $value The value to cache.
* @param int $expiration Time until expiration in seconds.
*
* @return bool
*/
- public function set( string $key, $value, int $expiration = 0 ): bool {
+ public function set( string $key, $value, int $expiration = 0 ) : bool {
return (bool) set_transient( $this->prefix . $key, $value, $expiration );
}
}
From c89c60277634e701cc41a9ef80ccd685bb48ccf9 Mon Sep 17 00:00:00 2001
From: Philipp Stracker
Date: Wed, 26 Mar 2025 17:44:31 +0100
Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=A8=20Add=20a=20new=20=E2=80=9Cflush?=
=?UTF-8?q?=E2=80=9D=20API=20method=20to=20the=20cache=20system?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
modules/ppcp-api-client/src/Helper/Cache.php | 27 ++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/modules/ppcp-api-client/src/Helper/Cache.php b/modules/ppcp-api-client/src/Helper/Cache.php
index 2ae82a115..e77f78825 100644
--- a/modules/ppcp-api-client/src/Helper/Cache.php
+++ b/modules/ppcp-api-client/src/Helper/Cache.php
@@ -75,4 +75,31 @@ class Cache {
public function set( string $key, $value, int $expiration = 0 ) : bool {
return (bool) set_transient( $this->prefix . $key, $value, $expiration );
}
+
+ /**
+ * Flushes all items of the current "cache group", i.e., items that use the defined prefix.
+ *
+ * @return void
+ */
+ public function flush() : void {
+ global $wpdb;
+
+ // Get a list of all transients with the relevant "group prefix" from the DB.
+ $transients = $wpdb->get_col(
+ $wpdb->prepare(
+ "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s",
+ $wpdb->esc_like( '_transient_' . $this->prefix ) . '%'
+ )
+ );
+
+ /**
+ * Delete each cache item individually to ensure WP can fire all relevant
+ * actions, perform checks and other cleanup tasks and ensures eventually
+ * object cache systems, like Redis, are kept in-sync with the DB.
+ */
+ foreach ( $transients as $transient ) {
+ $key = str_replace( '_transient_' . $this->prefix, '', $transient );
+ $this->delete( $key );
+ }
+ }
}
From 37532fa0dd140599b17353df6bf0c60719fa8ecc Mon Sep 17 00:00:00 2001
From: Philipp Stracker
Date: Wed, 26 Mar 2025 17:55:11 +0100
Subject: [PATCH 3/3] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20cache=20flu?=
=?UTF-8?q?shing=20to=20clear=20entire=20groups?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
modules/ppcp-api-client/src/ApiModule.php | 17 +++++------------
1 file changed, 5 insertions(+), 12 deletions(-)
diff --git a/modules/ppcp-api-client/src/ApiModule.php b/modules/ppcp-api-client/src/ApiModule.php
index 12cdd12a6..be55b951e 100644
--- a/modules/ppcp-api-client/src/ApiModule.php
+++ b/modules/ppcp-api-client/src/ApiModule.php
@@ -113,23 +113,16 @@ class ApiModule implements ServiceModule, ExtendingModule, ExecutableModule {
'woocommerce_paypal_payments_flush_api_cache',
static function () use ( $c ) {
$caches = array(
- 'api.paypal-bearer-cache' => array(
- PayPalBearer::CACHE_KEY,
- ),
- 'api.client-credentials-cache' => array(
- SdkClientToken::CACHE_KEY,
- ),
+ 'api.paypal-bearer-cache',
+ 'api.client-credentials-cache',
+ 'settings.service.signup-link-cache',
);
- foreach ( $caches as $cache_id => $keys ) {
+ foreach ( $caches as $cache_id ) {
$cache = $c->get( $cache_id );
assert( $cache instanceof Cache );
- foreach ( $keys as $key ) {
- if ( $cache->has( $key ) ) {
- $cache->delete( $key );
- }
- }
+ $cache->flush();
}
}
);