| Current Path : /var/www/magento.test.indacotrentino.com/www/vendor/iubenda/module-cookiesolution/Helper/ |
| Current File : /var/www/magento.test.indacotrentino.com/www/vendor/iubenda/module-cookiesolution/Helper/Data.php |
<?php
namespace Iubenda\CookieSolution\Helper;
use \Magento\Store\Model\ScopeInterface;
use \Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Config\Storage\WriterInterface;
use \Magento\Framework\App\Cache\TypeListInterface;
use \Magento\Framework\App\Cache\Type\Config;
use Magento\Framework\App\Helper\Context;
use Magento\Backend\Model\UrlInterface;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* @var UrlInterface
*
* Backend URL Interface to fetch backend-related data
*/
protected $backendUrl;
/**
* Constructor
*
* @param Context $context
* @param UrlInterface $backendUrl
*/
public function __construct(
Context $context,
UrlInterface $backendUrl
) {
parent::__construct($context);
$this->backendUrl = $backendUrl;
}
/**
* Show the review notification after extension activation with 7 days
*
* @var float|int
*/
private $diffToShowReviewNotification = ( 7 * 24 * 3600 );
/**
* Show the notification again every 6 months
*
* @var float|int
*/
private $timeToShowNotificationAgain = 6 * 30 * 24 * 3600;
/**
* Review config
*
* @var string
*/
private $tempHideReviewConfig = 'iubenda/review/temp-hide';
/**
* Never show review again config
*
* @var string
*/
private $permanentHideReviewConfig = 'iubenda/review/permanent-hide';
/**
* Activation date
*
* @var string
*/
private $activationDateConfig = 'iubenda/activation/date';
/**
* Check if extension is enabled.
*
* @return bool
*/
public function isBlockingEnabled()
{
return $this->scopeConfig->isSetFlag('iubenda/settings/blocking_enabled', ScopeInterface::SCOPE_STORE);
}
/**
* Check if extension use experimental mode.
*
* @return bool
*/
public function isExperimentalMode()
{
return $this->scopeConfig->isSetFlag('iubenda/settings/experimental_mode', ScopeInterface::SCOPE_STORE);
}
/**
* Get the selecting parsing engine
*
* @return bool
*/
public function getParsingEngine()
{
return $this->scopeConfig->getValue('iubenda/settings/experimental_mode', ScopeInterface::SCOPE_STORE);
}
/**
* Get JavaScript code.
*
* @return string
*/
public function getJsCode()
{
return $this->parseCode($this->scopeConfig->getValue('iubenda/code/jscode', ScopeInterface::SCOPE_STORE));
}
/**
* Get custom scripts.
*
* @return array
*/
public function getCustomScripts()
{
// get custom scripts
$customScripts = $this->scopeConfig->getValue('iubenda/custom/scripts', ScopeInterface::SCOPE_STORE);
// empty scripts?
if (empty($customScripts)) {
$customScripts = [];
} else {
// decode scripts
$data = json_decode($customScripts, true);
// valid scripts? new format
if (json_last_error() == JSON_ERROR_NONE) {
// any data?
if (! empty($data) && is_array($data)) {
$customScripts = [];
foreach ($data as $element) {
$customScripts[$element['url']] = (int) $element['purpose'];
}
} else {
$customScripts = [];
}
// old scripts format
} else {
// filter scripts
$new_scripts = array_filter(array_map('trim', explode("\n", str_replace("\r", '', $customScripts))));
$customScripts = [];
// set all old scripts to 0 type
foreach ($new_scripts as $script) {
$customScripts[$script] = 0;
}
}
}
return $this->prepareCustomData($customScripts);
}
/**
* Get custom iframes.
*
* @return array
*/
public function getCustomIframes()
{
// get custom iframes
$customIframes = $this->scopeConfig->getValue('iubenda/custom/iframes', ScopeInterface::SCOPE_STORE);
// empty iframes?
if (empty($customIframes)) {
$customIframes = [];
} else {
// decode iframes
$data = json_decode($customIframes, true);
// valid iframes? new format
if (json_last_error() == JSON_ERROR_NONE) {
// any data?
if (! empty($data) && is_array($data)) {
$customIframes = [];
foreach ($data as $element) {
$customIframes[$element['url']] = (int) $element['purpose'];
}
} else {
$customIframes = [];
}
// old iframes format
} else {
// filter iframes
$new_iframes = array_filter(array_map('trim', explode("\n", str_replace("\r", '', $customIframes))));
$customIframes = [];
// set all old iframes to 0 type
foreach ($new_iframes as $iframe) {
$customIframes[$iframe] = 0;
}
}
}
return $this->prepareCustomData($customIframes);
}
/**
* Prepare scripts/iframes.
*
* @param array $data
* @return array
*/
public function prepareCustomData($data)
{
$newData = [];
foreach ($data as $script => $type) {
if (! array_key_exists($type, $newData)) {
$newData[$type] = [];
}
$newData[$type][] = $script;
}
return $newData;
}
/**
* Fix iubenda code to display it correctly.
*
* @param string $source JavaScript code
* @return string
*/
public function parseCode($source)
{
$source = trim($source ?: '');
// check HTML content
preg_match_all('/(\"(?:html|content)\"(?:\s+)?\:(?:\s+)?)\"((?:.*?)(?:[^\\\\]))\"/s', $source, $matches);
// found subgroup?
if (! empty($matches[1]) && ! empty($matches[2])) {
foreach ($matches[2] as $no => $match) {
// replace content with special tags
$replace = $matches[1][$no] . '[[IUBENDA_TAG_START]]' . $match . '[[IUBENDA_TAG_END]]';
$source = str_replace($matches[0][$no], $replace, $source);
}
// check special tags
preg_match_all('/\[\[IUBENDA_TAG_START\]\](.*?)\[\[IUBENDA_TAG_END\]\]/s', $source, $matchesTags);
// found any matches?
if (! empty($matchesTags[1])) {
foreach ($matchesTags[1] as $no => $match) {
// fix end tags
$replace = '"' . str_replace('</', '<\/', $matches[2][$no]) . '"';
$source = str_replace($matchesTags[0][$no], $replace, $source);
}
}
}
return $source;
}
/**
* Hide review notification
*/
public function hideReviewNotification():void
{
$this->getInstanceOf(WriterInterface::class)->save($this->tempHideReviewConfig, time());
$this->getInstanceOf(TypeListInterface::class)->cleanType(Config::TYPE_IDENTIFIER);
}
/**
* permanent review notification again
*/
public function permanentHideReviewNotification():void
{
$this->getInstanceOf(WriterInterface::class)->save($this->permanentHideReviewConfig, 'yes');
$this->getInstanceOf(TypeListInterface::class)->cleanType(Config::TYPE_IDENTIFIER);
}
/**
* Check if I can show review notification again
*
* @return bool
*/
private function isPermanentHideReviewNotification():bool {
// Flag is not declared yet
if ( ! $this->scopeConfig->isSetFlag( $this->permanentHideReviewConfig ) ) {
return false;
}
// Flag is set and user don't want to show notification again
if ( 'yes' === $this->scopeConfig->getValue( $this->permanentHideReviewConfig ) ) {
return true;
}
return false;
}
/**
* Get review notification flag
*
* @return array
*/
private function getHideTimeOfReviewNotification():string
{
$data = $this->scopeConfig->getValue($this->tempHideReviewConfig);
return "$data";
}
/**
* Check if can I display the review notification
*
* @return bool
*/
public function canShowReviewNotification():bool
{
// Checks order is important
// 1. Don't show if the it's permanent hide review notification
if ( $this->isPermanentHideReviewNotification() ) {
return false;
}
// 2. Check the extension activation is before 7 days from now
if (( time() - $this->getActivationDate() ) < $this->diffToShowReviewNotification) {
return false;
}
$time = $this->getHideTimeOfReviewNotification();
// 3. Show the review notification if the user didn't hide it before
if ( empty( $time ) || null === $time ) {
return true;
}
// Check the time difference is more than 6 months to show the notification again
if ($time) {
// Check time difference is more than six months
if (( time() - $time ) > $this->timeToShowNotificationAgain) {
return true;
}
}
return false;
}
/**
* Get the activation date
*
* @return mixed
*/
public function getActivationDate()
{
return $this->scopeConfig->getValue($this->activationDateConfig);
}
/**
* Set activation date if not set before
*/
public function setActivationDate()
{
if ($this->scopeConfig->isSetFlag($this->activationDateConfig)) {
return;
}
$this->getInstanceOf(WriterInterface::class)->save($this->activationDateConfig, time());
$this->getInstanceOf(TypeListInterface::class)->cleanType(Config::TYPE_IDENTIFIER);
}
/**
* Helper to get class from object manager
*
* @param string $class
*
* @return mixed
*/
private function getInstanceOf(string $class)
{
return ObjectManager::getInstance()->get($class);
}
/**
* Get Admin FrontName.
*
* This method returns the frontName for the admin area, which is used
* in constructing URLs for admin panel functionalities.
*
* @return string The admin area frontName.
*/
public function getAdminFrontName()
{
return $this->backendUrl->getAreaFrontName();
}
}