PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.4.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.4.0
4.7.0 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 All 111 releases
wp-optimize / cache / file-based-page-cache.php

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

149 lines 5.4 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 * File based page cache drop in
7 */
8 require_once(dirname(__FILE__) . '/file-based-page-cache-functions.php');
9
10 if (!defined('WPO_CACHE_DIR')) define('WPO_CACHE_DIR', untrailingslashit(WP_CONTENT_DIR) . '/wpo-cache');
11
12 if (wpo_is_activity_stream_requested()) return;
13 if (wpo_is_robots_txt_requested()) return;
14
15 // Fix for compatibility issue with Jetpack's infinity scroll feature
16 if (isset($_GET['infinity']) && 'scrolling' === $_GET['infinity']) return;
17
18 /**
19 * Load extensions.
20 */
21 wpo_cache_load_extensions();
22
23 /**
24 * Action triggered when the cache extensions are all loaded. Allows to execute code depending on an other extension, without knowing the order in which the files are loaded.
25 */
26 if (function_exists('do_action')) {
27 do_action('wpo_cache_extensions_loaded');
28 }
29
30 $no_cache_because = array();
31
32 // check if we want to cache current page.
33 if (function_exists('add_filter') && function_exists('apply_filters')) {
34 add_filter('wpo_restricted_cache_page_type', 'wpo_restricted_cache_page_type');
35 add_filter('wpo_url_in_conditional_tags_exceptions', 'wpo_url_in_conditional_tags_exceptions');
36 $restricted_cache_page_type = apply_filters('wpo_restricted_cache_page_type', false);
37 $conditional_tag_exceptions = wpo_url_in_conditional_tags_exceptions();
38 } else {
39 // On old WP versions, you can't filter the result
40 $restricted_cache_page_type = wpo_restricted_cache_page_type(false);
41 $conditional_tag_exceptions = wpo_url_in_conditional_tags_exceptions();
42 }
43
44 if ($restricted_cache_page_type) {
45 $no_cache_because[] = $restricted_cache_page_type;
46 }
47
48 if ($conditional_tag_exceptions) {
49 $no_cache_because[] = $conditional_tag_exceptions;
50 }
51
52 // Don't cache non-GET requests.
53 if (!isset($_SERVER['REQUEST_METHOD']) || 'GET' !== $_SERVER['REQUEST_METHOD']) {
54 $no_cache_because[] = 'The request method was not GET ('.(isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '-').')';
55 }
56
57 $file_extension = $_SERVER['REQUEST_URI'];
58 $file_extension = preg_replace('#^(.*?)\?.*$#', '$1', $file_extension);
59 $file_extension = trim(preg_replace('#^.*\.(.*)$#', '$1', $file_extension));
60
61 // Don't cache disallowed extensions. Prevents wp-cron.php, xmlrpc.php, etc.
62 if (!preg_match('#index\.php$#i', $_SERVER['REQUEST_URI']) && preg_match('#sitemap([a-zA-Z0-9_-]+)?\.xml$#i', $_SERVER['REQUEST_URI']) && in_array($file_extension, array('php', 'xml', 'xsl'))) {
63 $no_cache_because[] = 'The request extension is not suitable for caching';
64 }
65
66 // Don't cache if logged in.
67 if (!empty($_COOKIE)) {
68 $wp_cookies = array('wordpressuser_', 'wordpresspass_', 'wordpress_sec_', 'wordpress_logged_in_');
69
70 if (!wpo_cache_loggedin_users()) {
71 foreach ($_COOKIE as $key => $value) {
72 foreach ($wp_cookies as $cookie) {
73 if (false !== strpos($key, $cookie)) {
74 $no_cache_because[] = 'WordPress login cookies were detected';
75 break(2);
76 }
77 }
78 }
79 }
80
81 if (!empty($_COOKIE['wpo_commented_post'])) {
82 $no_cache_because[] = 'The user has commented on a post (comment cookie set)';
83 }
84
85 // get cookie exceptions from options.
86 $cache_exception_cookies = !empty($GLOBALS['wpo_cache_config']['cache_exception_cookies']) ? $GLOBALS['wpo_cache_config']['cache_exception_cookies'] : array();
87 // filter cookie exceptions, since WP 4.6
88 $cache_exception_cookies = function_exists('apply_filters') ? apply_filters('wpo_cache_exception_cookies', $cache_exception_cookies) : $cache_exception_cookies;
89
90 // check if any cookie exists from exception list.
91 if (!empty($cache_exception_cookies)) {
92 foreach ($_COOKIE as $key => $value) {
93 foreach ($cache_exception_cookies as $cookie) {
94 if ('' != trim($cookie) && false !== strpos($key, $cookie)) {
95 $no_cache_because[] = 'An excepted cookie was set ('.$key.')';
96 break 2;
97 }
98 }
99 }
100 }
101 }
102
103 // check in not disabled current user agent
104 if (!empty($_SERVER['HTTP_USER_AGENT']) && false === wpo_is_accepted_user_agent($_SERVER['HTTP_USER_AGENT'])) {
105 $no_cache_because[] = "In the settings, caching is disabled for matches for this request's user agent";
106 }
107
108 // Deal with optional cache exceptions.
109 if (wpo_url_in_exceptions(wpo_current_url())) {
110 $no_cache_because[] = 'In the settings, caching is disabled for matches for the current URL';
111 }
112
113 if (!empty($_GET)) {
114 // get variables used for building filename.
115 $get_variable_names = wpo_cache_query_variables();
116
117 $get_variables = wpo_cache_maybe_ignore_query_variables(array_keys($_GET));
118
119 // if GET variables include one or more undefined variable names then we don't cache.
120 $get_variables_diff = array_diff($get_variables, $get_variable_names);
121 if (!empty($get_variables_diff)) {
122 $no_cache_because[] = "In the settings, caching is disabled for matches for one of the current request's GET parameters";
123 }
124 }
125
126 if (!empty($no_cache_because)) {
127 $no_cache_because_message = implode(', ', $no_cache_because);
128
129 // Add http header
130 if (!defined('DOING_CRON') || !DOING_CRON) {
131 wpo_cache_add_nocache_http_header_with_send_headers_action($no_cache_because_message);
132 }
133
134 if ((!defined('DOING_CRON') || !DOING_CRON)) {
135 $not_cached_details = "";
136
137 // Output the reason only when the user has turned on debugging
138 if (((defined('WP_DEBUG') && WP_DEBUG) || isset($_GET['wpo_cache_debug']))) {
139 $not_cached_details = "because: ".htmlspecialchars($no_cache_because_message);
140 }
141 wpo_cache_add_footer_output(sprintf("Page not served from cache %s", $not_cached_details));
142 }
143 return;
144 }
145
146 wpo_serve_cache();
147
148 ob_start('wpo_cache');
149