OptimizerWebPageCache.php
8 months ago
OptimizerWebPageCacheWP.php
8 months ago
OptimizerWebPageValidations.php
1 year ago
advanced-cache.php
3 years ago
OptimizerWebPageValidations.php
339 lines
| 1 | <?php |
| 2 | |
| 3 | namespace TenWebOptimizer\WebPageCache; |
| 4 | |
| 5 | use TenWebOptimizer\OptimizerUrl; |
| 6 | |
| 7 | class OptimizerWebPageValidations |
| 8 | { |
| 9 | protected static $instance = null; |
| 10 | |
| 11 | public function __construct() |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | public function allowed_request_method() |
| 16 | { |
| 17 | if (isset($_SERVER['REQUEST_METHOD'])) { |
| 18 | return in_array(strtoupper($_SERVER['REQUEST_METHOD']), ['GET', 'HEAD']); |
| 19 | } |
| 20 | |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Tell if the constant DONOTCACHEPAGE is set and not overridden. |
| 26 | * When defined, the page must not be cached. |
| 27 | * |
| 28 | * @return bool |
| 29 | */ |
| 30 | public function has_donotcachepage() |
| 31 | { |
| 32 | if (! defined('DONOTCACHEPAGE') || ! DONOTCACHEPAGE) { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * At this point the constant DONOTCACHEPAGE is set to true. |
| 38 | * This filter allows to force the page caching. |
| 39 | * It prevents conflict with some plugins like Thrive Leads. |
| 40 | * |
| 41 | * @since 2.5 |
| 42 | * |
| 43 | * @param bool $override_donotcachepage True will force the page to be cached. |
| 44 | */ |
| 45 | return ! apply_filters('two_override_donotcachepage', false); |
| 46 | } |
| 47 | |
| 48 | public function allowed_by_optimizer() |
| 49 | { |
| 50 | global $TwoSettings; |
| 51 | |
| 52 | return (!isset($_GET['two_critical_status']) && !isset($_GET['two_action']) && !isset($_GET['two_nooptimize']) && !isset($_GET['elementor-preview']) // phpcs:ignore |
| 53 | && OptimizerUrl::urlIsOptimizable() //whether url is excluded from fastcgi cached pages or manually excluded |
| 54 | && !(defined('REST_REQUEST') && REST_REQUEST) |
| 55 | && ('on' != $TwoSettings->get_settings('two_test_mode') || isset($_GET[ 'twbooster' ]))) // phpcs:ignore |
| 56 | || (isset($_GET['rest_route'])); // phpcs:ignore |
| 57 | } |
| 58 | |
| 59 | public function allowed_wp_page() |
| 60 | { |
| 61 | if (is_admin()) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | if (defined('DOING_AJAX') && DOING_AJAX) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | if (isset($_POST['wp_customize'])) { // phpcs:ignore |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | public function rejected_file() |
| 77 | { |
| 78 | $files = ['robots.txt', '.htaccess']; |
| 79 | |
| 80 | foreach ($files as $file) { |
| 81 | if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/' . $file) !== false) { // phpcs:ignore |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | public function reject_uri() |
| 90 | { |
| 91 | $uri_pattern = '/(?:.+/)?feed(?:/(?:.+/?)?)?$|/(?:.+/)?embed/|/(index\.php/)?wp\-json(/.*|$)'; |
| 92 | |
| 93 | return !isset($_SERVER['REQUEST_URI']) || (bool) preg_match('#^(' . $uri_pattern . ')$#i', $_SERVER['REQUEST_URI']); // phpcs:ignore |
| 94 | } |
| 95 | |
| 96 | public function rejected_cookie() |
| 97 | { |
| 98 | if (empty($_COOKIE)) { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | global $TwoSettings; |
| 103 | $rejected_cookies = '#wp-postpass_|wptouch_switch_toggle|comment_author_|comment_author_email_'; |
| 104 | |
| 105 | if ('on' !== $TwoSettings->get_settings('two_page_cache_user')) { |
| 106 | $rejected_cookies .= '|wordpress_logged_in_.+'; |
| 107 | } |
| 108 | $rejected_cookies .= '#'; |
| 109 | $excluded_cookies = []; |
| 110 | |
| 111 | foreach (array_keys($_COOKIE) as $cookie_name) { // phpcs:ignore |
| 112 | if (preg_match($rejected_cookies, $cookie_name)) { |
| 113 | $excluded_cookies[] = $cookie_name; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return count($excluded_cookies) > 0; |
| 118 | } |
| 119 | |
| 120 | public function reject_ua() |
| 121 | { |
| 122 | $rejected_uas = 'facebookexternalhit|WhatsApp'; |
| 123 | |
| 124 | return !isset($_SERVER['REQUEST_URI'], $_SERVER['HTTP_USER_AGENT']) || (bool) preg_match('#' . $rejected_uas . '#', $_SERVER['HTTP_USER_AGENT']); // phpcs:ignore |
| 125 | } |
| 126 | |
| 127 | public function allowed_query_string() |
| 128 | { |
| 129 | $get_params = $this->get_query_params(); |
| 130 | |
| 131 | if (empty($get_params)) { |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | $allowed_params = [ |
| 136 | 'lang', |
| 137 | 's', |
| 138 | 'permalink_name', |
| 139 | 'lp-variation-id', |
| 140 | 'twbooster' |
| 141 | ]; |
| 142 | |
| 143 | return count(array_intersect(array_keys($get_params), $allowed_params)) > 0; |
| 144 | } |
| 145 | |
| 146 | public function valid_buffer_to_cache($buffer) |
| 147 | { |
| 148 | if (strlen($buffer) <= 255) { |
| 149 | // Buffer length must be > 255 (IE does not read pages under 255 c). |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | if (http_response_code() !== 200) { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | if (!function_exists('is_404') || is_404()) { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | if (!function_exists('is_search') && is_search()) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | if (!preg_match('/<\s*\/\s*html\s*>/i', $buffer) && !preg_match('/<\s*\/\s*body\s*>/i', $buffer)) { |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | public function get_query_params() |
| 173 | { |
| 174 | if (empty($_GET)) { // phpcs:ignore |
| 175 | return []; |
| 176 | } |
| 177 | |
| 178 | $params_to_ignore = [ |
| 179 | 'utm_source' => 1, |
| 180 | 'utm_medium' => 1, |
| 181 | 'utm_campaign' => 1, |
| 182 | 'utm_expid' => 1, |
| 183 | 'utm_term' => 1, |
| 184 | 'utm_content' => 1, |
| 185 | 'mtm_source' => 1, |
| 186 | 'mtm_medium' => 1, |
| 187 | 'mtm_campaign' => 1, |
| 188 | 'mtm_keyword' => 1, |
| 189 | 'mtm_cid' => 1, |
| 190 | 'mtm_content' => 1, |
| 191 | 'pk_source' => 1, |
| 192 | 'pk_medium' => 1, |
| 193 | 'pk_campaign' => 1, |
| 194 | 'pk_keyword' => 1, |
| 195 | 'pk_cid' => 1, |
| 196 | 'pk_content' => 1, |
| 197 | 'fb_action_ids' => 1, |
| 198 | 'fb_action_types' => 1, |
| 199 | 'fb_source' => 1, |
| 200 | 'fbclid' => 1, |
| 201 | 'campaignid' => 1, |
| 202 | 'adgroupid' => 1, |
| 203 | 'adid' => 1, |
| 204 | 'gclid' => 1, |
| 205 | 'age-verified' => 1, |
| 206 | 'ao_noptimize' => 1, |
| 207 | 'usqp' => 1, |
| 208 | 'cn-reloaded' => 1, |
| 209 | '_ga' => 1, |
| 210 | 'sscid' => 1, |
| 211 | 'gclsrc' => 1, |
| 212 | '_gl' => 1, |
| 213 | 'mc_cid' => 1, |
| 214 | 'mc_eid' => 1, |
| 215 | '_bta_tid' => 1, |
| 216 | '_bta_c' => 1, |
| 217 | 'trk_contact' => 1, |
| 218 | 'trk_msg' => 1, |
| 219 | 'trk_module' => 1, |
| 220 | 'trk_sid' => 1, |
| 221 | 'gdfms' => 1, |
| 222 | 'gdftrk' => 1, |
| 223 | 'gdffi' => 1, |
| 224 | '_ke' => 1, |
| 225 | 'redirect_log_mongo_id' => 1, |
| 226 | 'redirect_mongo_id' => 1, |
| 227 | 'sb_referer_host' => 1, |
| 228 | 'mkwid' => 1, |
| 229 | 'pcrid' => 1, |
| 230 | 'ef_id' => 1, |
| 231 | 's_kwcid' => 1, |
| 232 | 'msclkid' => 1, |
| 233 | 'dm_i' => 1, |
| 234 | 'epik' => 1, |
| 235 | 'pp' => 1, |
| 236 | 'gbraid' => 1, |
| 237 | 'wbraid' => 1, |
| 238 | 'zenid' => 1, |
| 239 | 'lons1' => 1, |
| 240 | 'appns' => 1, |
| 241 | 'tstt1' => 1, |
| 242 | 'tsts6' => 1, |
| 243 | 'tsts12' => 1, |
| 244 | 'lpcid' => 1, |
| 245 | '3x-7' => 1, |
| 246 | 'mm_src' => 1, |
| 247 | 'muid' => 1, |
| 248 | 'vgo_ee' => 1, |
| 249 | 'mn' => 1, |
| 250 | 'irclickid' => 1, |
| 251 | 'irgwc' => 1, |
| 252 | '_xuid' => 1, |
| 253 | 'ref' => 1, |
| 254 | 'dclid' => 1, |
| 255 | 'mkt_tok' => 1, |
| 256 | 'ck_subscriber_id' => 1, |
| 257 | 'pk_kwd' => 1, |
| 258 | 'piwik_campaign' => 1, |
| 259 | 'piwik_kwd' => 1, |
| 260 | 'piwik_keyword' => 1, |
| 261 | 'mtm_group' => 1, |
| 262 | 'mtm_placement' => 1, |
| 263 | 'matomo_campaign' => 1, |
| 264 | 'matomo_keyword' => 1, |
| 265 | 'matomo_source' => 1, |
| 266 | 'matomo_medium' => 1, |
| 267 | 'matomo_content' => 1, |
| 268 | 'matomo_cid' => 1, |
| 269 | 'matomo_group' => 1, |
| 270 | 'matomo_placement' => 1, |
| 271 | 'hsa_cam' => 1, |
| 272 | 'hsa_grp' => 1, |
| 273 | 'hsa_mt' => 1, |
| 274 | 'hsa_src' => 1, |
| 275 | 'hsa_ad' => 1, |
| 276 | 'hsa_acc' => 1, |
| 277 | 'hsa_net' => 1, |
| 278 | 'hsa_kw' => 1, |
| 279 | 'hsa_tgt' => 1, |
| 280 | 'hsa_ver' => 1, |
| 281 | '_branch_match_id' => 1, |
| 282 | 'mkevt' => 1, |
| 283 | 'mkcid' => 1, |
| 284 | 'mkrid' => 1, |
| 285 | 'campid' => 1, |
| 286 | 'toolid' => 1, |
| 287 | 'customid' => 1, |
| 288 | 'igshid' => 1, |
| 289 | 'si' => 1, |
| 290 | 'iid' => 1, |
| 291 | '_iid' => 1, |
| 292 | 'pretty' => 1, |
| 293 | 'mod' => 1, |
| 294 | 'li_fat_id' => 1, |
| 295 | 'ttclid' => 1, |
| 296 | 'trueroas' => 1, |
| 297 | '_vsrefdom' => 1, |
| 298 | ]; |
| 299 | $params = array_diff_key($_GET, $params_to_ignore); // phpcs:ignore |
| 300 | |
| 301 | if ($params) { |
| 302 | // order is important because cache file name is being generated using $params |
| 303 | ksort($params); |
| 304 | } |
| 305 | |
| 306 | return $params; |
| 307 | } |
| 308 | |
| 309 | public function get_cookies() |
| 310 | { |
| 311 | if (empty($_COOKIE)) { |
| 312 | return []; |
| 313 | } |
| 314 | |
| 315 | $cookies_to_keep = [ |
| 316 | 'wcml_client_currency' |
| 317 | ]; |
| 318 | |
| 319 | $cookies = []; |
| 320 | |
| 321 | foreach ($cookies_to_keep as $cookie_name) { |
| 322 | if (isset($_COOKIE[$cookie_name])) { // phpcs:ignore |
| 323 | $cookies[$cookie_name] = $_COOKIE[$cookie_name]; //phpcs:ignore WordPressVIPMinimum.Variables.RestrictedVariables.cache_constraints___COOKIE,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return $cookies; |
| 328 | } |
| 329 | |
| 330 | public static function get_instance() |
| 331 | { |
| 332 | if (null == self::$instance) { |
| 333 | self::$instance = new self(); |
| 334 | } |
| 335 | |
| 336 | return self::$instance; |
| 337 | } |
| 338 | } |
| 339 |