Skip to content

Commit

Permalink
Add CookieBot compatibility. (#37)
Browse files Browse the repository at this point in the history
* Add CookieBot compatibility.

* Use strict comparison for command type check.
  • Loading branch information
chris-pook authored Jan 31, 2022
1 parent 0778f43 commit aa541c7
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 81 deletions.
2 changes: 1 addition & 1 deletion app/code/Ometria/AbandonedCarts/etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Ometria_AbandonedCarts" setup_version="2.3.2"/>
<module name="Ometria_AbandonedCarts" setup_version="2.4.0"/>
</config>
2 changes: 1 addition & 1 deletion app/code/Ometria/Api/etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Ometria_Api" setup_version="2.3.2"/>
<module name="Ometria_Api" setup_version="2.4.0"/>
</config>
16 changes: 16 additions & 0 deletions app/code/Ometria/Core/Block/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
16 changes: 16 additions & 0 deletions app/code/Ometria/Core/Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
206 changes: 147 additions & 59 deletions app/code/Ometria/Core/Helper/Cookiechannel.php
Original file line number Diff line number Diff line change
@@ -1,97 +1,185 @@
<?php
namespace Ometria\Core\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
class Cookiechannel extends AbstractHelper
{
namespace Ometria\Core\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Ometria\Core\Helper\Is\Frontend as IsFrontend;

const COOKIE_NAME = 'ommage';
class Cookiechannel extends AbstractHelper
{
const COOKIE_NAME = 'ommage';
const COOKIEBOT_COOKIE_NAME = 'CookieConsent';
const SEPERATOR_BETWEEN_COMMANDS = ';';
const SEPERATOR_IN_COMMANDS = ':';

protected $helperConfig;

protected $frontendAreaChecker;

protected $cookie_did_change = false;

const SEPERATOR_IN_COMMANDS = ':';

/** @var Config */
private $helperConfig;

/** @var IsFrontend */
private $frontendAreaChecker;

/** @var bool */
private $cookieDidChange = false;

/**
* @param Context $context
* @param Config $helperConfig
* @param IsFrontend $frontendAreaChecker
*/
public function __construct(
Context $context,
\Ometria\Core\Helper\Config $helperConfig,
\Ometria\Core\Helper\Is\Frontend $frontendAreaChecker
)
{
$this->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);
$commands = array_values($commands);
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;
}
}
$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);
}
}
47 changes: 29 additions & 18 deletions app/code/Ometria/Core/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,60 @@
<label>Include Data Layer</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="checkout_tracking_enabled" translate="label" type="select" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="checkout_tracking_enabled" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Include Data Layer on Checkout</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Allow tracking javascript to load from the Ometria CDN on the Magento checkout page.</comment>
</field>
<field id="pageview_on_variant" translate="label comment" type="select" sortOrder="15" showInDefault="1" showInWebsite="0" showInStore="0">
<field id="pageview_on_variant" translate="label comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Trigger Ometria Page View on variant selection</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>This applies to configurable/swatch products, should Ometria record a page view on each variant selection.</comment>
</field>
<field id="ping" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0">
<field id="ping" translate="label" type="select" sortOrder="40" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Ping Ometria on record update</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="debug" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Debug Mode</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="preferred_product_attribute" translate="label" type="select" sortOrder="1120" showInDefault="1" showInWebsite="0" showInStore="0">
<field id="preferred_product_attribute" translate="label" type="select" sortOrder="50" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Preferred Product Attribute</label>
<source_model>Ometria\Core\Model\Config\Source\PreferredProduct</source_model>
<comment>Select the configurable product attribute which defines the SKU of the preferred product variant.</comment>
</field>
<field id="use_configurable_image" translate="label" type="select" sortOrder="1130" showInDefault="1" showInWebsite="0" showInStore="0">
<field id="use_configurable_image" translate="label" type="select" sortOrder="60" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Use Configurable Image</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Select "yes" here to override using the preferred product's image if the configurable already has an image set.</comment>
</field>
<field id="unique_id" translate="label" type="Ometria\Core\Data\Form\Element\Text\Disabled" sortOrder="1140" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Unique Magento ID</label>
<comment>This will be auto-generated by the extension. Do not change.</comment>
</field>
<field id="override_memory" translate="label" type="select" sortOrder="40" showInDefault="1" showInWebsite="0" showInStore="0">
<field id="stock_push_scope" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Out of stock product push scope</label>
<source_model>Ometria\Core\Model\Config\Source\StockPushScope</source_model>
</field>
<field id="enable_cookiebot" translate="label" type="select" sortOrder="80" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Enable Cookiebot restrictions</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="cookiebot_classification" translate="label" type="text" sortOrder="80" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Cookiebot Classification</label>
<depends>
<field id="enable_cookiebot">1</field>
</depends>
<comment>Defines the CookieBot classification Ometria cookies will be allowed by.</comment>
</field>
<field id="override_memory" translate="label" type="select" sortOrder="90" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Override PHP 'memory_limit' value in API</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="override_execution" translate="label" type="select" sortOrder="50" showInDefault="1" showInWebsite="0" showInStore="0">
<field id="override_execution" translate="label" type="select" sortOrder="100" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Override PHP 'max_execution_time' value in API</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="stock_push_scope" translate="label" type="select" sortOrder="60" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Out of stock product push scope</label>
<source_model>Ometria\Core\Model\Config\Source\StockPushScope</source_model>
<field id="debug" translate="label" type="select" sortOrder="100" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Debug Mode</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="unique_id" translate="label" type="Ometria\Core\Data\Form\Element\Text\Disabled" sortOrder="110" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Unique Magento ID</label>
<comment>This will be auto-generated by the extension. Do not change.</comment>
</field>
</group>
</section>
Expand Down
2 changes: 2 additions & 0 deletions app/code/Ometria/Core/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<override_memory>0</override_memory>
<override_execution>0</override_execution>
<stock_push_scope>1</stock_push_scope>
<enable_cookiebot>0</enable_cookiebot>
<cookiebot_classification>marketing</cookiebot_classification>
</advanced>
</ometria>
</default>
Expand Down
2 changes: 1 addition & 1 deletion app/code/Ometria/Core/etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Ometria_Core" setup_version="2.3.2"/>
<module name="Ometria_Core" setup_version="2.4.0"/>
</config>
Loading

0 comments on commit aa541c7

Please sign in to comment.