| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if ( ! class_exists( 'MywpTransient' ) ) : |
| 8 |
|
| 9 |
final class MywpTransient { |
| 10 |
|
| 11 |
private $id = false; |
| 12 |
private $transient_id = false; |
| 13 |
private $type = false; |
| 14 |
private $network = false; |
| 15 |
|
| 16 |
private $option_key = false; |
| 17 |
private $option_timeout_key = false; |
| 18 |
|
| 19 |
private $trainsient_key = false; |
| 20 |
|
| 21 |
private $pre_get_data = false; |
| 22 |
private $transient = false; |
| 23 |
private $change_get_data = false; |
| 24 |
|
| 25 |
public function __construct( $transient_id = false , $type = false , $network = false ) { |
| 26 |
|
| 27 |
if( empty( $transient_id ) ) { |
| 28 |
|
| 29 |
$called_text = sprintf( 'new %s( %s , %s , %s )' , __CLASS__ , '$transient_id' , '$type' , '$network' ); |
| 30 |
|
| 31 |
MywpHelper::error_require_message( '$transient_id' , $called_text ); |
| 32 |
|
| 33 |
return false; |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
$this->transient_id = strip_tags( $transient_id ); |
| 38 |
|
| 39 |
if( empty( $type ) ) { |
| 40 |
|
| 41 |
$called_text = sprintf( 'new %s( %s , %s , %s )' , __CLASS__ , '$transient_id' , '$type' , '$network' ); |
| 42 |
|
| 43 |
MywpHelper::error_require_message( '$type' , $called_text ); |
| 44 |
|
| 45 |
return false; |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
$this->type = strip_tags( $type ); |
| 50 |
|
| 51 |
$this->id = "mywp_{$this->transient_id}"; |
| 52 |
|
| 53 |
if( ! empty( $network ) ) { |
| 54 |
|
| 55 |
$this->network = true; |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
if( $this->network ) { |
| 60 |
|
| 61 |
$this->option_key = "_site_transient_{$this->id}"; |
| 62 |
$this->option_timeout_key = "_site_transient_timeout_{$this->id}"; |
| 63 |
|
| 64 |
} else { |
| 65 |
|
| 66 |
$this->option_key = "_transient_{$this->id}"; |
| 67 |
$this->option_timeout_key = "_transient_timeout_{$this->id}"; |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
public function get_id() { |
| 74 |
|
| 75 |
return $this->id; |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
public function get_type() { |
| 80 |
|
| 81 |
return $this->type; |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
public function is_network() { |
| 86 |
|
| 87 |
if( ! empty( $this->network ) ) { |
| 88 |
|
| 89 |
return true; |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
return false; |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
public function get_option_key() { |
| 98 |
|
| 99 |
$id = $this->get_id(); |
| 100 |
|
| 101 |
if( empty( $id ) ) { |
| 102 |
|
| 103 |
return false; |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
$this->option_key = apply_filters( "mywp_transient_get_option_key_{$id}" , $id , $this->get_type() , $this->is_network() ); |
| 108 |
|
| 109 |
return $this->option_key; |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
public function get_option_timeout_key() { |
| 114 |
|
| 115 |
$id = $this->get_id(); |
| 116 |
|
| 117 |
if( empty( $id ) ) { |
| 118 |
|
| 119 |
return false; |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
$this->option_timeout_key = apply_filters( "mywp_transient_get_option_timeout_key_{$id}" , $id , $this->get_type() , $this->is_network() ); |
| 124 |
|
| 125 |
return $this->option_timeout_key; |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
public function get_transient_key() { |
| 130 |
|
| 131 |
$id = $this->get_id(); |
| 132 |
|
| 133 |
if( empty( $id ) ) { |
| 134 |
|
| 135 |
return false; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
$this->trainsient_key = apply_filters( "mywp_transient_get_key_{$id}" , $this->id , $this->get_type() , $this->is_network() ); |
| 140 |
|
| 141 |
return $this->trainsient_key; |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
public function get_data() { |
| 146 |
|
| 147 |
$transient_key = $this->get_transient_key(); |
| 148 |
|
| 149 |
if( empty( $transient_key ) ) { |
| 150 |
|
| 151 |
$called_text = sprintf( '(object) %s->%s()' , __CLASS__ , __FUNCTION__ ); |
| 152 |
|
| 153 |
MywpHelper::error_not_found_message( '$transient_key' , $called_text ); |
| 154 |
|
| 155 |
return false; |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
do_action( 'mywp_transient_before_get_data' , $transient_key , $this->get_type() , $this->is_network() ); |
| 160 |
|
| 161 |
$this->pre_get_data = apply_filters( "mywp_transient_pre_get_data_{$transient_key}" , false , $this->get_type() , $this->is_network() ); |
| 162 |
|
| 163 |
if( $this->pre_get_data !== false ) { |
| 164 |
|
| 165 |
return $this->pre_get_data; |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
if( $this->network ) { |
| 170 |
|
| 171 |
$this->transient = get_site_transient( $transient_key ); |
| 172 |
|
| 173 |
} else { |
| 174 |
|
| 175 |
$this->transient = get_transient( $transient_key ); |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
$change_get_data = apply_filters( "mywp_transient_change_get_data_{$transient_key}" , $this->transient , $this->get_type() , $this->is_network() ); |
| 180 |
|
| 181 |
do_action( 'mywp_transient_after_get_data' , $this->transient , $change_get_data , $transient_key , $this->get_type() , $this->is_network() ); |
| 182 |
|
| 183 |
if( $change_get_data !== $this->transient ) { |
| 184 |
|
| 185 |
$this->change_get_data = $change_get_data; |
| 186 |
|
| 187 |
return $this->change_get_data; |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
return $this->transient; |
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
public function update_data( $update_data , $timeout = HOUR_IN_SECONDS ) { |
| 196 |
|
| 197 |
$transient_key = $this->get_transient_key(); |
| 198 |
|
| 199 |
if( empty( $transient_key ) ) { |
| 200 |
|
| 201 |
$called_text = sprintf( '(object) %s->%s( %s , %s )' , __CLASS__ , __FUNCTION__ , '$update_data' , '$timeout' ); |
| 202 |
|
| 203 |
MywpHelper::error_not_found_message( '$transient_key' , $called_text ); |
| 204 |
|
| 205 |
return false; |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
do_action( "mywp_transient_before_update_data_{$transient_key}" , $update_data , $this->get_type() , $this->is_network() ); |
| 210 |
|
| 211 |
if( $this->is_network() ) { |
| 212 |
|
| 213 |
$return = set_site_transient( $transient_key , $update_data , $timeout ); |
| 214 |
|
| 215 |
} else { |
| 216 |
|
| 217 |
$return = set_transient( $transient_key , $update_data , $timeout ); |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
do_action( "mywp_transient_after_update_data_{$transient_key}" , $return , $update_data , $this->get_type() , $this->is_network() ); |
| 222 |
|
| 223 |
return $return; |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
public function remove_data() { |
| 228 |
|
| 229 |
$transient_key = $this->get_transient_key(); |
| 230 |
|
| 231 |
if( empty( $transient_key ) ) { |
| 232 |
|
| 233 |
$called_text = sprintf( '(object) %s->%s()' , __CLASS__ , __FUNCTION__ ); |
| 234 |
|
| 235 |
MywpHelper::error_not_found_message( '$transient_key' , $called_text ); |
| 236 |
|
| 237 |
return false; |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
do_action( "mywp_transient_before_remove_data_{$transient_key}" , $this->get_type() , $this->is_network() ); |
| 242 |
|
| 243 |
if( $this->is_network() ) { |
| 244 |
|
| 245 |
$return = delete_site_transient( $transient_key ); |
| 246 |
|
| 247 |
} else { |
| 248 |
|
| 249 |
$return = delete_transient( $transient_key ); |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
do_action( "mywp_transient_after_remove_data_{$transient_key}" , $return , $this->get_type() , $this->is_network() ); |
| 254 |
|
| 255 |
return $return; |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
endif; |
| 262 |
|