Skip to content

Commit e76a1c3

Browse files
authored
fix(widget): improve conditions decoding and deterministic cache key
Harden decoding of widget conditions and stabilize cache key generation. - Use injected Json serializer ($this->serializer) consistently - Catch \InvalidArgumentException and log context safely
1 parent 808114c commit e76a1c3

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

Block/Set/Widget/CustomEntityWidget.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class CustomEntityWidget extends Template implements BlockInterface
125125
*
126126
* @var Json
127127
*/
128-
private $json;
128+
private $serializer;
129129

130130
/**
131131
* View constructor.
@@ -152,7 +152,7 @@ public function __construct(
152152
Conditions $conditionsHelper,
153153
ImageFactory $imageFactory,
154154
array $data = [],
155-
Json $json = null
155+
Json $serializer = null
156156
) {
157157
$this->attributeSetRepository = $attributeSetRepository;
158158
$this->customEntityRepository = $customEntityRepository;
@@ -162,7 +162,7 @@ public function __construct(
162162
$this->rule = $rule;
163163
$this->conditionsHelper = $conditionsHelper;
164164
$this->imageFactory = $imageFactory;
165-
$this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
165+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
166166

167167
parent::__construct($context, $data);
168168
}
@@ -208,7 +208,7 @@ public function getCacheKeyInfo()
208208
$this->getItemsPerPage(),
209209
$this->getItemsCount(),
210210
$conditions,
211-
$this->json->serialize($this->getRequest()->getParams()),
211+
$this->serializer->serialize($this->getRequest()->getParams()),
212212
$this->getTemplate()
213213
];
214214
}
@@ -546,9 +546,8 @@ public function getConditions()
546546
public function getIdentities()
547547
{
548548
$identities = [];
549-
550-
if ($entities = $this->getEntities()) {
551-
foreach ($entities as $entity) {
549+
if ($this->getEntities()) {
550+
foreach ($this->getEntities() as $entity) {
552551
$identities[] = $entity->getIdentities();
553552
}
554553
}
@@ -569,25 +568,34 @@ private function getWidgetPagerBlockName()
569568
{
570569
$pageName = $this->getData('page_var_name');
571570
$pagerBlockName = 'widget.smile.set.list.pager';
572-
573571
if (!$pageName) {
574572
return $pagerBlockName;
575573
}
576574

577575
return $pagerBlockName . '.' . $pageName;
578576
}
579577

580-
581-
582578
/**
583-
* Decode encoded special characters and unserialize conditions into array
579+
* Decode widget conditions.
584580
*
585-
* @param string $encodedConditions
586-
* @return array
581+
* @param string $encodedConditions Conditions encoded as JSON.
582+
* @return array<mixed> Decoded conditions array.
587583
* @see \Magento\Widget\Model\Widget::getDirectiveParam
588584
*/
589-
private function decodeConditions(string $encodedConditions): array
585+
public function decodeConditions(string $encodedConditions): array
590586
{
591-
return $this->conditionsHelper->decode(htmlspecialchars_decode($encodedConditions));
587+
try {
588+
$conditions = $this->serializer->unserialize(htmlspecialchars_decode($encodedConditions));
589+
return is_array($conditions) ? $conditions : [];
590+
} catch (\InvalidArgumentException $exception) {
591+
/** @var array{exception:\Throwable, encoded_conditions:string, uri:string} $context */
592+
$context = [
593+
'exception' => $exception,
594+
'encoded_conditions' => $encodedConditions,
595+
'uri' => $this->_request->getRequestUri(),
596+
];
597+
$this->_logger->error($exception->getMessage(), $context);
598+
return [];
599+
}
592600
}
593-
}
601+
}

0 commit comments

Comments
 (0)