Skip to content

Commit 3aa9e80

Browse files
committed
Add clear cache action on routable entries
1 parent 43bedff commit 3aa9e80

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

src/Actions/ClearCache.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Thoughtco\StatamicCacheTracker\Actions;
4+
5+
use Statamic\Actions\Action;
6+
use Statamic\Contracts\Entries\Entry;
7+
use Statamic\Facades\Blink;
8+
use Thoughtco\StatamicCacheTracker\Facades\Tracker;
9+
10+
class ClearCache extends Action
11+
{
12+
public function run($items, $values)
13+
{
14+
$items->filter(fn ($item) => $item->absoluteUrl())
15+
->each(fn ($item) => Tracker::remove($item->absoluteUrl()));
16+
17+
return __('Cache cleared');
18+
}
19+
20+
public static function title()
21+
{
22+
return __('Clear cache');
23+
}
24+
25+
public function visibleTo($item)
26+
{
27+
if (! $item instanceof Entry) {
28+
return false;
29+
}
30+
31+
return ! Blink::once(
32+
'cache-action::'.$item->collectionHandle.'::'.$item->locale(),
33+
fn () => is_null($item->collection()->route($item->locale()))
34+
);
35+
}
36+
}

src/Http/Middleware/CacheTracker.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,21 @@ private function setupAugmentationHooks(string $url)
123123
return $next($augmented);
124124
});
125125

126-
app(Entry::class)::hook('augmented', function ($augmented, $next) use ($self, $url) {
126+
app(Entry::class)::hook('augmented', function ($augmented, $next) use ($self) {
127127
$self->addContentTag($this->collection()->handle().':'.$this->id());
128128

129129
return $next($augmented);
130130
});
131131

132-
Page::hook('augmented', function ($augmented, $next) use ($self, $url) {
132+
Page::hook('augmented', function ($augmented, $next) use ($self) {
133133
if ($entry = $this->entry()) {
134134
$self->addContentTag($entry->collection()->handle().':'.$entry->id());
135135
}
136136

137137
return $next($augmented);
138138
});
139139

140-
LocalizedTerm::hook('augmented', function ($augmented, $next) use ($self, $url) {
140+
LocalizedTerm::hook('augmented', function ($augmented, $next) use ($self) {
141141
$self->addContentTag('term:'.$this->id());
142142

143143
return $next($augmented);

src/ServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
class ServiceProvider extends AddonServiceProvider
88
{
9+
protected $actions = [
10+
Actions\ClearCache::class,
11+
];
12+
913
protected $middlewareGroups = [
1014
'web' => [
1115
Http\Middleware\CacheTracker::class,

0 commit comments

Comments
 (0)