Skip to content

Commit 11d157b

Browse files
committed
Added additional block lazy settings
1 parent 58db76d commit 11d157b

File tree

4 files changed

+129
-59
lines changed

4 files changed

+129
-59
lines changed

Model/Config.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* Copyright © Ihor Vansach ([email protected]). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\LazyLoad\Model;
10+
11+
use Magento\Framework\App\Action\Action;
12+
13+
/**
14+
* Lazy load config
15+
*/
16+
class Config extends \Magento\Framework\App\Helper\AbstractHelper
17+
{
18+
const XML_PATH_ENABLED = 'mflazyzoad/general/enabled';
19+
const XML_PATH_AMP_ENABLED = 'pramp/general/enabled';
20+
const XML_PATH_LAZY_BLOCKS = 'mflazyzoad/general/lazy_blocks';
21+
22+
/**
23+
* @var array
24+
*/
25+
protected $blocks;
26+
27+
/**
28+
* @var bool
29+
*/
30+
protected $enabled;
31+
32+
/**
33+
* Retrieve store config value
34+
* @param string $path
35+
* @return mixed
36+
*/
37+
public function getConfig($path)
38+
{
39+
return $this->scopeConfig->getValue(
40+
$path,
41+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
42+
);
43+
}
44+
45+
/**
46+
* Retrieve alloved blocks info
47+
* @return array
48+
*/
49+
public function getBlocks()
50+
{
51+
if (null === $this->blocks) {
52+
$blocks = $this->getConfig(self::XML_PATH_LAZY_BLOCKS);
53+
54+
$blocks = str_replace(["\r\n", "\n'\r", "\n"], "\r", $blocks);
55+
$blocks = explode("\r", $blocks);
56+
$this->blocks = [];
57+
foreach ($blocks as $block) {
58+
if ($block = trim($block)) {
59+
$this->blocks[] = $block;
60+
}
61+
}
62+
}
63+
64+
return $this->blocks;
65+
}
66+
67+
/**
68+
* Retrieve true if enabled
69+
* @return int
70+
*/
71+
public function getEnabled()
72+
{
73+
if (null === $this->enabled) {
74+
$this->enabled = $this->getConfig(self::XML_PATH_ENABLED);
75+
76+
/* check if Plumrocket AMP enabled */
77+
if ($this->enabled) {
78+
$isAmpRequest = $this->getConfig(self::XML_PATH_AMP_ENABLED);
79+
if ($isAmpRequest) {
80+
/* We know that using objectManager is not a not a good practice,
81+
but if Plumrocket_AMP is not installed on your magento instance
82+
you'll get error during di:compile */
83+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
84+
$isAmpRequest = $objectManager->get('\Plumrocket\Amp\Helper\Data')
85+
->isAmpRequest();
86+
}
87+
$this->enabled = !$isAmpRequest;
88+
}
89+
}
90+
91+
return $this->enabled;
92+
}
93+
}

Plugin/BlockPlugin.php

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
*/
1414
class BlockPlugin
1515
{
16+
const LAZY_TAG = '<!-- MAGEFAN_LAZY_LOAD -->';
17+
1618
/**
1719
* Request
1820
* @var \Magento\Framework\App\RequestInterface
1921
*/
2022
protected $request;
23+
2124
/**
2225
* Core store config
2326
*
@@ -30,17 +33,31 @@ class BlockPlugin
3033
*/
3134
protected $blocks;
3235

36+
/**
37+
* Lazy store config
38+
*
39+
* @var \Magefan\LazyLoad\Model\Config
40+
*/
41+
protected $config;
3342

3443
/**
35-
* @param \Magento\Framework\App\RequestInterface $request
44+
* BlockPlugin constructor
45+
* @param \Magento\Framework\App\RequestInterface $request
3646
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
47+
* @param null|\Magefan\LazyLoad\Model\Config $config
3748
*/
3849
public function __construct(
3950
\Magento\Framework\App\RequestInterface $request,
40-
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
51+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
52+
$config = null
4153
) {
4254
$this->request = $request;
4355
$this->scopeConfig = $scopeConfig;
56+
57+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
58+
$this->config = $config ?: $objectManager->get(
59+
\Magefan\LazyLoad\Model\Config::class
60+
);
4461
}
4562

4663

@@ -51,88 +68,48 @@ public function __construct(
5168
*/
5269
public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block, $html)
5370
{
54-
if (!$this->isEnabled($block)) {
71+
if (!$this->isEnabled($block, $html)) {
5572
return $html;
5673
}
5774

5875
$html = preg_replace('#<img\s+([^>]*)(?:src="([^"]*)")([^>]*)\/?>#isU', '<img src="' .
5976
$block->getViewFileUrl('Magefan_LazyLoad::images/pixel.jpg') . '" ' .
6077
'data-original="$2" $1 $3/>', $html);
6178

62-
/*
63-
$i = $block->getNameInLayout(). ' ' . $block->getBlockId() . ' ' . get_class($block);
64-
$html = 'start of <br/><br/>'. $i . $html;
65-
*/
79+
$html = str_replace(self::LAZY_TAG, '', $html);
80+
6681
return $html;
6782
}
6883

6984
/**
70-
* Check if lazy load is available
85+
* Check if lazy load is available for block
7186
* @param \Magento\Framework\View\Element\AbstractBlock $block
87+
* @param string $html
7288
* @return boolean
7389
*/
74-
protected function isEnabled($block)
90+
protected function isEnabled($block, $html)
7591
{
7692
if (PHP_SAPI === 'cli' || $this->request->isXmlHttpRequest()) {
7793
return false;
7894
}
7995

96+
if (!$this->config->getEnabled()) {
97+
return false;
98+
}
99+
80100
$blockName = $block->getBlockId() ?: $block->getNameInLayout();
81-
$blocks = $this->getBlocks();
101+
$blockTemplate = $block->getTemplate();
102+
$blocks = $this->config->getBlocks();
82103

83104
if (!in_array($blockName, $blocks)
84105
&& !in_array(get_class($block), $blocks)
106+
&& !in_array($blockTemplate, $blocks)
107+
&& (false === strpos($html, self::LAZY_TAG))
85108
) {
86109
return false;
87110
}
88111

89-
$enabled = $this->scopeConfig->getValue(
90-
'mflazyzoad/general/enabled',
91-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
92-
);
93-
94-
/* check if Plumrocket AMP enabled */
95-
if ($enabled) {
96-
$isAmpRequest = $this->scopeConfig->getValue(
97-
'pramp/general/enabled',
98-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
99-
);
100-
if ($isAmpRequest) {
101-
/* We know that using objectManager is not a not a good practice,
102-
but if Plumrocket_AMP is not installed on your magento instance
103-
you'll get error during di:compile */
104-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
105-
$isAmpRequest = $objectManager->get('\Plumrocket\Amp\Helper\Data')
106-
->isAmpRequest();
107-
}
108-
$enabled = !$isAmpRequest;
109-
}
110-
111-
return $enabled;
112+
return true;
112113
}
113114

114-
/**
115-
* Retrieve alloved blocks info
116-
* @return array
117-
*/
118-
protected function getBlocks()
119-
{
120-
if (null === $this->blocks) {
121-
$blocks = $this->scopeConfig->getValue(
122-
'mflazyzoad/general/lazy_blocks',
123-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
124-
);
125-
126-
$blocks = str_replace(["\r\n", "\n'\r", "\n"], "\r", $blocks);
127-
$blocks = explode("\r", $blocks);
128-
$this->blocks = [];
129-
foreach ($blocks as $block) {
130-
if ($block = trim($block)) {
131-
$this->blocks[] = $block;
132-
}
133-
}
134-
}
135-
136-
return $this->blocks;
137-
}
138115
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "magefan/module-lazyload",
33
"description": "Images lazyloading on Magento 2 store.",
44
"type": "magento2-module",
5-
"version": "2.0.2",
5+
"version": "2.0.3",
66
"license": "OSL-3.0",
77
"authors": [
88
{

etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
*/
99
-->
1010
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
11-
<module name="Magefan_LazyLoad" setup_version="2.0.2"/>
11+
<module name="Magefan_LazyLoad" setup_version="2.0.3"/>
1212
</config>

0 commit comments

Comments
 (0)