Admin
8 years ago
Ajax
8 years ago
Asset
8 years ago
Customizer
8 years ago
Documentation
8 years ago
Duplicate
8 years ago
Image
8 years ago
JSON_LD
8 years ago
Languages
8 years ago
Log
8 years ago
Meta
8 years ago
PUE
8 years ago
Process
8 years ago
REST
8 years ago
Service_Providers
8 years ago
Support
8 years ago
Tabbed_View
8 years ago
Utils
8 years ago
Validator
8 years ago
Abstract_Deactivation.php
8 years ago
App_Shop.php
8 years ago
Assets.php
8 years ago
Assets_Pipeline.php
8 years ago
Autoloader.php
8 years ago
Cache.php
8 years ago
Cache_Listener.php
8 years ago
Changelog_Reader.php
8 years ago
Container.php
8 years ago
Context.php
8 years ago
Cost_Utils.php
8 years ago
Credits.php
8 years ago
Customizer.php
8 years ago
Data.php
8 years ago
Date_Utils.php
8 years ago
Debug.php
8 years ago
Dependency.php
8 years ago
Deprecation.php
8 years ago
Error.php
8 years ago
Exception.php
8 years ago
Extension.php
8 years ago
Extension_Loader.php
8 years ago
Field.php
8 years ago
Field_Conditional.php
8 years ago
Log.php
8 years ago
Main.php
8 years ago
Notices.php
8 years ago
Plugin_Meta_Links.php
8 years ago
Plugins.php
8 years ago
Plugins_API.php
8 years ago
Post_History.php
8 years ago
Post_Transient.php
8 years ago
Rewrite.php
8 years ago
Settings.php
8 years ago
Settings_Manager.php
8 years ago
Settings_Tab.php
8 years ago
Simple_Table.php
8 years ago
Support.php
8 years ago
Tabbed_View.php
8 years ago
Template.php
8 years ago
Template_Factory.php
8 years ago
Template_Part_Cache.php
8 years ago
Templates.php
8 years ago
Terms.php
8 years ago
Timezones.php
8 years ago
Tracker.php
8 years ago
Validate.php
8 years ago
View_Helpers.php
8 years ago
Cache.php
253 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Manage setting and expiring cached data |
| 5 | * |
| 6 | * Select actions can be used to force cached |
| 7 | * data to expire. Implemented so far: |
| 8 | * - save_post |
| 9 | * |
| 10 | * When used in its ArrayAccess API the cache will provide non persistent storage. |
| 11 | */ |
| 12 | class Tribe__Cache implements ArrayAccess { |
| 13 | const NO_EXPIRATION = 0; |
| 14 | const NON_PERSISTENT = - 1; |
| 15 | |
| 16 | /** |
| 17 | * @var array |
| 18 | */ |
| 19 | protected $non_persistent_keys = array(); |
| 20 | |
| 21 | public static function setup() { |
| 22 | wp_cache_add_non_persistent_groups( array( 'tribe-events-non-persistent' ) ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @param string $id |
| 27 | * @param mixed $value |
| 28 | * @param int $expiration |
| 29 | * @param string $expiration_trigger |
| 30 | * |
| 31 | * @return bool |
| 32 | */ |
| 33 | public function set( $id, $value, $expiration = 0, $expiration_trigger = '' ) { |
| 34 | $key = $this->get_id( $id, $expiration_trigger ); |
| 35 | |
| 36 | if ( $expiration == self::NON_PERSISTENT ) { |
| 37 | $group = 'tribe-events-non-persistent'; |
| 38 | $this->non_persistent_keys[] = $key; |
| 39 | $expiration = 1; |
| 40 | } else { |
| 41 | $group = 'tribe-events'; |
| 42 | } |
| 43 | |
| 44 | return wp_cache_set( $key, $value, $group, $expiration ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param $id |
| 49 | * @param $value |
| 50 | * @param int $expiration |
| 51 | * @param string $expiration_trigger |
| 52 | * |
| 53 | * @return bool |
| 54 | */ |
| 55 | public function set_transient( $id, $value, $expiration = 0, $expiration_trigger = '' ) { |
| 56 | return set_transient( $this->get_id( $id, $expiration_trigger ), $value, $expiration ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get cached data. Optionally set data if not previously set. |
| 61 | * |
| 62 | * Note: When a default value or callback is specified, this value gets set in the cache. |
| 63 | * |
| 64 | * @param string $id The key for the cached value. |
| 65 | * @param string $expiration_trigger Optional. Hook to trigger cache invalidation. |
| 66 | * @param mixed $default Optional. A default value or callback that returns a default value. |
| 67 | * @param int $expiration Optional. When the default value expires, if it gets set. |
| 68 | * @param mixed $args Optional. Args passed to callback. |
| 69 | * |
| 70 | * @return mixed |
| 71 | */ |
| 72 | public function get( $id, $expiration_trigger = '', $default = false, $expiration = 0, $args = array() ) { |
| 73 | $group = in_array( $id, $this->non_persistent_keys ) ? 'tribe-events-non-persistent' : 'tribe-events'; |
| 74 | $value = wp_cache_get( $this->get_id( $id, $expiration_trigger ), $group ); |
| 75 | |
| 76 | // Value found. |
| 77 | if ( false !== $value ) { |
| 78 | return $value; |
| 79 | } |
| 80 | |
| 81 | if ( is_callable( $default ) ) { |
| 82 | // A callback has been specified. |
| 83 | $value = call_user_func_array( $default, $args ); |
| 84 | } else { |
| 85 | // Default is a value. |
| 86 | $value = $default; |
| 87 | } |
| 88 | |
| 89 | // No need to set a cache value to false since non-existent values return false. |
| 90 | if ( false !== $value ) { |
| 91 | $this->set( $id, $value, $expiration, $expiration_trigger ); |
| 92 | } |
| 93 | |
| 94 | return $value; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @param string $id |
| 99 | * @param string $expiration_trigger |
| 100 | * |
| 101 | * @return mixed |
| 102 | */ |
| 103 | public function get_transient( $id, $expiration_trigger = '' ) { |
| 104 | return get_transient( $this->get_id( $id, $expiration_trigger ) ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @param string $id |
| 109 | * @param string $expiration_trigger |
| 110 | * |
| 111 | * @return bool |
| 112 | */ |
| 113 | public function delete( $id, $expiration_trigger = '' ) { |
| 114 | return wp_cache_delete( $this->get_id( $id, $expiration_trigger ), 'tribe-events' ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @param string $id |
| 119 | * @param string $expiration_trigger |
| 120 | * |
| 121 | * @return bool |
| 122 | */ |
| 123 | public function delete_transient( $id, $expiration_trigger = '' ) { |
| 124 | return delete_transient( $this->get_id( $id, $expiration_trigger ) ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param string $key |
| 129 | * @param string $expiration_trigger |
| 130 | * |
| 131 | * @return string |
| 132 | */ |
| 133 | public function get_id( $key, $expiration_trigger = '' ) { |
| 134 | $last = empty( $expiration_trigger ) ? '' : $this->get_last_occurrence( $expiration_trigger ); |
| 135 | $id = $key . $last; |
| 136 | if ( strlen( $id ) > 40 ) { |
| 137 | $id = md5( $id ); |
| 138 | } |
| 139 | |
| 140 | return $id; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @param string $action |
| 145 | * |
| 146 | * @return int |
| 147 | */ |
| 148 | public function get_last_occurrence( $action ) { |
| 149 | return (int) get_option( 'tribe_last_' . $action, time() ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @param string $action |
| 154 | * @param int $timestamp |
| 155 | */ |
| 156 | public function set_last_occurrence( $action, $timestamp = 0 ) { |
| 157 | if ( empty( $timestamp ) ) { |
| 158 | $timestamp = time(); |
| 159 | } |
| 160 | update_option( 'tribe_last_' . $action, (int) $timestamp ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Builds a key from an array of components and an optional prefix. |
| 165 | * |
| 166 | * @param mixed $components Either a single component of the key or an array of key components. |
| 167 | * @param string $prefix |
| 168 | * @param bool $sort Whether component arrays should be sorted or not to generate the key; defaults to `true`. |
| 169 | * |
| 170 | * @return string The resulting key. |
| 171 | */ |
| 172 | public function make_key( $components, $prefix = '', $sort = true ) { |
| 173 | $key = ''; |
| 174 | $components = is_array( $components ) ? $components : array( $components ); |
| 175 | foreach ( $components as $component ) { |
| 176 | if ( $sort && is_array( $component ) ) { |
| 177 | $is_associative = count( array_filter( array_keys( $component ), 'is_numeric' ) ) < count( array_keys( $component ) ); |
| 178 | if ( $is_associative ) { |
| 179 | ksort( $component ); |
| 180 | } else { |
| 181 | sort( $component ); |
| 182 | } |
| 183 | } |
| 184 | $key .= maybe_serialize( $component ); |
| 185 | } |
| 186 | |
| 187 | return $this->get_id( $prefix . md5( $key ) ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Whether a offset exists |
| 192 | * |
| 193 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
| 194 | * @param mixed $offset <p> |
| 195 | * An offset to check for. |
| 196 | * </p> |
| 197 | * @return boolean true on success or false on failure. |
| 198 | * </p> |
| 199 | * <p> |
| 200 | * The return value will be casted to boolean if non-boolean was returned. |
| 201 | * @since 5.0.0 |
| 202 | */ |
| 203 | public function offsetExists( $offset ) { |
| 204 | return in_array( $offset, $this->non_persistent_keys ); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Offset to retrieve |
| 209 | * |
| 210 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
| 211 | * @param mixed $offset <p> |
| 212 | * The offset to retrieve. |
| 213 | * </p> |
| 214 | * @return mixed Can return all value types. |
| 215 | * @since 5.0.0 |
| 216 | */ |
| 217 | public function offsetGet( $offset ) { |
| 218 | return $this->get( $offset ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Offset to set |
| 223 | * |
| 224 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
| 225 | * @param mixed $offset <p> |
| 226 | * The offset to assign the value to. |
| 227 | * </p> |
| 228 | * @param mixed $value <p> |
| 229 | * The value to set. |
| 230 | * </p> |
| 231 | * @return void |
| 232 | * @since 5.0.0 |
| 233 | */ |
| 234 | public function offsetSet( $offset, $value ) { |
| 235 | $this->set( $offset, $value, self::NON_PERSISTENT ); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Offset to unset |
| 240 | * |
| 241 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
| 242 | * @param mixed $offset <p> |
| 243 | * The offset to unset. |
| 244 | * </p> |
| 245 | * @return void |
| 246 | * @since 5.0.0 |
| 247 | */ |
| 248 | public function offsetUnset( $offset ) { |
| 249 | $this->delete( $offset ); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 |