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( '/(?:]*?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; } }