Skip to content

Commit dc85c92

Browse files
authored
Support Statamic 6 (#25)
2 parents 43bedff + 85ab5b7 commit dc85c92

File tree

19 files changed

+487
-18
lines changed

19 files changed

+487
-18
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
php: [8.3, 8.2]
16-
laravel: [11.*]
17-
statamic: [^5.0]
15+
php: [8.4, 8.3]
16+
laravel: [12.*]
17+
statamic: [^6.0]
1818
os: [ubuntu-latest]
1919

2020
name: ${{ matrix.php }} - ${{ matrix.statamic }} - ${{ matrix.laravel }}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,4 @@ To invalidate all pages containing your tracked data call:
8585
Tracker::flush();
8686
```
8787

88+
Alternatively you can use the Cache Tracker Utility in the CP to enter a list of URLs you want to clear.

composer.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
}
1212
},
1313
"require": {
14-
"php": "^8.1",
14+
"php": "^8.2",
1515
"pixelfear/composer-dist-plugin": "^0.1.5",
16-
"statamic/cms": "^4.55 || ^5.0"
16+
"statamic/cms": "^6.0"
1717
},
1818
"require-dev": {
1919
"laravel/pint": "^1.13",
2020
"mockery/mockery": "^1.3.1",
2121
"nunomaduro/collision": "^8.0",
22-
"orchestra/testbench": "^9.0",
23-
"pestphp/pest": "^2.24",
24-
"phpunit/phpunit": "^10.0"
22+
"orchestra/testbench": "^10.0",
23+
"pestphp/pest": "^4.0",
24+
"phpunit/phpunit": "^12.0"
2525
},
2626
"extra": {
2727
"statamic": {
@@ -39,5 +39,6 @@
3939
"pestphp/pest-plugin": true,
4040
"pixelfear/composer-dist-plugin": true
4141
}
42-
}
42+
},
43+
"minimum-stability": "alpha"
4344
}

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"dependencies": {
3+
"@statamic/cms": "file:./vendor/statamic/cms/resources/dist-package",
4+
"axios": "^1.12.2"
5+
},
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vite build",
9+
"production": "vite build"
10+
},
11+
"devDependencies": {
12+
"laravel-vite-plugin": "^1.2.0",
13+
"vite": "^6.3.4"
14+
}
15+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<script setup>
2+
import axios from 'axios';
3+
import { ref } from 'vue';
4+
5+
const props = defineProps({
6+
action: { type: Object, required: true },
7+
});
8+
9+
const getColor = (tag) => {
10+
if (tag.indexOf('collection:') === 0) {
11+
return 'yellow';
12+
}
13+
14+
if (tag.indexOf('term:') === 0) {
15+
return 'rose';
16+
}
17+
18+
if (tag.indexOf('global:') === 0) {
19+
return 'violet';
20+
}
21+
22+
if (tag.indexOf('partial:') === 0) {
23+
return 'blue';
24+
}
25+
26+
return 'green';
27+
}
28+
29+
const setShowWhat = (what) => {
30+
show.value = what;
31+
}
32+
33+
const item = props.action?.item_title ?? '';
34+
const url = props.action?.item_url ?? '';
35+
const show = ref('tags');
36+
const tags = ref([]);
37+
const urls = ref([]);
38+
39+
axios
40+
.post(cp_url(`/cache-tracker/tags`), { url: url })
41+
.then(response => {
42+
tags.value = response.data;
43+
})
44+
.catch((e) => { });
45+
46+
axios
47+
.post(cp_url(`/cache-tracker/urls`), { url: url })
48+
.then(response => {
49+
urls.value = response.data;
50+
})
51+
.catch((e) => { });
52+
</script>
53+
54+
<template>
55+
<div>
56+
<ui-button-group class="mb-4">
57+
<ui-button :variant="show == 'tags' ? 'primary' : 'default'" size="sm" @click="setShowWhat('tags')">Tags on this URL</ui-button>
58+
<ui-button :variant="show == 'urls' ? 'primary' : 'default'" size="sm" @click="setShowWhat('urls')">URLs with this item</ui-button>
59+
</ui-button-group>
60+
61+
<div v-if="show == 'tags'">
62+
63+
<ui-description v-text="__('There are no tags tracked on :url.', { url: url })" v-if="tags.length < 1"></ui-description>
64+
65+
<ui-description v-text="__('The following tags are being tracked on :url:', { url: url })" v-if="tags.length"></ui-description>
66+
67+
<div class="flex gap-2 mt-4" v-if="tags.length">
68+
69+
<template v-for="tag in tags">
70+
<ui-badge pill :color="getColor(tag)" v-text="tag"></ui-badge>
71+
</template>
72+
73+
</div>
74+
75+
</div>
76+
77+
<div v-if="show == 'urls'">
78+
<ui-description v-text="__('There are no urls tracked containing :item.', { item: item })" v-if="urls.length < 1"></ui-description>
79+
80+
<ui-description v-text="__('The following URLs contain :item:', { item: 'ryan' })" v-if="urls.length"></ui-description>
81+
82+
<div class="flex gap-2 mt-4" v-if="urls.length">
83+
84+
<template v-for="url in urls">
85+
<a :href="url" v-text="url"></a><br />
86+
</template>
87+
88+
</div>
89+
90+
</div>
91+
</div>
92+
</template>

resources/js/cp.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import CacheTrackerModal from './components/CacheTrackerModal.vue';
2+
import ClearUtility from "./pages/ClearUtility.vue";
3+
4+
Statamic.booting(() => {
5+
Statamic.$components.register('cache-tracker-modal', CacheTrackerModal);
6+
7+
Statamic.$components.register('Pages/CacheTracker/ClearUtility', ClearUtility);
8+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<script setup>
2+
import axios from 'axios';
3+
import { ref } from 'vue';
4+
5+
const urls = ref('');
6+
7+
const clearCache = async () => {
8+
let response = await axios.post(cp_url('utilities/static-cache-tracker/clear'), { urls: urls.value });
9+
10+
Statamic.$toast.success(response.data.message);
11+
}
12+
13+
</script>
14+
15+
<template>
16+
<ui-header :title="__('Cache Tracker')" icon="taxonomies">
17+
<ui-button class="btn-primary" v-text="__('Clear everything')" @click="urls = '*'; clearCache();" />
18+
</ui-header>
19+
20+
<ui-panel class="h-full flex flex-col">
21+
<ui-panel-header class="flex items-center justify-between min-h-10">
22+
<ui-heading>{{ __('Enter URLs to clear, with each on a new line. You can use * as a wildcard at the end of your URL.') }}</ui-heading>
23+
</ui-panel-header>
24+
25+
<ui-card class="flex-1">
26+
27+
<ui-field>
28+
<ui-textarea label="Enter URLs" v-model="urls" />
29+
</ui-field>
30+
31+
<ui-field class="mt-4 flex justify-end">
32+
<ui-button variant="primary" v-text="__('Clear')" @click="clearCache" />
33+
</ui-field>
34+
35+
</ui-card>
36+
</ui-panel>
37+
38+
</template>

routes/cp.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Route;
4+
use Thoughtco\StatamicCacheTracker\Http\Controllers;
5+
6+
Route::name('cache-tracker.')->prefix('cache-tracker')->group(function () {
7+
Route::post('tags', [Controllers\GetTagsController::class, '__invoke'])->name('tags');
8+
Route::post('urls', [Controllers\GetUrlsController::class, '__invoke'])->name('url');
9+
});

src/Actions/ClearCache.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Thoughtco\StatamicCacheTracker\Actions;
4+
5+
use Statamic\Actions\Action;
6+
use Statamic\Contracts\Entries\Entry;
7+
use Statamic\Contracts\Taxonomies\Term;
8+
use Statamic\Facades\Blink;
9+
use Thoughtco\StatamicCacheTracker\Facades\Tracker;
10+
11+
class ClearCache extends Action
12+
{
13+
public function run($items, $values)
14+
{
15+
$items->filter(fn ($item) => $item->absoluteUrl())
16+
->each(fn ($item) => Tracker::remove($item->absoluteUrl()));
17+
18+
return __('Cache cleared');
19+
}
20+
21+
public function icon(): string
22+
{
23+
return 'rewind';
24+
}
25+
26+
public static function title()
27+
{
28+
return __('Clear cache');
29+
}
30+
31+
public function confirmationText()
32+
{
33+
return __('Are you sure you want to clear the static cache for the url: :url ?', ['url' => $this->items->first()->absoluteUrl()]);
34+
}
35+
36+
public function visibleTo($item)
37+
{
38+
if (! auth()->user()->can('clear cache tracker tags')) {
39+
return false;
40+
}
41+
42+
if (! ($item instanceof Entry || $item instanceof Term)) {
43+
return false;
44+
}
45+
46+
return ! Blink::once(
47+
'cache-action::'.$item->collectionHandle.'::'.$item->locale(),
48+
fn () => is_null($item->collection()->route($item->locale()))
49+
);
50+
}
51+
}

src/Actions/ViewCacheTags.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
9+
class ViewCacheTags extends Action
10+
{
11+
protected $component = 'cache-tracker-modal';
12+
13+
protected $runnable = false;
14+
15+
public function run($items, $values)
16+
{
17+
// no running in the corridor
18+
}
19+
20+
public function icon(): string
21+
{
22+
return 'taxonomies';
23+
}
24+
25+
public static function title()
26+
{
27+
return __('View cache tags');
28+
}
29+
30+
public function visibleTo($item)
31+
{
32+
if (! auth()->user()->can('view cache tracker tags')) {
33+
return false;
34+
}
35+
36+
if (! $item instanceof Entry) {
37+
return false;
38+
}
39+
40+
return ! Blink::once(
41+
'cache-action::'.$item->collectionHandle.'::'.$item->locale(),
42+
fn () => is_null($item->collection()->route($item->locale()))
43+
);
44+
}
45+
46+
public function visibleToBulk($items)
47+
{
48+
return false;
49+
}
50+
51+
public function buttonText()
52+
{
53+
/** @translation */
54+
return __('Clear cache');
55+
}
56+
57+
public function toArray()
58+
{
59+
return [
60+
...parent::toArray(),
61+
'item_title' => $this->items->first()?->title,
62+
'item_url' => $this->items->first()?->absoluteUrl(),
63+
];
64+
}
65+
}

0 commit comments

Comments
 (0)