diff --git a/Block/Adminhtml/System/Config/Form/DynamicRow.php b/Block/Adminhtml/System/Config/Form/DynamicRow.php
index 84d00a8..572e9db 100644
--- a/Block/Adminhtml/System/Config/Form/DynamicRow.php
+++ b/Block/Adminhtml/System/Config/Form/DynamicRow.php
@@ -24,4 +24,4 @@ protected function _prepareToRender()
$this->_addAfter = false;
$this->_addButtonLabel = __('Add');
}
-}
\ No newline at end of file
+}
diff --git a/Model/Config.php b/Model/Config.php
index a30e37b..dd6ffa7 100644
--- a/Model/Config.php
+++ b/Model/Config.php
@@ -47,8 +47,7 @@ class Config extends \Magento\Framework\App\Helper\AbstractHelper
public function __construct(
Context $context,
SerializerInterface $serializer
- )
- {
+ ) {
$this->serializer = $serializer;
parent::__construct($context);
}
@@ -96,19 +95,25 @@ public function getBlocks(): array
* @param $blockIdentifier
* @return int
*/
- public function getBlockFirstImagesToSkip($blockIdentifier): int {
- return (int)($this->getBlocksInfo()[$blockIdentifier] ?? 0);
+ public function getBlockFirstImagesToSkip($blockIdentifier): int
+ {
+ $blockInfo = $this->getBlocksInfo();
+ if (isset($blockInfo[$blockIdentifier])) {
+ return (int)$blockInfo[$blockIdentifier];
+ }
+
+ return 0;
}
/**
* @return array
*/
- public function getBlocksInfo(): array {
+ public function getBlocksInfo(): array
+ {
if (null === $this->blocks) {
try {
$blocks = $this->serializer->unserialize($this->getConfig(self::XML_PATH_LAZY_BLOCKS));
- }
- catch (\InvalidArgumentException $e) {
+ } catch (\InvalidArgumentException $e) {
return [];
}
diff --git a/Plugin/BlockPlugin.php b/Plugin/BlockPlugin.php
index 0420b4a..2c8c900 100644
--- a/Plugin/BlockPlugin.php
+++ b/Plugin/BlockPlugin.php
@@ -78,14 +78,16 @@ public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block
return $html;
}
- $blockIdentifier = $this->getBlockIdentifier($block);
- $numberOfReplacements = $this->config->getBlockFirstImagesToSkip($blockIdentifier);
+ if ($this->config->getIsJavascriptLazyLoadMethod()) {
- if ($numberOfReplacements) {
- $html = $this->removeFirstNImagesWithCustomLabel($html, $numberOfReplacements);
- }
+ $numberOfReplacements = $this->config->getBlockFirstImagesToSkip(
+ $this->getBlockIdentifier($block)
+ );
+
+ if ($numberOfReplacements) {
+ $html = $this->removeFirstNImagesWithCustomLabel($html, $numberOfReplacements);
+ }
- if ($this->config->getIsJavascriptLazyLoadMethod()) {
$pixelSrc = ' src="' . $block->getViewFileUrl('Magefan_LazyLoad::images/pixel.jpg') . '"';
$tmpSrc = 'TMP_SRC';
@@ -120,17 +122,16 @@ public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block
$html = str_replace('background-image-', 'mflazy-background-image mflazy-background-image-', $html);
$html = str_replace('.tmpbgimg-', '.background-image-', $html);
}
+
+ if ($numberOfReplacements) {
+ $html = $this->revertFirstNImageToInital($html);
+ }
} else {
$html = preg_replace('#]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU', '
', $html);
}
- if ($numberOfReplacements) {
- $html = $this->revertFirstNImageToInital($html);
- return $this->deleteFirstNLoadingLazy($html, $numberOfReplacements);
- }
-
return $html;
}
@@ -138,18 +139,17 @@ public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block
* @param \Magento\Framework\View\Element\AbstractBlock $block
* @return string
*/
- protected function getBlockIdentifier(\Magento\Framework\View\Element\AbstractBlock $block): string {
+ protected function getBlockIdentifier(\Magento\Framework\View\Element\AbstractBlock $block): string
+ {
$blockName = $block->getBlockId() ?: $block->getNameInLayout();
$blockTemplate = $block->getTemplate();
$blocks = $this->config->getBlocks();
if (in_array($blockName, $blocks)) {
return $blockName;
- }
- else if (in_array(get_class($block), $blocks)) {
+ } elseif (in_array(get_class($block), $blocks)) {
return get_class($block);
- }
- else if (in_array($blockTemplate, $blocks)) {
+ } elseif (in_array($blockTemplate, $blocks)) {
return $blockTemplate;
}
@@ -161,7 +161,8 @@ protected function getBlockIdentifier(\Magento\Framework\View\Element\AbstractBl
* @param int $numberOfReplacements
* @return array|string|string[]|null
*/
- protected function removeFirstNImagesWithCustomLabel($html, int $numberOfReplacements) {
+ protected function removeFirstNImagesWithCustomLabel($html, int $numberOfReplacements)
+ {
$count = 0;
return preg_replace_callback('#]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU', function ($match) use (&$count, &$numberOfReplacements) {
$count++;
@@ -180,21 +181,13 @@ protected function removeFirstNImagesWithCustomLabel($html, int $numberOfReplace
* @param $html
* @return array|string|string[]|null
*/
- protected function revertFirstNImageToInital($html) {
- return preg_replace_callback('/' . self::REPLACEMENT_LABEL .'_\d+\b(.*?)/', function($match) use (&$count) {
+ protected function revertFirstNImageToInital($html)
+ {
+ return preg_replace_callback('/' . self::REPLACEMENT_LABEL .'_\d+\b(.*?)/', function ($match) use (&$count) {
return $this->labelsValues[$match[0]] ?? $match[0];
}, $html);
}
- /**
- * @param $html
- * @param int $numberOfDeletions
- * @return array|string|string[]|null
- */
- protected function deleteFirstNLoadingLazy($html,int $numberOfDeletions) {
- return preg_replace('/loading="lazy"/', '', $html, $numberOfDeletions, $count);
- }
-
/**
* Check if lazy load is available for block
* @param \Magento\Framework\View\Element\AbstractBlock $block
@@ -216,15 +209,11 @@ protected function isEnabled($block, string $html): bool
return false;
}
- $blockName = $block->getBlockId() ?: $block->getNameInLayout();
- $blockTemplate = $block->getTemplate();
- $blocks = $this->config->getBlocks();
+ if (false !== strpos($html, self::LAZY_TAG)) {
+ return true;
+ }
- if (!in_array($blockName, $blocks)
- && !in_array(get_class($block), $blocks)
- && !in_array($blockTemplate, $blocks)
- && (false === strpos($html, self::LAZY_TAG))
- ) {
+ if (!$this->getBlockIdentifier($block)) {
return false;
}
diff --git a/Setup/Patch/Data/ConvertConfigToJsonPatch.php b/Setup/Patch/Data/ConvertConfigToJsonPatch.php
index a6414d7..fe32bf3 100644
--- a/Setup/Patch/Data/ConvertConfigToJsonPatch.php
+++ b/Setup/Patch/Data/ConvertConfigToJsonPatch.php
@@ -118,7 +118,8 @@ public function apply()
* @param $blocks
* @return bool|string
*/
- protected function getJsonForBlocks($blocks) {
+ protected function getJsonForBlocks($blocks)
+ {
$arrayBlocks = [];
$counter = 1;
foreach ($blocks as $block) {
@@ -154,13 +155,14 @@ public function getAliases()
* @param $blocks
* @return array
*/
- protected function getBlocks ($blocks): array {
+ protected function getBlocks($blocks): array
+ {
json_decode($blocks);
if (json_last_error() === JSON_ERROR_NONE) {
return [];
}
- $blocks = str_replace(["\r\n", "\n'\r", "\n"], "\r", $blocks);
+ $blocks = str_replace(["\r\n", "\n\r", "\n"], "\r", $blocks);
$blocks = explode("\r", $blocks);
$result = [];
foreach ($blocks as $block) {
@@ -171,4 +173,4 @@ protected function getBlocks ($blocks): array {
return $result;
}
-}
\ No newline at end of file
+}