Admin
1 month ago
OptimiseAssets
1 month ago
ThirdParty
1 month ago
AdminBar.php
1 month ago
AssetsManager.php
1 month ago
BulkChanges.php
1 month ago
CleanUp.php
1 month ago
Debug.php
1 month ago
FileSystem.php
1 month ago
HardcodedAssets.php
1 month ago
Lite.php
1 month ago
Main.php
1 month ago
MainFront.php
1 month ago
Maintenance.php
1 month ago
Menu.php
1 month ago
MetaBoxes.php
1 month ago
Misc.php
1 month ago
ObjectCache.php
1 month ago
OwnAssets.php
1 month ago
PluginNotifications.php
1 month ago
PluginTracking.php
1 month ago
Preloads.php
1 month ago
Settings.php
1 month ago
Tips.php
1 month ago
Update.php
1 month ago
ObjectCache.php
819 lines
| 1 | <?php |
| 2 | /** @noinspection ALL */ |
| 3 | /** @noinspection MultipleReturnStatementsInspection */ |
| 4 | |
| 5 | namespace WpAssetCleanUp; |
| 6 | |
| 7 | /** |
| 8 | * NOTE: This is from the original core file located /wp-includes/class-wp-object-cache.php |
| 9 | * Avoid the WordPress core $wp_object_cache global variable which is sometimes altered by 3rd party plugins |
| 10 | * This would make this plugin compatible with plugins such as "Redis Object Cache" |
| 11 | * |
| 12 | * Object Cache API: ObjectCache class |
| 13 | * |
| 14 | * @package WordPress |
| 15 | * @subpackage Cache |
| 16 | * @since 5.4.0 |
| 17 | */ |
| 18 | |
| 19 | /** |
| 20 | * Core class that implements an object cache. |
| 21 | * |
| 22 | * The WordPress Object Cache is used to save on trips to the database. The |
| 23 | * Object Cache stores all the cache data to memory and makes the cache |
| 24 | * contents available by using a key, which is used to name and later retrieve |
| 25 | * the cache contents. |
| 26 | * |
| 27 | * The Object Cache can be replaced by other caching mechanisms by placing files |
| 28 | * in the wp-content folder which is looked at in wp-settings. If that file |
| 29 | * exists, then this file will not be included. |
| 30 | * |
| 31 | * @since 2.0.0 |
| 32 | */ |
| 33 | class ObjectCache { |
| 34 | |
| 35 | /** |
| 36 | * Holds the cached objects. |
| 37 | * |
| 38 | * @since 2.0.0 |
| 39 | * @var array |
| 40 | */ |
| 41 | private $cache = array(); |
| 42 | |
| 43 | /** |
| 44 | * The amount of times the cache data was already stored in the cache. |
| 45 | * |
| 46 | * @since 2.5.0 |
| 47 | * @var int |
| 48 | */ |
| 49 | public $cache_hits = 0; |
| 50 | |
| 51 | /** |
| 52 | * Amount of times the cache did not have the request in cache. |
| 53 | * |
| 54 | * @since 2.0.0 |
| 55 | * @var int |
| 56 | */ |
| 57 | public $cache_misses = 0; |
| 58 | |
| 59 | /** |
| 60 | * List of global cache groups. |
| 61 | * |
| 62 | * @since 3.0.0 |
| 63 | * @var array |
| 64 | */ |
| 65 | protected $global_groups = array(); |
| 66 | |
| 67 | /** |
| 68 | * The blog prefix to prepend to keys in non-global groups. |
| 69 | * |
| 70 | * @since 3.5.0 |
| 71 | * @var string |
| 72 | */ |
| 73 | private $blog_prefix; |
| 74 | |
| 75 | /** |
| 76 | * Holds the value of is_multisite(). |
| 77 | * |
| 78 | * @since 3.5.0 |
| 79 | * @var bool |
| 80 | */ |
| 81 | private $multisite; |
| 82 | |
| 83 | /** |
| 84 | * @var string|void |
| 85 | */ |
| 86 | public static $objNotInitErrorMsg; |
| 87 | |
| 88 | /** |
| 89 | * Sets up object properties; PHP 5 style constructor. |
| 90 | * |
| 91 | * @since 2.0.8 |
| 92 | */ |
| 93 | public function __construct() { |
| 94 | self::$objNotInitErrorMsg = self::showTextDomainObjNotInitErrorMsg(); |
| 95 | |
| 96 | $this->multisite = is_multisite(); |
| 97 | $this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : ''; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @return string|null |
| 102 | */ |
| 103 | public static function showTextDomainObjNotInitErrorMsg() |
| 104 | { |
| 105 | if (did_action('after_setup_theme')) { |
| 106 | return __('Asset CleanUp\'s object cache is not valid (from method "[method]").', 'wp-asset-clean-up'); |
| 107 | } else { |
| 108 | return 'Asset CleanUp\'s object cache is not valid (from method "[method]").'; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Makes private properties readable for backward compatibility. |
| 114 | * |
| 115 | * @since 4.0.0 |
| 116 | * |
| 117 | * @param string $name Property to get. |
| 118 | * @return mixed Property. |
| 119 | */ |
| 120 | public function __get( $name ) { |
| 121 | return $this->$name; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Makes private properties settable for backward compatibility. |
| 126 | * |
| 127 | * @since 4.0.0 |
| 128 | * |
| 129 | * @param string $name Property to set. |
| 130 | * @param mixed $value Property value. |
| 131 | * @return mixed Newly-set property. |
| 132 | */ |
| 133 | public function __set( $name, $value ) { |
| 134 | return $this->$name = $value; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Makes private properties checkable for backward compatibility. |
| 139 | * |
| 140 | * @since 4.0.0 |
| 141 | * |
| 142 | * @param string $name Property to check if set. |
| 143 | * @return bool Whether the property is set. |
| 144 | */ |
| 145 | public function __isset( $name ) { |
| 146 | return isset( $this->$name ); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Makes private properties un-settable for backward compatibility. |
| 151 | * |
| 152 | * @since 4.0.0 |
| 153 | * |
| 154 | * @param string $name Property to unset. |
| 155 | */ |
| 156 | public function __unset( $name ) { |
| 157 | unset( $this->$name ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Adds data to the cache if it doesn't already exist. |
| 162 | * |
| 163 | * @since 2.0.0 |
| 164 | * |
| 165 | * @uses ObjectCache::_exists() Checks to see if the cache already has data. |
| 166 | * @uses ObjectCache::set() Sets the data after the checking the cache |
| 167 | * contents existence. |
| 168 | * |
| 169 | * @param int|string $key What to call the contents in the cache. |
| 170 | * @param mixed $data The contents to store in the cache. |
| 171 | * @param string $group Optional. Where to group the cache contents. Default 'default'. |
| 172 | * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration). |
| 173 | * @return bool True on success, false if cache key and group already exist. |
| 174 | */ |
| 175 | public function add( $key, $data, $group = 'default', $expire = 0 ) { |
| 176 | if ( wp_suspend_cache_addition() ) { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | if ( empty( $group ) ) { |
| 181 | $group = 'default'; |
| 182 | } |
| 183 | |
| 184 | $id = $key; |
| 185 | if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
| 186 | $id = $this->blog_prefix . $key; |
| 187 | } |
| 188 | |
| 189 | if ( $this->_exists( $id, $group ) ) { |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | return $this->set( $key, $data, $group, (int) $expire ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Sets the list of global cache groups. |
| 198 | * |
| 199 | * @since 3.0.0 |
| 200 | * |
| 201 | * @param array $groups List of groups that are global. |
| 202 | */ |
| 203 | public function add_global_groups( $groups ) { |
| 204 | $groups = (array) $groups; |
| 205 | |
| 206 | $groups = array_fill_keys( $groups, true ); |
| 207 | $this->global_groups = array_merge( $this->global_groups, $groups ); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Decrements numeric cache item's value. |
| 212 | * |
| 213 | * @since 3.3.0 |
| 214 | * |
| 215 | * @param int|string $key The cache key to decrement. |
| 216 | * @param int $offset Optional. The amount by which to decrement the item's value. Default 1. |
| 217 | * @param string $group Optional. The group the key is in. Default 'default'. |
| 218 | * @return int|false The item's new value on success, false on failure. |
| 219 | */ |
| 220 | public function decr( $key, $offset = 1, $group = 'default' ) { |
| 221 | if ( empty( $group ) ) { |
| 222 | $group = 'default'; |
| 223 | } |
| 224 | |
| 225 | if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
| 226 | $key = $this->blog_prefix . $key; |
| 227 | } |
| 228 | |
| 229 | if ( ! $this->_exists( $key, $group ) ) { |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) { |
| 234 | $this->cache[ $group ][ $key ] = 0; |
| 235 | } |
| 236 | |
| 237 | $offset = (int) $offset; |
| 238 | |
| 239 | $this->cache[ $group ][ $key ] -= $offset; |
| 240 | |
| 241 | if ( $this->cache[ $group ][ $key ] < 0 ) { |
| 242 | $this->cache[ $group ][ $key ] = 0; |
| 243 | } |
| 244 | |
| 245 | return $this->cache[ $group ][ $key ]; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Removes the contents of the cache key in the group. |
| 250 | * |
| 251 | * If the cache key does not exist in the group, then nothing will happen. |
| 252 | * |
| 253 | * @since 2.0.0 |
| 254 | * |
| 255 | * @param int|string $key What the contents in the cache are called. |
| 256 | * @param string $group Optional. Where the cache contents are grouped. Default 'default'. |
| 257 | * @param bool $deprecated Optional. Unused. Default false. |
| 258 | * @return bool False if the contents weren't deleted and true on success. |
| 259 | */ |
| 260 | public function delete( $key, $group = 'default', $deprecated = false ) { |
| 261 | if ( empty( $group ) ) { |
| 262 | $group = 'default'; |
| 263 | } |
| 264 | |
| 265 | if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
| 266 | $key = $this->blog_prefix . $key; |
| 267 | } |
| 268 | |
| 269 | if ( ! $this->_exists( $key, $group ) ) { |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | unset( $this->cache[ $group ][ $key ] ); |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Clears the object cache of all data. |
| 279 | * |
| 280 | * @since 2.0.0 |
| 281 | * |
| 282 | * @return true Always returns true. |
| 283 | */ |
| 284 | public function flush() { |
| 285 | $this->cache = array(); |
| 286 | |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Retrieves the cache contents, if it exists. |
| 292 | * |
| 293 | * The contents will be first attempted to be retrieved by searching by the |
| 294 | * key in the cache group. If the cache is hit (success) then the contents |
| 295 | * are returned. |
| 296 | * |
| 297 | * On failure, the number of cache misses will be incremented. |
| 298 | * |
| 299 | * @since 2.0.0 |
| 300 | * |
| 301 | * @param int|string $key What the contents in the cache are called. |
| 302 | * @param string $group Optional. Where the cache contents are grouped. Default 'default'. |
| 303 | * @param bool $force Optional. Unused. Whether to force a refetch rather than relying on the local |
| 304 | * cache. Default false. |
| 305 | * @param bool $found Optional. Whether the key was found in the cache (passed by reference). |
| 306 | * Disambiguates a return of false, a storable value. Default null. |
| 307 | * @return mixed|false The cache contents on success, false on failure to retrieve contents. |
| 308 | */ |
| 309 | public function get( $key, $group = 'default', $force = false, &$found = null ) { |
| 310 | if ( empty( $group ) ) { |
| 311 | $group = 'default'; |
| 312 | } |
| 313 | |
| 314 | if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
| 315 | $key = $this->blog_prefix . $key; |
| 316 | } |
| 317 | |
| 318 | if ( $this->_exists( $key, $group ) ) { |
| 319 | $found = true; |
| 320 | $this->cache_hits += 1; |
| 321 | if ( is_object( $this->cache[ $group ][ $key ] ) ) { |
| 322 | return clone $this->cache[ $group ][ $key ]; |
| 323 | } else { |
| 324 | return $this->cache[ $group ][ $key ]; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | $found = false; |
| 329 | $this->cache_misses += 1; |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Increments numeric cache item's value. |
| 335 | * |
| 336 | * @since 3.3.0 |
| 337 | * |
| 338 | * @param int|string $key The cache key to increment |
| 339 | * @param int $offset Optional. The amount by which to increment the item's value. Default 1. |
| 340 | * @param string $group Optional. The group the key is in. Default 'default'. |
| 341 | * @return int|false The item's new value on success, false on failure. |
| 342 | */ |
| 343 | public function incr( $key, $offset = 1, $group = 'default' ) { |
| 344 | if ( empty( $group ) ) { |
| 345 | $group = 'default'; |
| 346 | } |
| 347 | |
| 348 | if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
| 349 | $key = $this->blog_prefix . $key; |
| 350 | } |
| 351 | |
| 352 | if ( ! $this->_exists( $key, $group ) ) { |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) { |
| 357 | $this->cache[ $group ][ $key ] = 0; |
| 358 | } |
| 359 | |
| 360 | $offset = (int) $offset; |
| 361 | |
| 362 | $this->cache[ $group ][ $key ] += $offset; |
| 363 | |
| 364 | if ( $this->cache[ $group ][ $key ] < 0 ) { |
| 365 | $this->cache[ $group ][ $key ] = 0; |
| 366 | } |
| 367 | |
| 368 | return $this->cache[ $group ][ $key ]; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Replaces the contents in the cache, if contents already exist. |
| 373 | * |
| 374 | * @since 2.0.0 |
| 375 | * |
| 376 | * @see ObjectCache::set() |
| 377 | * |
| 378 | * @param int|string $key What to call the contents in the cache. |
| 379 | * @param mixed $data The contents to store in the cache. |
| 380 | * @param string $group Optional. Where to group the cache contents. Default 'default'. |
| 381 | * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration). |
| 382 | * @return bool False if not exists, true if contents were replaced. |
| 383 | */ |
| 384 | public function replace( $key, $data, $group = 'default', $expire = 0 ) { |
| 385 | if ( empty( $group ) ) { |
| 386 | $group = 'default'; |
| 387 | } |
| 388 | |
| 389 | $id = $key; |
| 390 | if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
| 391 | $id = $this->blog_prefix . $key; |
| 392 | } |
| 393 | |
| 394 | if ( ! $this->_exists( $id, $group ) ) { |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | return $this->set( $key, $data, $group, (int) $expire ); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Resets cache keys. |
| 403 | * |
| 404 | * @since 3.0.0 |
| 405 | * |
| 406 | * @deprecated 3.5.0 Use switch_to_blog() |
| 407 | * @see switch_to_blog() |
| 408 | */ |
| 409 | public function reset() { |
| 410 | _deprecated_function( __FUNCTION__, '3.5.0', 'switch_to_blog()' ); |
| 411 | |
| 412 | // Clear out non-global caches since the blog ID has changed. |
| 413 | foreach ( array_keys( $this->cache ) as $group ) { |
| 414 | if ( ! isset( $this->global_groups[ $group ] ) ) { |
| 415 | unset( $this->cache[ $group ] ); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Sets the data contents into the cache. |
| 422 | * |
| 423 | * The cache contents are grouped by the $group parameter followed by the |
| 424 | * $key. This allows for duplicate ids in unique groups. Therefore, naming of |
| 425 | * the group should be used with care and should follow normal function |
| 426 | * naming guidelines outside of core WordPress usage. |
| 427 | * |
| 428 | * The $expire parameter is not used, because the cache will automatically |
| 429 | * expire for each time a page is accessed and PHP finishes. The method is |
| 430 | * more for cache plugins which use files. |
| 431 | * |
| 432 | * @since 2.0.0 |
| 433 | * |
| 434 | * @param int|string $key What to call the contents in the cache. |
| 435 | * @param mixed $data The contents to store in the cache. |
| 436 | * @param string $group Optional. Where to group the cache contents. Default 'default'. |
| 437 | * @param int $expire Not Used. |
| 438 | * @return true Always returns true. |
| 439 | */ |
| 440 | public function set( $key, $data, $group = 'default', $expire = 0 ) { |
| 441 | if ( empty( $group ) ) { |
| 442 | $group = 'default'; |
| 443 | } |
| 444 | |
| 445 | if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
| 446 | $key = $this->blog_prefix . $key; |
| 447 | } |
| 448 | |
| 449 | if ( is_object( $data ) ) { |
| 450 | $data = clone $data; |
| 451 | } |
| 452 | |
| 453 | $this->cache[ $group ][ $key ] = $data; |
| 454 | return true; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Echoes the stats of the caching. |
| 459 | * |
| 460 | * Gives the cache hits, and cache misses. Also prints every cached group, |
| 461 | * key and the data. |
| 462 | * |
| 463 | * @since 2.0.0 |
| 464 | */ |
| 465 | public function stats() { |
| 466 | echo '<p>'; |
| 467 | echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />"; |
| 468 | echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />"; |
| 469 | echo '</p>'; |
| 470 | echo '<ul>'; |
| 471 | foreach ( $this->cache as $group => $cache ) { |
| 472 | echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>'; |
| 473 | } |
| 474 | echo '</ul>'; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Switches the internal blog ID. |
| 479 | * |
| 480 | * This changes the blog ID used to create keys in blog specific groups. |
| 481 | * |
| 482 | * @since 3.5.0 |
| 483 | * |
| 484 | * @param int $blog_id Blog ID. |
| 485 | */ |
| 486 | public function switch_to_blog( $blog_id ) { |
| 487 | $blog_id = (int) $blog_id; |
| 488 | $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Serves as a utility function to determine whether a key exists in the cache. |
| 493 | * |
| 494 | * @since 3.4.0 |
| 495 | * |
| 496 | * @param int|string $key Cache key to check for existence. |
| 497 | * @param string $group Cache group for the key existence check. |
| 498 | * @return bool Whether the key exists in the cache for the given group. |
| 499 | */ |
| 500 | protected function _exists( $key, $group ) { |
| 501 | return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) ); |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * [START] Functions to call (reference: /wp-includes/cache.php) |
| 506 | */ |
| 507 | /** |
| 508 | * Adds data to the cache, if the cache key doesn't already exist. |
| 509 | * |
| 510 | * @since 2.0.0 |
| 511 | * |
| 512 | * @see ObjectCache::add() |
| 513 | * @global ObjectCache $wpacu_object_cache Object cache global instance. |
| 514 | * |
| 515 | * @param int|string $key The cache key to use for retrieval later. |
| 516 | * @param mixed $data The data to add to the cache. |
| 517 | * @param string $group Optional. The group to add the cache to. Enables the same key |
| 518 | * to be used across groups. Default empty. |
| 519 | * @param int $expire Optional. When the cache data should expire, in seconds. |
| 520 | * Default 0 (no expiration). |
| 521 | * @return bool True on success, false if cache key and group already exist. |
| 522 | */ |
| 523 | public static function wpacu_cache_add( $key, $data, $group = '', $expire = 0 ) { |
| 524 | global $wpacu_object_cache; |
| 525 | |
| 526 | return $wpacu_object_cache->add( $key, $data, $group, (int) $expire ); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Closes the cache. |
| 531 | * |
| 532 | * This function has ceased to do anything since WordPress 2.5. The |
| 533 | * functionality was removed along with the rest of the persistent cache. |
| 534 | * |
| 535 | * This does not mean that plugins can't implement this function when they need |
| 536 | * to make sure that the cache is cleaned up after WordPress no longer needs it. |
| 537 | * |
| 538 | * @since 2.0.0 |
| 539 | * |
| 540 | * @return true Always returns true. |
| 541 | */ |
| 542 | public static function wpacu_cache_close() { |
| 543 | return true; |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Decrements numeric cache item's value. |
| 548 | * |
| 549 | * @since 3.3.0 |
| 550 | * |
| 551 | * @see ObjectCache::decr() |
| 552 | * @global ObjectCache $wpacu_object_cache Object cache global instance. |
| 553 | * |
| 554 | * @param int|string $key The cache key to decrement. |
| 555 | * @param int $offset Optional. The amount by which to decrement the item's value. Default 1. |
| 556 | * @param string $group Optional. The group the key is in. Default empty. |
| 557 | * @return int|false The item's new value on success, false on failure. |
| 558 | */ |
| 559 | public static function wpacu_cache_decr( $key, $offset = 1, $group = '' ) { |
| 560 | if ( ! self::ensureInitialized(__METHOD__) ) { return; } |
| 561 | |
| 562 | global $wpacu_object_cache; |
| 563 | |
| 564 | return $wpacu_object_cache->decr( $key, $offset, $group ); |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Removes the cache contents matching key and group. |
| 569 | * |
| 570 | * @since 2.0.0 |
| 571 | * |
| 572 | * @see ObjectCache::delete() |
| 573 | * @global ObjectCache $wpacu_object_cache Object cache global instance. |
| 574 | * |
| 575 | * @param int|string $key What the contents in the cache are called. |
| 576 | * @param string $group Optional. Where the cache contents are grouped. Default empty. |
| 577 | * @return bool True on successful removal, false on failure. |
| 578 | */ |
| 579 | public static function wpacu_cache_delete( $key, $group = '' ) { |
| 580 | if ( ! self::ensureInitialized(__METHOD__) ) { return; } |
| 581 | |
| 582 | global $wpacu_object_cache; |
| 583 | |
| 584 | return $wpacu_object_cache->delete( $key, $group ); |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Removes all cache items. |
| 589 | * |
| 590 | * @since 2.0.0 |
| 591 | * |
| 592 | * @see ObjectCache::flush() |
| 593 | * @global ObjectCache $wpacu_object_cache Object cache global instance. |
| 594 | * |
| 595 | * @return bool True on success, false on failure. |
| 596 | */ |
| 597 | public static function wpacu_cache_flush() { |
| 598 | if ( ! self::ensureInitialized(__METHOD__) ) { return; } |
| 599 | |
| 600 | global $wpacu_object_cache; |
| 601 | |
| 602 | return $wpacu_object_cache->flush(); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Retrieves the cache contents from the cache by key and group. |
| 607 | * |
| 608 | * @since 2.0.0 |
| 609 | * |
| 610 | * @see ObjectCache::get() |
| 611 | * @global ObjectCache $wpacu_object_cache Object cache global instance. |
| 612 | * |
| 613 | * @param int|string $key The key under which the cache contents are stored. |
| 614 | * @param string $group Optional. Where the cache contents are grouped. Default empty. |
| 615 | * @param bool $force Optional. Whether to force an update of the local cache from the persistent |
| 616 | * cache. Default false. |
| 617 | * @param bool $found Optional. Whether the key was found in the cache (passed by reference). |
| 618 | * Disambiguates a return of false, a storable value. Default null. |
| 619 | * @return bool|mixed False on failure to retrieve contents or the cache |
| 620 | * contents on success |
| 621 | */ |
| 622 | public static function wpacu_cache_get( $key, $group = '', $force = false, &$found = null ) { |
| 623 | if ( ! self::ensureInitialized(__METHOD__) ) { return; } |
| 624 | |
| 625 | global $wpacu_object_cache; |
| 626 | |
| 627 | return $wpacu_object_cache->get( $key, $group, $force, $found ); |
| 628 | } |
| 629 | |
| 630 | /** |
| 631 | * Increment numeric cache item's value |
| 632 | * |
| 633 | * @since 3.3.0 |
| 634 | * |
| 635 | * @see ObjectCache::incr() |
| 636 | * @global ObjectCache $wpacu_object_cache Object cache global instance. |
| 637 | * |
| 638 | * @param int|string $key The key for the cache contents that should be incremented. |
| 639 | * @param int $offset Optional. The amount by which to increment the item's value. Default 1. |
| 640 | * @param string $group Optional. The group the key is in. Default empty. |
| 641 | * @return int|false The item's new value on success, false on failure. |
| 642 | */ |
| 643 | public static function wpacu_cache_incr( $key, $offset = 1, $group = '' ) { |
| 644 | if ( ! self::ensureInitialized(__METHOD__) ) { return; } |
| 645 | |
| 646 | global $wpacu_object_cache; |
| 647 | |
| 648 | return $wpacu_object_cache->incr( $key, $offset, $group ); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * @param $callingMethod |
| 653 | * |
| 654 | * @return bool |
| 655 | */ |
| 656 | private static function ensureInitialized( $callingMethod = '' ) { |
| 657 | if ( self::isValidObjectCache() ) { |
| 658 | return true; |
| 659 | } |
| 660 | |
| 661 | // Fallback init attempt |
| 662 | self::wpacu_cache_init(); |
| 663 | |
| 664 | if ( ! self::isValidObjectCache() ) { |
| 665 | error_log(str_replace('[method]', $callingMethod, self::$objNotInitErrorMsg)); |
| 666 | return false; |
| 667 | } |
| 668 | |
| 669 | return true; |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * Sets up Object Cache Global and assigns it. |
| 674 | * |
| 675 | * @since 2.0.0 |
| 676 | * |
| 677 | * @global ObjectCache $wpacu_object_cache |
| 678 | */ |
| 679 | public static function wpacu_cache_init() { |
| 680 | $GLOBALS['wpacu_object_cache'] = new self(); |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Replaces the contents of the cache with new data. |
| 685 | * |
| 686 | * @since 2.0.0 |
| 687 | * |
| 688 | * @see ObjectCache::replace() |
| 689 | * @global ObjectCache $wpacu_object_cache Object cache global instance. |
| 690 | * |
| 691 | * @param int|string $key The key for the cache data that should be replaced. |
| 692 | * @param mixed $data The new data to store in the cache. |
| 693 | * @param string $group Optional. The group for the cache data that should be replaced. |
| 694 | * Default empty. |
| 695 | * @param int $expire Optional. When to expire the cache contents, in seconds. |
| 696 | * Default 0 (no expiration). |
| 697 | * @return bool False if original value does not exist, true if contents were replaced |
| 698 | */ |
| 699 | public static function wpacu_cache_replace( $key, $data, $group = '', $expire = 0 ) { |
| 700 | if ( ! self::ensureInitialized(__METHOD__) ) { return; } |
| 701 | |
| 702 | global $wpacu_object_cache; |
| 703 | |
| 704 | return $wpacu_object_cache->replace( $key, $data, $group, (int) $expire ); |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Saves the data to the cache. |
| 709 | * |
| 710 | * Differs from ObjectCache::wpacu_cache_add() and wp_cache_replace() in that it will always write data. |
| 711 | * |
| 712 | * @since 2.0.0 |
| 713 | * |
| 714 | * @see ObjectCache::set() |
| 715 | * @global ObjectCache $wpacu_object_cache Object cache global instance. |
| 716 | * |
| 717 | * @param int|string $key The cache key to use for retrieval later. |
| 718 | * @param mixed $data The contents to store in the cache. |
| 719 | * @param string $group Optional. Where to group the cache contents. Enables the same key |
| 720 | * to be used across groups. Default empty. |
| 721 | * @param int $expire Optional. When to expire the cache contents, in seconds. |
| 722 | * Default 0 (no expiration). |
| 723 | * @return bool True on success, false on failure. |
| 724 | */ |
| 725 | public static function wpacu_cache_set( $key, $data, $group = '', $expire = 0 ) { |
| 726 | global $wpacu_object_cache; |
| 727 | |
| 728 | return $wpacu_object_cache->set( $key, $data, $group, (int) $expire ); |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * Switches the internal blog ID. |
| 733 | * |
| 734 | * This changes the blog id used to create keys in blog specific groups. |
| 735 | * |
| 736 | * @since 3.5.0 |
| 737 | * |
| 738 | * @see ObjectCache::switch_to_blog() |
| 739 | * @global ObjectCache $wpacu_object_cache Object cache global instance. |
| 740 | * |
| 741 | * @param int $blog_id Site ID. |
| 742 | */ |
| 743 | public static function wpacu_cache_switch_to_blog( $blog_id ) { |
| 744 | global $wpacu_object_cache; |
| 745 | |
| 746 | $wpacu_object_cache->switch_to_blog( $blog_id ); |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * Adds a group or set of groups to the list of global groups. |
| 751 | * |
| 752 | * @since 2.6.0 |
| 753 | * |
| 754 | * @see ObjectCache::add_global_groups() |
| 755 | * @global ObjectCache $wpacu_object_cache Object cache global instance. |
| 756 | * |
| 757 | * @param string|array $groups A group or an array of groups to add. |
| 758 | */ |
| 759 | public static function wpacu_cache_add_global_groups( $groups ) { |
| 760 | global $wpacu_object_cache; |
| 761 | |
| 762 | $wpacu_object_cache->add_global_groups( $groups ); |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * Adds a group or set of groups to the list of non-persistent groups. |
| 767 | * |
| 768 | * @since 2.6.0 |
| 769 | * |
| 770 | * @param string|array $groups A group or an array of groups to add. |
| 771 | */ |
| 772 | public static function wpacu_cache_add_non_persistent_groups( $groups ) { |
| 773 | // Default cache doesn't persist so nothing to do here. |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * Reset internal cache keys and structures. |
| 778 | * |
| 779 | * If the cache back end uses global blog or site IDs as part of its cache keys, |
| 780 | * this function instructs the back end to reset those keys and perform any cleanup |
| 781 | * since blog or site IDs have changed since cache init. |
| 782 | * |
| 783 | * This function is deprecated. Use wp_cache_switch_to_blog() instead of this |
| 784 | * function when preparing the cache for a blog switch. For clearing the cache |
| 785 | * during unit tests, consider using wp_cache_init(). wp_cache_init() is not |
| 786 | * recommended outside of unit tests as the performance penalty for using it is |
| 787 | * high. |
| 788 | * |
| 789 | * @since 2.6.0 |
| 790 | * @deprecated 3.5.0 ObjectCache::reset() |
| 791 | * @see ObjectCache::reset() |
| 792 | * |
| 793 | * @global ObjectCache $wpacu_object_cache Object cache global instance. |
| 794 | */ |
| 795 | public static function wpacu_cache_reset() { |
| 796 | _deprecated_function( __FUNCTION__, '3.5.0', 'ObjectCache::reset()' ); |
| 797 | |
| 798 | if ( ! self::ensureInitialized(__METHOD__) ) { return; } |
| 799 | |
| 800 | global $wpacu_object_cache; |
| 801 | |
| 802 | $wpacu_object_cache->reset(); |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * Main purpose: Avoid errors such as "PHP Fatal error: Uncaught Error: Call to a member function get() on null" |
| 807 | * |
| 808 | * @return bool |
| 809 | */ |
| 810 | public static function isValidObjectCache() |
| 811 | { |
| 812 | global $wpacu_object_cache; |
| 813 | return isset( $wpacu_object_cache ) && ! is_null( $wpacu_object_cache ); |
| 814 | } |
| 815 | /** |
| 816 | * [END] Functions to call (reference: /wp-includes/cache.php) |
| 817 | */ |
| 818 | } |
| 819 |