forked from craftapi/htmlcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HtmlcachePlugin.php
240 lines (218 loc) · 6.49 KB
/
HtmlcachePlugin.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* HTML Cache plugin for Craft CMS
*
* Generate HTML-file based caching for your Craft CMS
*
* @author Chris - CraftAPI
* @copyright Copyright (c) 2016 CraftAPI
* @link https://github.com/craftapi
* @package HTMLCache
* @since 1.0.0
* @version 1.0.5.1
*/
namespace Craft;
class HtmlcachePlugin extends BasePlugin
{
/**
* Call the service to check if we already have a cache file; register events
*
* @return mixed
*/
public function init()
{
if (!function_exists('\htmlcache_index')) {
include_once 'functions/htmlcache.php';
}
if (!$this->isEnabled) {
\htmlcache_indexEnabled(false);
}
if ($this->isInstalled && $this->isEnabled) {
craft()->htmlcache_htmlcache->checkForCacheFile();
craft()->attachEventHandler('onEndRequest', function () {
craft()->htmlcache_htmlcache->createCacheFile();
});
craft()->on('entries.saveEntry', function (Event $event) {
craft()->htmlcache_htmlcache->clearCacheFiles();
});
}
}
/**
* Returns the user-facing name.
*
* @return mixed
*/
public function getName()
{
return Craft::t('HTML Cache');
}
/**
* Plugins can have descriptions of themselves displayed on the Plugins page by adding a getDescription() method
* on the primary plugin class:
*
* @return mixed
*/
public function getDescription()
{
return Craft::t('Generate HTML-file based caching for your Craft CMS');
}
/**
* Plugins can have links to their documentation on the Plugins page by adding a getDocumentationUrl() method on
* the primary plugin class:
*
* @return string
*/
public function getDocumentationUrl()
{
return 'https://github.com/craftapi/htmlcache/blob/master/README.md';
}
/**
* Plugins can now take part in Craft’s update notifications, and display release notes on the Updates page, by
* providing a JSON feed that describes new releases, and adding a getReleaseFeedUrl() method on the primary
* plugin class.
*
* @return string
*/
public function getReleaseFeedUrl()
{
return 'https://raw.githubusercontent.com/craftapi/htmlcache/master/releases.json';
}
/**
* Returns the version number.
*
* @return string
*/
public function getVersion()
{
return '1.0.5.1';
}
/**
* As of Craft 2.5, Craft no longer takes the whole site down every time a plugin’s version number changes, in
* case there are any new migrations that need to be run. Instead plugins must explicitly tell Craft that they
* have new migrations by returning a new (higher) schema version number with a getSchemaVersion() method on
* their primary plugin class:
*
* @return string
*/
public function getSchemaVersion()
{
return '1.0.0';
}
/**
* Returns the developer’s name.
*
* @return string
*/
public function getDeveloper()
{
return 'CraftAPI';
}
/**
* Returns the developer’s website URL.
*
* @return string
*/
public function getDeveloperUrl()
{
return 'https://github.com/craftapi';
}
/**
* Returns whether the plugin should get its own tab in the CP header.
*
* @return bool
*/
public function hasCpSection()
{
return false;
}
public function hasSettings()
{
return true;
}
/**
* Returns whether the plugin should get its own tab in the CP header.
*
* @return bool
*/
protected function defineSettings()
{
return [
'cacheDuration' => [AttributeType::Mixed, 'required' => true, 'default' => 3600],
'enableIndex' => [AttributeType::Bool, 'default' => 0, 'required' => true],
'enableGeneral' => [AttributeType::Bool, 'default' => 1, 'required' => true]
];
}
/**
* Returns the plugin settings
*
* @return html
*/
public function getSettingsHtml()
{
return craft()->templates->render('htmlcache/_settings', ['settings' => $this->getSettings()]);
}
/**
* Process the settings and check if the index needs to be altered
*
* @return function
*/
public function setSettings($values)
{
if (!function_exists('\htmlcache_indexEnabled')) {
include_once 'functions/htmlcache.php';
}
if (!empty($values['htmlcacheSettingsForm'])) {
// Write these settings to a .json file for offline reference
$path = craft()->path->getStoragePath() . 'runtime' . DIRECTORY_SEPARATOR . 'htmlcache' . DIRECTORY_SEPARATOR;
IOHelper::ensureFolderExists($path);
$fp = fopen($path . 'settings.json', 'w+');
if ($fp) {
fwrite($fp, json_encode($values));
fclose($fp);
}
\htmlcache_indexEnabled($values['enableIndex'] == 1 ? true : false);
// Check if it actually worked
if (stristr(file_get_contents($_SERVER['SCRIPT_FILENAME']), 'htmlcache') === false && $values['enableIndex'] == 1) {
craft()->userSession->setError(Craft::t('The file ' . $_SERVER['SCRIPT_FILENAME'] . ' could not be edited'));
return false;
}
}
if (!empty($values['purgeCache'])) {
craft()->htmlcache_htmlcache->clearCacheFiles();
}
return parent::setSettings($values);
}
/**
* Set the default settings
*
* @return function
*/
public function onAfterInstall()
{
craft()->request->redirect(UrlHelper::getCpUrl('/settings/plugins/htmlcache'));
}
/**
* Removes the index.php modification if set
*
* @return bool
*/
public function onBeforeUninstall()
{
if (!function_exists('\htmlcache_indexEnabled')) {
include_once 'functions/htmlcache.php';
}
// Make sure to delete any reference in the public/index.php file
\htmlcache_indexEnabled(false);
}
/**
* Adds HTML_cache paths to the list of things the Clear Caches tool can delete.
*
* @return array
*/
public function registerCachePaths()
{
return array(
craft()->htmlcache_htmlcache->clearCacheFiles() => Craft::t('Htmlcache cached pages')
);
}
}