Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Caching
File: WPCacheEngine.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Automattic\WooCommerce\Caching;
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Implementation of CacheEngine that uses the built-in WordPress cache.
[5] Fix | Delete
*/
[6] Fix | Delete
class WPCacheEngine implements CacheEngine {
[7] Fix | Delete
use CacheNameSpaceTrait;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Retrieves an object cached under a given key.
[11] Fix | Delete
*
[12] Fix | Delete
* @param string $key The key under which the object to retrieve is cached.
[13] Fix | Delete
* @param string $group The group under which the object is cached.
[14] Fix | Delete
*
[15] Fix | Delete
* @return array|object|null The cached object, or null if there's no object cached under the passed key.
[16] Fix | Delete
*/
[17] Fix | Delete
public function get_cached_object( string $key, string $group = '' ) {
[18] Fix | Delete
$prefixed_key = self::get_prefixed_key( $key, $group );
[19] Fix | Delete
$value = wp_cache_get( $prefixed_key, $group );
[20] Fix | Delete
return false === $value ? null : $value;
[21] Fix | Delete
}
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Retrieves a set of objects cached under the given keys.
[25] Fix | Delete
*
[26] Fix | Delete
* @param string[] $keys The keys under which the object to retrieve is cached.
[27] Fix | Delete
* @param string $group The group under which the object is cached.
[28] Fix | Delete
*
[29] Fix | Delete
* @return array The cached array of objects keyed by the given keys, values will be null for any non-cached keys.
[30] Fix | Delete
*/
[31] Fix | Delete
public function get_cached_objects( array $keys, string $group = '' ) {
[32] Fix | Delete
$prefix = self::get_cache_prefix( $group );
[33] Fix | Delete
$key_map = array_combine(
[34] Fix | Delete
$keys,
[35] Fix | Delete
array_map(
[36] Fix | Delete
function ( $key ) use ( $prefix ) {
[37] Fix | Delete
return $prefix . $key;
[38] Fix | Delete
},
[39] Fix | Delete
$keys
[40] Fix | Delete
)
[41] Fix | Delete
);
[42] Fix | Delete
[43] Fix | Delete
$cached_values = wp_cache_get_multiple( array_values( $key_map ), $group );
[44] Fix | Delete
$return_values = array();
[45] Fix | Delete
foreach ( $key_map as $key => $prefixed_key ) {
[46] Fix | Delete
if ( isset( $cached_values[ $prefixed_key ] ) && false !== $cached_values[ $prefixed_key ] ) {
[47] Fix | Delete
$return_values[ $key ] = $cached_values[ $prefixed_key ];
[48] Fix | Delete
} else {
[49] Fix | Delete
$return_values[ $key ] = null;
[50] Fix | Delete
}
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
return $return_values;
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
/**
[57] Fix | Delete
* Caches an object under a given key, and with a given expiration.
[58] Fix | Delete
*
[59] Fix | Delete
* @param string $key The key under which the object will be cached.
[60] Fix | Delete
* @param array|object $object The object to cache.
[61] Fix | Delete
* @param int $expiration Expiration for the cached object, in seconds.
[62] Fix | Delete
* @param string $group The group under which the object will be cached.
[63] Fix | Delete
*
[64] Fix | Delete
* @return bool True if the object is cached successfully, false otherwise.
[65] Fix | Delete
*/
[66] Fix | Delete
public function cache_object( string $key, $object, int $expiration, string $group = '' ): bool {
[67] Fix | Delete
$prefixed_key = self::get_prefixed_key( $key, $group );
[68] Fix | Delete
return false !== wp_cache_set( $prefixed_key, $object, $group, $expiration );
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
/**
[72] Fix | Delete
* Caches an object under a given key, and with a given expiration.
[73] Fix | Delete
*
[74] Fix | Delete
* @param array $objects The objects to cache keyed by the key to cache under.
[75] Fix | Delete
* @param int $expiration Expiration for the cached object, in seconds.
[76] Fix | Delete
* @param string $group The group under which the object will be cached.
[77] Fix | Delete
*
[78] Fix | Delete
* @return array Array of return values, grouped by key. Each value is either
[79] Fix | Delete
* true on success, or false on failure
[80] Fix | Delete
*/
[81] Fix | Delete
public function cache_objects( array $objects, int $expiration, string $group = '' ): array {
[82] Fix | Delete
$prefix = self::get_cache_prefix( $group );
[83] Fix | Delete
[84] Fix | Delete
$objects = array_combine(
[85] Fix | Delete
array_map(
[86] Fix | Delete
function ( $key ) use ( $prefix ) {
[87] Fix | Delete
return $prefix . $key;
[88] Fix | Delete
},
[89] Fix | Delete
array_keys( $objects )
[90] Fix | Delete
),
[91] Fix | Delete
$objects,
[92] Fix | Delete
);
[93] Fix | Delete
[94] Fix | Delete
return wp_cache_set_multiple( $objects, $group, $expiration );
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
/**
[98] Fix | Delete
* Removes a cached object from the cache.
[99] Fix | Delete
*
[100] Fix | Delete
* @param string $key They key under which the object is cached.
[101] Fix | Delete
* @param string $group The group under which the object is cached.
[102] Fix | Delete
*
[103] 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).
[104] Fix | Delete
*/
[105] Fix | Delete
public function delete_cached_object( string $key, string $group = '' ): bool {
[106] Fix | Delete
$prefixed_key = self::get_prefixed_key( $key, $group );
[107] Fix | Delete
return false !== wp_cache_delete( $prefixed_key, $group );
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
/**
[111] Fix | Delete
* Checks if an object is cached under a given key.
[112] Fix | Delete
*
[113] Fix | Delete
* @param string $key The key to verify.
[114] Fix | Delete
* @param string $group The group under which the object is cached.
[115] Fix | Delete
*
[116] Fix | Delete
* @return bool True if there's an object cached under the given key, false otherwise.
[117] Fix | Delete
*/
[118] Fix | Delete
public function is_cached( string $key, string $group = '' ): bool {
[119] Fix | Delete
$prefixed_key = self::get_prefixed_key( $key, $group );
[120] Fix | Delete
return false !== wp_cache_get( $prefixed_key, $group );
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
/**
[124] Fix | Delete
* Deletes all cached objects under a given group.
[125] Fix | Delete
*
[126] Fix | Delete
* @param string $group The group to delete.
[127] Fix | Delete
*
[128] Fix | Delete
* @return bool True if the group is deleted successfully, false otherwise.
[129] Fix | Delete
*/
[130] Fix | Delete
public function delete_cache_group( string $group = '' ): bool {
[131] Fix | Delete
return false !== self::invalidate_cache_group( $group );
[132] Fix | Delete
}
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function