197 lines
4.9 KiB
PHP
197 lines
4.9 KiB
PHP
<?php
|
|
/**
|
|
* @package Plumrocket_CookieConsent
|
|
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
|
* @license https://plumrocket.com/license End-user License Agreement
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Plumrocket\CookieConsent\Block;
|
|
|
|
use Magento\Framework\Code\Minifier\Adapter\Js\JShrink;
|
|
use Magento\Framework\ObjectManagerInterface;
|
|
use Magento\Framework\Serialize\SerializerInterface;
|
|
use Magento\Framework\View\Element\Template\Context;
|
|
use Plumrocket\CookieConsent\Block\Html\HeadScripts;
|
|
use Plumrocket\CookieConsent\Helper\Config;
|
|
|
|
/**
|
|
* Google Tag Manager Page Block
|
|
*/
|
|
class Gtm extends HeadScripts
|
|
{
|
|
/**
|
|
* @var Config
|
|
*/
|
|
private $config;
|
|
|
|
/**
|
|
* @var SerializerInterface
|
|
*/
|
|
private $serializer;
|
|
|
|
/**
|
|
* @param \Magento\Framework\View\Element\Template\Context $context
|
|
* @param \Plumrocket\CookieConsent\Helper\Config $config
|
|
* @param \Magento\Framework\Serialize\SerializerInterface $serializer
|
|
* @param \Magento\Framework\Code\Minifier\Adapter\Js\JShrink $jsMinifier
|
|
* @param \Magento\Framework\ObjectManagerInterface $objectManager
|
|
* @param array $data
|
|
*/
|
|
public function __construct(
|
|
Context $context,
|
|
Config $config,
|
|
SerializerInterface $serializer,
|
|
JShrink $jsMinifier,
|
|
ObjectManagerInterface $objectManager,
|
|
array $data = []
|
|
) {
|
|
parent::__construct(
|
|
$context,
|
|
$jsMinifier,
|
|
$objectManager,
|
|
$data
|
|
);
|
|
$this->config = $config;
|
|
$this->serializer = $serializer;
|
|
}
|
|
|
|
/**
|
|
* Render GTM tracking scripts
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function _toHtml()
|
|
{
|
|
if (! $this->config->isGtmEnabled()) {
|
|
return '';
|
|
}
|
|
|
|
return parent::_toHtml();
|
|
}
|
|
|
|
/**
|
|
* Return cookie restriction mode value.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getGtmContainerId(): string
|
|
{
|
|
return $this->config->getGtmContainerId();
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getGtmServerBaseUrl(): string
|
|
{
|
|
return $this->config->getGtmServerBaseUrl();
|
|
}
|
|
|
|
/**
|
|
* Return cookie restriction mode value.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getGmtAssociatedCategoryKey(): string
|
|
{
|
|
return $this->config->getGmtAssociatedCategoryKey();
|
|
}
|
|
|
|
/**
|
|
* Check if consent mode enabled
|
|
*
|
|
* @return int
|
|
*/
|
|
public function isConsentModeEnabled(): int
|
|
{
|
|
return (int) $this->config->isGtmConsentModeEnabled();
|
|
}
|
|
|
|
/**
|
|
* Retrieve consent types default status
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getDefaultConsentTypesStatus(): string
|
|
{
|
|
$result = [];
|
|
|
|
if ($this->isConsentModeEnabled()) {
|
|
foreach ($this->config->getGtmConsentTypesConfig() as $config) {
|
|
$result[$config['consent_type']] = $config['status'];
|
|
}
|
|
}
|
|
|
|
return $this->serializer->serialize($result);
|
|
}
|
|
|
|
/**
|
|
* Retrieve consent types default status
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getDefaultConsentStatus(): array
|
|
{
|
|
$result = [];
|
|
if ($this->isConsentModeEnabled()) {
|
|
foreach ($this->config->getGtmConsentTypesConfig() as $config) {
|
|
$result[$config['consent_type']] = $config['status'];
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Retrieve cookie categories assigned to the consent types
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getConsentTypesCategories(): string
|
|
{
|
|
$result = [];
|
|
|
|
if ($this->isConsentModeEnabled()) {
|
|
foreach ($this->config->getGtmConsentTypesConfig() as $config) {
|
|
$result[$config['consent_type']] = $config['cookie_category'];
|
|
}
|
|
}
|
|
|
|
return $this->serializer->serialize($result);
|
|
}
|
|
|
|
/**
|
|
* Get cookie categories to consent types mapping.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getCategoriesMapping(): array
|
|
{
|
|
$result = [];
|
|
if ($this->isConsentModeEnabled()) {
|
|
foreach ($this->config->getGtmConsentTypesConfig() as $config) {
|
|
$result[$config['consent_type']] = $config['cookie_category'];
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Get serialized config.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getJsonConfig(): string
|
|
{
|
|
return $this->serializer->serialize([
|
|
'categoryKey' => $this->getGmtAssociatedCategoryKey(),
|
|
'containerId' => $this->getGtmContainerId(),
|
|
'consentMode' => $this->isConsentModeEnabled(),
|
|
'defaultConsentTypes' => $this->getDefaultConsentStatus(),
|
|
'consentTypesCategories' => $this->getCategoriesMapping()
|
|
]);
|
|
}
|
|
}
|