ACF.php
1 year ago
AdvancedMathCaptcha.php
1 year ago
AeliaCurrencySwitcher.php
11 months ago
BeaverBuilder.php
1 year ago
CF_Helper.php
5 months ago
CURCY_MultiCurrency.php
1 year ago
Cloudflare.php
5 months ago
CommonHelpers.php
1 year ago
CookieNotice.php
1 year ago
DownloadManager.php
1 year ago
Elementor.php
5 months ago
Ezoic.php
1 year ago
FusionBuilder.php
1 year ago
GeoTargetingWP.php
1 year ago
GravityForms.php
1 year ago
JetPackNP.php
1 year ago
MPG.php
11 months ago
NginxHelper.php
1 year ago
RC.php
11 months ago
RankMathNP.php
1 year ago
ShortPixel.php
1 year ago
SquirrlySEO.php
1 year ago
TheEventsCalendar.php
1 year ago
ThriveTheme.php
1 year ago
WCML.php
1 year ago
WPBakeryNP.php
1 year ago
WPCacheHelper.php
1 year ago
WPForms.php
1 year ago
WPML.php
1 year ago
WPRocket.php
1 year ago
WooCommerce.php
11 months ago
WoocommerceCacheHandler.php
1 year ago
YoastSEO.php
1 year ago
GeoTargetingWP.php
243 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack\Integration\Plugin; |
| 4 | |
| 5 | class GeoTargetingWP { |
| 6 | const STAGE = "very_early"; |
| 7 | const allGeoWpCookies = ['geot_rocket_country', 'geot_rocket_state', 'geot_rocket_city', 'STYXKEY_geot_country']; |
| 8 | const defaultVariationCookies = ['nitro_geot_country_code']; |
| 9 | const cookieMap = [ |
| 10 | 'geot_rocket_country' => 'nitro_geot_country_code', |
| 11 | 'geot_rocket_state' => 'nitro_geot_region', |
| 12 | 'geot_rocket_city' => 'nitro_geot_city_code', |
| 13 | 'STYXKEY_geot_country' => 'nitro_geot_country_code', |
| 14 | ]; |
| 15 | private $printedCookies = []; |
| 16 | private $cache = array(); |
| 17 | private $cacheDir = NULL; |
| 18 | |
| 19 | public static function isActive() { |
| 20 | return defined("GEOWP_VERSION"); |
| 21 | } |
| 22 | |
| 23 | public function init($stage) { |
| 24 | $siteConfig = get_nitropack()->getSiteConfig(); |
| 25 | $geotSettings = null; |
| 26 | |
| 27 | if (empty($siteConfig["isGeoTargetingWPActive"])) { |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | // no need for variation cookies with GEOWP if using Ajax mode |
| 32 | if (function_exists('geot_settings')) { |
| 33 | $geotSettings = geot_settings(); |
| 34 | } elseif (function_exists('get_option')) { |
| 35 | $geotSettings = apply_filters('geot/settings_page/opts', get_option('geot_settings')); |
| 36 | } |
| 37 | |
| 38 | if (!empty($geotSettings) && !empty($geotSettings['ajax_mode'])) { |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | $this->cacheDir = nitropack_trailingslashit(NITROPACK_DATA_DIR) . nitropack_trailingslashit("geotwp-cache"); |
| 43 | |
| 44 | // !!! IMPORTANT !!! |
| 45 | // We should not purge the response data cache because cache warmup will not work properly then |
| 46 | // The add_action line below is left here as a place to attach this comment on |
| 47 | // If response data cache is purged then the overrides for cache warmup requests will not happen |
| 48 | // and we will always get the default response data or wherever the IP of the optimization server is |
| 49 | //add_action('nitropack_execute_purge_all', [$this, 'purgeCache']); |
| 50 | |
| 51 | add_filter('geot/response_data', function($value) { |
| 52 | // TODO: Add more parametrs to the key. Example region and city. Base the parameters on the variation cookie settings |
| 53 | $key = $value->country->iso_code; |
| 54 | if (nitropack_is_optimizer_request() && !empty($_COOKIE["nitro_geot_country_code"])) { |
| 55 | // Look for a response override |
| 56 | $key = $_COOKIE["nitro_geot_country_code"]; |
| 57 | |
| 58 | if ($this->hasCache($key)) { |
| 59 | // Override based on the request cookie |
| 60 | return unserialize($this->getCache($key)); |
| 61 | } |
| 62 | } else { |
| 63 | $this->setCache($key, serialize($value)); |
| 64 | } |
| 65 | |
| 66 | return $value; |
| 67 | }, 10, 1); |
| 68 | |
| 69 | // enable geot cookies |
| 70 | add_filter('geot/enable_rocket_cookies', '__return_true'); |
| 71 | add_filter('geot/disable_cookies', '__return_false'); |
| 72 | |
| 73 | // require geot cookies for serving cache |
| 74 | add_filter("nitropack_passes_cookie_requirements", [$this, "hasGeoTargetingWpCookies"]); |
| 75 | |
| 76 | // serve cache after geowp has added geot cookies |
| 77 | add_action('init', function () { |
| 78 | nitropack_handle_request('geotargetingwp'); |
| 79 | }, 16); |
| 80 | |
| 81 | add_action('np_set_cookie_filter', function () { |
| 82 | \NitroPack\SDK\NitroPack::addCookieFilter([$this, "filterCookies"]); |
| 83 | }); |
| 84 | |
| 85 | if ( nitropack_is_optimizer_request() ) { |
| 86 | add_filter('geot/enable_crawl_detection', '__return_false'); |
| 87 | } |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | public static function getCustomVariationCookies() { |
| 93 | $enabledCookies = self::defaultVariationCookies; |
| 94 | // apply_filter() is unavailable at stage 'very_early' |
| 95 | // $enabledCookies = apply_filter("nitropack_geotargetingwp_enabled_cookies", self::defaultVariationCookies); |
| 96 | return array_intersect(self::allGeoWpCookies, $enabledCookies); |
| 97 | } |
| 98 | |
| 99 | public static function configureVariationCookies() { |
| 100 | return; // TODO: Reimplement to work with nitro_geot_* cookies |
| 101 | $siteConfig = get_nitropack()->getSiteConfig(); |
| 102 | |
| 103 | if (empty($siteConfig["isGeoTargetingWPActive"])) { |
| 104 | removeVariationCookies(self::allGeoWpCookies); |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | // standard cookie integration |
| 109 | initVariationCookies(self::getCustomVariationCookies()); |
| 110 | } |
| 111 | |
| 112 | public function hasGeoTargetingWpCookies($currentState) { |
| 113 | $allCookies = array_merge($_COOKIE, getNewCookies()); |
| 114 | $neededCookies = self::getCustomVariationCookies(); |
| 115 | |
| 116 | foreach ($neededCookies as $c) { |
| 117 | if (!empty($allCookies[$c])) { |
| 118 | // Needed so reverse proxies don't end up caching these pages. |
| 119 | if (!in_array($c, $this->printedCookies)) { |
| 120 | $val = $allCookies[$c]; |
| 121 | if (is_array($val)) { |
| 122 | $val = end($val); |
| 123 | } |
| 124 | nitropack_setcookie($c, $val, time() + 86000); |
| 125 | $this->printedCookies[] = $c; |
| 126 | } |
| 127 | $neededCookies = array_diff($neededCookies, [$c]); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (!empty($neededCookies)) { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | return $currentState; |
| 136 | } |
| 137 | |
| 138 | public function filterCookies(&$cookies) { |
| 139 | foreach (self::cookieMap as $geotCookie => $nitroCookie) { |
| 140 | if (!empty($_COOKIE[$geotCookie])) { |
| 141 | $cookies[$nitroCookie] = $_COOKIE[$geotCookie]; |
| 142 | } else { |
| 143 | $newlySetCookie = getNewCookie($geotCookie); |
| 144 | if (!empty($newlySetCookie)) { |
| 145 | $cookies[$nitroCookie] = $newlySetCookie; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | //foreach (self::getCustomVariationCookies() as $cookieName) { |
| 151 | // $newlySetCookie = getNewCookie($cookieName); |
| 152 | // if (!empty($_COOKIE[$cookieName])) { |
| 153 | // $cookies[] |
| 154 | // } |
| 155 | // if (empty($_COOKIE[$cookieName]) && !empty($newlySetCookie)) { |
| 156 | // $cookies[$cookieName] = $newlySetCookie; |
| 157 | // } |
| 158 | //} |
| 159 | } |
| 160 | |
| 161 | public function purgeCache($key = NULL) { |
| 162 | if (!$this->cacheDir || !is_dir($this->cacheDir)) return; |
| 163 | |
| 164 | if ($key) { |
| 165 | $cacheFile = $this->getCacheFile($key); |
| 166 | if (file_exists($cacheFile)) { |
| 167 | unlink($cacheFile); |
| 168 | } |
| 169 | |
| 170 | $cacheFileWp = $this->getCacheFile("wpremote-" . $key); |
| 171 | if (file_exists($cacheFileWp)) { |
| 172 | unlink($cacheFileWp); |
| 173 | } |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | $dh = opendir($this->cacheDir); |
| 178 | while (($entry = readdir($dh)) !== false) { |
| 179 | if ($entry == "." || $entry == "..") continue; |
| 180 | |
| 181 | $cacheFile = $this->cacheDir . $entry; |
| 182 | if (!is_file($cacheFile)) continue; |
| 183 | unlink($cacheFile); |
| 184 | } |
| 185 | closedir($dh); |
| 186 | rmdir($this->cacheDir); |
| 187 | } |
| 188 | |
| 189 | public function hasCache($key) { |
| 190 | if(!empty($this->cache[$key])) { |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | $cacheFile = $this->getCacheFile($key); |
| 195 | if (file_exists($cacheFile)) { |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | public function getCache($key) { |
| 203 | if(empty($this->cache[$key])) { |
| 204 | $cacheFile = $this->getCacheFile($key); |
| 205 | if (file_exists($cacheFile)) { |
| 206 | $this->cache[$key] = file_get_contents($cacheFile); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if(!empty($this->cache[$key])) { |
| 211 | return $this->cache[$key]; |
| 212 | } |
| 213 | |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | public function setCache($key, $content) { |
| 218 | $this->cache[$key] = $content; |
| 219 | |
| 220 | if (!$this->cacheDir) { |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | if (!is_dir($this->cacheDir) && !mkdir($this->cacheDir, 0755, true)) { |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | file_put_contents($this->getCacheFile($key), $content); |
| 229 | } |
| 230 | |
| 231 | private function getCacheFile($key) { |
| 232 | if (!$this->cacheDir) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | return $this->cacheDir . md5($key); |
| 237 | } |
| 238 | |
| 239 | public function isCacheAllowed($key) { |
| 240 | return true; |
| 241 | } |
| 242 | } |
| 243 |