Skip to content

Commit 20be636

Browse files
committed
Added lazy load method option
1 parent 8916dd9 commit 20be636

File tree

12 files changed

+184
-72
lines changed

12 files changed

+184
-72
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Glory to Ukraine! Glory to the heroes!
77
*/
88

9+
declare(strict_types=1);
10+
911
namespace Magefan\LazyLoad\Block\Adminhtml\System\Config\Form;
1012

1113
/**

Block/Lazy.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,41 @@
66
* Glory to Ukraine! Glory to the heroes!
77
*/
88

9+
declare(strict_types=1);
10+
911
namespace Magefan\LazyLoad\Block;
1012

11-
use Magento\Store\Model\ScopeInterface;
13+
use Magento\Framework\View\Element\Template;
14+
use Magento\Framework\View\Element\Template\Context;
15+
use Magefan\LazyLoad\Model\Config;
1216

1317
/**
1418
* Init lazy load
1519
*/
16-
class Lazy extends \Magento\Framework\View\Element\Template
20+
class Lazy extends Template
1721
{
22+
/**
23+
* @param Context $context
24+
* @param Config $config
25+
* @param array $data
26+
*/
27+
public function __construct(
28+
Context $context,
29+
Config $config,
30+
array $data = []
31+
) {
32+
parent::__construct($context, $data);
33+
$this->config = $config;
34+
}
35+
1836
/**
1937
* Retrieve block html
2038
*
2139
* @return string
2240
*/
2341
protected function _toHtml()
2442
{
25-
if ($this->_scopeConfig->getValue(
26-
'mflazyzoad/general/enabled',
27-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
28-
)) {
43+
if ($this->config->getEnabled()) {
2944
return parent::_toHtml();
3045
}
3146

@@ -35,20 +50,25 @@ protected function _toHtml()
3550
/**
3651
* @return bool
3752
*/
38-
public function isNoScriptEnabled()
53+
public function isNoScriptEnabled(): bool
54+
{
55+
return (bool)$this->config->isNoScriptEnabled();
56+
}
57+
58+
/**
59+
* @return bool
60+
*/
61+
public function getIsJavascriptLazyLoadMethod(): bool
3962
{
40-
return (bool)$this->_scopeConfig->getValue(
41-
\Magefan\LazyLoad\Model\Config::XML_PATH_LAZY_NOSCRIPT,
42-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
43-
);
63+
return $this->config->getIsJavascriptLazyLoadMethod();
4464
}
4565

4666
/**
4767
* Retrieve lazy load config json string
4868
*
4969
* @return string
5070
*/
51-
public function getLazyLoadConfig()
71+
public function getLazyLoadConfig(): string
5272
{
5373
$config = $this->getData('lazy_load_config');
5474

Model/Config.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* Glory to Ukraine! Glory to the heroes!
77
*/
88

9-
namespace Magefan\LazyLoad\Model;
9+
declare(strict_types=1);
1010

11-
use Magento\Framework\App\Action\Action;
11+
namespace Magefan\LazyLoad\Model;
1212

1313
/**
1414
* Lazy load config
@@ -18,8 +18,10 @@ class Config extends \Magento\Framework\App\Helper\AbstractHelper
1818
const XML_PATH_ENABLED = 'mflazyzoad/general/enabled';
1919
const XML_PATH_AMP_ENABLED = 'pramp/general/enabled';
2020
const XML_PATH_LAZY_BLOCKS = 'mflazyzoad/general/lazy_blocks';
21+
const XML_PATH_LAZY_METHOD = 'mflazyzoad/general/method';
2122
const XML_PATH_LAZY_NOSCRIPT = 'mflazyzoad/general/noscript';
2223

24+
2325
/**
2426
* @var array
2527
*/
@@ -46,16 +48,25 @@ public function getConfig($path)
4648
/**
4749
* @return bool
4850
*/
49-
public function isNoScriptEnabled()
51+
public function getIsJavascriptLazyLoadMethod(): bool
52+
{
53+
return (0 === (int)$this->getConfig(self::XML_PATH_LAZY_METHOD));
54+
}
55+
56+
/**
57+
* @return bool
58+
*/
59+
public function isNoScriptEnabled(): bool
5060
{
51-
return (bool)$this->getConfig(self::XML_PATH_LAZY_NOSCRIPT);
61+
return (bool)$this->getConfig(self::XML_PATH_LAZY_NOSCRIPT)
62+
&& $this->getIsJavascriptLazyLoadMethod();
5263
}
5364

5465
/**
5566
* Retrieve alloved blocks info
5667
* @return array
5768
*/
58-
public function getBlocks()
69+
public function getBlocks(): array
5970
{
6071
if (null === $this->blocks) {
6172
$blocks = $this->getConfig(self::XML_PATH_LAZY_BLOCKS);
@@ -75,12 +86,12 @@ public function getBlocks()
7586

7687
/**
7788
* Retrieve true if enabled
78-
* @return int
89+
* @return bool
7990
*/
80-
public function getEnabled()
91+
public function getEnabled(): bool
8192
{
8293
if (null === $this->enabled) {
83-
$this->enabled = $this->getConfig(self::XML_PATH_ENABLED);
94+
$this->enabled = (bool)$this->getConfig(self::XML_PATH_ENABLED);
8495

8596
/* check if Plumrocket AMP enabled */
8697
if ($this->enabled) {

Model/Config/Source/Method.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
declare(strict_types=1);
9+
10+
namespace Magefan\LazyLoad\Model\Config\Source;
11+
12+
/**
13+
* Comment statuses
14+
*/
15+
class Method implements \Magento\Framework\Option\ArrayInterface
16+
{
17+
/**
18+
* @const string
19+
*/
20+
const JAVASCRIPT = 0;
21+
22+
/**
23+
* @const int
24+
*/
25+
const NATIVE = 1;
26+
27+
28+
/**
29+
* Options int
30+
*
31+
* @return array
32+
*/
33+
public function toOptionArray(): array
34+
{
35+
return [
36+
['value' => self::JAVASCRIPT, 'label' => __('Non-jQuer JavaScript Library')],
37+
['value' => self::NATIVE, 'label' => __('Native Browser Lazy Loading')],
38+
];
39+
}
40+
41+
/**
42+
* Get options in "key-value" format
43+
*
44+
* @return array
45+
*/
46+
public function toArray(): array
47+
{
48+
$array = [];
49+
foreach ($this->toOptionArray() as $item) {
50+
$array[$item['value']] = $item['label'];
51+
}
52+
return $array;
53+
}
54+
}

Plugin/Amasty/PageSpeedOptimizer/Model/Output/LazyLoadProcessorPlugin.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Glory to Ukraine! Glory to the heroes!
77
*/
88

9+
declare(strict_types=1);
10+
911
namespace Magefan\LazyLoad\Plugin\Amasty\PageSpeedOptimizer\Model\Output;
1012

1113
use Magefan\LazyLoad\Model\Config;
@@ -16,8 +18,8 @@
1618
class LazyLoadProcessorPlugin
1719
{
1820
/**
19-
* @var Config
20-
*/
21+
* @var Config
22+
*/
2123
private $config;
2224

2325
/**
@@ -26,7 +28,6 @@ class LazyLoadProcessorPlugin
2628
*/
2729
public function __construct(
2830
Config $config
29-
3031
) {
3132
$this->config = $config;
3233
}
@@ -40,7 +41,7 @@ public function __construct(
4041
*/
4142
public function aroundReplaceWithPictureTag($subject, callable $proceed, $image, $imagePath)
4243
{
43-
if (!$this->config->getEnabled()) {
44+
if (!$this->config->getEnabled() || !$this->config->getIsJavascriptLazyLoadMethod()) {
4445
return $proceed($image, $imagePath);
4546
}
4647

@@ -91,7 +92,7 @@ public function aroundReplaceWithPictureTag($subject, callable $proceed, $image,
9192
public function aroundReplace($subject, callable $proceed, $algorithm, $image, $imagePath)
9293
{
9394

94-
if (!$this->config->getEnabled()) {
95+
if (!$this->config->getEnabled() || !$this->config->getIsJavascriptLazyLoadMethod()) {
9596
return $proceed($algorithm, $image, $imagePath);
9697
}
9798

Plugin/BlockPlugin.php

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace Magefan\LazyLoad\Plugin;
1212

13+
use Magefan\LazyLoad\Model\Config;
14+
1315
/**
1416
* Plugin for sitemap generation
1517
*/
@@ -43,23 +45,18 @@ class BlockPlugin
4345
protected $config;
4446

4547
/**
46-
* BlockPlugin constructor
4748
* @param \Magento\Framework\App\RequestInterface $request
4849
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
49-
* @param null|\Magefan\LazyLoad\Model\Config $config
50+
* @param Config $config
5051
*/
5152
public function __construct(
5253
\Magento\Framework\App\RequestInterface $request,
5354
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
54-
$config = null
55+
Config $config
5556
) {
5657
$this->request = $request;
5758
$this->scopeConfig = $scopeConfig;
58-
59-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
60-
$this->config = $config ?: $objectManager->get(
61-
\Magefan\LazyLoad\Model\Config::class
62-
);
59+
$this->config = $config;
6360
}
6461

6562

@@ -74,39 +71,46 @@ public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block
7471
return $html;
7572
}
7673

77-
$pixelSrc = ' src="' . $block->getViewFileUrl('Magefan_LazyLoad::images/pixel.jpg') . '"';
78-
$tmpSrc = 'TMP_SRC';
79-
80-
$html = str_replace($pixelSrc, $tmpSrc, $html);
81-
82-
$noscript = '';
83-
if ($this->config->isNoScriptEnabled()) {
84-
$noscript = '<noscript>
85-
<img src="$2" $1 $3 />
86-
</noscript>';
87-
}
88-
89-
$html = preg_replace('#<img(?!\s+mfdislazy)([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU', '<img ' .
90-
' data-original="$2" $1 $3/>
91-
' . $noscript, $html);
92-
93-
$html = str_replace(' data-original=', $pixelSrc . ' data-original=', $html);
94-
95-
$html = str_replace($tmpSrc, $pixelSrc, $html);
96-
$html = str_replace(self::LAZY_TAG, '', $html);
97-
98-
/* Disable Owl Slider LazyLoad */
99-
$html = str_replace(
100-
['"lazyLoad":true,', '&quot;lazyLoad&quot;:true,', 'owl-lazy'],
101-
['"lazyLoad":false,', '&quot;lazyLoad&quot;:false,', ''],
102-
$html
103-
);
104-
105-
/* Fix for page builder bg images */
106-
if (false !== strpos($html, 'background-image-')) {
107-
$html = str_replace('.background-image-', '.tmpbgimg-', $html);
108-
$html = str_replace('background-image-', 'mflazy-background-image mflazy-background-image-', $html);
109-
$html = str_replace('.tmpbgimg-', '.background-image-', $html);
74+
if ($this->config->getIsJavascriptLazyLoadMethod()) {
75+
76+
$pixelSrc = ' src="' . $block->getViewFileUrl('Magefan_LazyLoad::images/pixel.jpg') . '"';
77+
$tmpSrc = 'TMP_SRC';
78+
79+
$html = str_replace($pixelSrc, $tmpSrc, $html);
80+
81+
$noscript = '';
82+
if ($this->config->isNoScriptEnabled()) {
83+
$noscript = '<noscript>
84+
<img src="$2" $1 $3 />
85+
</noscript>';
86+
}
87+
88+
$html = preg_replace('#<img(?!\s+mfdislazy)([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU', '<img ' .
89+
' data-original="$2" $1 $3/>
90+
' . $noscript, $html);
91+
92+
$html = str_replace(' data-original=', $pixelSrc . ' data-original=', $html);
93+
94+
$html = str_replace($tmpSrc, $pixelSrc, $html);
95+
$html = str_replace(self::LAZY_TAG, '', $html);
96+
97+
/* Disable Owl Slider LazyLoad */
98+
$html = str_replace(
99+
['"lazyLoad":true,', '&quot;lazyLoad&quot;:true,', 'owl-lazy'],
100+
['"lazyLoad":false,', '&quot;lazyLoad&quot;:false,', ''],
101+
$html
102+
);
103+
104+
/* Fix for page builder bg images */
105+
if (false !== strpos($html, 'background-image-')) {
106+
$html = str_replace('.background-image-', '.tmpbgimg-', $html);
107+
$html = str_replace('background-image-', 'mflazy-background-image mflazy-background-image-', $html);
108+
$html = str_replace('.tmpbgimg-', '.background-image-', $html);
109+
}
110+
} else {
111+
$html = preg_replace('#<img(?!\s+mfdislazy)([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU', '<img ' .
112+
' src="$2" $1 $3 loading="lazy" />
113+
', $html);
110114
}
111115

112116
return $html;

Plugin/Magefan/WebP/Api/HtmlParserInterfacePlugin.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Glory to Ukraine! Glory to the heroes!
77
*/
88

9+
declare(strict_types=1);
10+
911
namespace Magefan\LazyLoad\Plugin\Magefan\WebP\Api;
1012

1113
use Magefan\LazyLoad\Model\Config;
@@ -27,7 +29,6 @@ class HtmlParserInterfacePlugin
2729
*/
2830
public function __construct(
2931
Config $config
30-
3132
) {
3233
$this->config = $config;
3334
}
@@ -41,7 +42,7 @@ public function __construct(
4142
*/
4243
public function aroundGetNewHtmlTag($subject, callable $proceed, $imagePath, $image)
4344
{
44-
if (!$this->config->getEnabled()) {
45+
if (!$this->config->getEnabled() || !$this->config->getIsJavascriptLazyLoadMethod()) {
4546
return $proceed($imagePath, $image);
4647
}
4748

0 commit comments

Comments
 (0)