Skip to content

Commit 3065368

Browse files
authored
Merge pull request #11 from magefan/amasty-pagespeedoptimizer-integration
integration with amasty lazy load
2 parents b8bd3ef + 60b6d00 commit 3065368

File tree

3 files changed

+83
-21
lines changed

3 files changed

+83
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © 2017 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\Plugin\Amasty\PageSpeedOptimizer\Model\Output;
10+
11+
/**
12+
* Class LazyLoadProcessorPlugin
13+
* @package Magefan\LazyLoad\Plugin\Amasty\PageSpeedOptimizer\Model\Output
14+
*/
15+
class LazyLoadProcessorPlugin
16+
{
17+
/**
18+
* @param $subject
19+
* @param callable $proceed
20+
* @param $image
21+
* @param $imagePath
22+
* @return mixed|null|string|string[]
23+
*/
24+
public function aroundReplaceWithPictureTag($subject, callable $proceed, $image, $imagePath)
25+
{
26+
$originImagePath = $imagePath;
27+
if (strpos($imagePath, 'pixel.jpg')) {
28+
29+
$doStr = 'data-original="';
30+
$p1 = strpos($image, $doStr);
31+
if ($p1 !== false) {
32+
$p1 += strlen($doStr);
33+
$p2 = strpos($image, '"', $p1);
34+
if ($p2 !== false) {
35+
$imagePath = substr($image, $p1, $p2 - $p1);
36+
}
37+
}
38+
}
39+
40+
$html = $proceed($image, $imagePath);
41+
42+
if ($originImagePath != $imagePath) {
43+
if (strpos($html, '<picture') !== false) {
44+
$tmpSrc = 'TMP_SRC';
45+
$pixelSrc = 'srcset="' . $originImagePath . '"';
46+
47+
$html = str_replace($pixelSrc, $tmpSrc, $html);
48+
49+
$html = preg_replace('#<source\s+([^>]*)(?:srcset="([^"]*)")([^>]*)?>#isU', '<source ' . $pixelSrc .
50+
' data-originalset="$2" $1 $3/>', $html);
51+
52+
$html = str_replace($tmpSrc, $pixelSrc, $html);
53+
}
54+
}
55+
56+
return $html;
57+
58+
}
59+
}

etc/frontend/di.xml

+5
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@
1616
<plugin name="magefan_lazyload_lock"
1717
type="Magefan\LazyLoad\Plugin\BlockPlugin" sortOrder="1000"/>
1818
</type>
19+
<!-- Integration with Amasty_PageSpeedOptimizer -->
20+
<type name="Amasty\PageSpeedOptimizer\Model\Output\LazyLoadProcessor">
21+
<plugin name="magefan_lazyLoad_plugin_amasty_pagespeedoptimizer_model_output_lazyloadprocessor_plugin"
22+
type="Magefan\LazyLoad\Plugin\Amasty\PageSpeedOptimizer\Model\Output\LazyLoadProcessorPlugin" sortOrder="1000"/>
23+
</type>
1924
</config>

view/frontend/templates/lazy.phtml

+19-21
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@
88
?>
99

1010
<script data-rocketjavascript="false">
11-
(function(){
12-
var jsSrc = '<?php echo $this->getViewFileUrl('Magefan_LazyLoad::js/lazyload.min.js'); ?>'
13-
function loadScript(e,t){var a,n,r;n=!1,(a=document.createElement("script")).type="text/javascript",a.src=e,a.onload=a.onreadystatechange=function(){n||this.readyState&&"complete"!=this.readyState||(n=!0,t())},(r=document.getElementsByTagName("script")[0]).parentNode.insertBefore(a,r)}
14-
loadScript(jsSrc, function(){
15-
var lazyLoadConfig = {
16-
'elements_selector' : 'img,div',
17-
'data_srcset' : 'originalset'
18-
};
19-
if (document.readyState !== 'loading') {
20-
var myLazyLoad = new LazyLoad(lazyLoadConfig);
21-
} else {
22-
document.addEventListener("DOMContentLoaded", function() {
23-
var myLazyLoad = new LazyLoad(lazyLoadConfig);
24-
});
25-
}
26-
return true;
27-
});
28-
})();
11+
(function(){
12+
var jsSrc = '<?php echo $this->getViewFileUrl('Magefan_LazyLoad::js/lazyload.min.js'); ?>'
13+
function loadScript(e,t){var a,n,r;n=!1,(a=document.createElement("script")).type="text/javascript",a.src=e,a.onload=a.onreadystatechange=function(){n||this.readyState&&"complete"!=this.readyState||(n=!0,t())},(r=document.getElementsByTagName("script")[0]).parentNode.insertBefore(a,r)}
14+
loadScript(jsSrc, function(){
15+
var lazyLoadConfig = {
16+
'elements_selector' : 'img,div',
17+
'data_srcset' : 'originalset'
18+
};
19+
if (document.readyState !== 'loading') {
20+
var myLazyLoad = new LazyLoad(lazyLoadConfig);
21+
} else {
22+
document.addEventListener("DOMContentLoaded", function() {
23+
var myLazyLoad = new LazyLoad(lazyLoadConfig);
24+
});
25+
}
26+
return true;
27+
});
28+
})();
2929
</script>
3030
<script>
3131
require(['jquery', 'domReady!'], function($){
@@ -42,12 +42,10 @@
4242
}
4343
});
4444
});
45-
4645
/* Fix for sliders */
4746
var intr = setInterval(function(){
4847
$sliders = $('.slick-track, .swiper-wrapper, .owl-carousel');
4948
if (!$sliders.length) clearInterval(intr);
50-
5149
$sliders.each(function() {
5250
var $imgs = $(this).find('img');
5351
var c = 0;
@@ -69,4 +67,4 @@
6967
});
7068
}, 2000);
7169
});
72-
</script>
70+
</script>

0 commit comments

Comments
 (0)