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