Skip to content

Commit c3bae80

Browse files
authored
Merge pull request #4 from magefan/11776-hyva-compatibility-and-secure-renderer
added Hyva compatibility and magefan secure renderer for inline js
2 parents d55c271 + 950fb30 commit c3bae80

File tree

8 files changed

+184
-62
lines changed

8 files changed

+184
-62
lines changed

Block/Adminhtml/System/Config/Form/Info.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,42 @@ protected function getModuleTitle()
2828
{
2929
return 'Product Label Extension';
3030
}
31+
32+
/**
33+
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element
34+
* @return string
35+
*/
36+
public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
37+
{
38+
return $this->processHyva() . parent::render($element);
39+
}
40+
41+
/**
42+
* @return string
43+
*/
44+
private function processHyva()
45+
{
46+
$result = '';
47+
48+
$hyvaThemeDetector = \Magento\Framework\App\ObjectManager::getInstance()->get(
49+
\Magefan\Community\Api\HyvaThemeDetectionInterface::class
50+
);
51+
52+
if ($hyvaThemeDetector->execute(0)) {
53+
$script = 'document.body.classList.add("hyva")';
54+
$result = $this->mfSecureRenderer->renderTag('script', [], $script, false);
55+
$result .= '
56+
<style>
57+
#mfproductlabel_general .hyva {display: block; font-weight: bold;}
58+
</style>
59+
';
60+
} else {
61+
$result = '
62+
<style>
63+
#mfproductlabel_general .hyva {display: none;}
64+
</style>';
65+
}
66+
67+
return $result;
68+
}
3169
}

Plugin/Frontend/Magento/Catalog/Block/Product/View/Gallery.php

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Magefan\ProductLabel\Model\Config;
1212
use Magefan\ProductLabel\Model\Parser\Html;
13+
use Magefan\Community\Api\HyvaThemeDetectionInterface;
1314

1415
class Gallery
1516
{
@@ -18,13 +19,21 @@ class Gallery
1819
*/
1920
protected $config;
2021

22+
/**
23+
* @var HyvaThemeDetectionInterface
24+
*/
25+
protected $hyvaThemeDetection;
26+
2127
/**
2228
* @param Config $config
29+
* @param HyvaThemeDetectionInterface $hyvaThemeDetection
2330
*/
2431
public function __construct(
25-
Config $config
32+
Config $config,
33+
HyvaThemeDetectionInterface $hyvaThemeDetection
2634
) {
2735
$this->config = $config;
36+
$this->hyvaThemeDetection = $hyvaThemeDetection;
2837
}
2938

3039
/**
@@ -34,13 +43,44 @@ public function __construct(
3443
*/
3544
public function afterToHtml($subject, $result)
3645
{
37-
if ($this->config->isEnabled()
38-
&& ($product = $subject->getProduct())
39-
&& (false !== strpos($result, 'gallery-placeholder__image')))
40-
{
41-
$result = $result . Html::COMMENT_PREFIX . $product->getId() . Html::COMMENT_SUFFIX;
46+
if ($this->config->isEnabled() && ($product = $subject->getProduct())) {
47+
$mfPlComment = Html::COMMENT_PREFIX . $product->getId() . Html::COMMENT_SUFFIX;
48+
49+
if ($this->hyvaThemeDetection->execute()) {
50+
$result = $this->addMfLabelContainerToImageWrapperTag($result, (int)$product->getId());
51+
} else {
52+
$result = $result . $mfPlComment;
53+
}
4254
}
4355

4456
return $result;
4557
}
58+
59+
/**
60+
* @param string $html
61+
* @param int $productId
62+
* @return string
63+
*/
64+
private function addMfLabelContainerToImageWrapperTag(string $html, int $productId): string
65+
{
66+
$wrapperClass = ltrim($this->config->getProductPageContainerSelector(), '.');
67+
68+
$imgTagPosition = strpos($html, '<img');
69+
70+
if ($imgTagPosition !== false) {
71+
$wrapperClassPosition = strpos($html, $wrapperClass);
72+
73+
if ($wrapperClassPosition !== false) {
74+
$endOfTagWithWrapperClassPosition = strpos($html, '>', $wrapperClassPosition);
75+
76+
if ($endOfTagWithWrapperClassPosition !== false) {
77+
$mfPlContainer = Html::COMMENT_PREFIX . $productId . Html::COMMENT_SUFFIX;
78+
79+
$html = substr_replace($html, $mfPlContainer, $endOfTagWithWrapperClassPosition + 1, 0);
80+
}
81+
}
82+
}
83+
84+
return $html;
85+
}
4686
}

etc/adminhtml/system.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
<label>Product Label</label>
1313
<tab>magefan</tab>
1414
<resource>Magefan_ProductLabel::mfproductlabel</resource>
15-
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
15+
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
1616
<label>General Configuration</label>
1717
<field id="version" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
1818
<frontend_model>Magefan\ProductLabel\Block\Adminhtml\System\Config\Form\Info</frontend_model>
1919
</field>
20-
<field id="enabled" translate="label" type="select" sortOrder="15" showInDefault="1" showInWebsite="0" showInStore="0">
20+
<field id="enabled" translate="label" type="select" sortOrder="15" showInDefault="1" showInWebsite="1" showInStore="1">
2121
<label>Enable Extension</label>
2222
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
2323
</field>
@@ -28,12 +28,18 @@
2828

2929
<field id="pp_selector" translate="css class parent label container" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
3030
<label>Product Page Label Container</label>
31-
<comment>Selector for Container</comment>
31+
<comment><![CDATA[
32+
Selector for Container <span class="hyva">[For Hyva Theme use: "relative self-center w-full"]</span>
33+
]]>
34+
</comment>
3235
</field>
3336

3437
<field id="pl_selector" translate="css class parent label container" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
3538
<label>Product List Label Container</label>
36-
<comment>Only one class that located above the product image!</comment>
39+
<comment><![CDATA[
40+
Only one class that located above the product image! <span class="hyva">[For Hyva Theme use: "product-image-photo"]</span>
41+
]]>
42+
</comment>
3743
</field>
3844
<field id="exclude_page_types" translate="label" type="multiselect" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
3945
<label>Don't Use On Pages</label>

view/adminhtml/templates/form/action_switcher.phtml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
* Copyright © Magefan (support@magefan.com). All rights reserved.
44
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
55
*/
6+
7+
/**
8+
* @var $mfSecureRenderer \Magefan\Community\Api\SecureHtmlRendererInterface
9+
*/
610
?>
711

8-
<script type="text/javascript">
9-
var checkbox = document.querySelector('[data-index="use_the_same_label_for_product_page"] input[type="checkbox"]');
12+
<?php $script = "
13+
var checkbox = document.querySelector('[data-index=\"use_the_same_label_for_product_page\"] input[type=\"checkbox\"]');
1014
1115
function toggleClassForUseTheSameLabelForProductPage() {
1216
@@ -19,4 +23,6 @@
1923
2024
checkbox.addEventListener('click', toggleClassForUseTheSameLabelForProductPage);
2125
toggleClassForUseTheSameLabelForProductPage();
22-
</script>
26+
"; ?>
27+
28+
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', [], $script, false) ?>

view/adminhtml/templates/form/available_attributes_list.phtml

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
* Copyright © Magefan (support@magefan.com). All rights reserved.
44
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
55
*/
6+
7+
/**
8+
* @var $block \Magefan\ProductLabel\Block\Adminhtml\Rule\Edit\AvailableAttributes
9+
* @var $escaper \Magento\Framework\Escaper
10+
* @var $mfSecureRenderer \Magefan\Community\Api\SecureHtmlRendererInterface
11+
*/
612
?>
713

814
<div id="search-for-attributes" style="display: none; width: 0px;">
@@ -30,30 +36,32 @@
3036
<button class="openbtn show-attr-list" onclick="openNav()">☰ <?=$escaper->escapeHtml(__('Attributes List')) ?></button>
3137
</div>
3238

33-
<script type="text/javascript">
39+
<?php $script = "
3440
function filterItems()
3541
{
36-
var filterValue = document.getElementById("attribute-input").value.toUpperCase();
42+
var filterValue = document.getElementById('attribute-input').value.toUpperCase();
3743
38-
document.querySelectorAll("#attributes-list li").forEach((el, i) => {
44+
document.querySelectorAll('#attributes-list li').forEach((el, i) => {
3945
el.style.display = (el.innerText.toUpperCase().includes(filterValue)) ? '' : 'none';
4046
});
4147
}
4248
4349
function openNav()
4450
{
45-
let el = document.getElementById("search-for-attributes");
46-
el.style.width = "350px";
47-
el.style.display = "";
51+
let el = document.getElementById('search-for-attributes');
52+
el.style.width = '350px';
53+
el.style.display = '';
4854
}
4955
5056
function closeNav()
5157
{
52-
let el = document.getElementById("search-for-attributes");
53-
el.style.width = "0";
54-
el.style.display = "none";
58+
let el = document.getElementById('search-for-attributes');
59+
el.style.width = '0';
60+
el.style.display = 'none';
5561
}
56-
</script>
62+
"; ?>
63+
64+
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', [], $script, false) ?>
5765

5866
<style>
5967
#search-for-attributes {

view/adminhtml/templates/form/versionsManager.phtml

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
* Copyright © Magefan (support@magefan.com). All rights reserved.
44
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
55
*/
6+
7+
/**
8+
* @var $mfSecureRenderer \Magefan\Community\Api\SecureHtmlRendererInterface
9+
*/
610
?>
711

812
<?php
@@ -18,33 +22,38 @@ if ($getModuleVersion->execute('Magefan_ProductLabelExtra')) {
1822
}
1923
?>
2024

21-
<script type="text/javascript">
22-
var versionsManager = {
23-
_currentPlan: '<?= /*@noEscape*/ $currentPlan ?>',
25+
26+
<?php $script = "
27+
var versionsManager = {
28+
_currentPlan: '" . $currentPlan . "',
2429
_selector: {
2530
'Extra': [
26-
'[name="customer_group_ids"]',
31+
'[name=\"customer_group_ids\"]',
2732
'[name=discard_subsequent_rules]',
28-
'[data-index="product_list"] [data-mf-type="1"]',
29-
'[data-index="product_page"] [data-mf-type="1"]',
30-
'[data-index="active_from"]',
31-
'[data-index="active_to"]',
32-
'[data-index="active_from"] button',
33-
'[data-index="active_to"] button',
34-
'[name="apply_by"]'
35-
],
36-
<?php if ($currentPlan != 'Plus') { ?>
37-
'Plus': [
38-
'.show-attr-list',
39-
'[data-index="product_list"] [data-mf-type="2"]',
40-
'[data-index="product_page"] [data-mf-type="2"]',
41-
'[name="use_the_same_label_for_product_page"]',
42-
'[data-index="preview_button"]',
43-
'[data-index="hide_preview_button"]',
44-
'[data-index="position"]',
45-
'[data-index="pp_position"]'
46-
]
47-
<?php } ?>
33+
'[data-index=\"product_list\"] [data-mf-type=\"1\"]',
34+
'[data-index=\"product_page\"] [data-mf-type=\"1\"]',
35+
'[data-index=\"active_from\"]',
36+
'[data-index=\"active_to\"]',
37+
'[data-index=\"active_from\"] button',
38+
'[data-index=\"active_to\"] button',
39+
'[name=\"apply_by\"]'
40+
],";
41+
42+
if ($currentPlan != 'Plus') {
43+
$script .= "
44+
'Plus': [
45+
'.show-attr-list',
46+
'[data-index=\"product_list\"] [data-mf-type=\"2\"]',
47+
'[data-index=\"product_page\"] [data-mf-type=\"2\"]',
48+
'[name=\"use_the_same_label_for_product_page\"]',
49+
'[data-index=\"preview_button\"]',
50+
'[data-index=\"hide_preview_button\"]',
51+
'[data-index=\"position\"]',
52+
'[data-index=\"pp_position\"]'
53+
]";
54+
}
55+
56+
$script .= "
4857
},
4958
5059
initListener: function () {
@@ -78,15 +87,15 @@ if ($getModuleVersion->execute('Magefan_ProductLabelExtra')) {
7887
},
7988
8089
showAlert: function (extensionPlan) {
81-
require(["jquery", "Magento_Ui/js/modal/alert"], function($, alert) {
90+
require(['jquery', 'Magento_Ui/js/modal/alert'], function($, alert) {
8291
alert({
83-
title: "You cannot use this option.",
84-
content: "This feature is available in <strong>" + extensionPlan + "</strong> plan only.",
92+
title: 'You cannot use this option.',
93+
content: 'This feature is available in <strong>' + extensionPlan + '</strong> plan only.',
8594
buttons: [{
86-
text: "Upgrade Plan Now",
87-
class: "action primary accept",
95+
text: 'Upgrade Plan Now',
96+
class: 'action primary accept',
8897
click: function () {
89-
window.open("https://magefan.com");
98+
window.open('https://magefan.com');
9099
}
91100
}]
92101
});
@@ -95,5 +104,6 @@ if ($getModuleVersion->execute('Magefan_ProductLabelExtra')) {
95104
};
96105
97106
versionsManager.initListener();
107+
"; ?>
98108

99-
</script>
109+
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', [], $script, false) ?>

view/frontend/templates/label-css.phtml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
/*.mf-label-container {display: block;top:0;right: 0;left: 0;bottom: 0; width: 100%; height: 100%}*/
1313
.mf-label-container .mf-label {position: absolute; z-index: 9; width: fit-content;}
1414
.mf-label-container .mf-label.mf-label-position-top-left {top: 0;left: 0;}
15+
16+
/* HYVA START */
17+
.product-item-photo {position: relative;}
18+
/* HYVA END */
1519
</style>
1620

1721

view/frontend/templates/product/initLabel.phtml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,28 @@
55
*/
66
?>
77
<?php
8-
/** @var $block \Magefan\ProductLabel\Block\LabelCss */
8+
/**
9+
* @var $block \Magefan\ProductLabel\Block\LabelCss
10+
* @var $mfSecureRenderer \Magefan\Community\Api\SecureHtmlRendererInterface
11+
* @var $mfHyvaThemeDetection \Magefan\Community\Api\HyvaThemeDetectionInterface
12+
* @var $escaper \Magento\Framework\Escaper
13+
*/
914
?>
1015

11-
<style>
12-
.fotorama .mf-label-position-custom {display: none;}
13-
.product.media > .mf-label-container {display: none;}
14-
</style>
16+
<?php if (!$mfHyvaThemeDetection->execute()) { ?>
17+
<style>
18+
.fotorama .mf-label-position-custom {display: none;}
19+
.product.media > .mf-label-container {display: none;}
20+
</style>
1521

16-
<script type="text/javascript">
22+
<?php $script = "
1723
require(['jquery'], function ($) {
1824
$('[data-gallery-role=gallery-placeholder]').on('gallery:loaded', function () {
19-
$(this).parent().find('.mf-label-container').prependTo('<?= $escaper->escapeHtml($block->getConfig()->getProductPageContainerSelector()) ?>')
25+
$(this).parent().find('.mf-label-container').prependTo('" . $escaper->escapeHtml($block->getConfig()->getProductPageContainerSelector()) . "')
2026
});
2127
});
22-
</script>
28+
"; ?>
29+
30+
<?= /* @noEscape */ $mfSecureRenderer->renderTag('script', [], $script, false) ?>
31+
32+
<?php } ?>

0 commit comments

Comments
 (0)