Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Caching
File: ObjectCache.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Automattic\WooCommerce\Caching;
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Base class for caching objects (or associative arrays) that have a unique identifier.
[5] Fix | Delete
* At the very least, derived classes need to implement the 'get_object_type' method,
[6] Fix | Delete
* but usually it will be convenient to override some of the other protected members.
[7] Fix | Delete
*
[8] Fix | Delete
* The actual caching is delegated to an instance of CacheEngine. By default WpCacheEngine is used,
[9] Fix | Delete
* but a different engine can be used by either overriding the get_cache_engine_instance method
[10] Fix | Delete
* or capturing the wc_object_cache_get_engine filter.
[11] Fix | Delete
*
[12] Fix | Delete
* Objects are identified by ids that are either integers or strings. The actual cache keys passed
[13] Fix | Delete
* to the cache engine will be prefixed with the object type and a random string. The 'flush' operation
[14] Fix | Delete
* just forces the generation a new prefix and lets the old cached objects expire.
[15] Fix | Delete
*/
[16] Fix | Delete
abstract class ObjectCache {
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* Expiration value to be passed to 'set' to use the value of $default_expiration.
[20] Fix | Delete
*/
[21] Fix | Delete
public const DEFAULT_EXPIRATION = -1;
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Maximum expiration time value, in seconds, that can be passed to 'set'.
[25] Fix | Delete
*/
[26] Fix | Delete
public const MAX_EXPIRATION = MONTH_IN_SECONDS;
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* This needs to be set in each derived class.
[30] Fix | Delete
*
[31] Fix | Delete
* @var string
[32] Fix | Delete
*/
[33] Fix | Delete
private $object_type;
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Default value for the duration of the objects in the cache, in seconds
[37] Fix | Delete
* (may not be used depending on the cache engine used WordPress cache implementation).
[38] Fix | Delete
*
[39] Fix | Delete
* @var int
[40] Fix | Delete
*/
[41] Fix | Delete
protected $default_expiration = HOUR_IN_SECONDS;
[42] Fix | Delete
[43] Fix | Delete
/**
[44] Fix | Delete
* Temporarily used when retrieving data in 'get'.
[45] Fix | Delete
*
[46] Fix | Delete
* @var array
[47] Fix | Delete
*/
[48] Fix | Delete
private $last_cached_data;
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* The cache engine to use.
[52] Fix | Delete
*
[53] Fix | Delete
* @var ?CacheEngine
[54] Fix | Delete
*/
[55] Fix | Delete
private $cache_engine = null;
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* Gets an identifier for the types of objects cached by this class.
[59] Fix | Delete
* This identifier will be used to compose the keys passed to the cache engine.
[60] Fix | Delete
* It must be unique for each class inheriting from ObjectCache.
[61] Fix | Delete
*
[62] Fix | Delete
* @return string
[63] Fix | Delete
*/
[64] Fix | Delete
abstract public function get_object_type(): string;
[65] Fix | Delete
[66] Fix | Delete
/**
[67] Fix | Delete
* Creates a new instance of the class.
[68] Fix | Delete
*
[69] Fix | Delete
* @throws CacheException If get_object_type returns null or an empty string.
[70] Fix | Delete
*/
[71] Fix | Delete
public function __construct() {
[72] Fix | Delete
$this->object_type = $this->get_object_type();
[73] Fix | Delete
if ( empty( $this->object_type ) ) {
[74] Fix | Delete
throw new CacheException( 'Class ' . get_class( $this ) . ' returns an empty value for get_object_type', $this );
[75] Fix | Delete
}
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* Get the default expiration time for cached objects, in seconds.
[80] Fix | Delete
*
[81] Fix | Delete
* @return int
[82] Fix | Delete
*/
[83] Fix | Delete
public function get_default_expiration_value(): int {
[84] Fix | Delete
return $this->default_expiration;
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
/**
[88] Fix | Delete
* Get the cache engine to use and cache it internally.
[89] Fix | Delete
*
[90] Fix | Delete
* @return CacheEngine
[91] Fix | Delete
*/
[92] Fix | Delete
private function get_cache_engine(): CacheEngine {
[93] Fix | Delete
if ( null === $this->cache_engine ) {
[94] Fix | Delete
$engine = $this->get_cache_engine_instance();
[95] Fix | Delete
[96] Fix | Delete
/**
[97] Fix | Delete
* Filters the underlying cache engine to be used by an instance of ObjectCache.
[98] Fix | Delete
*
[99] Fix | Delete
* @since 7.4.0
[100] Fix | Delete
*
[101] Fix | Delete
* @param CacheEngine $engine The cache engine to be used by default.
[102] Fix | Delete
* @param ObjectCache $cache_instance The instance of ObjectCache that will use the cache engine.
[103] Fix | Delete
* @returns CacheEngine The actual cache engine that will be used.
[104] Fix | Delete
*/
[105] Fix | Delete
$this->cache_engine = apply_filters( 'wc_object_cache_get_engine', $engine, $this );
[106] Fix | Delete
}
[107] Fix | Delete
return $this->cache_engine;
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
/**
[111] Fix | Delete
* Add an object to the cache, or update an already cached object.
[112] Fix | Delete
*
[113] Fix | Delete
* @param object|array $object The object to be cached.
[114] Fix | Delete
* @param int|string|null $id Id of the object to be cached, if null, get_object_id will be used to get it.
[115] Fix | Delete
* @param int $expiration Expiration of the cached data in seconds from the current time, or DEFAULT_EXPIRATION to use the default value.
[116] Fix | Delete
* @return bool True on success, false on error.
[117] Fix | Delete
* @throws CacheException Invalid parameter, or null id was passed and get_object_id returns null too.
[118] Fix | Delete
*/
[119] Fix | Delete
public function set( $object, $id = null, int $expiration = self::DEFAULT_EXPIRATION ): bool {
[120] Fix | Delete
if ( null === $object ) {
[121] Fix | Delete
throw new CacheException( "Can't cache a null value", $this, $id );
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
if ( ! is_array( $object ) && ! is_object( $object ) ) {
[125] Fix | Delete
throw new CacheException( "Can't cache a non-object, non-array value", $this, $id );
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
if ( ! is_string( $id ) && ! is_int( $id ) && ! is_null( $id ) ) {
[129] Fix | Delete
throw new CacheException( "Object id must be an int, a string, or null for 'set'", $this, $id );
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
$this->verify_expiration_value( $expiration );
[133] Fix | Delete
[134] Fix | Delete
$errors = $this->validate( $object );
[135] Fix | Delete
if ( ! is_null( $errors ) ) {
[136] Fix | Delete
try {
[137] Fix | Delete
$id = $this->get_id_from_object_if_null( $object, $id );
[138] Fix | Delete
} catch ( \Throwable $ex ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
[139] Fix | Delete
// Nothing else to do, we won't be able to add any significant object id to the CacheException and that's it.
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
if ( count( $errors ) === 1 ) {
[143] Fix | Delete
throw new CacheException( 'Object validation/serialization failed: ' . $errors[0], $this, $id, $errors );
[144] Fix | Delete
} elseif ( ! empty( $errors ) ) {
[145] Fix | Delete
throw new CacheException( 'Object validation/serialization failed', $this, $id, $errors );
[146] Fix | Delete
}
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
$id = $this->get_id_from_object_if_null( $object, $id );
[150] Fix | Delete
[151] Fix | Delete
$this->last_cached_data = $object;
[152] Fix | Delete
return $this->get_cache_engine()->cache_object(
[153] Fix | Delete
$id,
[154] Fix | Delete
$object,
[155] Fix | Delete
self::DEFAULT_EXPIRATION === $expiration ? $this->default_expiration : $expiration,
[156] Fix | Delete
$this->get_object_type()
[157] Fix | Delete
);
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
/**
[161] Fix | Delete
* Update an object in the cache, but only if an object is already cached with the same id.
[162] Fix | Delete
*
[163] Fix | Delete
* @param object|array $object The new object that will replace the already cached one.
[164] Fix | Delete
* @param int|string|null $id Id of the object to be cached, if null, get_object_id will be used to get it.
[165] Fix | Delete
* @param int $expiration Expiration of the cached data in seconds from the current time, or DEFAULT_EXPIRATION to use the default value.
[166] Fix | Delete
* @return bool True on success, false on error or if no object with the supplied id was cached.
[167] Fix | Delete
* @throws CacheException Invalid parameter, or null id was passed and get_object_id returns null too.
[168] Fix | Delete
*/
[169] Fix | Delete
public function update_if_cached( $object, $id = null, int $expiration = self::DEFAULT_EXPIRATION ): bool {
[170] Fix | Delete
$id = $this->get_id_from_object_if_null( $object, $id );
[171] Fix | Delete
[172] Fix | Delete
if ( ! $this->is_cached( $id ) ) {
[173] Fix | Delete
return false;
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
return $this->set( $object, $id, $expiration );
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
/**
[180] Fix | Delete
* Get the id from an object if the id itself is null.
[181] Fix | Delete
*
[182] Fix | Delete
* @param object|array $object The object to get the id from.
[183] Fix | Delete
* @param int|string|null $id An object id or null.
[184] Fix | Delete
*
[185] Fix | Delete
* @return int|string|null Passed $id if it wasn't null, otherwise id obtained from $object using get_object_id.
[186] Fix | Delete
*
[187] Fix | Delete
* @throws CacheException Passed $id is null and get_object_id returned null too.
[188] Fix | Delete
*/
[189] Fix | Delete
private function get_id_from_object_if_null( $object, $id ) {
[190] Fix | Delete
if ( null === $id ) {
[191] Fix | Delete
$id = $this->get_object_id( $object );
[192] Fix | Delete
if ( null === $id ) {
[193] Fix | Delete
throw new CacheException( "Null id supplied and the cache class doesn't implement get_object_id", $this );
[194] Fix | Delete
}
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
return $id;
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
/**
[201] Fix | Delete
* Check if the given expiration time value is valid, throw an exception if not.
[202] Fix | Delete
*
[203] Fix | Delete
* @param int $expiration Expiration time to check.
[204] Fix | Delete
* @return void
[205] Fix | Delete
* @throws CacheException Expiration time is negative or higher than MAX_EXPIRATION.
[206] Fix | Delete
*/
[207] Fix | Delete
private function verify_expiration_value( int $expiration ): void {
[208] Fix | Delete
if ( self::DEFAULT_EXPIRATION !== $expiration && ( ( $expiration < 1 ) || ( $expiration > self::MAX_EXPIRATION ) ) ) {
[209] Fix | Delete
throw new CacheException( 'Invalid expiration value, must be ObjectCache::DEFAULT_EXPIRATION or a value between 1 and ObjectCache::MAX_EXPIRATION', $this );
[210] Fix | Delete
}
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
/**
[214] Fix | Delete
* Retrieve a cached object, and if no object is cached with the given id,
[215] Fix | Delete
* try to get one via get_from_datastore method or by supplying a callback and then cache it.
[216] Fix | Delete
*
[217] Fix | Delete
* If you want to provide a callable but still use the default expiration value,
[218] Fix | Delete
* pass "ObjectCache::DEFAULT_EXPIRATION" as the second parameter.
[219] Fix | Delete
*
[220] Fix | Delete
* @param int|string $id The id of the object to retrieve.
[221] Fix | Delete
* @param int $expiration Expiration of the cached data in seconds from the current time, used if an object is retrieved from datastore and cached.
[222] Fix | Delete
* @param callable|null $get_from_datastore_callback Optional callback to get the object if it's not cached, it must return an object/array or null.
[223] Fix | Delete
* @return object|array|null Cached object, or null if it's not cached and can't be retrieved from datastore or via callback.
[224] Fix | Delete
* @throws CacheException Invalid id parameter.
[225] Fix | Delete
*/
[226] Fix | Delete
public function get( $id, int $expiration = self::DEFAULT_EXPIRATION, ?callable $get_from_datastore_callback = null ) {
[227] Fix | Delete
if ( ! is_string( $id ) && ! is_int( $id ) ) {
[228] Fix | Delete
throw new CacheException( "Object id must be an int or a string for 'get'", $this );
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
$this->verify_expiration_value( $expiration );
[232] Fix | Delete
[233] Fix | Delete
$data = $this->get_cache_engine()->get_cached_object( $id, $this->get_object_type() );
[234] Fix | Delete
if ( null === $data ) {
[235] Fix | Delete
$object = null;
[236] Fix | Delete
if ( $get_from_datastore_callback ) {
[237] Fix | Delete
$object = $get_from_datastore_callback( $id );
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
if ( null === $object ) {
[241] Fix | Delete
return null;
[242] Fix | Delete
}
[243] Fix | Delete
$this->set( $object, $id, $expiration );
[244] Fix | Delete
$data = $this->last_cached_data;
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
return $data;
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
/**
[251] Fix | Delete
* Remove an object from the cache.
[252] Fix | Delete
*
[253] Fix | Delete
* @param int|string $id The id of the object to remove.
[254] Fix | Delete
* @return bool True if the object is removed from the cache successfully, false otherwise (because the object wasn't cached or for other reason).
[255] Fix | Delete
*/
[256] Fix | Delete
public function remove( $id ): bool {
[257] Fix | Delete
return $this->get_cache_engine()->delete_cached_object( $id, $this->get_object_type() );
[258] Fix | Delete
}
[259] Fix | Delete
[260] Fix | Delete
/**
[261] Fix | Delete
* Remove all the objects from the cache.
[262] Fix | Delete
*
[263] Fix | Delete
* @return bool True on success, false on error.
[264] Fix | Delete
*/
[265] Fix | Delete
public function flush(): bool {
[266] Fix | Delete
return $this->get_cache_engine()->delete_cache_group( $this->get_object_type() );
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
/**
[270] Fix | Delete
* Is a given object cached?
[271] Fix | Delete
*
[272] Fix | Delete
* @param int|string $id The id of the object to check.
[273] Fix | Delete
* @return bool True if there's a cached object with the specified id.
[274] Fix | Delete
*/
[275] Fix | Delete
public function is_cached( $id ): bool {
[276] Fix | Delete
return $this->get_cache_engine()->is_cached( $id, $this->get_object_type() );
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
/**
[280] Fix | Delete
* Get the id of an object. This is used by 'set' when a null id is passed.
[281] Fix | Delete
* If the object id can't be determined the method must return null.
[282] Fix | Delete
*
[283] Fix | Delete
* @param array|object $object The object to get the id for.
[284] Fix | Delete
* @return int|string|null
[285] Fix | Delete
*/
[286] Fix | Delete
abstract protected function get_object_id( $object );
[287] Fix | Delete
[288] Fix | Delete
/**
[289] Fix | Delete
* Validate an object before it's cached.
[290] Fix | Delete
*
[291] Fix | Delete
* @param array|object $object Object to validate.
[292] Fix | Delete
* @return array|null An array with validation error messages, null or an empty array if there are no errors.
[293] Fix | Delete
*/
[294] Fix | Delete
abstract protected function validate( $object ): ?array;
[295] Fix | Delete
[296] Fix | Delete
/**
[297] Fix | Delete
* Get the instance of the cache engine to use.
[298] Fix | Delete
*
[299] Fix | Delete
* @return CacheEngine
[300] Fix | Delete
*/
[301] Fix | Delete
protected function get_cache_engine_instance(): CacheEngine {
[302] Fix | Delete
return wc_get_container()->get( WPCacheEngine::class );
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
/**
[306] Fix | Delete
* Get a random string to be used to compose the cache key prefix.
[307] Fix | Delete
* It should return a different string each time.
[308] Fix | Delete
*
[309] Fix | Delete
* @return string
[310] Fix | Delete
*/
[311] Fix | Delete
protected function get_random_string(): string {
[312] Fix | Delete
return dechex( microtime( true ) * 1000 ) . bin2hex( random_bytes( 8 ) );
[313] Fix | Delete
}
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function