Skip to content

Commit 8706d60

Browse files
authored
Merge pull request #318 from magento-cia/AC-11662
AC-11662 [PCI] - improve script rendering
2 parents be23363 + 78fab80 commit 8706d60

File tree

3 files changed

+77
-48
lines changed

3 files changed

+77
-48
lines changed

app/code/Magento/PageBuilder/view/adminhtml/templates/stage/render.phtml

+36-28
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,39 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
/** @var \Magento\PageBuilder\Block\Adminhtml\Stage\Render $block */
7+
/**
8+
* @var \Magento\PageBuilder\Block\Adminhtml\Stage\Render $block
9+
* @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
10+
*/
811
?>
9-
<script>
10-
<?php
11-
/**
12-
* Override the text! plugin within the iframe to ensure we can pipe any XHR requests through to the parent window
13-
* as the same origin policy will not allow us to load the templates within this iframe.
14-
*/
15-
?>
16-
require.config({
17-
'map': {
18-
'*': {
19-
'text': 'Magento_PageBuilder/js/master-format/render/requirejs/text',
20-
'Magento_PageBuilder/js/events': 'Magento_PageBuilder/js/master-format/render/events'
21-
}
22-
}
23-
});
2412

25-
<?php
26-
/**
27-
* To be able to override the text plugin we need the Magento template engine to be used, as the template engine
28-
* within lib has a dependency on the text! plugin we need to ensure we set the template engine before the
29-
* dependency blocks us. If we try to just override using the RequireJS config above our !text plugin will never
30-
* get overridden as our template engine cannot load.
31-
*/
32-
?>
13+
<?php
14+
/**
15+
* Override the text! plugin within the iframe to ensure we can pipe any XHR requests through to the parent window
16+
* as the same origin policy will not allow us to load the templates within this iframe.
17+
*/
18+
?>
19+
<?php
20+
$pageBuilderConfig = $block->getPageBuilderConfig();
21+
22+
$script = <<<SCRIPT
23+
require.config({
24+
'map': {
25+
'*': {
26+
'text': 'Magento_PageBuilder/js/master-format/render/requirejs/text',
27+
'Magento_PageBuilder/js/events': 'Magento_PageBuilder/js/master-format/render/events'
28+
}
29+
}
30+
});
31+
SCRIPT;
32+
33+
/**
34+
* To be able to override the text plugin we need the Magento template engine to be used, as the template engine
35+
* within lib has a dependency on the text! plugin we need to ensure we set the template engine before the
36+
* dependency blocks us. If we try to just override using the RequireJS config above our !text plugin will never
37+
* get overridden as our template engine cannot load.
38+
*/
39+
$script .= <<<SCRIPT
3340
require([
3441
'ko',
3542
'Magento_Ui/js/lib/knockout/template/engine'
@@ -39,10 +46,11 @@
3946
ko.uid = 0;
4047
ko.setTemplateEngine(templateEngine);
4148
});
42-
</script>
43-
<script>
49+
4450
require(['Magento_PageBuilder/js/master-format/render/frame'], function (listen) {
45-
listen(<?= /* @noEscape */ $block->getPageBuilderConfig(); ?>);
51+
listen({$pageBuilderConfig});
4652
});
47-
</script>
53+
SCRIPT;
54+
?>
55+
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $script, false) ?>
4856
<div>Page Builder Render Frame</div>

app/code/Magento/PageBuilder/view/adminhtml/templates/wysiwyg_setup.phtml

+11-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
/** @var Magento\PageBuilder\Block\WysiwygSetup $block */
7+
/**
8+
* @var Magento\PageBuilder\Block\WysiwygSetup $block
9+
* @var Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
10+
*/
811

912
?>
10-
<script>
13+
<?php
14+
$config = $block->getConfigJson();
15+
$script = <<<SCRIPT
1116
require.config({
1217
config: {
1318
'mage/adminhtml/wysiwyg/tiny_mce/setup': {
14-
config: <?= /* @noEscape */ $block->getConfigJson() ?>
19+
config: {$config}
1520
}
1621
}
1722
});
18-
</script>
23+
SCRIPT;
24+
?>
25+
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $script, false) ?>

app/code/Magento/PageBuilder/view/base/templates/googlemaps.phtml

+30-16
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,53 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
/** @var Magento\PageBuilder\Block\GoogleMapsApi $block */
8-
?>
9-
<script>
7+
/**
8+
* @var Magento\PageBuilder\Block\GoogleMapsApi $block
9+
* @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
10+
* @var \Magento\Framework\Escaper $escaper
11+
*/
12+
13+
$libraryUrl = $escaper->escapeJs($block->getLibraryUrl());
14+
$style = $escaper->escapeJs($block->getStyle());
15+
$apiKey = $escaper->escapeJs($block->getApiKey());
16+
$invalidApiKeyMessage = $escaper->escapeJs($block->getInvalidApiKeyMessage());
17+
18+
$script = <<<SCRIPT
1019
require.config({
1120
paths: {
12-
googleMaps: '<?= $block->escapeJs($block->getLibraryUrl()); ?>'
21+
googleMaps: '{$libraryUrl}'
1322
},
1423
config: {
1524
'Magento_PageBuilder/js/utils/map': {
16-
style: '<?= $block->escapeJs($block->getStyle()); ?>'
25+
style: '{$style}',
1726
},
1827
'Magento_PageBuilder/js/content-type/map/preview': {
19-
apiKey: '<?= $block->escapeJs($block->getApiKey()); ?>',
20-
apiKeyErrorMessage: '<?= $block->escapeJs($block->getInvalidApiKeyMessage()); ?>'
28+
apiKey: '{$apiKey}',
29+
apiKeyErrorMessage: '{$invalidApiKeyMessage}'
2130
},
2231
'Magento_PageBuilder/js/form/element/map': {
23-
apiKey: '<?= $block->escapeJs($block->getApiKey()); ?>',
24-
apiKeyErrorMessage: '<?= $block->escapeJs($block->getInvalidApiKeyMessage()); ?>'
32+
apiKey: '{$apiKey}',
33+
apiKeyErrorMessage: '{$invalidApiKeyMessage}'
2534
},
2635
}
2736
});
28-
</script>
37+
SCRIPT;
38+
// phpcs:ignore
39+
echo /* @noEscape */ $secureRenderer->renderTag('script', [], $script, false);
40+
41+
/** Include the googleMaps dependency only if we have an API key set, this removes unnecessary calls to Google */
42+
if ($block->shouldIncludeGoogleMapsLibrary()) {
43+
$script = <<<SCRIPT
2944
30-
<?php
31-
// Include the googleMaps dependency only if we have an API key set, this removes unnecessary calls to Google
32-
if ($block->shouldIncludeGoogleMapsLibrary()) : ?>
33-
<script>
3445
require.config({
3546
shim: {
3647
'Magento_PageBuilder/js/utils/map': {
3748
deps: ['googleMaps']
3849
}
3950
}
4051
});
41-
</script>
42-
<?php endif; ?>
52+
SCRIPT;
53+
54+
// phpcs:ignore
55+
echo /* @noEscape */ $secureRenderer->renderTag('script', [], $script, false);
56+
}

0 commit comments

Comments
 (0)