123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\Catalog\Helper;
- use Magento\Catalog\Model\Category as ModelCategory;
- use Magento\Catalog\Model\Product as ModelProduct;
- use Magento\Framework\Filter\Template;
- class Output extends \Magento\Framework\App\Helper\AbstractHelper
- {
- /**
- * Array of existing handlers
- *
- * @var array
- */
- protected $_handlers;
- /**
- * Template processor instance
- *
- * @var Template
- */
- protected $_templateProcessor = null;
- /**
- * Catalog data
- *
- * @var Data
- */
- protected $_catalogData = null;
- /**
- * Eav config
- *
- * @var \Magento\Eav\Model\Config
- */
- protected $_eavConfig;
- /**
- * @var \Magento\Framework\Escaper
- */
- protected $_escaper;
- /**
- * @var array
- */
- private $directivePatterns;
- /**
- * Output constructor.
- * @param \Magento\Framework\App\Helper\Context $context
- * @param \Magento\Eav\Model\Config $eavConfig
- * @param Data $catalogData
- * @param \Magento\Framework\Escaper $escaper
- * @param array $directivePatterns
- */
- public function __construct(
- \Magento\Framework\App\Helper\Context $context,
- \Magento\Eav\Model\Config $eavConfig,
- Data $catalogData,
- \Magento\Framework\Escaper $escaper,
- $directivePatterns = []
- ) {
- $this->_eavConfig = $eavConfig;
- $this->_catalogData = $catalogData;
- $this->_escaper = $escaper;
- $this->directivePatterns = $directivePatterns;
- parent::__construct($context);
- }
- /**
- * @return Template
- */
- protected function _getTemplateProcessor()
- {
- if (null === $this->_templateProcessor) {
- $this->_templateProcessor = $this->_catalogData->getPageTemplateProcessor();
- }
- return $this->_templateProcessor;
- }
- /**
- * Adding method handler
- *
- * @param string $method
- * @param object $handler
- * @return $this
- */
- public function addHandler($method, $handler)
- {
- if (!is_object($handler)) {
- return $this;
- }
- $method = strtolower($method);
- if (!isset($this->_handlers[$method])) {
- $this->_handlers[$method] = [];
- }
- $this->_handlers[$method][] = $handler;
- return $this;
- }
- /**
- * Get all handlers for some method
- *
- * @param string $method
- * @return array
- */
- public function getHandlers($method)
- {
- $method = strtolower($method);
- return $this->_handlers[$method] ?? [];
- }
- /**
- * Process all method handlers
- *
- * @param string $method
- * @param mixed $result
- * @param array $params
- * @return mixed
- */
- public function process($method, $result, $params)
- {
- foreach ($this->getHandlers($method) as $handler) {
- if (method_exists($handler, $method)) {
- $result = $handler->{$method}($this, $result, $params);
- }
- }
- return $result;
- }
- /**
- * Prepare product attribute html output
- *
- * @param ModelProduct $product
- * @param string $attributeHtml
- * @param string $attributeName
- * @return string
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function productAttribute($product, $attributeHtml, $attributeName)
- {
- $attribute = $this->_eavConfig->getAttribute(ModelProduct::ENTITY, $attributeName);
- if ($attribute &&
- $attribute->getId() &&
- $attribute->getFrontendInput() != 'media_image' &&
- (!$attribute->getIsHtmlAllowedOnFront() &&
- !$attribute->getIsWysiwygEnabled())
- ) {
- if ($attribute->getFrontendInput() != 'price') {
- $attributeHtml = $this->_escaper->escapeHtml($attributeHtml);
- }
- if ($attribute->getFrontendInput() == 'textarea') {
- $attributeHtml = nl2br($attributeHtml);
- }
- }
- if ($attributeHtml !== null
- && $attribute->getIsHtmlAllowedOnFront()
- && $attribute->getIsWysiwygEnabled()
- && $this->isDirectivesExists($attributeHtml)
- ) {
- $attributeHtml = $this->_getTemplateProcessor()->filter($attributeHtml);
- }
- $attributeHtml = $this->process(
- 'productAttribute',
- $attributeHtml,
- ['product' => $product, 'attribute' => $attributeName]
- );
- return $attributeHtml;
- }
- /**
- * Prepare category attribute html output
- *
- * @param ModelCategory $category
- * @param string $attributeHtml
- * @param string $attributeName
- * @return string
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function categoryAttribute($category, $attributeHtml, $attributeName)
- {
- $attribute = $this->_eavConfig->getAttribute(ModelCategory::ENTITY, $attributeName);
- if ($attribute &&
- $attribute->getFrontendInput() != 'image' &&
- (!$attribute->getIsHtmlAllowedOnFront() &&
- !$attribute->getIsWysiwygEnabled())
- ) {
- $attributeHtml = $this->_escaper->escapeHtml($attributeHtml);
- }
- if ($attributeHtml !== null
- && $attribute->getIsHtmlAllowedOnFront()
- && $attribute->getIsWysiwygEnabled()
- && $this->isDirectivesExists($attributeHtml)
- ) {
- $attributeHtml = $this->_getTemplateProcessor()->filter($attributeHtml);
- }
- $attributeHtml = $this->process(
- 'categoryAttribute',
- $attributeHtml,
- ['category' => $category, 'attribute' => $attributeName]
- );
- return $attributeHtml;
- }
- /**
- * Check if string has directives
- *
- * @param string $attributeHtml
- * @return bool
- */
- public function isDirectivesExists($attributeHtml)
- {
- $matches = false;
- foreach ($this->directivePatterns as $pattern) {
- if (preg_match($pattern, $attributeHtml)) {
- $matches = true;
- break;
- }
- }
- return $matches;
- }
- }
|