Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions inc/sso/class-wordpress-simple-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,16 @@
* @return mixed The value of the item from the cache, or $default in case of cache miss.
* @throws InvalidArgumentException If $key is not a legal value.
*/
public function get($key, $default = null) {

Check warning on line 58 in inc/sso/class-wordpress-simple-cache.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

It is recommended not to use reserved keyword "default" as function parameter name. Found: $default
$this->validateKey($key);
$value = get_transient($this->prefix . $key);
return false !== $value ? $value : $default;
$raw = get_site_transient($this->prefix . $key);

// Check if key exists by looking for wrapped structure.
if (false === $raw || ! is_array($raw) || ! array_key_exists('v', $raw)) {
return $default;
}

return $raw['v'];
}

/**
Expand All @@ -75,7 +81,8 @@
public function set($key, $value, $ttl = null) {
$this->validateKey($key);
$expiration = $this->ttlToSeconds($ttl);
return set_transient($this->prefix . $key, $value, $expiration);
// Wrap value in array to distinguish false values from missing keys.
return set_site_transient($this->prefix . $key, array('v' => $value), $expiration);
}

/**
Expand All @@ -89,7 +96,7 @@
*/
public function delete($key) {
$this->validateKey($key);
return delete_transient($this->prefix . $key);
return delete_site_transient($this->prefix . $key);
}

/**
Expand All @@ -106,21 +113,21 @@
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s",
$wpdb->esc_like('_transient_' . $this->prefix) . '%'
"SELECT meta_key FROM {$wpdb->sitemeta} WHERE meta_key LIKE %s",
$wpdb->esc_like('_site_transient_' . $this->prefix) . '%'
)
);
// phpcs:enable

if (!is_array($results)) {
if (! is_array($results)) {
return false;
}

$success = true;
foreach ($results as $result) {
// Extract the transient name from option_name
$transient_name = str_replace('_transient_', '', $result->option_name);
if (!delete_transient($transient_name)) {
// Extract the transient name from meta_key.
$transient_name = str_replace('_site_transient_', '', $result->meta_key);
if (! delete_site_transient($transient_name)) {
$success = false;
}
}
Expand All @@ -137,15 +144,15 @@
* @param mixed $default Default value for missing keys.
* @return iterable A list of key => value pairs.
* @throws InvalidArgumentException If $keys is not iterable or contains illegal values.
*/

Check failure on line 147 in inc/sso/class-wordpress-simple-cache.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Missing @throws tag for "\Exception" exception
public function getMultiple($keys, $default = null) {

Check warning on line 148 in inc/sso/class-wordpress-simple-cache.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

It is recommended not to use reserved keyword "default" as function parameter name. Found: $default
if (!is_array($keys) && !($keys instanceof \Traversable)) {

Check failure on line 149 in inc/sso/class-wordpress-simple-cache.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Expected 1 space after "!"; 0 found

Check failure on line 149 in inc/sso/class-wordpress-simple-cache.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Expected 1 space after "!"; 0 found
throw new class('Keys must be an array or Traversable') extends \Exception implements InvalidArgumentException {};
}

$result = [];
foreach ($keys as $key) {
$result[$key] = $this->get($key, $default);

Check failure on line 155 in inc/sso/class-wordpress-simple-cache.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Array keys must be surrounded by spaces unless they contain a string or an integer.
}

return $result;
Expand All @@ -160,15 +167,15 @@
* @param null|int|\DateInterval $ttl Optional. The TTL value in seconds.
* @return bool True on success, false on failure.
* @throws InvalidArgumentException If $values is not iterable or contains illegal values.
*/

Check failure on line 170 in inc/sso/class-wordpress-simple-cache.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Missing @throws tag for "\Exception" exception
public function setMultiple($values, $ttl = null) {
if (!is_array($values) && !($values instanceof \Traversable)) {

Check failure on line 172 in inc/sso/class-wordpress-simple-cache.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Expected 1 space after "!"; 0 found

Check failure on line 172 in inc/sso/class-wordpress-simple-cache.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Expected 1 space after "!"; 0 found
throw new class('Values must be an array or Traversable') extends \Exception implements InvalidArgumentException {};
}

$success = true;
foreach ($values as $key => $value) {
if (!$this->set($key, $value, $ttl)) {

Check failure on line 178 in inc/sso/class-wordpress-simple-cache.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Expected 1 space after "!"; 0 found
$success = false;
}
}
Expand All @@ -184,9 +191,9 @@
* @param iterable $keys A list of keys to delete.
* @return bool True if all removed, false otherwise.
* @throws InvalidArgumentException If $keys is not iterable or contains illegal values.
*/

Check failure on line 194 in inc/sso/class-wordpress-simple-cache.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Missing @throws tag for "\Exception" exception
public function deleteMultiple($keys) {
if (!is_array($keys) && !($keys instanceof \Traversable)) {

Check failure on line 196 in inc/sso/class-wordpress-simple-cache.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Expected 1 space after "!"; 0 found
throw new class('Keys must be an array or Traversable') extends \Exception implements InvalidArgumentException {};
}

Expand All @@ -211,7 +218,8 @@
*/
public function has($key) {
$this->validateKey($key);
return false !== get_transient($this->prefix . $key);
$raw = get_site_transient($this->prefix . $key);
return false !== $raw && is_array($raw) && array_key_exists('v', $raw);
}

/**
Expand All @@ -231,7 +239,7 @@
throw new class('Cache key cannot be empty') extends \Exception implements InvalidArgumentException {};
}

// PSR-16 reserved characters: {}()/\@:

Check warning on line 242 in inc/sso/class-wordpress-simple-cache.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

This comment is 47% valid code; is this commented out code?
if (preg_match('/[{}()\/\\@:]/', $key)) {
throw new class('Cache key contains reserved characters') extends \Exception implements InvalidArgumentException {};
}
Expand Down
Loading
Loading