| 1 |
<?php |
| 2 |
|
| 3 |
namespace KaizenCoders\UpdateURLS; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Cache |
| 7 |
* |
| 8 |
* @since 1.2 |
| 9 |
*/ |
| 10 |
class Cache { |
| 11 |
|
| 12 |
/** |
| 13 |
* Cache version |
| 14 |
* |
| 15 |
* @var string |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
static $version = '1.2'; |
| 20 |
|
| 21 |
static $prefix = 'kc_uu_'; |
| 22 |
|
| 23 |
/** @var bool */ |
| 24 |
static $enabled = true; |
| 25 |
|
| 26 |
/** |
| 27 |
* Get prefix |
| 28 |
* |
| 29 |
* @return string |
| 30 |
*/ |
| 31 |
static function get_prefix() { |
| 32 |
return self::$prefix . str_replace('.', '', self::$version) . '_'; |
| 33 |
} |
| 34 |
/** |
| 35 |
* @return mixed|void |
| 36 |
*/ |
| 37 |
static function get_default_transient_expiration() { |
| 38 |
return apply_filters( 'cache_default_expiration', 10 ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @param $key |
| 43 |
* @param $value |
| 44 |
* @param bool $expiration |
| 45 |
* |
| 46 |
* @return bool |
| 47 |
*/ |
| 48 |
static function set_transient( $key, $value, $expiration = false ) { |
| 49 |
if ( ! self::$enabled ) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
if ( ! $expiration ) { |
| 53 |
$expiration = self::get_default_transient_expiration(); |
| 54 |
} |
| 55 |
|
| 56 |
return set_transient( self::get_prefix() . 'cache_' . $key, $value, $expiration ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* @param string $key |
| 61 |
* |
| 62 |
* @return bool|mixed |
| 63 |
* |
| 64 |
* @since 1.2 |
| 65 |
*/ |
| 66 |
static function get_transient( $key ) { |
| 67 |
if ( ! self::$enabled ) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
return get_transient( self::get_prefix() . 'cache_' . $key ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @param $key |
| 76 |
* |
| 77 |
* @since 1.2 |
| 78 |
*/ |
| 79 |
static function delete_transient( $key ) { |
| 80 |
delete_transient( self::get_prefix() . 'cache_' . $key ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Only sets if key is not falsy |
| 85 |
* |
| 86 |
* @param string $key |
| 87 |
* @param mixed $value |
| 88 |
* @param string $group |
| 89 |
* |
| 90 |
* @since 1.2 |
| 91 |
*/ |
| 92 |
static function set( $key, $value, $group ) { |
| 93 |
if ( ! $key ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
wp_cache_set( (string) $key, $value, self::get_prefix() . $group ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Only gets if key is not falsy |
| 102 |
* |
| 103 |
* @param string $key |
| 104 |
* @param string $group |
| 105 |
* |
| 106 |
* @return bool|mixed |
| 107 |
* |
| 108 |
* @since 1.2 |
| 109 |
*/ |
| 110 |
static function get( $key, $group ) { |
| 111 |
if ( ! $key ) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
return wp_cache_get( (string) $key, self::get_prefix() . $group ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @param string $key |
| 120 |
* @param string $group |
| 121 |
* |
| 122 |
* @return bool |
| 123 |
* |
| 124 |
* @since 1.2 |
| 125 |
*/ |
| 126 |
static function exists( $key, $group ) { |
| 127 |
if ( ! $key ) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
$found = false; |
| 131 |
wp_cache_get( (string) $key, self::get_prefix() . $group, false, $found ); |
| 132 |
|
| 133 |
return $found; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* Only deletes if key is not falsy |
| 139 |
* |
| 140 |
* @param string $key |
| 141 |
* @param string $group |
| 142 |
* |
| 143 |
* @since 1.2 |
| 144 |
*/ |
| 145 |
static function delete( $key, $group ) { |
| 146 |
if ( ! $key ) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
wp_cache_delete( (string) $key, self::get_prefix() . $group ); |
| 151 |
} |
| 152 |
|
| 153 |
} |