1313 */
1414class 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}
0 commit comments