| 1 |
<?php |
| 2 |
/** |
| 3 |
* class-groups-cache-object.php |
| 4 |
* |
| 5 |
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com |
| 6 |
* |
| 7 |
* This code is released under the GNU General Public License. |
| 8 |
* See COPYRIGHT.txt and LICENSE.txt. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* This header and all notices must be kept intact. |
| 16 |
* |
| 17 |
* @author Karim Rahimpur |
| 18 |
* @package groups |
| 19 |
* @since groups 1.9.0 |
| 20 |
*/ |
| 21 |
|
| 22 |
if ( !defined( 'ABSPATH' ) ) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Cache entry encapsulation. |
| 28 |
* |
| 29 |
* @property string $key |
| 30 |
* @property mixed $value |
| 31 |
*/ |
| 32 |
class Groups_Cache_Object { |
| 33 |
|
| 34 |
/** |
| 35 |
* Cache key. |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
private $key = null; |
| 40 |
|
| 41 |
/** |
| 42 |
* Cached value. |
| 43 |
* |
| 44 |
* @var mixed |
| 45 |
*/ |
| 46 |
private $value = null; |
| 47 |
|
| 48 |
/** |
| 49 |
* Timestamp. |
| 50 |
* |
| 51 |
* @since 4.0.0 |
| 52 |
* |
| 53 |
* @var int |
| 54 |
*/ |
| 55 |
private $created = null; |
| 56 |
|
| 57 |
/** |
| 58 |
* Expiration. |
| 59 |
* |
| 60 |
* @since 4.0.0 |
| 61 |
* |
| 62 |
* @var int |
| 63 |
*/ |
| 64 |
private $expires = null; |
| 65 |
|
| 66 |
/** |
| 67 |
* Create a cache entry object that holds a value for the given key. |
| 68 |
* |
| 69 |
* @param string $key |
| 70 |
* @param mixed $value |
| 71 |
* @param int $expires |
| 72 |
*/ |
| 73 |
public function __construct( $key, $value, $expires = null ) { |
| 74 |
if ( is_string( $key ) ) { |
| 75 |
$this->key = $key; |
| 76 |
} |
| 77 |
$this->value = $value; |
| 78 |
$this->created = time(); |
| 79 |
$this->expires = is_numeric( $expires ) ? max( 0, intval( $expires ) ) : null; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Getter implementation for key and value properties. |
| 84 |
* |
| 85 |
* @deprecated use proper getters |
| 86 |
* |
| 87 |
* @param string $name |
| 88 |
* |
| 89 |
* @return mixed property value or null |
| 90 |
*/ |
| 91 |
public function __get( $name ) { |
| 92 |
$result = null; |
| 93 |
switch ( $name ) { |
| 94 |
case 'key' : |
| 95 |
case 'value' : |
| 96 |
$result = $this->$name; |
| 97 |
break; |
| 98 |
} |
| 99 |
return $result; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Setter for key and value properties. |
| 104 |
* |
| 105 |
* @deprecated use proper setters |
| 106 |
* |
| 107 |
* @param string $name |
| 108 |
* @param mixed $value |
| 109 |
*/ |
| 110 |
public function __set( $name, $value ) { |
| 111 |
switch ( $name ) { |
| 112 |
case 'key' : |
| 113 |
if ( is_string( $value ) ) { |
| 114 |
$this->key = $value; |
| 115 |
} |
| 116 |
break; |
| 117 |
case 'value' : |
| 118 |
$this->value = $value; |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Provide the key of this object. |
| 125 |
* |
| 126 |
* @since 3.6.0 |
| 127 |
* |
| 128 |
* @return string |
| 129 |
*/ |
| 130 |
public function get_key() { |
| 131 |
return $this->key; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Set the key of this object. |
| 136 |
* |
| 137 |
* @since 3.6.0 |
| 138 |
* |
| 139 |
* @param string $key |
| 140 |
*/ |
| 141 |
public function set_key( $key ) { |
| 142 |
if ( is_string( $key ) ) { |
| 143 |
$this->key = $key; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Provide the value of this object. |
| 149 |
* |
| 150 |
* @since 3.6.0 |
| 151 |
* |
| 152 |
* @return mixed |
| 153 |
*/ |
| 154 |
public function get_value() { |
| 155 |
return $this->value; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Set the value of this object. |
| 160 |
* |
| 161 |
* @since 3.6.0 |
| 162 |
* |
| 163 |
* @param mixed $value |
| 164 |
*/ |
| 165 |
public function set_value( $value ) { |
| 166 |
$this->value = $value; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Set expires. |
| 171 |
* |
| 172 |
* @since 4.0.0 |
| 173 |
* |
| 174 |
* @param int|null $expires |
| 175 |
*/ |
| 176 |
public function set_expires( $expires ) { |
| 177 |
$this->expires = is_numeric( $expires ) ? max( 0, intval( $expires ) ) : null; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Provide expires. |
| 182 |
* |
| 183 |
* @since 4.0.0 |
| 184 |
* |
| 185 |
* @return int|null |
| 186 |
*/ |
| 187 |
public function get_expires() { |
| 188 |
return $this->expires; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Expired. |
| 193 |
* |
| 194 |
* @since 4.0.0 |
| 195 |
* |
| 196 |
* @return boolean |
| 197 |
*/ |
| 198 |
public function has_expired() { |
| 199 |
$expired = false; |
| 200 |
// auto-invalidate objects without timestamp |
| 201 |
if ( $this->created === null ) { |
| 202 |
$expired = true; |
| 203 |
} else { |
| 204 |
if ( $this->expires !== null ) { |
| 205 |
if ( time() > ( $this->created + $this->expires ) ) { |
| 206 |
$expired = true; |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
return $expired; |
| 211 |
} |
| 212 |
} |
| 213 |
|