forked from supercool/Cache-Monster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheMonsterPlugin.php
192 lines (151 loc) · 4.53 KB
/
CacheMonsterPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace Craft;
/**
* CacheMonster by Supercool
*
* @package CacheMonster
* @author Josh Angell
* @copyright Copyright (c) 2015, Supercool Ltd
* @link http://plugins.supercooldesign.co.uk
*/
class CacheMonsterPlugin extends BasePlugin
{
// Properties
// =========================================================================
/**
* @var
*/
private $_settings;
// Public Methods
// =========================================================================
public function getName()
{
return Craft::t('CacheMonster');
}
public function getVersion()
{
return '0.9';
}
public function getDeveloper()
{
return 'Supercool';
}
public function getDeveloperUrl()
{
return 'http://plugins.supercooldesign.co.uk';
}
public function init()
{
/**
* Get plugin settings
*/
$plugin = craft()->plugins->getPlugin('cachemonster');
$this->_settings = $plugin->getSettings();
if ($this->_settings->uiWidget)
{
$this->initUIWidget();
}
/**
* Before we save, grab the paths that are going to be purged
* and save them to a cache
*/
craft()->on('elements.onBeforeSaveElement', function(Event $event)
{
// Get the element ID
$element = $event->params['element'];
$initialPath = $element->uri;
// for some reason, this function gets called twice, once with an empty path. we're not interested in that.
if (empty($initialPath)) return;
$elementId = $element->id;
$locale = $element->locale;
// If we have an element, go ahead and get its paths
if ($elementId)
{
// Clear our cacheMonsterPaths cache, just in case
craft()->cache->delete('cacheMonsterPaths-' . $elementId . '-' . $locale);
// Get the paths we need
$paths = craft()->cacheMonster->getPaths($elementId, $initialPath, $locale);
if ($paths)
{
// Store them in the cache so we can get them after
// the element has actually saved
craft()->cache->set('cacheMonsterPaths-' . $elementId . '-' . $locale, $paths);
}
}
});
/**
* After the element has saved run the purging and warming tasks
*/
craft()->on('elements.onSaveElement', function(Event $event)
{
$element = $event->params['element'];
$elementId = $element->id;
$startingPath = $element->uri;
$locale = $element->locale;
if ($elementId)
{
// Remove this, as it might cause issues if its used again
// CacheMonsterPlugin::log(print_r($paths, true), LogLevel::Error);
// Get the paths out of the cache for that element
$paths = craft()->cache->get('cacheMonsterPaths-' . $elementId . '-' . $locale);
// Use those paths to purge (if on) and warm
if ($paths)
{
if ($this->_settings['varnish'])
{
craft()->cacheMonster->makeTask('CacheMonster_Purge', $paths);
craft()->cache->delete('cacheMonsterPaths-' . $elementId . '-' . $locale);
}
craft()->cacheMonster->makeTask('CacheMonster_Warm', $paths);
}
}
});
}
public function getSettingsHtml()
{
return craft()->templates->render('cacheMonster/settings', array(
'settings' => $this->getSettings(),
'servers' => $this->getSettings()->servers,
'localeHosts' => $this->getSettings()->localeHosts
));
}
public function prepSettings($settings)
{
// Empty all servers
if (!isset($settings['servers']))
{
$settings['servers'] = array();
}
if (!isset($settings['localeHosts']))
{
$settings['localeHosts'] = array();
}
return $settings;
}
protected function defineSettings()
{
return array(
'varnish' => array(AttributeType::Bool, 'default' => false),
'uiWidget' => array(AttributeType::Bool, 'default' => false),
'servers' => array(AttributeType::Mixed, 'default' => array()),
'localeHosts' => array(AttributeType::Mixed, 'default' => array())
);
}
protected function initUIWidget()
{
// include this in every site request. show the widget to logged-in users with JS.
if ( craft()->request->isSiteRequest() )
{
$path = craft()->request->getRequestUri();
$host = craft()->request->getHostname();
$actionUserLoggedIn = UrlHelper::getActionUrl('cacheMonster/userLoggedIn');
$actionPurgeUrl = UrlHelper::getActionUrl('cacheMonster/purgeUrl', array(
'path'=> urlencode($path),
'host' => urlencode($host)
));
craft()->templates->includeCssResource('cachemonster/css/widget.css');
craft()->templates->includeJsResource('cachemonster/js/widget.js');
craft()->templates->includeJs('jQuery("body").cmUiWidget("' . $actionUserLoggedIn . '", "'.$actionPurgeUrl.'")');
}
}
}