Skip to content

Commit

Permalink
feat: add filter to cache methods (#2878)
Browse files Browse the repository at this point in the history
Add `timber/cache/transient_key` filter to allow filtering the transient key.

---------

Co-authored-by: Nicolas Lemoine <nico.lemoine@gmail.com>
  • Loading branch information
bitfactory-robin-martijn and nlemoine committed Feb 24, 2024
1 parent d1356fd commit b347677
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Loader.php
Expand Up @@ -668,6 +668,25 @@ public function get_cache($key, $group = self::CACHEGROUP, $cache_mode = self::C
$value = false;
$trans_key = \substr($group . '_' . $key, 0, self::TRANS_KEY_LEN);

/**
* Filters the transient key used for caching.
*
* @api
* @since 2.1.0
* @example
* ```
* add_filter( 'timber/cache/transient_key', function( $trans_key, $key, $group, $cache_mode ) {
* return $trans_key . '_my_suffix';
* }, 10, 4 );
* ```
*
* @param string $trans_key The transient key.
* @param string $key The cache key.
* @param string $group The cache group.
* @param string $cache_mode The cache mode.
*/
$trans_key = apply_filters('timber/cache/transient_key', $trans_key, $key, $group, $cache_mode);

if (self::CACHE_TRANSIENT === $cache_mode) {
$value = \get_transient($trans_key);
} elseif (self::CACHE_SITE_TRANSIENT === $cache_mode) {
Expand Down Expand Up @@ -696,6 +715,25 @@ public function set_cache($key, $value, $group = self::CACHEGROUP, $expires = 0,
$cache_mode = $this->_get_cache_mode($cache_mode);
$trans_key = \substr($group . '_' . $key, 0, self::TRANS_KEY_LEN);

/**
* Filters the transient key used for caching.
*
* @api
* @since 2.1.0
* @example
* ```
* add_filter( 'timber/cache/transient_key', function( $trans_key, $key, $group, $cache_mode ) {
* return $trans_key . '_my_suffix';
* }, 10, 4 );
* ```
*
* @param string $trans_key The transient key.
* @param string $key The cache key.
* @param string $group The cache group.
* @param string $cache_mode The cache mode.
*/
$trans_key = apply_filters('timber/cache/transient_key', $trans_key, $key, $group, $cache_mode);

if (self::CACHE_TRANSIENT === $cache_mode) {
\set_transient($trans_key, $value, $expires);
} elseif (self::CACHE_SITE_TRANSIENT === $cache_mode) {
Expand Down
15 changes: 15 additions & 0 deletions tests/test-timber-cache.php
Expand Up @@ -543,6 +543,21 @@ public function testTimberLoaderCacheTransientsButKeepOtherTransients()
$this->assertSame(2, $wpdb->num_rows);
$this->assertEquals('foo', get_transient('random_600'));
}

public function testCacheTransientKeyFilter()
{
$filter = function ($key) {
return 'my_custom_key';
};
add_filter('timber/cache/transient_key', $filter);

$loader = new Timber\Loader();
$loader->set_cache('test', 'foobar', \Timber\Loader::CACHE_TRANSIENT);

remove_filter('timber/cache/transient_key', $filter);

$this->assertEquals('foobar', get_transient('my_custom_key'));
}
}

class MyFakeThing implements TimberKeyGeneratorInterface
Expand Down

0 comments on commit b347677

Please sign in to comment.