Skip to content

Commit 80188e5

Browse files
committed
Merge remote-tracking branch 'remotes/mainline/1.0-develop' into MC-15970
2 parents 8116e05 + ddd415c commit 80188e5

File tree

7 files changed

+145
-40
lines changed

7 files changed

+145
-40
lines changed

app/code/Magento/PageBuilder/Controller/ContentType/Preview.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,28 @@ class Preview extends \Magento\Framework\App\Action\Action implements HttpPostAc
2626
*/
2727
private $rendererPool;
2828

29+
/**
30+
* @var \Magento\Backend\Model\Auth
31+
*/
32+
private $auth;
33+
2934
/**
3035
* Constructor
3136
*
3237
* @param \Magento\Backend\App\Action\Context $context
3338
* @param \Magento\PageBuilder\Model\Stage\RendererPool $rendererPool
39+
* @param \Magento\Backend\Model\Auth $auth
3440
*/
3541
public function __construct(
3642
\Magento\Backend\App\Action\Context $context,
37-
\Magento\PageBuilder\Model\Stage\RendererPool $rendererPool
43+
\Magento\PageBuilder\Model\Stage\RendererPool $rendererPool,
44+
\Magento\Backend\Model\Auth $auth = null
3845
) {
3946
parent::__construct($context);
4047

4148
$this->rendererPool = $rendererPool;
49+
$this->auth = $auth ?? \Magento\Framework\App\ObjectManager::getInstance()
50+
->get(\Magento\Backend\Model\Auth::class);
4251
}
4352

4453
/**
@@ -48,14 +57,18 @@ public function __construct(
4857
*/
4958
public function execute()
5059
{
51-
$pageResult = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
52-
// Some template filters and directive processors expect this to be called in order to function.
53-
$pageResult->initLayout();
60+
if ($this->auth->isLoggedIn()) {
61+
$pageResult = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
62+
// Some template filters and directive processors expect this to be called in order to function.
63+
$pageResult->initLayout();
64+
65+
$params = $this->getRequest()->getParams();
66+
$renderer = $this->rendererPool->getRenderer($params['role']);
67+
$result = ['data' => $renderer->render($params)];
5468

55-
$params = $this->getRequest()->getParams();
56-
$renderer = $this->rendererPool->getRenderer($params['role']);
57-
$result = ['data' => $renderer->render($params)];
69+
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
70+
}
5871

59-
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
72+
$this->_forward('noroute');
6073
}
6174
}

app/code/Magento/PageBuilder/Model/Stage/Config.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ public function getConfig()
135135
'content_types' => $this->getContentTypes(),
136136
'stage_config' => $this->data,
137137
'media_url' => $this->urlBuilder->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]),
138-
'preview_url' => $this->frontendUrlBuilder->getUrl('pagebuilder/contenttype/preview'),
138+
'preview_url' => $this->frontendUrlBuilder
139+
->addSessionParam()
140+
->getUrl('pagebuilder/contenttype/preview'),
139141
'column_grid_default' => $this->scopeConfig->getValue(self::XML_PATH_COLUMN_GRID_DEFAULT),
140142
'column_grid_max' => $this->scopeConfig->getValue(self::XML_PATH_COLUMN_GRID_MAX),
141143
'can_use_inline_editing_on_stage' => $this->isWysiwygProvisionedForEditingOnStage(),
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageBuilder\Plugin\Filter;
9+
10+
use Magento\Store\Model\Store;
11+
use Magento\Framework\Escaper;
12+
13+
/**
14+
* Plugin to the template filter to escape custom variable directives
15+
*/
16+
class CustomVarTemplate
17+
{
18+
/**
19+
* @var Escaper
20+
*/
21+
private $escaper;
22+
23+
/**
24+
* @param Escaper $escaper
25+
*/
26+
public function __construct(
27+
Escaper $escaper
28+
) {
29+
$this->escaper = $escaper;
30+
}
31+
32+
/**
33+
* Determine if custom variable within a Page Builder CMS Block directive's return value needs to be escaped
34+
*
35+
* @param \Magento\Email\Model\Template\Filter $subject
36+
* @param string $result
37+
* @return string
38+
*/
39+
public function afterCustomvarDirective(
40+
\Magento\Email\Model\Template\Filter $subject,
41+
$result
42+
) {
43+
// Determine the need to escape the return value of observed method.
44+
// Admin context requires store ID of 0; in that context return value should be escaped
45+
$shouldEscape = $subject->getStoreId() !== null && (int) $subject->getStoreId() === Store::DEFAULT_STORE_ID;
46+
47+
if ($shouldEscape) {
48+
return $this->escaper->escapeHtml($result);
49+
} else {
50+
return $result;
51+
}
52+
}
53+
}

app/code/Magento/PageBuilder/Plugin/Filter/TemplatePlugin.php

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace Magento\PageBuilder\Plugin\Filter;
99

10-
use Magento\Store\Model\Store;
11-
1210
/**
1311
* Plugin to the template filter to process any background images added by Page Builder
1412
*/
@@ -38,19 +36,27 @@ class TemplatePlugin
3836
*/
3937
private $mathRandom;
4038

39+
/**
40+
* @var \Magento\Framework\Serialize\Serializer\Json
41+
*/
42+
private $json;
43+
4144
/**
4245
* @param \Psr\Log\LoggerInterface $logger
4346
* @param \Magento\Framework\View\ConfigInterface $viewConfig
4447
* @param \Magento\Framework\Math\Random $mathRandom
48+
* @param \Magento\Framework\Serialize\Serializer\Json $json
4549
*/
4650
public function __construct(
4751
\Psr\Log\LoggerInterface $logger,
4852
\Magento\Framework\View\ConfigInterface $viewConfig,
49-
\Magento\Framework\Math\Random $mathRandom
53+
\Magento\Framework\Math\Random $mathRandom,
54+
\Magento\Framework\Serialize\Serializer\Json $json
5055
) {
5156
$this->logger = $logger;
5257
$this->viewConfig = $viewConfig;
5358
$this->mathRandom = $mathRandom;
59+
$this->json = $json;
5460
}
5561

5662
/**
@@ -107,32 +113,6 @@ public function afterFilter(\Magento\Framework\Filter\Template $subject, string
107113
return $result;
108114
}
109115

110-
/**
111-
* Determine if custom variable directive's return value needs to be escaped and do so if true
112-
*
113-
* @param \Magento\Framework\Filter\Template $subject
114-
* @param \Closure $proceed
115-
* @param string[] $construction
116-
* @return string
117-
*/
118-
public function aroundCustomvarDirective(
119-
\Magento\Framework\Filter\Template $subject,
120-
\Closure $proceed,
121-
$construction
122-
) {
123-
// Determine the need to escape the return value of observed method.
124-
// Admin context requires store ID of 0; in that context return value should be escaped
125-
$shouldEscape = $subject->getStoreId() !== null && (int) $subject->getStoreId() === Store::DEFAULT_STORE_ID;
126-
127-
if (!$shouldEscape) {
128-
return $proceed($construction);
129-
}
130-
131-
$result = $proceed($construction);
132-
133-
return htmlspecialchars($result);
134-
}
135-
136116
/**
137117
* Create a DOM document from a given string
138118
*
@@ -161,7 +141,7 @@ private function createDomDocument(string $html) : \DOMDocument
161141
$domDocument = new \DOMDocument('1.0', 'UTF-8');
162142
set_error_handler(
163143
function ($errorNumber, $errorString) {
164-
throw new \Exception($errorString, $errorNumber);
144+
throw new \DOMException($errorString, $errorNumber);
165145
}
166146
);
167147
$string = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
@@ -225,6 +205,7 @@ private function generateDecodedHtmlPlaceholderMappingInDocument(\DOMDocument $d
225205
$preDecodedOuterHtml = $document->saveHTML($htmlContentTypeNode);
226206

227207
// clear empty <div> wrapper around outerHTML to replace with $clonedHtmlContentTypeNode
208+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
228209
$decodedInnerHtml = preg_replace('#^<[^>]*>|</[^>]*>$#', '', html_entity_decode($preDecodedOuterHtml));
229210

230211
// Use $clonedHtmlContentTypeNode's placeholder to inject decoded inner html
@@ -258,7 +239,8 @@ private function generateBackgroundImageStyles(\DOMDocument $document) : void
258239
$backgroundImages = $node->attributes->getNamedItem('data-background-images');
259240
if ($backgroundImages->nodeValue !== '') {
260241
$elementClass = uniqid('background-image-');
261-
$images = json_decode(stripslashes($backgroundImages->nodeValue), true);
242+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
243+
$images = $this->json->unserialize(stripslashes($backgroundImages->nodeValue));
262244
if (count($images) > 0) {
263245
$style = $xpath->document->createElement(
264246
'style',
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\PageBuilder\Plugin\Framework\Session;
7+
8+
/**
9+
* Plugin for SID resolver.
10+
*/
11+
class SidResolver
12+
{
13+
/**
14+
* @var \Magento\Framework\App\RequestInterface
15+
*/
16+
private $request;
17+
18+
/**
19+
* @param \Magento\Framework\App\RequestInterface $request
20+
*/
21+
public function __construct(
22+
\Magento\Framework\App\RequestInterface $request
23+
) {
24+
$this->request = $request;
25+
}
26+
27+
/**
28+
* Get Sid for pagebuilder preview
29+
*
30+
* @param \Magento\Framework\Session\SidResolver $subject
31+
* @param string|null $result
32+
* @param \Magento\Framework\Session\SessionManagerInterface $session
33+
*
34+
* @return string|null
35+
*/
36+
public function afterGetSid(
37+
\Magento\Framework\Session\SidResolver $subject,
38+
$result,
39+
\Magento\Framework\Session\SessionManagerInterface $session
40+
) {
41+
if (strpos($this->request->getPathInfo(), '/pagebuilder/contenttype/preview') === 0) {
42+
return $this->request->getQuery(
43+
$subject->getSessionIdQueryParam($session)
44+
);
45+
}
46+
47+
return $result;
48+
}
49+
}

app/code/Magento/PageBuilder/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,7 @@
140140
</argument>
141141
</arguments>
142142
</type>
143+
<type name="Magento\Framework\Session\SidResolver">
144+
<plugin name="pagebuilder_preview_sid_resolving" type="Magento\PageBuilder\Plugin\Framework\Session\SidResolver" />
145+
</type>
143146
</config>

app/code/Magento/PageBuilder/etc/frontend/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
<type name="Magento\Framework\Filter\Template">
1313
<plugin name="convertBackgroundImages" type="Magento\PageBuilder\Plugin\Filter\TemplatePlugin"/>
1414
</type>
15+
<type name="Magento\Email\Model\Template\Filter">
16+
<plugin name="escapeCustomVarDirectives" type="Magento\PageBuilder\Plugin\Filter\CustomVarTemplate"/>
17+
</type>
1518
</config>

0 commit comments

Comments
 (0)