PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.16.0
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.16.0
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
AdvancedMathCaptcha.php 3 years ago AeliaCurrencySwitcher.php 3 years ago BeaverBuilder.php 4 years ago CF_Helper.php 3 years 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 2 years 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 3 years ago WPBakeryNP.php 3 years ago WPCacheHelper.php 3 years ago WPForms.php 3 years ago WPRocket.php 4 years ago Woocommerce.php 4 years ago WoocommerceCacheHandler.php 4 years ago YoastSEO.php 3 years ago
GeoTargetingWP.php
105 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'];
8 const defaultVariationCookies = ['geot_rocket_country'];
9 private $printedCookies = [];
10
11 public static function isActive() {
12 return defined("GEOWP_VERSION");
13 }
14
15 public function init($stage) {
16 $siteConfig = get_nitropack()->getSiteConfig();
17 $geotSettings = null;
18
19 if (empty($siteConfig["isGeoTargetingWPActive"])) {
20 return true;
21 }
22
23 // no need for variation cookies with GEOWP if using Ajax mode
24 if (function_exists( 'geot_settings' )) {
25 $geotSettings = geot_settings();
26 } elseif (function_exists('get_option')) {
27 $geotSettings = apply_filters( 'geot/settings_page/opts', get_option( 'geot_settings' ) );
28 }
29
30 if (!empty($geotSettings) && !empty($geotSettings['ajax_mode'])) {
31 return true;
32 }
33
34 // enable geot cookies
35 add_filter( 'geot/enable_cookies', '__return_true');
36
37 // require geot cookies for serving cache
38 add_filter("nitropack_passes_cookie_requirements", [$this, "hasGeoTargetingWpCookies"]);
39
40 // serve cache after geowp has added geot cookies
41 add_action('init', function() {
42 nitropack_handle_request('geotargetingwp');
43 }, 16);
44
45 add_action('np_set_cookie_filter', function() {
46 \NitroPack\SDK\NitroPack::addCookieFilter([$this, "filterCookies"]);
47 });
48
49 return true;
50 }
51
52 public static function getCustomVariationCookies() {
53 $enabledCookies = self::defaultVariationCookies;
54 // apply_filter() is unavailable at stage 'very_early'
55 // $enabledCookies = apply_filter("nitropack_geotargetingwp_enabled_cookies", self::defaultVariationCookies);
56 return array_intersect(self::allGeoWpCookies, $enabledCookies);
57 }
58
59 public static function configureVariationCookies() {
60 $siteConfig = get_nitropack()->getSiteConfig();
61
62 if (empty($siteConfig["isGeoTargetingWPActive"])) {
63 removeVariationCookies(self::allGeoWpCookies);
64 return true;
65 }
66
67 // standard cookie integration
68 initVariationCookies(self::getCustomVariationCookies());
69 }
70
71 public function hasGeoTargetingWpCookies($currentState) {
72 $allCookies = array_merge($_COOKIE, getNewCookies());
73 $neededCookies = self::getCustomVariationCookies();
74
75 foreach($neededCookies as $c) {
76 if (!empty($allCookies[$c])) {
77 // Needed so reverse proxies don't end up caching these pages.
78 if (!in_array($c, $this->printedCookies)) {
79 $val = $allCookies[$c];
80 if (is_array($val)) {
81 $val = end($val);
82 }
83 setcookie($c, $val, time() + 86000);
84 $this->printedCookies[] = $c;
85 }
86 $neededCookies = array_diff($neededCookies, [$c]);
87 }
88 }
89
90 if (!empty($neededCookies)) {
91 return false;
92 }
93 return $currentState;
94 }
95
96 public function filterCookies(&$cookies) {
97 foreach (self::getCustomVariationCookies() as $cookieName) {
98 $newlySetCookie = getNewCookie($cookieName);
99 if (empty($_COOKIE[$cookieName]) && !empty($newlySetCookie)) {
100 $cookies[$cookieName] = $newlySetCookie;
101 }
102 }
103 }
104 }
105