diff --git a/app/code/Ometria/AbandonedCarts/etc/module.xml b/app/code/Ometria/AbandonedCarts/etc/module.xml index 6c5d764..18c634a 100755 --- a/app/code/Ometria/AbandonedCarts/etc/module.xml +++ b/app/code/Ometria/AbandonedCarts/etc/module.xml @@ -1,4 +1,4 @@ - + diff --git a/app/code/Ometria/Api/etc/module.xml b/app/code/Ometria/Api/etc/module.xml index fbdc90b..0f4faf0 100755 --- a/app/code/Ometria/Api/etc/module.xml +++ b/app/code/Ometria/Api/etc/module.xml @@ -1,4 +1,4 @@ - + diff --git a/app/code/Ometria/Core/Block/Head.php b/app/code/Ometria/Core/Block/Head.php index bb5fc02..a8b10d4 100755 --- a/app/code/Ometria/Core/Block/Head.php +++ b/app/code/Ometria/Core/Block/Head.php @@ -109,6 +109,22 @@ public function isDatalayerEnabled() ); } + /** + * @return bool + */ + public function isCookiebotEnabled() + { + return (bool) $this->scopeConfig->isSetFlag('ometria/advanced/enable_cookiebot'); + } + + /** + * @return string + */ + public function getCookiebotClass() + { + return (string) $this->scopeConfig->getValue('ometria/advanced/cookiebot_classification'); + } + /** * @return bool */ diff --git a/app/code/Ometria/Core/Helper/Config.php b/app/code/Ometria/Core/Helper/Config.php index 360a936..4452de8 100755 --- a/app/code/Ometria/Core/Helper/Config.php +++ b/app/code/Ometria/Core/Helper/Config.php @@ -106,4 +106,20 @@ public function getStockPushScope() { return (string) $this->coreHelperMageConfig->get('ometria/advanced/stock_push_scope'); } + + /** + * @return bool + */ + public function isCookiebotEnabled() + { + return (bool) $this->coreHelperMageConfig->get('ometria/advanced/enable_cookiebot'); + } + + /** + * @return string + */ + public function getCookiebotClass() + { + return (string) $this->coreHelperMageConfig->get('ometria/advanced/cookiebot_classification'); + } } diff --git a/app/code/Ometria/Core/Helper/Cookiechannel.php b/app/code/Ometria/Core/Helper/Cookiechannel.php index 7ea061e..a672301 100644 --- a/app/code/Ometria/Core/Helper/Cookiechannel.php +++ b/app/code/Ometria/Core/Helper/Cookiechannel.php @@ -1,73 +1,100 @@ helperConfig = $helperConfig; - $this->frontendAreaChecker = $frontendAreaChecker; + Config $helperConfig, + IsFrontend $frontendAreaChecker + ) { + $this->helperConfig = $helperConfig; + $this->frontendAreaChecker = $frontendAreaChecker; + return parent::__construct($context); } - - public function addCommand($command, $replace_if_exists=false, $set_cookie=true){ - if (!$command || !is_array($command)) return; + /** + * @param $command + * @param bool $replaceIfExists + */ + public function addCommand($command, bool $replaceIfExists = false) + { + if (!$command || !is_array($command)) { + return; + } // Return if admin area or API call - // if (Mage::app()->getStore()->isAdmin()) return; - // if (Mage::getSingleton('api/server')->getAdapter() != null) return; - if(!$this->frontendAreaChecker->check()) - { + if(!$this->frontendAreaChecker->check()) { + return; + } + + if (!$this->helperConfig->isConfigured()) { return; } - //$ometria_config_helper = Mage::helper('ometria/config'); - $ometria_config_helper = $this->helperConfig; - if (!$ometria_config_helper->isConfigured()) return; - if (!$ometria_config_helper->isUnivarEnabled()) return; + if (!$this->helperConfig->isUnivarEnabled()) { + return; + } + + if ($command[0] === 'identify') { + $command[1] = ''; + } - if ($command[0]=='identify') $command[1] = ''; - $str = implode(self::SEPERATOR_IN_COMMANDS, $command); - $this->appendCookieCommand($command[0], $str, $replace_if_exists); + $this->appendCookieCommand($command[0], $str, $replaceIfExists); } - private function appendCookieCommand($command_name, $str, $replace_if_exists=false, $set_cookie=true){ - $existing_cookie = isset($_COOKIE[self::COOKIE_NAME]) ? $_COOKIE[self::COOKIE_NAME] : ''; - $commands = explode(self::SEPERATOR_BETWEEN_COMMANDS, $existing_cookie); - $new_cookie = ''; + /** + * @param $commandName + * @param $str + * @param bool $replaceIfExists + */ + private function appendCookieCommand($commandName, $str, bool $replaceIfExists = false) + { + $existingCookie = isset($_COOKIE[self::COOKIE_NAME]) ? $_COOKIE[self::COOKIE_NAME] : ''; + $commands = explode(self::SEPERATOR_BETWEEN_COMMANDS, $existingCookie); + $newCookie = ''; - if ($replace_if_exists && $commands) { - $commands_filtered = array(); + if ($replaceIfExists && $commands) { + $commandsFiltered = array(); foreach($commands as $command){ - if (strpos($command, $command_name.self::SEPERATOR_IN_COMMANDS)!==0) { - $commands_filtered[] = $command; + if (strpos($command, $commandName . self::SEPERATOR_IN_COMMANDS) !== 0) { + $commandsFiltered[] = $command; } } - $commands = $commands_filtered; + $commands = $commandsFiltered; $commands = array_filter($commands); } $commands[] = $str; - if (count($commands)>6) $commands = array_slice($commands, 0, 6); + if (count($commands) > 6) { + $commands = array_slice($commands, 0, 6); + } $commands = array_unique($commands); $commands = array_filter($commands); @@ -75,23 +102,84 @@ private function appendCookieCommand($command_name, $str, $replace_if_exists=fal sort($commands); $commands = array_values($commands); - $new_cookie = implode(self::SEPERATOR_BETWEEN_COMMANDS, $commands); - if (strlen($new_cookie)>1000) $new_cookie = ''; + $newCookie = implode(self::SEPERATOR_BETWEEN_COMMANDS, $commands); + if (strlen($newCookie) > 1000) { + $newCookie = ''; + } - if (!headers_sent() && ($new_cookie!=$existing_cookie)) { - $this->cookie_did_change = true; - $_COOKIE[self::COOKIE_NAME] = $new_cookie; - //setcookie(self::COOKIE_NAME, $new_cookie, 0, '/'); - //if ($set_cookie) Mage::getModel('core/cookie') - // ->set(self::COOKIE_NAME, $new_cookie, 0, '/'); - if ($set_cookie) $this->sendCookie(); + if (!headers_sent() && ($newCookie != $existingCookie)) { + $this->cookieDidChange = true; + $_COOKIE[self::COOKIE_NAME] = $newCookie; + + $this->sendCookie(); } } - - public function sendCookie(){ - if (!$this->cookie_did_change) return; + + public function sendCookie() + { + if (!$this->cookieDidChange) { + return; + } + + if ($this->helperConfig->isCookiebotEnabled() && $this->cookiebotCookieAllowed() === false) { + return; + } + $cookie = isset($_COOKIE[self::COOKIE_NAME]) ? $_COOKIE[self::COOKIE_NAME] : ''; setcookie(self::COOKIE_NAME, $cookie, 0, '/'); - $this->cookie_did_change = false; - } -} \ No newline at end of file + $this->cookieDidChange = false; + } + + /** + * @return bool + */ + private function cookiebotCookieAllowed() + { + $cookieAllowed = false; + + if (isset($_COOKIE[self::COOKIEBOT_COOKIE_NAME])) { + switch ($_COOKIE[self::COOKIEBOT_COOKIE_NAME]) { + case "-1": + //The user is not within a region that requires consent - all cookies are accepted + $cookieAllowed = true; + break; + + default: + // The user must give their consent + $cookieConsent = $this->getCookiebotConsent(); + if ($cookieConsent) { + $cookieClass = $this->helperConfig->getCookiebotClass(); + // Consent cookie was found + if (isset($cookieConsent[$cookieClass]) && filter_var($cookieConsent[$cookieClass], FILTER_VALIDATE_BOOLEAN)) { + //Current user accepts Ometria cookies + $cookieAllowed = true; + } + } + break; + } + } + + return $cookieAllowed; + } + + /** + * Read current user consent in encoded JavaScript format from Cookiebot cookie + * + * @see https://www.cookiebot.com/en/developer/ + * @return mixed + */ + private function getCookiebotConsent() + { + $json = preg_replace('/\s*:\s*([a-zA-Z0-9_]+?)([}\[,])/', + ':"$1"$2', + preg_replace('/([{\[,])\s*([a-zA-Z0-9_]+?):/', + '$1"$2":', + str_replace("'", + '"', + stripslashes($_COOKIE[self::COOKIEBOT_COOKIE_NAME]) + ) + ) + ); + return json_decode($json, true); + } +} diff --git a/app/code/Ometria/Core/etc/adminhtml/system.xml b/app/code/Ometria/Core/etc/adminhtml/system.xml index 1760342..85b3b62 100755 --- a/app/code/Ometria/Core/etc/adminhtml/system.xml +++ b/app/code/Ometria/Core/etc/adminhtml/system.xml @@ -35,49 +35,60 @@ Magento\Config\Model\Config\Source\Yesno - + Magento\Config\Model\Config\Source\Yesno Allow tracking javascript to load from the Ometria CDN on the Magento checkout page. - + Magento\Config\Model\Config\Source\Yesno This applies to configurable/swatch products, should Ometria record a page view on each variant selection. - + Magento\Config\Model\Config\Source\Yesno - - - Magento\Config\Model\Config\Source\Yesno - - + Ometria\Core\Model\Config\Source\PreferredProduct Select the configurable product attribute which defines the SKU of the preferred product variant. - + Magento\Config\Model\Config\Source\Yesno Select "yes" here to override using the preferred product's image if the configurable already has an image set. - - - This will be auto-generated by the extension. Do not change. - - + + + Ometria\Core\Model\Config\Source\StockPushScope + + + + Magento\Config\Model\Config\Source\Yesno + + + + + 1 + + Defines the CookieBot classification Ometria cookies will be allowed by. + + Magento\Config\Model\Config\Source\Yesno - + Magento\Config\Model\Config\Source\Yesno - - - Ometria\Core\Model\Config\Source\StockPushScope + + + Magento\Config\Model\Config\Source\Yesno + + + + This will be auto-generated by the extension. Do not change. diff --git a/app/code/Ometria/Core/etc/config.xml b/app/code/Ometria/Core/etc/config.xml index ff0b3be..48672ac 100755 --- a/app/code/Ometria/Core/etc/config.xml +++ b/app/code/Ometria/Core/etc/config.xml @@ -19,6 +19,8 @@ 0 0 1 + 0 + marketing diff --git a/app/code/Ometria/Core/etc/module.xml b/app/code/Ometria/Core/etc/module.xml index 45253cc..ed2aabc 100755 --- a/app/code/Ometria/Core/etc/module.xml +++ b/app/code/Ometria/Core/etc/module.xml @@ -1,4 +1,4 @@ - + diff --git a/app/code/Ometria/Core/view/frontend/templates/head.phtml b/app/code/Ometria/Core/view/frontend/templates/head.phtml index d1f24f8..e3fb1e9 100755 --- a/app/code/Ometria/Core/view/frontend/templates/head.phtml +++ b/app/code/Ometria/Core/view/frontend/templates/head.phtml @@ -37,6 +37,13 @@ var sc = document.createElement('script'); sc.src = url; sc.setAttribute('async', 'true'); + isCookiebotEnabled()): ?> + sc.setAttribute('type', 'text/plain'); + sc.setAttribute( + 'data-cookieconsent', + 'escapeJs($this->getCookiebotClass()) ?>' + ); + document.getElementsByTagName("head")[0].appendChild(sc); }, 50); }); diff --git a/composer.json b/composer.json index b213394..a49320c 100755 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "ometria/magento2", "type": "magento2-module", - "version": "2.3.2", + "version": "2.4.0", "description": "Dev composer package for Ometria Extension", "authors": [ {