116 lines
3.1 KiB
PHP
116 lines
3.1 KiB
PHP
<?php
|
|
/**
|
|
* @package Plumrocket_CookieConsent
|
|
* @copyright Copyright (c) 2022 Plumrocket Inc. (https://plumrocket.com)
|
|
* @license https://plumrocket.com/license End-user License Agreement
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Plumrocket\CookieConsent\Model\Integration;
|
|
|
|
use Magento\Framework\Exception\NoSuchEntityException;
|
|
use Plumrocket\Base\Api\ConfigUtilsInterface;
|
|
use Plumrocket\CookieConsent\Api\CategoryRepositoryInterface;
|
|
use Plumrocket\CookieConsent\Helper\Config;
|
|
|
|
/**
|
|
* @since 1.9.0
|
|
*/
|
|
class NoCookieYoutube
|
|
{
|
|
|
|
public const XML_PATH_CATEGORY_KEY = 'pr_cookie/no_cookie_youtube/category';
|
|
|
|
/**
|
|
* @var \Plumrocket\CookieConsent\Helper\Config
|
|
*/
|
|
private $config;
|
|
|
|
/**
|
|
* @var \Plumrocket\CookieConsent\Api\CategoryRepositoryInterface
|
|
*/
|
|
private $cookieCategoryRepository;
|
|
|
|
/**
|
|
* @var \Plumrocket\Base\Api\ConfigUtilsInterface
|
|
*/
|
|
private $configUtils;
|
|
|
|
/**
|
|
* @param \Plumrocket\CookieConsent\Helper\Config $config
|
|
* @param \Plumrocket\CookieConsent\Api\CategoryRepositoryInterface $cookieCategoryRepository
|
|
* @param \Plumrocket\Base\Api\ConfigUtilsInterface $configUtils
|
|
*/
|
|
public function __construct(
|
|
Config $config,
|
|
CategoryRepositoryInterface $cookieCategoryRepository,
|
|
ConfigUtilsInterface $configUtils
|
|
) {
|
|
$this->config = $config;
|
|
$this->cookieCategoryRepository = $cookieCategoryRepository;
|
|
$this->configUtils = $configUtils;
|
|
}
|
|
|
|
/**
|
|
* Check if the no cookie mode enabled.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isEnabled(): bool
|
|
{
|
|
if (! $this->config->isModuleEnabled() || ! $this->getAssociatedCategoryKey()) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
$cookieCategory = $this->cookieCategoryRepository->get($this->getAssociatedCategoryKey());
|
|
return $cookieCategory->isEnabled() && ! $cookieCategory->isEssential();
|
|
} catch (NoSuchEntityException $e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get key of associated cookie category.
|
|
*
|
|
* @param int|string|null $store
|
|
* @return string
|
|
*/
|
|
public function getAssociatedCategoryKey($store = null): string
|
|
{
|
|
return (string) $this->configUtils->getStoreConfig(self::XML_PATH_CATEGORY_KEY, $store);
|
|
}
|
|
|
|
/**
|
|
* Replace domain in YouTube iframes to no cookie mode.
|
|
*
|
|
* @param string $html
|
|
* @return string
|
|
*/
|
|
public function convertHtml(string $html): string
|
|
{
|
|
preg_match_all(
|
|
'/(?:<iframe[^>]*?youtube\.com[^>]*)(?:(?:\/>)|(?:>.*?<\/iframe>))/s',
|
|
$html,
|
|
$matches,
|
|
PREG_PATTERN_ORDER
|
|
);
|
|
|
|
foreach ($matches[0] as $iframe) {
|
|
$iframeWithNoCookieDomain = str_replace(
|
|
'youtube.com',
|
|
'youtube-nocookie.com',
|
|
$iframe,
|
|
$domainReplaced
|
|
);
|
|
|
|
if ($domainReplaced) {
|
|
$html = str_replace($iframe, $iframeWithNoCookieDomain, $html);
|
|
}
|
|
}
|
|
|
|
return $html;
|
|
}
|
|
}
|