-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThemeCache.php
executable file
·233 lines (183 loc) · 6.26 KB
/
ThemeCache.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
<?php
namespace NetLimeTheme\Extensions;
use NetLimeTheme\Core\Lib\ThemeModuleBase;
use NetLimeTheme\Core\Lib\ThemeSectionBase;
class ThemeCache extends ThemeModuleBase
{
protected $cachePath = WP_CONTENT_DIR . "/cache/theme/";
protected $sectionsRendered = [];
public function init()
{
do_action("before_theme_cache_init");
$this->setupCacheDirectory();
$this->flushOldCache();
$this->setupFlushCache();
do_action("after_theme_cache_init");
}
protected function setupCacheDirectory()
{
do_action("before_theme_cache_setup_directory");
if (!file_exists($this->cachePath)):
mkdir($this->cachePath, 0755, true);
endif;
do_action("before_theme_cache_setup_directory");
}
/**
* Registers link & action for it on admin bar to flush cache
*/
protected function setupFlushCache()
{
do_action("before_theme_setup_flush_cache");
# Add flush cache link to admin bar
add_action('admin_bar_menu', function ($wp_admin_bar) {
$args = array(
'id' => 'cache_flush_link',
'title' => 'Flush cache',
'href' => get_admin_url() . '?flushCache',
'meta' => array('class' => 'cache-flush-link')
);
$wp_admin_bar->add_node($args);
}, 999);
# Force cleat all cache, otherwise just remove old
if (isset($_GET["flushCache"]) && is_admin() && is_user_logged_in()):
$this->flushCache();
$this->doNotice(__("Cache has been flushed."));
endif;
do_action("after_theme_setup_flush_cache");
}
/**
* Show notice in admin area
*
* @param $message string A notice message
*/
protected function doNotice($message)
{
do_action("before_theme_notice");
add_action('admin_notices', function () use ($message) {
echo '<div class="notice notice-success is-dismissible">
<p>' . $message . '</p>
</div>';
});
do_action("after_theme_notice");
}
/**
* Force clear all cached files
*/
protected function flushCache()
{
do_action("before_theme_flush_cache");
wp_cache_flush();
array_map("unlink", glob($this->cachePath . "*"));
do_action("after_theme_flush_cache");
}
/**
* Clears all cached files that is older than specified cache lifetime
* because sometimes bots are requesting urls that is visited only once and
* after a while there will be thousands of files without any reason.
*/
protected function flushOldCache()
{
do_action("before_theme_flush_old_cache");
$files = glob($this->cachePath . "*");
$files = (is_array($files) && $files ? $files : array());
$cache_config = $this->getConfig("cache");
foreach ($files as $file):
if (file_exists($file) && (time() - filemtime($file) > $cache_config["lifetime"])):
unlink($file);
endif;
endforeach;
do_action("after_ntl_flush_old_cache");
}
/**
* Get absolute path of cache file
*
* @param $cacheKey
* @return string
*/
public function getCacheFile($cacheKey)
{
do_action("before_theme_get_cache_file", $cacheKey);
$filename = md5($_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . "-" . $cacheKey);
do_action("after_theme_get_cache_file", $filename);
return $this->cachePath . $filename;
}
/**
* Create the cache
*
* @param string $sectionKey Key of section as registered
* @param ThemeSectionBase $section Section
*
* @return string Cached content
*/
public function doCache($sectionKey, &$section)
{
do_action("before_theme_do_cache", $sectionKey);
$cacheKey = $this->getCacheKey($sectionKey, $section);
# Get cache file absolute path
$file = $this->getCacheFile($cacheKey);
# Start buffering output
ob_start();
# Render the template
$section->init();
$section->render();
$cacheConfig = $this->getConfig("cache");
# Minify the output
if ($cacheConfig["minify"]):
$cache = str_replace([" ", "\n", "\t"], ["", "", ""], ob_get_clean());
else:
$cache = ob_get_clean();
endif;
# Write output to memory or file
if ($cacheConfig["type"] == "wp_cache"):
wp_cache_add(basename($file), $cache, "sections_cache", $cacheConfig["lifetime"]);
else:
# Delete file if already exists
if (file_exists($file)):
unlink($file);
endif;
# Prevents creating empty cache file
if (!empty($cache) || $cache != ""):
file_put_contents($file, $cache);
endif;
endif;
do_action("after_theme_do_cache", $cache);
# Return output
return $cache;
}
/**
* Get cached html
*
* @param $sectionKey
* @param ThemeSectionBase $section Section
* @return bool|mixed|string
*/
public function getCache($sectionKey, &$section)
{
do_action("before_theme_get_cache", $sectionKey);
$cacheKey = $this->getCacheKey($sectionKey, $section);
# Get cache file absolute path
$file = $this->getCacheFile($cacheKey);
# Define variable with false
$cache = false;
$cacheConfig = $this->getConfig("cache");
# TODO: When not redis, than wp_cache wont load from cache
# Get cached html depending on cache type
if ($cacheConfig["type"] == "wp_cache"):
$cache = wp_cache_get(basename($file), "sections_cache");
else:
if (file_exists($file) && (time() - filemtime($file) < $cacheConfig["lifetime"])):
$cache = file_get_contents($file);
endif;
endif;
do_action("after_ntl_get_cache", $cache);
# Return cached html
return $cache;
}
protected function getCacheKey(&$sectionKey, &$section)
{
if ($section->data !== []):
return $sectionKey . md5(json_encode($section->data));
endif;
return $sectionKey;
}
}