1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FastCGI Cache Bust plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Bust the Nginx FastCGI Cache when entries are saved or created. |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace nystudio107\fastcgicachebust; |
12
|
|
|
|
13
|
|
|
use nystudio107\fastcgicachebust\services\Cache as CacheService; |
14
|
|
|
use nystudio107\fastcgicachebust\models\Settings; |
15
|
|
|
|
16
|
|
|
use Craft; |
17
|
|
|
use craft\base\Element; |
18
|
|
|
use craft\base\Plugin; |
19
|
|
|
use craft\elements\Entry; |
20
|
|
|
use craft\events\ElementEvent; |
21
|
|
|
use craft\events\RegisterCacheOptionsEvent; |
22
|
|
|
use craft\services\Elements; |
23
|
|
|
use craft\utilities\ClearCaches; |
24
|
|
|
|
25
|
|
|
use yii\base\Event; |
26
|
|
|
use yii\base\Exception; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class FastcgiCacheBust |
30
|
|
|
* |
31
|
|
|
* @author nystudio107 |
32
|
|
|
* @package FastcgiCacheBust |
33
|
|
|
* @since 1.0.0 |
34
|
|
|
* |
35
|
|
|
* @property CacheService cache |
36
|
|
|
*/ |
37
|
|
|
class FastcgiCacheBust extends Plugin |
38
|
|
|
{ |
39
|
|
|
// Static Properties |
40
|
|
|
// ========================================================================= |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var FastcgiCacheBust |
44
|
|
|
*/ |
45
|
|
|
public static $plugin; |
46
|
|
|
|
47
|
|
|
// Public Methods |
48
|
|
|
// ========================================================================= |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
|
|
public function init() |
54
|
|
|
{ |
55
|
|
|
parent::init(); |
56
|
|
|
self::$plugin = $this; |
57
|
|
|
|
58
|
|
|
// Handler: Elements::EVENT_AFTER_SAVE_ELEMENT |
59
|
|
|
Event::on( |
60
|
|
|
Elements::class, |
61
|
|
|
Elements::EVENT_AFTER_SAVE_ELEMENT, |
62
|
|
|
function (ElementEvent $event) { |
63
|
|
|
Craft::debug( |
64
|
|
|
'Elements::EVENT_AFTER_SAVE_ELEMENT', |
65
|
|
|
__METHOD__ |
66
|
|
|
); |
67
|
|
|
/** @var Element $element */ |
68
|
|
|
$element = $event->element; |
69
|
|
|
// Only bust the cache if it's not certain excluded element types |
70
|
|
|
if ($this->shouldBustCache($element)) { |
71
|
|
|
Craft::debug( |
72
|
|
|
'Cache busted due to saving: '.\get_class($element).' - '.$element->title, |
73
|
|
|
__METHOD__ |
74
|
|
|
); |
75
|
|
|
FastcgiCacheBust::$plugin->cache->clearAll(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
// Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS |
81
|
|
|
Event::on( |
82
|
|
|
ClearCaches::class, |
83
|
|
|
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, |
84
|
|
|
function (RegisterCacheOptionsEvent $event) { |
85
|
|
|
$event->options[] = [ |
86
|
|
|
'key' => 'fastcgi-cache-bust', |
87
|
|
|
'label' => Craft::t('fastcgi-cache-bust', 'FastCGI Cache'), |
88
|
|
|
'action' => function () { |
89
|
|
|
FastcgiCacheBust::$plugin->cache->clearAll(); |
90
|
|
|
}, |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
Craft::info( |
96
|
|
|
Craft::t( |
97
|
|
|
'fastcgi-cache-bust', |
98
|
|
|
'{name} plugin loaded', |
99
|
|
|
['name' => $this->name] |
100
|
|
|
), |
101
|
|
|
__METHOD__ |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Protected Methods |
106
|
|
|
// ========================================================================= |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $element |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
protected function shouldBustCache(Element $element): bool |
114
|
|
|
{ |
115
|
|
|
$bustCache = true; |
116
|
|
|
// Only bust the cache if the element is ENABLED or LIVE |
117
|
|
|
if (($element->getStatus() !== Element::STATUS_ENABLED) |
118
|
|
|
&& ($element->getStatus() !== Entry::STATUS_LIVE) |
119
|
|
|
) { |
120
|
|
|
$bustCache = false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/* @TODO: These need to be updated once the plugins are released for Craft 3 |
124
|
|
|
* if (($element instanceof 'SproutSeo_Redirect') |
125
|
|
|
* || ($element instanceof 'PushNotifications_Device') |
126
|
|
|
* ) { |
127
|
|
|
* $bustCache = false; |
128
|
|
|
* } |
129
|
|
|
*/ |
130
|
|
|
|
131
|
|
|
return $bustCache; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritdoc |
136
|
|
|
*/ |
137
|
|
|
protected function createSettingsModel() |
138
|
|
|
{ |
139
|
|
|
return new Settings(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritdoc |
144
|
|
|
*/ |
145
|
|
|
protected function settingsHtml(): string |
146
|
|
|
{ |
147
|
|
|
try { |
148
|
|
|
return Craft::$app->view->renderTemplate( |
149
|
|
|
'fastcgi-cache-bust/settings', |
150
|
|
|
[ |
151
|
|
|
'settings' => $this->getSettings(), |
152
|
|
|
] |
153
|
|
|
); |
154
|
|
|
} catch (\Twig_Error_Loader $e) { |
155
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
156
|
|
|
return ''; |
157
|
|
|
} catch (Exception $e) { |
158
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
159
|
|
|
return ''; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|