@@ -51,30 +51,37 @@ class BlockPlugin
5151 */
5252 protected $ labelsValues = [];
5353
54+ /**
55+ * @var array
56+ */
57+ protected $ skipBlocks = [];
58+
5459 /**
5560 * @param \Magento\Framework\App\RequestInterface $request
5661 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
5762 * @param Config $config
63+ * @param array $skipBlocks
5864 */
5965 public function __construct (
6066 \Magento \Framework \App \RequestInterface $ request ,
6167 \Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig ,
62- Config $ config
68+ Config $ config ,
69+ array $ skipBlocks
6370 ) {
6471 $ this ->request = $ request ;
6572 $ this ->scopeConfig = $ scopeConfig ;
6673 $ this ->config = $ config ;
74+ $ this ->skipBlocks = $ skipBlocks ;
6775 }
6876
69-
7077 /**
7178 * @param \Magento\Framework\View\Element\AbstractBlock $block
7279 * @param string $html
7380 * @return string
7481 */
7582 public function afterToHtml (\Magento \Framework \View \Element \AbstractBlock $ block , $ html )
7683 {
77- if (!$ this ->isEnabled ($ block , (string )$ html )) {
84+ if (!$ html || ! $ this ->isEnabled ($ block , (string )$ html )) {
7885 return $ html ;
7986 }
8087
@@ -100,9 +107,11 @@ public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block
100107 </noscript> ' ;
101108 }
102109
103- $ html = preg_replace ('#<img(?!\s+mfdislazy)([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU ' , '<img ' .
104- ' data-original="$2" $1 $3/>
105- ' . $ noscript , $ html );
110+ $ html = preg_replace (
111+ '#<img(?!\s+mfdislazy)([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU ' ,
112+ '<img data-original="$2" $1 $3/> ' . $ noscript ,
113+ $ html
114+ );
106115
107116 $ html = str_replace (' data-original= ' , $ pixelSrc . ' data-original= ' , $ html );
108117
@@ -168,7 +177,13 @@ protected function removeFirstNImagesWithCustomLabel($html, int $numberOfReplace
168177 $ count ++;
169178 if ($ count <= $ numberOfReplacements ) {
170179 $ label = self ::REPLACEMENT_LABEL . '_ ' . $ count ;
171- $ this ->labelsValues [$ label ] = $ match [0 ];
180+ $ imgTag = $ match [0 ];
181+
182+ if (strpos ($ imgTag , 'mfdislazy ' ) === false ) {
183+ $ imgTag = str_replace ('<img ' , '<img mfdislazy="1" ' , $ imgTag );
184+ }
185+
186+ $ this ->labelsValues [$ label ] = $ imgTag ;
172187
173188 return $ label ;
174189 }
@@ -179,13 +194,15 @@ protected function removeFirstNImagesWithCustomLabel($html, int $numberOfReplace
179194
180195 /**
181196 * @param $html
182- * @return array|string|string[]|null
197+ * @return array|mixed| string|string[]
183198 */
184199 protected function revertFirstNImageToInital ($ html )
185200 {
186- return preg_replace_callback ('/ ' . self ::REPLACEMENT_LABEL .'_\d+\b(.*?)/ ' , function ($ match ) use (&$ count ) {
187- return $ this ->labelsValues [$ match [0 ]] ?? $ match [0 ];
188- }, $ html );
201+ foreach ($ this ->labelsValues as $ labelsValue => $ img ) {
202+ $ html = str_replace ($ labelsValue , $ img , $ html );
203+ }
204+
205+ return $ html ;
189206 }
190207
191208 /**
@@ -209,6 +226,10 @@ protected function isEnabled($block, string $html): bool
209226 return false ;
210227 }
211228
229+ if ($ this ->config ->getIsAllBlocksAddedToLazy () && !$ this ->isBlockSkiped ($ block )) {
230+ return true ;
231+ }
232+
212233 if (false !== strpos ($ html , self ::LAZY_TAG )) {
213234 return true ;
214235 }
@@ -219,4 +240,13 @@ protected function isEnabled($block, string $html): bool
219240
220241 return true ;
221242 }
243+
244+ /**
245+ * @param $block
246+ * @return bool
247+ */
248+ private function isBlockSkiped ($ block ): bool
249+ {
250+ return in_array (get_class ($ block ), $ this ->skipBlocks );
251+ }
222252}
0 commit comments