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