Skip to content

Commit e3c963d

Browse files
committed
Refactored last merge request
1 parent ced61b9 commit e3c963d

File tree

4 files changed

+44
-48
lines changed

4 files changed

+44
-48
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ protected function _prepareToRender()
2424
$this->_addAfter = false;
2525
$this->_addButtonLabel = __('Add');
2626
}
27-
}
27+
}

Model/Config.php

+12-7
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ class Config extends \Magento\Framework\App\Helper\AbstractHelper
4747
public function __construct(
4848
Context $context,
4949
SerializerInterface $serializer
50-
)
51-
{
50+
) {
5251
$this->serializer = $serializer;
5352
parent::__construct($context);
5453
}
@@ -96,19 +95,25 @@ public function getBlocks(): array
9695
* @param $blockIdentifier
9796
* @return int
9897
*/
99-
public function getBlockFirstImagesToSkip($blockIdentifier): int {
100-
return (int)($this->getBlocksInfo()[$blockIdentifier] ?? 0);
98+
public function getBlockFirstImagesToSkip($blockIdentifier): int
99+
{
100+
$blockInfo = $this->getBlocksInfo();
101+
if (isset($blockInfo[$blockIdentifier])) {
102+
return (int)$blockInfo[$blockIdentifier];
103+
}
104+
105+
return 0;
101106
}
102107

103108
/**
104109
* @return array
105110
*/
106-
public function getBlocksInfo(): array {
111+
public function getBlocksInfo(): array
112+
{
107113
if (null === $this->blocks) {
108114
try {
109115
$blocks = $this->serializer->unserialize($this->getConfig(self::XML_PATH_LAZY_BLOCKS));
110-
}
111-
catch (\InvalidArgumentException $e) {
116+
} catch (\InvalidArgumentException $e) {
112117
return [];
113118
}
114119

Plugin/BlockPlugin.php

+25-36
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,16 @@ public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block
7878
return $html;
7979
}
8080

81-
$blockIdentifier = $this->getBlockIdentifier($block);
82-
$numberOfReplacements = $this->config->getBlockFirstImagesToSkip($blockIdentifier);
81+
if ($this->config->getIsJavascriptLazyLoadMethod()) {
8382

84-
if ($numberOfReplacements) {
85-
$html = $this->removeFirstNImagesWithCustomLabel($html, $numberOfReplacements);
86-
}
83+
$numberOfReplacements = $this->config->getBlockFirstImagesToSkip(
84+
$this->getBlockIdentifier($block)
85+
);
86+
87+
if ($numberOfReplacements) {
88+
$html = $this->removeFirstNImagesWithCustomLabel($html, $numberOfReplacements);
89+
}
8790

88-
if ($this->config->getIsJavascriptLazyLoadMethod()) {
8991
$pixelSrc = ' src="' . $block->getViewFileUrl('Magefan_LazyLoad::images/pixel.jpg') . '"';
9092
$tmpSrc = 'TMP_SRC';
9193

@@ -120,36 +122,34 @@ public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block
120122
$html = str_replace('background-image-', 'mflazy-background-image mflazy-background-image-', $html);
121123
$html = str_replace('.tmpbgimg-', '.background-image-', $html);
122124
}
125+
126+
if ($numberOfReplacements) {
127+
$html = $this->revertFirstNImageToInital($html);
128+
}
123129
} else {
124130
$html = preg_replace('#<img(?!\s+mfdislazy)([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU', '<img ' .
125131
' src="$2" $1 $3 loading="lazy" />
126132
', $html);
127133
}
128134

129-
if ($numberOfReplacements) {
130-
$html = $this->revertFirstNImageToInital($html);
131-
return $this->deleteFirstNLoadingLazy($html, $numberOfReplacements);
132-
}
133-
134135
return $html;
135136
}
136137

137138
/**
138139
* @param \Magento\Framework\View\Element\AbstractBlock $block
139140
* @return string
140141
*/
141-
protected function getBlockIdentifier(\Magento\Framework\View\Element\AbstractBlock $block): string {
142+
protected function getBlockIdentifier(\Magento\Framework\View\Element\AbstractBlock $block): string
143+
{
142144
$blockName = $block->getBlockId() ?: $block->getNameInLayout();
143145
$blockTemplate = $block->getTemplate();
144146
$blocks = $this->config->getBlocks();
145147

146148
if (in_array($blockName, $blocks)) {
147149
return $blockName;
148-
}
149-
else if (in_array(get_class($block), $blocks)) {
150+
} elseif (in_array(get_class($block), $blocks)) {
150151
return get_class($block);
151-
}
152-
else if (in_array($blockTemplate, $blocks)) {
152+
} elseif (in_array($blockTemplate, $blocks)) {
153153
return $blockTemplate;
154154
}
155155

@@ -161,7 +161,8 @@ protected function getBlockIdentifier(\Magento\Framework\View\Element\AbstractBl
161161
* @param int $numberOfReplacements
162162
* @return array|string|string[]|null
163163
*/
164-
protected function removeFirstNImagesWithCustomLabel($html, int $numberOfReplacements) {
164+
protected function removeFirstNImagesWithCustomLabel($html, int $numberOfReplacements)
165+
{
165166
$count = 0;
166167
return preg_replace_callback('#<img([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU', function ($match) use (&$count, &$numberOfReplacements) {
167168
$count++;
@@ -180,21 +181,13 @@ protected function removeFirstNImagesWithCustomLabel($html, int $numberOfReplace
180181
* @param $html
181182
* @return array|string|string[]|null
182183
*/
183-
protected function revertFirstNImageToInital($html) {
184-
return preg_replace_callback('/' . self::REPLACEMENT_LABEL .'_\d+\b(.*?)/', function($match) use (&$count) {
184+
protected function revertFirstNImageToInital($html)
185+
{
186+
return preg_replace_callback('/' . self::REPLACEMENT_LABEL .'_\d+\b(.*?)/', function ($match) use (&$count) {
185187
return $this->labelsValues[$match[0]] ?? $match[0];
186188
}, $html);
187189
}
188190

189-
/**
190-
* @param $html
191-
* @param int $numberOfDeletions
192-
* @return array|string|string[]|null
193-
*/
194-
protected function deleteFirstNLoadingLazy($html,int $numberOfDeletions) {
195-
return preg_replace('/loading="lazy"/', '', $html, $numberOfDeletions, $count);
196-
}
197-
198191
/**
199192
* Check if lazy load is available for block
200193
* @param \Magento\Framework\View\Element\AbstractBlock $block
@@ -216,15 +209,11 @@ protected function isEnabled($block, string $html): bool
216209
return false;
217210
}
218211

219-
$blockName = $block->getBlockId() ?: $block->getNameInLayout();
220-
$blockTemplate = $block->getTemplate();
221-
$blocks = $this->config->getBlocks();
212+
if (false !== strpos($html, self::LAZY_TAG)) {
213+
return true;
214+
}
222215

223-
if (!in_array($blockName, $blocks)
224-
&& !in_array(get_class($block), $blocks)
225-
&& !in_array($blockTemplate, $blocks)
226-
&& (false === strpos($html, self::LAZY_TAG))
227-
) {
216+
if (!$this->getBlockIdentifier($block)) {
228217
return false;
229218
}
230219

Setup/Patch/Data/ConvertConfigToJsonPatch.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ public function apply()
118118
* @param $blocks
119119
* @return bool|string
120120
*/
121-
protected function getJsonForBlocks($blocks) {
121+
protected function getJsonForBlocks($blocks)
122+
{
122123
$arrayBlocks = [];
123124
$counter = 1;
124125
foreach ($blocks as $block) {
@@ -154,13 +155,14 @@ public function getAliases()
154155
* @param $blocks
155156
* @return array
156157
*/
157-
protected function getBlocks ($blocks): array {
158+
protected function getBlocks($blocks): array
159+
{
158160
json_decode($blocks);
159161
if (json_last_error() === JSON_ERROR_NONE) {
160162
return [];
161163
}
162164

163-
$blocks = str_replace(["\r\n", "\n'\r", "\n"], "\r", $blocks);
165+
$blocks = str_replace(["\r\n", "\n\r", "\n"], "\r", $blocks);
164166
$blocks = explode("\r", $blocks);
165167
$result = [];
166168
foreach ($blocks as $block) {
@@ -171,4 +173,4 @@ protected function getBlocks ($blocks): array {
171173

172174
return $result;
173175
}
174-
}
176+
}

0 commit comments

Comments
 (0)