Merge branch 'trunk' into PCP-4439-check-the-branded-only-flag-when-settings-ui-is-loaded-the-first-time

This commit is contained in:
Emili Castells Guasch 2025-03-28 14:39:03 +01:00
commit c77f3266e8
2 changed files with 39 additions and 18 deletions

View file

@ -113,23 +113,16 @@ class ApiModule implements ServiceModule, ExtendingModule, ExecutableModule {
'woocommerce_paypal_payments_flush_api_cache', 'woocommerce_paypal_payments_flush_api_cache',
static function () use ( $c ) { static function () use ( $c ) {
$caches = array( $caches = array(
'api.paypal-bearer-cache' => array( 'api.paypal-bearer-cache',
PayPalBearer::CACHE_KEY, 'api.client-credentials-cache',
), 'settings.service.signup-link-cache',
'api.client-credentials-cache' => array(
SdkClientToken::CACHE_KEY,
),
); );
foreach ( $caches as $cache_id => $keys ) { foreach ( $caches as $cache_id ) {
$cache = $c->get( $cache_id ); $cache = $c->get( $cache_id );
assert( $cache instanceof Cache ); assert( $cache instanceof Cache );
foreach ( $keys as $key ) { $cache->flush();
if ( $cache->has( $key ) ) {
$cache->delete( $key );
}
}
} }
} }
); );

View file

@ -5,7 +5,7 @@
* @package WooCommerce\PayPalCommerce\ApiClient\Helper * @package WooCommerce\PayPalCommerce\ApiClient\Helper
*/ */
declare( strict_types=1 ); declare( strict_types = 1 );
namespace WooCommerce\PayPalCommerce\ApiClient\Helper; namespace WooCommerce\PayPalCommerce\ApiClient\Helper;
@ -48,8 +48,9 @@ class Cache {
* *
* @return bool * @return bool
*/ */
public function has( string $key ): bool { public function has( string $key ) : bool {
$value = $this->get( $key ); $value = $this->get( $key );
return false !== $value; return false !== $value;
} }
@ -58,20 +59,47 @@ class Cache {
* *
* @param string $key The key. * @param string $key The key.
*/ */
public function delete( string $key ): void { public function delete( string $key ) : void {
delete_transient( $this->prefix . $key ); delete_transient( $this->prefix . $key );
} }
/** /**
* Caches a value. * Caches a value.
* *
* @param string $key The key under which the value should be cached. * @param string $key The key under which the value should be cached.
* @param mixed $value The value to cache. * @param mixed $value The value to cache.
* @param int $expiration Time until expiration in seconds. * @param int $expiration Time until expiration in seconds.
* *
* @return bool * @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 ); 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 );
}
}
} }