Skip to content

Commit 87f4e1e

Browse files
authored
Merge pull request #4740 from magento-performance/MC-19587
[Performance] [2.2.x] Fix missing shims and phtml files with mage-init directives
2 parents 5618bf8 + f99dff5 commit 87f4e1e

File tree

14 files changed

+174
-102
lines changed

14 files changed

+174
-102
lines changed

app/code/Magento/Braintree/view/frontend/templates/paypal/button.phtml

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@
1111
$id = $block->getContainerId() . random_int(0, PHP_INT_MAX);
1212

1313
$config = [
14-
'Magento_Braintree/js/paypal/button' => [
15-
'id' => $id,
16-
'clientToken' => $block->getClientToken(),
17-
'displayName' => $block->getMerchantName(),
18-
'actionSuccess' => $block->getActionSuccess(),
19-
'environment' => $block->getEnvironment()
20-
]
14+
'id' => $id,
15+
'clientToken' => $block->getClientToken(),
16+
'displayName' => $block->getMerchantName(),
17+
'actionSuccess' => $block->getActionSuccess(),
18+
'environment' => $block->getEnvironment()
2119
];
22-
2320
?>
24-
<div data-mage-init='<?= /* @noEscape */ json_encode($config); ?>'
21+
<div data-mage-init='{"Magento_Braintree/js/paypal/button":<?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($config) ?>}'
2522
class="paypal checkout paypal-logo braintree-paypal-logo<?= /* @noEscape */ $block->getContainerId(); ?>-container">
2623
<div data-currency="<?= /* @noEscape */ $block->getCurrency(); ?>"
2724
data-locale="<?= /* @noEscape */ $block->getLocale(); ?>"

app/code/Magento/Catalog/Test/Unit/ViewModel/Product/BreadcrumbsTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Catalog\Test\Unit\ViewModel\Product;
89

@@ -52,11 +53,14 @@ protected function setUp()
5253
->disableOriginalConstructor()
5354
->getMockForAbstractClass();
5455

56+
$escaper = $this->getObjectManager()->getObject(\Magento\Framework\Escaper::class);
57+
5558
$this->viewModel = $this->getObjectManager()->getObject(
5659
Breadcrumbs::class,
5760
[
5861
'catalogData' => $this->catalogHelper,
5962
'scopeConfig' => $this->scopeConfig,
63+
'escaper' => $escaper
6064
]
6165
);
6266
}
@@ -114,6 +118,59 @@ public function productDataProvider()
114118
];
115119
}
116120

121+
/**
122+
* @dataProvider productJsonEncodeDataProvider
123+
*
124+
* @param Product|null $product
125+
* @param string $expectedJson
126+
* @return void
127+
*/
128+
public function testGetJsonConfiguration($product, string $expectedJson)
129+
{
130+
$this->catalogHelper->expects($this->atLeastOnce())
131+
->method('getProduct')
132+
->willReturn($product);
133+
134+
$this->scopeConfig->expects($this->any())
135+
->method('isSetFlag')
136+
->with('catalog/seo/product_use_categories', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
137+
->willReturn(false);
138+
139+
$this->scopeConfig->expects($this->any())
140+
->method('getValue')
141+
->with('catalog/seo/category_url_suffix', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
142+
->willReturn('."html');
143+
144+
$this->assertEquals($expectedJson, $this->viewModel->getJsonConfiguration());
145+
}
146+
147+
/**
148+
* @return array
149+
*/
150+
public function productJsonEncodeDataProvider()
151+
{
152+
return [
153+
[
154+
$this->getObjectManager()->getObject(Product::class, ['data' => ['name' => 'Test ™']]),
155+
'{"breadcrumbs":{"categoryUrlSuffix":".&quot;html","useCategoryPathInUrl":0,"product":"Test \u2122"}}',
156+
],
157+
[
158+
$this->getObjectManager()->getObject(Product::class, ['data' => ['name' => 'Test "']]),
159+
'{"breadcrumbs":{"categoryUrlSuffix":".&quot;html","useCategoryPathInUrl":0,"product":"Test &quot;"}}',
160+
],
161+
[
162+
$this->getObjectManager()->getObject(Product::class, ['data' => ['name' => 'Test <b>x</b>']]),
163+
'{"breadcrumbs":{"categoryUrlSuffix":".&quot;html","useCategoryPathInUrl":0,"product":'
164+
. '"Test &lt;b&gt;x&lt;\/b&gt;"}}',
165+
],
166+
[
167+
$this->getObjectManager()->getObject(Product::class, ['data' => ['name' => 'Test \'abc\'']]),
168+
'{"breadcrumbs":'
169+
. '{"categoryUrlSuffix":".&quot;html","useCategoryPathInUrl":0,"product":"Test &#039;abc&#039;"}}'
170+
],
171+
];
172+
}
173+
117174
/**
118175
* @return ObjectManager
119176
*/

app/code/Magento/Catalog/ViewModel/Product/Breadcrumbs.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
use Magento\Framework\App\Config\ScopeConfigInterface;
1010
use Magento\Framework\App\ObjectManager;
1111
use Magento\Framework\DataObject;
12-
use Magento\Framework\Escaper;
1312
use Magento\Framework\Serialize\Serializer\Json;
1413
use Magento\Framework\View\Element\Block\ArgumentInterface;
14+
use Magento\Framework\Escaper;
1515

1616
/**
1717
* Product breadcrumbs view model.
@@ -30,10 +30,6 @@ class Breadcrumbs extends DataObject implements ArgumentInterface
3030
*/
3131
private $scopeConfig;
3232

33-
/**
34-
* @var Json
35-
*/
36-
private $json;
3733
/**
3834
* @var Escaper
3935
*/
@@ -42,8 +38,9 @@ class Breadcrumbs extends DataObject implements ArgumentInterface
4238
/**
4339
* @param Data $catalogData
4440
* @param ScopeConfigInterface $scopeConfig
45-
* @param Json $json
46-
* @param Escaper $escaper
41+
* @param Json|null $json
42+
* @param Escaper|null $escaper
43+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4744
*/
4845
public function __construct(
4946
Data $catalogData,
@@ -55,7 +52,6 @@ public function __construct(
5552

5653
$this->catalogData = $catalogData;
5754
$this->scopeConfig = $scopeConfig;
58-
$this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
5955
$this->escaper = $escaper ?: ObjectManager::getInstance()->get(Escaper::class);
6056
}
6157

@@ -97,19 +93,33 @@ public function getProductName()
9793
: '';
9894
}
9995

96+
/**
97+
* Returns breadcrumb json with html escaped names
98+
*
99+
* @return string
100+
*/
101+
public function getJsonConfigurationHtmlEscaped()
102+
{
103+
return json_encode(
104+
[
105+
'breadcrumbs' => [
106+
'categoryUrlSuffix' => $this->escaper->escapeHtml($this->getCategoryUrlSuffix()),
107+
'useCategoryPathInUrl' => (int)$this->isCategoryUsedInProductUrl(),
108+
'product' => $this->escaper->escapeHtml($this->getProductName())
109+
]
110+
],
111+
JSON_HEX_TAG
112+
);
113+
}
114+
100115
/**
101116
* Returns breadcrumb json.
102117
*
103118
* @return string
119+
* @deprecated in favor of new method with name {suffix}Html{postfix}()
104120
*/
105121
public function getJsonConfiguration()
106122
{
107-
return $this->escaper->escapeHtml($this->json->serialize([
108-
'breadcrumbs' => [
109-
'categoryUrlSuffix' => $this->escaper->escapeHtml($this->getCategoryUrlSuffix()),
110-
'useCategoryPathInUrl' => (int)$this->isCategoryUsedInProductUrl(),
111-
'product' => $this->getProductName()
112-
]
113-
]));
123+
return $this->getJsonConfigurationHtmlEscaped();
114124
}
115125
}

app/code/Magento/Catalog/view/frontend/templates/product/breadcrumbs.phtml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,15 @@
77
/** @var \Magento\Catalog\ViewModel\Product\Breadcrumbs $viewModel */
88
$viewModel = $block->getData('viewModel');
99
?>
10-
<div class="breadcrumbs" data-mage-init='<?= /* @noEscape */ $viewModel->getJsonConfiguration() ?>'></div>
10+
<div class="breadcrumbs"></div>
11+
<?php
12+
$widget = $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonDecode($viewModel->getJsonConfigurationHtmlEscaped());
13+
$widgetOptions = $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($widget['breadcrumbs']);
14+
?>
15+
<script type="text/x-magento-init">
16+
{
17+
".breadcrumbs": {
18+
"breadcrumbs": <?= /* @noEscape */ $widgetOptions ?>
19+
}
20+
}
21+
</script>

app/code/Magento/Catalog/view/frontend/templates/product/list/toolbar.phtml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
// phpcs:disable PSR2.Methods.FunctionCallSignature.SpaceBeforeOpenBracket
1616
?>
1717
<?php if ($block->getCollection()->getSize()) :?>
18-
<div class="toolbar toolbar-products" data-mage-init='<?= /* @noEscape */ $block->getWidgetOptionsJson() ?>'>
18+
<?php $widget = $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonDecode($block->getWidgetOptionsJson());
19+
$widgetOptions = $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($widget['productListToolbarForm']);
20+
?>
21+
<div class="toolbar toolbar-products" data-mage-init='{"productListToolbarForm":<?= /* @noEscape */ $widgetOptions ?>}'>
1922
<?php if ($block->isExpanded()) :?>
2023
<?php include ($block->getTemplateFile('Magento_Catalog::product/list/toolbar/viewmode.phtml')) ?>
2124
<?php endif; ?>

app/code/Magento/Customer/view/frontend/templates/js/customer-data.phtml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
/** @var \Magento\Customer\Block\CustomerData $block */
88
?>
99
<script type="text/x-magento-init">
10-
<?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode([
11-
'*' => ['Magento_Customer/js/customer-data' => [
12-
'sectionLoadUrl' => $block->getCustomerDataUrl('customer/section/load'),
13-
'expirableSectionLifetime' => $block->getExpirableSectionLifetime(),
14-
'expirableSectionNames' => $block->getExpirableSectionNames(),
15-
'cookieLifeTime' => $block->getCookieLifeTime(),
16-
'updateSessionUrl' => $block->getCustomerDataUrl('customer/account/updateSession'),
17-
]],
18-
]);
19-
?>
10+
{
11+
"*": {
12+
"Magento_Customer/js/customer-data": {
13+
"sectionLoadUrl": "<?= $block->escapeJs($block->escapeUrl($block->getCustomerDataUrl('customer/section/load'))) ?>",
14+
"expirableSectionLifetime": <?= (int)$block->getExpirableSectionLifetime() ?>,
15+
"expirableSectionNames": <?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($block->getExpirableSectionNames()) ?>,
16+
"cookieLifeTime": "<?= $block->escapeJs($block->getCookieLifeTime()) ?>",
17+
"updateSessionUrl": "<?= $block->escapeJs($block->escapeUrl($block->getCustomerDataUrl('customer/account/updateSession'))) ?>"
18+
}
19+
}
20+
}
2021
</script>

app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77
/* @var $block \Magento\Customer\Block\CustomerScopeData */
88
?>
99
<script type="text/x-magento-init">
10-
<?= /* @noEscape */ \Magento\Framework\Serialize\JsonConverter::convert([
11-
'*' => ['Magento_Customer/js/invalidation-processor' => [
12-
'invalidationRules' => [
13-
'website-rule' => [
14-
'Magento_Customer/js/invalidation-rules/website-rule' => [
15-
'scopeConfig' => [
16-
'websiteId' => $block->getWebsiteId(),
17-
]
18-
]
19-
]
20-
]
21-
]],
22-
]);
23-
?>
10+
{
11+
"*": {
12+
"Magento_Customer/js/invalidation-processor": {
13+
"invalidationRules": {
14+
"website-rule": {
15+
"Magento_Customer/js/invalidation-rules/website-rule": {
16+
"scopeConfig": {
17+
"websiteId": "<?= $block->escapeJs($block->getWebsiteId()) ?>"
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}
2425
</script>

app/code/Magento/Customer/view/frontend/templates/js/section-config.phtml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
/** @var \Magento\Customer\Block\SectionConfig $block */
88
?>
99
<script type="text/x-magento-init">
10-
<?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode([
11-
'*' => ['Magento_Customer/js/section-config' => [
12-
'sections' => $block->getSections(),
13-
'clientSideSections' => $block->getClientSideSections(),
14-
'baseUrls' => array_unique([
15-
$block->getUrl(null, ['_secure' => true]),
16-
$block->getUrl(null, ['_secure' => false]),
17-
]),
18-
]],
19-
]);
20-
?>
10+
{
11+
"*": {
12+
"Magento_Customer/js/section-config": {
13+
"sections": <?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($block->getSections()) ?>,
14+
"clientSideSections": <?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($block->getClientSideSections()) ?>,
15+
"baseUrls": <?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode(array_unique([
16+
$block->getUrl(null, ['_secure' => true]),
17+
$block->getUrl(null, ['_secure' => false]),
18+
])) ?>
19+
}
20+
}
21+
}
2122
</script>

app/code/Magento/Msrp/view/base/templates/product/price/msrp.phtml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ $priceElementIdPrefix = $block->getPriceElementIdPrefix() ? $block->getPriceElem
7070

7171
$priceElementId = $priceElementIdPrefix . $productId . $block->getIdSuffix();
7272
$popupId = 'msrp-popup-' . $productId . $block->getRandomString(20);
73-
$data = ['addToCart' => [
73+
$data = [
7474
'origin' => 'msrp',
7575
'popupId' => '#' . $popupId,
7676
'productName' => $block->escapeJs($block->escapeHtml($product->getName())),
@@ -83,11 +83,11 @@ $priceElementIdPrefix = $block->getPriceElementIdPrefix() ? $block->getPriceElem
8383
'closeButtonId' => '#map-popup-close',
8484
'addToCartUrl' => $addToCartUrl,
8585
'paymentButtons' => '[data-label=or]'
86-
]];
86+
];
8787
if ($block->getRequest()->getFullActionName() === 'catalog_product_view') {
88-
$data['addToCart']['addToCartButton'] = '#product_addtocart_form [type=submit]';
88+
$data['addToCartButton'] = '#product_addtocart_form [type=submit]';
8989
} else {
90-
$data['addToCart']['addToCartButton'] = sprintf(
90+
$data['addToCartButton'] = sprintf(
9191
'form:has(input[type="hidden"][name="product"][value="%s"]) button[type="submit"]',
9292
(int)$productId
9393
);
@@ -98,7 +98,7 @@ $priceElementIdPrefix = $block->getPriceElementIdPrefix() ? $block->getPriceElem
9898
<a href="javascript:void(0);"
9999
id="<?= /* @noEscape */ ($popupId) ?>"
100100
class="action map-show-info"
101-
data-mage-init='<?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($data) ?>'>
101+
data-mage-init='{"addToCart":<?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($data) ?>}'>
102102
<?= $block->escapeHtml(__('Click for price')) ?>
103103
</a>
104104
<?php else : ?>

app/code/Magento/Paypal/view/frontend/templates/express/in-context/component.phtml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@ use Magento\Paypal\Block\Express\InContext\Minicart\Button;
88
/** @var \Magento\Paypal\Block\Express\InContext\Component $block */
99

1010
$configuration = [
11-
'*' => [
12-
'Magento_Paypal/js/in-context/express-checkout' => [
13-
'id' => Button::PAYPAL_BUTTON_ID,
14-
'path' => $block->getUrl(
15-
'paypal/express/gettoken',
16-
[
17-
'_secure' => $block->getRequest()->isSecure()
18-
]
19-
),
20-
'merchantId' => $block->getMerchantId(),
21-
'button' => $block->isButtonContext(),
22-
'clientConfig' => [
23-
'locale' => $block->getLocale(),
24-
'environment' => $block->getEnvironment(),
25-
'button' => [
26-
Button::PAYPAL_BUTTON_ID,
27-
],
28-
]
11+
'id' => Button::PAYPAL_BUTTON_ID,
12+
'path' => $block->getUrl(
13+
'paypal/express/gettoken',
14+
[
15+
'_secure' => $block->getRequest()->isSecure()
16+
]
17+
),
18+
'merchantId' => $block->getMerchantId(),
19+
'button' => $block->isButtonContext(),
20+
'clientConfig' => [
21+
'locale' => $block->getLocale(),
22+
'environment' => $block->getEnvironment(),
23+
'button' => [
24+
Button::PAYPAL_BUTTON_ID,
2925
]
3026
]
3127
];
3228

3329
?>
3430
<div style="display: none;" id="<?= /* @noEscape */ Button::PAYPAL_BUTTON_ID ?>"></div>
3531
<script type="text/x-magento-init">
36-
<?= /* @noEscape */ json_encode($configuration) ?>
32+
{
33+
"*": {
34+
"Magento_Paypal/js/in-context/express-checkout": <?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($configuration) ?>
35+
}
36+
}
3737
</script>

0 commit comments

Comments
 (0)