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\events\DeleteTemplateCachesEvent; |
23
|
|
|
use craft\services\Elements; |
24
|
|
|
use craft\services\TemplateCaches; |
25
|
|
|
use craft\utilities\ClearCaches; |
26
|
|
|
|
27
|
|
|
use yii\base\Event; |
28
|
|
|
use yii\base\Exception; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class FastcgiCacheBust |
32
|
|
|
* |
33
|
|
|
* @author nystudio107 |
34
|
|
|
* @package FastcgiCacheBust |
35
|
|
|
* @since 1.0.0 |
36
|
|
|
* |
37
|
|
|
* @property CacheService cache |
38
|
|
|
*/ |
39
|
|
|
class FastcgiCacheBust extends Plugin |
40
|
|
|
{ |
41
|
|
|
// Static Properties |
42
|
|
|
// ========================================================================= |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var FastcgiCacheBust |
46
|
|
|
*/ |
47
|
|
|
public static $plugin; |
48
|
|
|
|
49
|
|
|
// Public Methods |
50
|
|
|
// ========================================================================= |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public function init() |
56
|
|
|
{ |
57
|
|
|
parent::init(); |
58
|
|
|
self::$plugin = $this; |
59
|
|
|
|
60
|
|
|
// Handler: Elements::EVENT_AFTER_SAVE_ELEMENT |
61
|
|
|
Event::on( |
62
|
|
|
Elements::class, |
63
|
|
|
Elements::EVENT_AFTER_SAVE_ELEMENT, |
64
|
|
|
function (ElementEvent $event) { |
65
|
|
|
Craft::debug( |
66
|
|
|
'Elements::EVENT_AFTER_SAVE_ELEMENT', |
67
|
|
|
__METHOD__ |
68
|
|
|
); |
69
|
|
|
/** @var Element $element */ |
70
|
|
|
$element = $event->element; |
71
|
|
|
// Only bust the cache if it's not certain excluded element types |
72
|
|
|
if ($this->shouldBustCache($element)) { |
73
|
|
|
Craft::debug( |
74
|
|
|
'Cache busted due to saving: '.\get_class($element).' - '.$element->title, |
75
|
|
|
__METHOD__ |
76
|
|
|
); |
77
|
|
|
FastcgiCacheBust::$plugin->cache->clearAll(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
); |
81
|
|
|
// Handler: TemplateCaches::EVENT_AFTER_DELETE_CACHES |
82
|
|
|
Event::on( |
83
|
|
|
TemplateCaches::class, |
84
|
|
|
TemplateCaches::EVENT_AFTER_DELETE_CACHES, |
85
|
|
|
function (DeleteTemplateCachesEvent $event) { |
|
|
|
|
86
|
|
|
FastcgiCacheBust::$plugin->cache->clearAll(); |
87
|
|
|
} |
88
|
|
|
); |
89
|
|
|
// Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS |
90
|
|
|
Event::on( |
91
|
|
|
ClearCaches::class, |
92
|
|
|
ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, |
93
|
|
|
function (RegisterCacheOptionsEvent $event) { |
94
|
|
|
$event->options[] = [ |
95
|
|
|
'key' => 'fastcgi-cache-bust', |
96
|
|
|
'label' => Craft::t('fastcgi-cache-bust', 'FastCGI Cache'), |
97
|
|
|
'action' => function () { |
98
|
|
|
FastcgiCacheBust::$plugin->cache->clearAll(); |
99
|
|
|
}, |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
Craft::info( |
105
|
|
|
Craft::t( |
106
|
|
|
'fastcgi-cache-bust', |
107
|
|
|
'{name} plugin loaded', |
108
|
|
|
['name' => $this->name] |
109
|
|
|
), |
110
|
|
|
__METHOD__ |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Protected Methods |
115
|
|
|
// ========================================================================= |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Determine whether the cache should be busted or not based on the $element |
119
|
|
|
* |
120
|
|
|
* @param $element |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
protected function shouldBustCache(Element $element): bool |
125
|
|
|
{ |
126
|
|
|
$bustCache = true; |
127
|
|
|
// Only bust the cache if the element is ENABLED or LIVE |
128
|
|
|
if (($element->getStatus() !== Element::STATUS_ENABLED) |
129
|
|
|
&& ($element->getStatus() !== Entry::STATUS_LIVE) |
130
|
|
|
) { |
131
|
|
|
$bustCache = false; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/* @TODO: These need to be updated once the plugins are released for Craft 3 |
135
|
|
|
* if (($element instanceof 'SproutSeo_Redirect') |
136
|
|
|
* || ($element instanceof 'PushNotifications_Device') |
137
|
|
|
* ) { |
138
|
|
|
* $bustCache = false; |
139
|
|
|
* } |
140
|
|
|
*/ |
141
|
|
|
|
142
|
|
|
return $bustCache; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @inheritdoc |
147
|
|
|
*/ |
148
|
|
|
protected function createSettingsModel() |
149
|
|
|
{ |
150
|
|
|
return new Settings(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @inheritdoc |
155
|
|
|
*/ |
156
|
|
|
protected function settingsHtml(): string |
157
|
|
|
{ |
158
|
|
|
try { |
159
|
|
|
return Craft::$app->view->renderTemplate( |
160
|
|
|
'fastcgi-cache-bust/settings', |
161
|
|
|
[ |
162
|
|
|
'settings' => $this->getSettings(), |
163
|
|
|
] |
164
|
|
|
); |
165
|
|
|
} catch (\Twig_Error_Loader $e) { |
166
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
167
|
|
|
return ''; |
168
|
|
|
} catch (Exception $e) { |
169
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
170
|
|
|
return ''; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.