@@ -78,14 +78,16 @@ public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block
78
78
return $ html ;
79
79
}
80
80
81
- $ blockIdentifier = $ this ->getBlockIdentifier ($ block );
82
- $ numberOfReplacements = $ this ->config ->getBlockFirstImagesToSkip ($ blockIdentifier );
81
+ if ($ this ->config ->getIsJavascriptLazyLoadMethod ()) {
83
82
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
+ }
87
90
88
- if ($ this ->config ->getIsJavascriptLazyLoadMethod ()) {
89
91
$ pixelSrc = ' src=" ' . $ block ->getViewFileUrl ('Magefan_LazyLoad::images/pixel.jpg ' ) . '" ' ;
90
92
$ tmpSrc = 'TMP_SRC ' ;
91
93
@@ -120,36 +122,34 @@ public function afterToHtml(\Magento\Framework\View\Element\AbstractBlock $block
120
122
$ html = str_replace ('background-image- ' , 'mflazy-background-image mflazy-background-image- ' , $ html );
121
123
$ html = str_replace ('.tmpbgimg- ' , '.background-image- ' , $ html );
122
124
}
125
+
126
+ if ($ numberOfReplacements ) {
127
+ $ html = $ this ->revertFirstNImageToInital ($ html );
128
+ }
123
129
} else {
124
130
$ html = preg_replace ('#<img(?!\s+mfdislazy)([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU ' , '<img ' .
125
131
' src="$2" $1 $3 loading="lazy" />
126
132
' , $ html );
127
133
}
128
134
129
- if ($ numberOfReplacements ) {
130
- $ html = $ this ->revertFirstNImageToInital ($ html );
131
- return $ this ->deleteFirstNLoadingLazy ($ html , $ numberOfReplacements );
132
- }
133
-
134
135
return $ html ;
135
136
}
136
137
137
138
/**
138
139
* @param \Magento\Framework\View\Element\AbstractBlock $block
139
140
* @return string
140
141
*/
141
- protected function getBlockIdentifier (\Magento \Framework \View \Element \AbstractBlock $ block ): string {
142
+ protected function getBlockIdentifier (\Magento \Framework \View \Element \AbstractBlock $ block ): string
143
+ {
142
144
$ blockName = $ block ->getBlockId () ?: $ block ->getNameInLayout ();
143
145
$ blockTemplate = $ block ->getTemplate ();
144
146
$ blocks = $ this ->config ->getBlocks ();
145
147
146
148
if (in_array ($ blockName , $ blocks )) {
147
149
return $ blockName ;
148
- }
149
- else if (in_array (get_class ($ block ), $ blocks )) {
150
+ } elseif (in_array (get_class ($ block ), $ blocks )) {
150
151
return get_class ($ block );
151
- }
152
- else if (in_array ($ blockTemplate , $ blocks )) {
152
+ } elseif (in_array ($ blockTemplate , $ blocks )) {
153
153
return $ blockTemplate ;
154
154
}
155
155
@@ -161,7 +161,8 @@ protected function getBlockIdentifier(\Magento\Framework\View\Element\AbstractBl
161
161
* @param int $numberOfReplacements
162
162
* @return array|string|string[]|null
163
163
*/
164
- protected function removeFirstNImagesWithCustomLabel ($ html , int $ numberOfReplacements ) {
164
+ protected function removeFirstNImagesWithCustomLabel ($ html , int $ numberOfReplacements )
165
+ {
165
166
$ count = 0 ;
166
167
return preg_replace_callback ('#<img([^>]*)(?:\ssrc="([^"]*)")([^>]*)\/?>#isU ' , function ($ match ) use (&$ count , &$ numberOfReplacements ) {
167
168
$ count ++;
@@ -180,21 +181,13 @@ protected function removeFirstNImagesWithCustomLabel($html, int $numberOfReplace
180
181
* @param $html
181
182
* @return array|string|string[]|null
182
183
*/
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 ) {
185
187
return $ this ->labelsValues [$ match [0 ]] ?? $ match [0 ];
186
188
}, $ html );
187
189
}
188
190
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
-
198
191
/**
199
192
* Check if lazy load is available for block
200
193
* @param \Magento\Framework\View\Element\AbstractBlock $block
@@ -216,15 +209,11 @@ protected function isEnabled($block, string $html): bool
216
209
return false ;
217
210
}
218
211
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
+ }
222
215
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 )) {
228
217
return false ;
229
218
}
230
219
0 commit comments