PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.17.9
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.17.9
1.19.8 1.19.7 1.19.6 1.19.5 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.12.0 1.13.0 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.16.7 1.16.8 1.17.0 1.17.6 1.17.7 1.17.8 1.17.9 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.18.5 1.18.6 1.18.7 1.18.8 1.18.9 1.19.0 1.19.1 1.19.2 1.19.3 1.19.4 1.3.19 1.3.20 1.4.0 1.4.1 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.16 1.5.17 1.5.18 1.5.19 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.8.1 1.8.3 1.9.0 1.9.1 1.9.2
nitropack / classes / Integration / Plugin / GeoTargetingWP.php
nitropack / classes / Integration / Plugin Last commit date
ACF.php 1 year ago AdvancedMathCaptcha.php 3 years ago AeliaCurrencySwitcher.php 3 years ago BeaverBuilder.php 4 years ago CF_Helper.php 3 years ago CURCY_MultiCurrency.php 1 year ago Cloudflare.php 2 years ago CommonHelpers.php 3 years ago CookieNotice.php 4 years ago DownloadManager.php 4 years ago Elementor.php 3 years ago Ezoic.php 4 years ago FusionBuilder.php 4 years ago GeoTargetingWP.php 1 year ago GravityForms.php 2 years ago JetPackNP.php 3 years ago NginxHelper.php 2 years ago RC.php 3 years ago RankMathNP.php 3 years ago ShortPixel.php 4 years ago SquirrlySEO.php 3 years ago TheEventsCalendar.php 3 years ago ThriveTheme.php 4 years ago WCML.php 1 year ago WPBakeryNP.php 3 years ago WPCacheHelper.php 3 years ago WPForms.php 3 years ago WPML.php 1 year ago WPRocket.php 4 years ago WooCommerce.php 1 year ago WoocommerceCacheHandler.php 4 years ago YoastSEO.php 3 years 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