_storeManager = $storeManager;
$this->filterProvider = $filterProvider;
$this->registry = $registry;
$this->cmsPage = $cmsPage;
$this->serializer = $serializer;
}
public function filter($str)
{
if (!$str || !is_string($str)) {
return $str;
}
$str = $this->decodeDirectiveImages($str);
$storeId = $this->_storeManager->getStore()->getId();
$filter = $this->filterProvider->getBlockFilter()->setStoreId($storeId);
$variables = [];
if ($this->cmsPage->getId()) {
$variables['page'] = $this->cmsPage;
}
if ($category = $this->getCurrentCategory()) {
$variables['category'] = $category;
}
if ($product = $this->getCurrentProduct()) {
$variables['product'] = $product;
}
$filter->setVariables($variables);
$productMetadata = ObjectManager::getInstance()->get(\Magento\Framework\App\ProductMetadataInterface::class);
if ($productMetadata->getVersion() >= '2.4.0') {
$filter->setStrictMode(false);
}
return $filter->filter($str);
}
/**
* @return \Magento\Catalog\Model\Category
*/
public function getCurrentCategory()
{
return $this->registry->registry('current_category');
}
/**
* @return \Magento\Catalog\Model\Product
*/
public function getCurrentProduct()
{
return $this->registry->registry('current_product');
}
/**
* @param string $content
* @return string
*/
public function decodeDirectiveImages($content)
{
$matches = $search = $replace = [];
preg_match_all('//is', $content, $matches);
foreach ($matches[0] as $imgHTML) {
$key = 'directive/___directive/';
if (strpos($imgHTML, $key) !== false) {
$srcKey = 'src="';
$start = strpos($imgHTML, $srcKey) + strlen($srcKey);
$end = strpos($imgHTML, '"', $start);
if ($end > $start) {
$imgSrc = substr($imgHTML, $start, $end - $start);
$start = strpos($imgSrc, $key) + strlen($key);
$imgBase64 = substr($imgSrc, $start);
$replaceHTML = str_replace($imgSrc, $this->urlDecoder->decode(urldecode($imgBase64)), $imgHTML);
$search[] = $imgHTML;
$replace[] = $replaceHTML;
}
}
}
return str_replace($search, $replace, $content);
}
public function unserialize($string)
{
if ($string && $this->isJSON($string)) {
return $this->serializer->unserialize($string);
}
return $string;
}
public function serialize($array = [])
{
return $this->serializer->serialize($array);
}
/**
* @param string $string
* @return boolean
*/
public function isJSON($string)
{
return is_string($string) && is_array(json_decode($string, true)) ? true : false;
}
/**
* @return string
*/
public function getMediaUrl()
{
$mediaUrl = $this->_storeManager->getStore()->getBaseUrl(
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
);
return $mediaUrl;
}
/**
* Remove base url
*/
public function convertImageUrl($string)
{
$mediaUrl = $this->getMediaUrl();
return str_replace($mediaUrl, '', $string);
}
/**
* @return boolean
*/
public function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
/**
* @return boolean
*/
public function endsWith($haystack, $needle)
{
$length = strlen($needle);
return $length === 0 ||
(substr($haystack, -$length) === $needle);
}
/**
* @param string $string
* @return string
*/
public function getImageUrl($string)
{
if ($string && is_string($string) && strpos($string, 'http') === false && (strpos($string, '