@@ -78,14 +78,16 @@ public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block
7878 return $ html ;
7979 }
8080
81- $ blockIdentifier = $ this ->getBlockIdentifier ($ block );
82- $ numberOfReplacements = $ this ->config ->getBlockFirstImagesToSkip ($ blockIdentifier );
81+ if ($ this ->config ->getIsJavascriptLazyLoadMethod ()) {
8382
84- if ($ numberOfReplacements ) {
85- $ html = $ this ->removeFirstNImagesWithCustomLabel ($ html , $ numberOfReplacements );
86- }
83+ $ numberOfReplacements = $ this ->config ->getBlockFirstImagesToSkip (
84+ $ this ->getBlockIdentifier ($ block )
85+ );
86+
87+ if ($ numberOfReplacements ) {
88+ $ html = $ this ->removeFirstNImagesWithCustomLabel ($ html , $ numberOfReplacements );
89+ }
8790
88- if ($ this ->config ->getIsJavascriptLazyLoadMethod ()) {
8991 $ pixelSrc = ' src=" ' . $ block ->getViewFileUrl ('Magefan_LazyLoad::images/pixel.jpg ' ) . '" ' ;
9092 $ tmpSrc = 'TMP_SRC ' ;
9193
@@ -120,36 +122,34 @@ public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block
120122 $ html = str_replace ('background-image- ' , 'mflazy-background-image mflazy-background-image- ' , $ html );
121123 $ html = str_replace ('.tmpbgimg- ' , '.background-image- ' , $ html );
122124 }
125+
126+ if ($ numberOfReplacements ) {
127+ $ html = $ this ->revertFirstNImageToInital ($ html );
128+ }
123129 } else {
124130 $ html = preg_replace ('#<img(?!\s+mfdislazy)([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU ' , '<img ' .
125131 ' src="$2" $1 $3 loading="lazy" />
126132 ' , $ html );
127133 }
128134
129- if ($ numberOfReplacements ) {
130- $ html = $ this ->revertFirstNImageToInital ($ html );
131- return $ this ->deleteFirstNLoadingLazy ($ html , $ numberOfReplacements );
132- }
133-
134135 return $ html ;
135136 }
136137
137138 /**
138139 * @param \Magento\Framework\View\Element\AbstractBlock $block
139140 * @return string
140141 */
141- protected function getBlockIdentifier (\Magento \Framework \View \Element \AbstractBlock $ block ): string {
142+ protected function getBlockIdentifier (\Magento \Framework \View \Element \AbstractBlock $ block ): string
143+ {
142144 $ blockName = $ block ->getBlockId () ?: $ block ->getNameInLayout ();
143145 $ blockTemplate = $ block ->getTemplate ();
144146 $ blocks = $ this ->config ->getBlocks ();
145147
146148 if (in_array ($ blockName , $ blocks )) {
147149 return $ blockName ;
148- }
149- else if (in_array (get_class ($ block ), $ blocks )) {
150+ } elseif (in_array (get_class ($ block ), $ blocks )) {
150151 return get_class ($ block );
151- }
152- else if (in_array ($ blockTemplate , $ blocks )) {
152+ } elseif (in_array ($ blockTemplate , $ blocks )) {
153153 return $ blockTemplate ;
154154 }
155155
@@ -161,7 +161,8 @@ protected function getBlockIdentifier(\Magento\Framework\View\Element\AbstractBl
161161 * @param int $numberOfReplacements
162162 * @return array|string|string[]|null
163163 */
164- protected function removeFirstNImagesWithCustomLabel ($ html , int $ numberOfReplacements ) {
164+ protected function removeFirstNImagesWithCustomLabel ($ html , int $ numberOfReplacements )
165+ {
165166 $ count = 0 ;
166167 return preg_replace_callback ('#<img([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU ' , function ($ match ) use (&$ count , &$ numberOfReplacements ) {
167168 $ count ++;
@@ -180,21 +181,13 @@ protected function removeFirstNImagesWithCustomLabel($html, int $numberOfReplace
180181 * @param $html
181182 * @return array|string|string[]|null
182183 */
183- protected function revertFirstNImageToInital ($ html ) {
184- return preg_replace_callback ('/ ' . self ::REPLACEMENT_LABEL .'_\d+\b(.*?)/ ' , function ($ match ) use (&$ count ) {
184+ protected function revertFirstNImageToInital ($ html )
185+ {
186+ return preg_replace_callback ('/ ' . self ::REPLACEMENT_LABEL .'_\d+\b(.*?)/ ' , function ($ match ) use (&$ count ) {
185187 return $ this ->labelsValues [$ match [0 ]] ?? $ match [0 ];
186188 }, $ html );
187189 }
188190
189- /**
190- * @param $html
191- * @param int $numberOfDeletions
192- * @return array|string|string[]|null
193- */
194- protected function deleteFirstNLoadingLazy ($ html ,int $ numberOfDeletions ) {
195- return preg_replace ('/loading="lazy"/ ' , '' , $ html , $ numberOfDeletions , $ count );
196- }
197-
198191 /**
199192 * Check if lazy load is available for block
200193 * @param \Magento\Framework\View\Element\AbstractBlock $block
@@ -216,15 +209,11 @@ protected function isEnabled($block, string $html): bool
216209 return false ;
217210 }
218211
219- $ blockName = $ block -> getBlockId () ?: $ block -> getNameInLayout ();
220- $ blockTemplate = $ block -> getTemplate () ;
221- $ blocks = $ this -> config -> getBlocks ();
212+ if ( false !== strpos ( $ html , self :: LAZY_TAG )) {
213+ return true ;
214+ }
222215
223- if (!in_array ($ blockName , $ blocks )
224- && !in_array (get_class ($ block ), $ blocks )
225- && !in_array ($ blockTemplate , $ blocks )
226- && (false === strpos ($ html , self ::LAZY_TAG ))
227- ) {
216+ if (!$ this ->getBlockIdentifier ($ block )) {
228217 return false ;
229218 }
230219
0 commit comments