PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 2.2.11
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v2.2.11
4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 3.8.0 All 110 releases
wp-optimize / cache / file-based-page-cache-functions.php

file-based-page-cache-functions.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 2.2.11, at cache/file-based-page-cache-functions.php

298 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) die('No direct access allowed');
4
5 /**
6 * Holds utility functions used by file based cache
7 */
8
9 /**
10 * Cache output before it goes to the browser
11 *
12 * @param string $buffer Page HTML.
13 * @param int $flags OB flags to be passed through.
14 * @return string
15 */
16 function wpo_cache($buffer, $flags) {
17 global $post;
18
19 if (strlen($buffer) < 255) {
20 return $buffer;
21 }
22
23 // Don't cache search, 404, or password protected.
24 if (is_404() || is_search() || !empty($post->post_password)) {
25 return $buffer;
26 }
27
28 // No root cache folder, exit here
29 if (!file_exists(WPO_CACHE_DIR)) {
30 // Can not cache!
31 return $buffer;
32 }
33
34 // Try creating a folder for cached files, if it was flushed recently
35 if (!file_exists(WPO_CACHE_FILES_DIR)) {
36 if (!mkdir(WPO_CACHE_FILES_DIR)) {
37 // Can not cache!
38 return $buffer;
39 }
40 }
41
42 $buffer = apply_filters('wpo_pre_cache_buffer', $buffer);
43
44 $url_path = wpo_get_url_path();
45
46 $dirs = explode('/', $url_path);
47
48 $path = WPO_CACHE_FILES_DIR;
49
50 foreach ($dirs as $dir) {
51 if (!empty($dir)) {
52 $path .= '/' . $dir;
53
54 if (!file_exists($path)) {
55 if (!mkdir($path)) {
56 // Can not cache!
57 return $buffer;
58 }
59 }
60 }
61 }
62
63 $modified_time = time(); // Make sure modified time is consistent.
64
65 // Prevent mixed content when there's an http request but the site URL uses https.
66 $home_url = get_home_url();
67
68 if (!is_ssl() && 'https' === strtolower(parse_url($home_url, PHP_URL_SCHEME))) {
69 $https_home_url = $home_url;
70 $http_home_url = str_ireplace('https://', 'http://', $https_home_url);
71 $buffer = str_replace(esc_url($http_home_url), esc_url($https_home_url), $buffer);
72 }
73
74 if (preg_match('#</html>#i', $buffer)) {
75 if (!empty($GLOBALS['wpo_cache_config']['enable_mobile_caching']) && wpo_is_mobile()) {
76 $buffer .= "\n<!-- Cached by WP Optimize for mobile devices - Last modified: " . gmdate('D, d M Y H:i:s', $modified_time) . " GMT -->\n";
77 } else {
78 $buffer .= "\n<!-- Cached by WP Optimize - Last modified: " . gmdate('D, d M Y H:i:s', $modified_time) . " GMT -->\n";
79 }
80 }
81
82 if (!empty($GLOBALS['wpo_cache_config']['enable_gzip_compression']) && function_exists('gzencode')) {
83 if (!empty($GLOBALS['wpo_cache_config']['enable_mobile_caching']) && wpo_is_mobile()) {
84 file_put_contents($path . '/mobile.index.gzip.html', gzencode($buffer, 3));
85 touch($path . '/mobile.index.gzip.html', $modified_time);
86 } else {
87 file_put_contents($path . '/index.gzip.html', gzencode($buffer, 3));
88 touch($path . '/index.gzip.html', $modified_time);
89 }
90 } else {
91 if (!empty($GLOBALS['wpo_cache_config']['enable_mobile_caching']) && wpo_is_mobile()) {
92 file_put_contents($path . '/mobile.index.html', $buffer);
93 touch($path . '/mobile.index.html', $modified_time);
94 } else {
95 file_put_contents($path . '/index.html', $buffer);
96 touch($path . '/index.html', $modified_time);
97 }
98 }
99
100 header('Cache-Control: no-cache'); // Check back every time to see if re-download is necessary.
101 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $modified_time) . ' GMT');
102
103 if (function_exists('ob_gzhandler') && !empty($GLOBALS['wpo_cache_config']['enable_gzip_compression'])) {
104 return ob_gzhandler($buffer, $flags);
105 } else {
106 return $buffer;
107 }
108 }
109
110 /**
111 * Serves the cache and exits
112 */
113 function wpo_serve_cache() {
114 $file_name = 'index.html';
115
116 if (function_exists('gzencode') && !empty($GLOBALS['wpo_cache_config']['enable_gzip_compression'])) {
117 $file_name = 'index.gzip.html';
118 }
119
120 if (!empty($GLOBALS['wpo_cache_config']['enable_mobile_caching']) && wpo_is_mobile()) {
121 $file_name = 'mobile.' . $file_name;
122 }
123
124 $path = WPO_CACHE_FILES_DIR . '/' . rtrim(wpo_get_url_path(), '/') . '/' . $file_name;
125
126
127 $modified_time = file_exists($path) ? (int) filemtime($path) : time();
128
129 header('Cache-Control: no-cache'); // Check back in an hour.
130
131 if (!empty($modified_time) && !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) === $modified_time) {
132 if (function_exists('gzencode') && !empty($GLOBALS['wpo_cache_config']['enable_gzip_compression'])) {
133 header('Content-Encoding: gzip');
134 }
135
136 header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified', true, 304);
137 exit;
138 }
139
140 if (file_exists($path) && is_readable($path)) {
141 if (function_exists('gzencode') && !empty($GLOBALS['wpo_cache_config']['enable_gzip_compression'])) {
142 header('Content-Encoding: gzip');
143 }
144
145 readfile($path);
146
147 exit;
148 }
149 }
150
151 /**
152 * Clears the cache
153 */
154 function wpo_cache_flush() {
155
156 $this->wpo_delete_files(WPO_CACHE_FILES_DIR);
157
158 if (function_exists('wp_cache_flush')) {
159 wp_cache_flush();
160 }
161 }
162
163 /**
164 * Get URL path for caching
165 *
166 * @since 1.0
167 * @return string
168 */
169 function wpo_get_url_path() {
170
171 $host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
172
173 return rtrim($host, '/') . $_SERVER['REQUEST_URI'];
174 }
175
176
177 /**
178 * Return true of exception url matches current url
179 *
180 * @param string $exception Exceptions to check URL against.
181 * @param bool $regex Whether to check with regex or not.
182 * @return bool true if matched, false otherwise
183 */
184 function wpo_url_exception_match($exception, $regex = false ) {
185 if (preg_match('#^[\s]*$#', $exception)) return false;
186
187 $exception = trim($exception);
188
189 if (!preg_match('#^/#', $exception)) {
190
191 $url = rtrim('http' . (isset($_SERVER['HTTPS']) ? 's' : '' ) . '://' . "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}", '/');
192
193 if ($regex) {
194 if (preg_match('#^' . $exception . '$#', $url)) {
195 // Exception match!
196 return true;
197 }
198 } elseif (preg_match('#\*$#', $exception)) {
199 $filtered_exception = str_replace('*', '', $exception);
200
201 if (preg_match('#^' . $filtered_exception . '#', $url)) {
202 // Exception match!
203 return true;
204 }
205 } else {
206 $exception = rtrim($exception, '/');
207
208 if (strtolower($exception) === strtolower($url)) {
209 // Exception match!
210 return true;
211 }
212 }
213 } else {
214 $path = $_SERVER['REQUEST_URI'];
215
216 if ($regex) {
217 if (preg_match('#^' . $exception . '$#', $path)) {
218 // Exception match!
219 return true;
220 }
221 } elseif (preg_match('#\*$#', $exception)) {
222 $filtered_exception = preg_replace('#/?\*#', '', $exception);
223
224 if (preg_match('#^' . $filtered_exception . '#i', $path)) {
225 // Exception match!
226 return true;
227 }
228 } else {
229 if ('/' !== $path) {
230 $path = rtrim($path, '/');
231 }
232
233 if ('/' !== $exception) {
234 $exception = rtrim($exception, '/');
235 }
236
237 if (strtolower($exception) === strtolower($path)) {
238 // Exception match!
239 return true;
240 }
241 }
242 }
243
244 return false;
245 }
246
247 /**
248 * Checks if its a mobile device
249 *
250 * @see https://developer.wordpress.org/reference/functions/wp_is_mobile/
251 */
252 function wpo_is_mobile() {
253 if (empty($_SERVER['HTTP_USER_AGENT'])) {
254 $is_mobile = false;
255 // many mobile devices (all iPhone, iPad, etc.)
256 } elseif (false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile')
257 || false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Android')
258 || false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/')
259 || false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle')
260 || false !== strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry')
261 || false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini')
262 || false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi')
263 ) {
264 $is_mobile = true;
265 } else {
266 $is_mobile = false;
267 }
268
269 return $is_mobile;
270 }
271
272 /**
273 * Delete function that deals with directories recursively
274 *
275 * @param string $src path of the folder
276 */
277 function wpo_delete_files($src) {
278 if (!file_exists($src)) return;
279
280 if (is_file($src)) unlink($src);
281
282 $dir = opendir($src);
283 $file = readdir($dir);
284
285 while (false !== $file) {
286 if ('.' != $file && '..' != $file) {
287 if (is_dir($src . '/' . $file)) {
288 wpo_delete_files($src . '/' . $file);
289 } else {
290 unlink($src . '/' . $file);
291 }
292 }
293 }
294
295 closedir($dir);
296 rmdir($src);
297 }
298