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 |