Files
ltsecurityinc/app/code/Plumrocket/CookieConsent/Helper/Config.php
T

221 lines
6.3 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\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Serialize\SerializerInterface;
use Plumrocket\Base\Model\ConfigUtils;
/**
* @since 1.0.0
*/
class Config extends AbstractHelper
{
public const XML_PATH_IS_ENABLED = 'pr_cookie/general/enabled';
public const XML_PATH_USE_COOKIE_BEFORE_OPT_IN = 'pr_cookie/main_settings/use_cookie_before_opt_in';
public const XML_PATH_BLOCK_UNKNOWN_COOKIE = 'pr_cookie/main_settings/block_unknown_cookie';
public const XML_PATH_CONSENT_EXPIRY = 'pr_cookie/main_settings/consent_expiry';
public const XML_PATH_RELOAD_AFTER_ACCEPT = 'pr_cookie/main_settings/reload_after_accept';
public const XML_PATH_RELOAD_AFTER_DECLINE = 'pr_cookie/main_settings/reload_after_decline';
public const XML_PATH_GTM_ENABLED = 'prgdpr/gtm/enabled';
public const XML_PATH_GTM_CATEGORY_KEY = 'pr_cookie/gtm/category';
public const XML_PATH_GTM_CONTAINER_ID = 'prgdpr/gtm/container_id';
public const XML_PATH_GTM_SERVER_SIDE_TAGGING = 'pr_cookie/gtm/server_side_tagging';
public const XML_PATH_GTM_SERVER_URL = 'pr_cookie/gtm/server_url';
public const XML_PATH_GTM_CONSENT_MODE_ENABLED = 'pr_cookie/gtm/consent_mode';
public const XML_PATH_GTM_CONSENT_TYPES = 'pr_cookie/gtm/consent_types';
/**
* @var \Plumrocket\Base\Model\Utils\Config
*/
private $configUtils;
/**
* @var SerializerInterface
*/
private $serializer;
/**
* @param \Magento\Framework\App\Helper\Context $context
* @param \Plumrocket\Base\Model\Utils\Config $configUtils
* @param SerializerInterface $serializer
*/
public function __construct(
Context $context,
ConfigUtils $configUtils,
SerializerInterface $serializer
) {
parent::__construct($context);
$this->configUtils = $configUtils;
$this->serializer = $serializer;
}
/**
* Check if module is enabled.
*
* @param null $store
* @param null $scope
* @return bool
*/
public function isModuleEnabled($store = null, $scope = null): bool
{
return $this->configUtils->isSetFlag(
self::XML_PATH_IS_ENABLED,
$store,
$scope
);
}
/**
* @param null $store
* @param null $scope
* @return bool
*/
public function canUseCookieBeforeOptIn($store = null, $scope = null): bool
{
return $this->configUtils->isSetFlag(
self::XML_PATH_USE_COOKIE_BEFORE_OPT_IN,
$store,
$scope
);
}
/**
* @param null $store
* @param null $scope
* @return bool
*/
public function canBlockUnknownCookie($store = null, $scope = null): bool
{
return $this->configUtils->isSetFlag(
self::XML_PATH_BLOCK_UNKNOWN_COOKIE,
$store,
$scope
);
}
/**
* @param null $store
* @param null $scope
* @return int
*/
public function getConsentExpiry($store = null, $scope = null): int
{
return (int) $this->configUtils->getConfig(self::XML_PATH_CONSENT_EXPIRY, $store, $scope);
}
/**
* @param null $store
* @param null $scope
* @return bool
*/
public function reloadAfterAccept($store = null, $scope = null): bool
{
return $this->configUtils->isSetFlag(self::XML_PATH_RELOAD_AFTER_ACCEPT, $store, $scope);
}
/**
* @param null $store
* @param null $scope
* @return bool
*/
public function reloadAfterDecline($store = null, $scope = null): bool
{
return $this->configUtils->isSetFlag(self::XML_PATH_RELOAD_AFTER_DECLINE, $store, $scope);
}
/** ------ GMT ------- */
/**
* @param null $store
* @param null $scope
* @return bool
*/
public function isGtmEnabled($store = null, $scope = null): bool
{
return $this->configUtils->isSetFlag(self::XML_PATH_GTM_ENABLED, $store, $scope);
}
/**
* @param null $store
* @param null $scope
* @return string
*/
public function getGmtAssociatedCategoryKey($store = null, $scope = null): string
{
return (string) $this->configUtils->getConfig(self::XML_PATH_GTM_CATEGORY_KEY, $store, $scope);
}
/**
* @param null $store
* @param null $scope
* @return string
*/
public function getGtmContainerId($store = null, $scope = null): string
{
return trim((string) $this->configUtils->getConfig(self::XML_PATH_GTM_CONTAINER_ID, $store, $scope));
}
/**
* @param $store
* @param $scope
* @return bool
*/
public function isGtmServerSideTaggingEnabled($store = null, $scope = null): bool
{
return $this->configUtils->isSetFlag(self::XML_PATH_GTM_SERVER_SIDE_TAGGING, $store, $scope);
}
/**
* @param $store
* @param $scope
* @return string
*/
public function getGtmServerBaseUrl($store = null, $scope = null): string
{
if (! $this->isGtmServerSideTaggingEnabled($store, $scope)) {
return 'https://www.googletagmanager.com';
}
return trim((string) $this->configUtils->getConfig(self::XML_PATH_GTM_SERVER_URL, $store, $scope), ' \/');
}
/**
* Check if gtm consent mode enabled
*
* @param null $store
* @param null $scope
* @return bool
*/
public function isGtmConsentModeEnabled($store = null, $scope = null): bool
{
return $this->configUtils->isSetFlag(self::XML_PATH_GTM_CONSENT_MODE_ENABLED, $store, $scope);
}
/**
* Retrieve gtm consent types configurations
*
* @param null $store
* @param null $scope
* @return array
*/
public function getGtmConsentTypesConfig($store = null, $scope = null): array
{
$config = $this->configUtils->getConfig(self::XML_PATH_GTM_CONSENT_TYPES, $store, $scope);
if (is_string($config)) {
$config = $this->serializer->unserialize($config);
}
return $config ?: [];
}
}