AdvancedMathCaptcha.php
3 years ago
AeliaCurrencySwitcher.php
3 years ago
BeaverBuilder.php
4 years ago
CF_Helper.php
3 years ago
Cloudflare.php
3 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
3 years ago
GravityForms.php
3 years ago
JetPackNP.php
3 years ago
NginxHelper.php
4 years ago
RC.php
3 years ago
RankMathNP.php
3 years ago
RocketNet_Helper.php
4 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
94 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack\Integration\Plugin; |
| 4 | |
| 5 | class GeoTargetingWP { |
| 6 | const STAGE = "very_early"; |
| 7 | const allGeoWpCookies = ['geot_country', 'geot_state', 'geot_city']; |
| 8 | const defaultVariationCookies = ['geot_country']; |
| 9 | |
| 10 | public static function isActive() { |
| 11 | return defined("GEOWP_VERSION"); |
| 12 | } |
| 13 | |
| 14 | public function init($stage) { |
| 15 | $siteConfig = get_nitropack()->getSiteConfig(); |
| 16 | $geotSettings = null; |
| 17 | |
| 18 | if (empty($siteConfig["isGeoTargetingWPActive"])) { |
| 19 | return true; |
| 20 | } |
| 21 | |
| 22 | // no need for variation cookies with GEOWP if using Ajax mode |
| 23 | if (function_exists( 'geot_settings' )) { |
| 24 | $geotSettings = geot_settings(); |
| 25 | } elseif (function_exists('get_option')) { |
| 26 | $geotSettings = apply_filters( 'geot/settings_page/opts', get_option( 'geot_settings' ) ); |
| 27 | } |
| 28 | if (!empty($geotSettings) && $geotSettings['ajax_mode'] == "1") { |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | // enable geot cookies |
| 33 | add_filter( 'geot/enable_cookies', '__return_true'); |
| 34 | |
| 35 | // require geot cookies for serving cache |
| 36 | add_filter("nitropack_passes_cookie_requirements", [$this, "hasGeoTargetingWpCookies"]); |
| 37 | |
| 38 | // serve cache after geowp has added geot cookies |
| 39 | add_action('init', function() { |
| 40 | nitropack_handle_request('geotargetingwp'); |
| 41 | }, 16); |
| 42 | |
| 43 | add_action('np_set_cookie_filter', function() { |
| 44 | \NitroPack\SDK\NitroPack::addCookieFilter([$this, "filterCookies"]); |
| 45 | }); |
| 46 | |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | public static function getCustomVariationCookies() { |
| 51 | $enabledCookies = self::defaultVariationCookies; |
| 52 | // apply_filter() is unavailable at stage 'very_early' |
| 53 | // $enabledCookies = apply_filter("nitropack_geotargetingwp_enabled_cookies", self::defaultVariationCookies); |
| 54 | return array_intersect(self::allGeoWpCookies, $enabledCookies); |
| 55 | } |
| 56 | |
| 57 | public static function configureVariationCookies() { |
| 58 | $siteConfig = get_nitropack()->getSiteConfig(); |
| 59 | |
| 60 | if (empty($siteConfig["isGeoTargetingWPActive"])) { |
| 61 | removeVariationCookies(self::allGeoWpCookies); |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | // standard cookie integration |
| 66 | initVariationCookies(self::getCustomVariationCookies()); |
| 67 | } |
| 68 | |
| 69 | public function hasGeoTargetingWpCookies($currentState) { |
| 70 | $allCookies = array_merge($_COOKIE, getNewCookies()); |
| 71 | $neededCookies = self::getCustomVariationCookies(); |
| 72 | |
| 73 | foreach($neededCookies as $c) { |
| 74 | if (!empty($allCookies[$c])) { |
| 75 | $neededCookies = array_diff($neededCookies, [$c]); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (!empty($neededCookies)) { |
| 80 | return false; |
| 81 | } |
| 82 | return $currentState; |
| 83 | } |
| 84 | |
| 85 | public function filterCookies(&$cookies) { |
| 86 | foreach (self::getCustomVariationCookies() as $cookieName) { |
| 87 | $newlySetCookie = getNewCookie($cookieName); |
| 88 | if (empty($_COOKIE[$cookieName]) && !empty($newlySetCookie)) { |
| 89 | $cookies[$cookieName] = $newlySetCookie; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 |