Skip to content

Instantly share code, notes, and snippets.

@monkeymonk
Last active March 7, 2022 12:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkeymonk/d3f2fa6c577efdb329f13733179acf8c to your computer and use it in GitHub Desktop.
Save monkeymonk/d3f2fa6c577efdb329f13733179acf8c to your computer and use it in GitHub Desktop.
Wordpress Transient Helper Class #wordpress
<?php
/**
* Helpers to work with Wordpress transient API
*
* @example
* $foo = Cache::get('foo', function () {
* // query DB or other complicated stuff...
* return $results;
* }, 24 * 60 * 60);
*
* @example
* function clearTransientCache() {
* Cache::clear();
* }
* // clear cache when a post is create or updated
* add_action('save_post', 'clearTransientCache');
*/
class Cache {
public static function clear()
{
// @see http://isabelcastillo.com/delete-all-transients-wordpress
global $wpdb;
$sql = 'DELETE FROM `' . $wpdb->options . '` WHERE `option_name` LIKE ("%\_transient\_%")';
if ($wpdb->query($sql)) {
return true;
}
return false;
}
/**
* @param string $uid - Unique identifier (default: null)
* @return bool
*/
public static function delete($uid = null)
{
if (!is_null($uid)) {
// @see http://codex.wordpress.org/Function_Reference/delete_transient
return delete_transient($uid);
}
return self::clear();
}
/**
* @param string $uid - Unique identifier
* @param $default - Callback or value if missing (default: null)
* @param int $expiration - Expiration in second (default: 0)
* @return mixed
*/
public static function get($uid, $default = null, $expiration = 0)
{
// @see http://codex.wordpress.org/Function_Reference/get_transient
$value = get_transient($uid);
if ($value === false) {
if (is_callable($default)) {
$value = $default($uid);
} else {
$value = $default;
}
self::set($uid, $value, $expiration);
}
return $value;
}
/**
* @param string $uid - Unique identifier
* @param $value
* @param int $expiration - Expiration in second (default: 0)
* @return bool
*/
public static function set($uid, $value, $expiration = 0)
{
// @see http://codex.wordpress.org/Function_Reference/set_transient
return set_transient($uid, $value, $expiration);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment