Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to insert widget with text value which contains }} string #29006

Merged
merged 13 commits into from
Sep 10, 2020
5 changes: 5 additions & 0 deletions app/code/Magento/Widget/Block/Adminhtml/Widget/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public function addFields()
* @return \Magento\Framework\Data\Form\Element\AbstractElement
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function _addField($parameter)
{
Expand All @@ -158,6 +159,10 @@ protected function _addField($parameter)
$data['value'] = $parameter->getValue();
}

if ($parameter->getType() == 'text' && $data['value'] != '') {
$data['value'] = $this->_widget->decodeReservedChars($data['value']);
}

//prepare unique id value
if ($fieldName == 'unique_id' && $data['value'] == '') {
$data['value'] = hash('sha256', microtime(1));
Expand Down
65 changes: 65 additions & 0 deletions app/code/Magento/Widget/Model/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public function getWidgetByClassType($type)
*/
public function getConfigAsXml($type)
{
// phpstan:ignore
return $this->getXmlElementByType($type);
}

Expand Down Expand Up @@ -293,6 +294,7 @@ public function getWidgetsArray($filters = [])
* @param array $params Pre-configured Widget Params
* @param bool $asIs Return result as widget directive(true) or as placeholder image(false)
* @return string Widget directive ready to parse
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function getWidgetDeclaration($type, $params = [], $asIs = true)
{
Expand All @@ -304,6 +306,8 @@ public function getWidgetDeclaration($type, $params = [], $asIs = true)
if ($name == 'conditions') {
$name = 'conditions_encoded';
$value = $this->conditionsHelper->encode($value);
} elseif ($this->isTextType($widget, $name)) {
$value = $this->encodeReservedChars($value);
} elseif (is_array($value)) {
$value = implode(',', $value);
} elseif (trim($value) == '') {
Expand Down Expand Up @@ -456,4 +460,65 @@ protected function sortParameters($firstElement, $secondElement)
$bOrder = (int)$secondElement->getData('sort_order');
return $aOrder < $bOrder ? -1 : ($aOrder > $bOrder ? 1 : 0);
}

/**
* Encode reserved chars
*
* @param string $string
* @return string|string[]
*/
private function encodeReservedChars($string)
{
$map = [
'{' => urlencode('{'),
'}' => urlencode('}')
];

return str_replace(
array_keys($map),
array_values($map),
$string
);
}

/**
* Decode reserved chars
*
* @param string $string
* @return array
*/
public function decodeReservedChars($string)
{
$map = [
'{' => urlencode('{'),
'}' => urlencode('}')
];

return str_replace(
array_values($map),
array_keys($map),
$string
);
}

/**
* Is text type Widget parameter
*
* @param \Magento\Framework\DataObject $widget
* @param string $name
* @return bool
*/
private function isTextType($widget, $name)
{
$parameters = $widget->getParameters();

if (isset($parameters[$name]) && is_object($parameters[$name])) {
$type = $parameters[$name]->getType();
if ($type == 'text') {
return true;
}
}

return false;
}
}