-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacheful-serve.php
75 lines (57 loc) · 1.52 KB
/
cacheful-serve.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
<?php
function prepare_index_file()
{
$fileName = $_SERVER['DOCUMENT_ROOT'] . '/index.php';
$fileContent = file_get_contents($fileName);
if (preg_match('/(serve_page_end\(\);)/im', $fileContent)) {
return;
}
$fileContent .= "\n\n/** Wrap up the cache bufer **/\n\\serve_page_end();";
$fileContent = preg_replace("/\n\n+/s","\n\n", $fileContent);
file_put_contents($fileName, $fileContent);
}
function has_wordpress_cookie()
{
foreach ($_COOKIE as $key => $value) {
if (fnmatch('wp-settings-*', $key)) {
return true;
}
}
return false;
}
function should_cache()
{
return $_SERVER['REQUEST_METHOD'] === 'GET'
&& isConfigured()
&& !has_wordpress_cookie();
}
function serve_page_start()
{
if (!should_cache()) {
return;
}
prepare_index_file();
$cachefile = WP_CONTENT_DIR . '/cache/' . md5($_SERVER['REQUEST_URI']) . date('M-d-Y') . '.php';
$cachetime = 18000;
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
readfile($cachefile);
exit;
}
ob_start();
if (shouldSimulateSlow()) {
cacheful_simulate_slow();
}
}
function serve_page_end()
{
if (!should_cache()) {
return;
}
$cachefile = WP_CONTENT_DIR . '/cache/' . md5($_SERVER['REQUEST_URI']) . date('M-d-Y') . '.php';
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush();
}
// serve cache if applicable
serve_page_start();