Skip to content

Commit

Permalink
Refactored last merge request
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorvansach committed Jun 9, 2023
1 parent ced61b9 commit e3c963d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Block/Adminhtml/System/Config/Form/DynamicRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ protected function _prepareToRender()
$this->_addAfter = false;
$this->_addButtonLabel = __('Add');
}
}
}
19 changes: 12 additions & 7 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 [];
}

Expand Down
61 changes: 25 additions & 36 deletions Plugin/BlockPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -120,36 +122,34 @@ 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('#<img(?!\s+mfdislazy)([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU', '<img ' .
' src="$2" $1 $3 loading="lazy" />
', $html);
}

if ($numberOfReplacements) {
$html = $this->revertFirstNImageToInital($html);
return $this->deleteFirstNLoadingLazy($html, $numberOfReplacements);
}

return $html;
}

/**
* @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;
}

Expand All @@ -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('#<img([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU', function ($match) use (&$count, &$numberOfReplacements) {
$count++;
Expand All @@ -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
Expand All @@ -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;
}

Expand Down
10 changes: 6 additions & 4 deletions Setup/Patch/Data/ConvertConfigToJsonPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -171,4 +173,4 @@ protected function getBlocks ($blocks): array {

return $result;
}
}
}

0 comments on commit e3c963d

Please sign in to comment.