|
9 | 9 | namespace Magefan\LazyLoad\Plugin\Magefan\WebP\Model; |
10 | 10 |
|
11 | 11 | use Magefan\LazyLoad\Model\Config; |
12 | | -use Magefan\WebP\Model\WebP; |
13 | | -use Magento\Framework\Filesystem\Driver\File; |
14 | 12 |
|
15 | 13 | /** |
16 | 14 | * Class WebPPlugin |
17 | 15 | */ |
18 | 16 | class WebPPlugin |
19 | 17 | { |
20 | | - const LAZY_TAG = '<!-- MAGEFAN_LAZY_LOAD -->'; |
21 | 18 |
|
22 | 19 | /** |
23 | | - * @var WebP |
| 20 | + * @var Config |
24 | 21 | */ |
25 | | - private $webp; |
| 22 | + private $config; |
26 | 23 |
|
27 | 24 | /** |
28 | | - * @var Config |
| 25 | + * WebPPlugin constructor. |
| 26 | + * @param Config $config |
29 | 27 | */ |
30 | | - private $config; |
31 | | - |
32 | 28 | public function __construct( |
33 | | - WebP $webp, |
34 | | - Config $config, |
35 | | - File $fileDriver |
| 29 | + Config $config |
36 | 30 |
|
37 | 31 | ) { |
38 | | - $this->webp = $webp; |
39 | 32 | $this->config = $config; |
40 | | - $this->fileDriver = $fileDriver; |
41 | 33 | } |
42 | 34 |
|
43 | | - public function aroundGetPictureTagHtml($subject, callable $proceed, $webpUrl, $imagePath, $htmlTag) |
| 35 | + /** |
| 36 | + * @param $subject |
| 37 | + * @param callable $proceed |
| 38 | + * @param $imagePath |
| 39 | + * @param $htmlTag |
| 40 | + * @return mixed|string|string[]|null |
| 41 | + */ |
| 42 | + public function aroundGetPictureTagHtml($subject, callable $proceed, $imagePath, $image) |
44 | 43 | { |
45 | | - |
46 | 44 | if (!$this->config->getEnabled()) { |
47 | | - return $proceed($webpUrl, $imagePath, $htmlTag); |
| 45 | + return $proceed($imagePath, $image); |
48 | 46 | } |
49 | 47 |
|
50 | 48 | $originImagePath = $imagePath; |
51 | 49 |
|
52 | 50 | if (strpos($imagePath, 'Magefan_LazyLoad/images/pixel.jpg')) { |
53 | 51 |
|
54 | 52 | $doStr = 'data-original="'; |
55 | | - $p1 = strpos($htmlTag, $doStr); |
| 53 | + $p1 = strpos($image, $doStr); |
56 | 54 |
|
57 | 55 | if ($p1 !== false) { |
58 | 56 | $p1 += strlen($doStr); |
59 | | - $p2 = strpos($htmlTag, '"', $p1); |
| 57 | + $p2 = strpos($image, '"', $p1); |
60 | 58 | if ($p2 !== false) { |
61 | | - $imagePath = substr($htmlTag, $p1, $p2 - $p1); |
62 | | - $webpUrl = $this->webp->getWebpUrl($imagePath); |
63 | | - |
| 59 | + $imagePath = substr($image, $p1, $p2 - $p1); |
64 | 60 | } |
65 | 61 | } |
66 | | - |
67 | | - } else { |
68 | | - |
69 | | - return $proceed($webpUrl, $imagePath, $htmlTag); |
70 | | - } |
71 | | - |
72 | | - if (!$this->fileDriver->isExists($this->webp->getPathFromUrl($imagePath))) { |
73 | | - return $htmlTag; |
74 | 62 | } |
75 | 63 |
|
76 | | - $html = $proceed($webpUrl, $imagePath, $htmlTag); |
| 64 | + $html = $proceed($imagePath, $image); |
77 | 65 |
|
78 | 66 | if ($originImagePath != $imagePath) { |
| 67 | + |
79 | 68 | if (strpos($html, '<picture') !== false) { |
80 | 69 | $tmpSrc = 'TMP_SRC'; |
81 | 70 | $pixelSrc = 'srcset="' . $originImagePath . '"'; |
| 71 | + |
82 | 72 | $html = str_replace($pixelSrc, $tmpSrc, $html); |
| 73 | + |
83 | 74 | $html = preg_replace('#<source\s+([^>]*)(?:srcset="([^"]*)")([^>]*)?>#isU', '<source ' . $pixelSrc . |
84 | 75 | ' data-originalset="$2" $1 $3/>', $html); |
| 76 | + |
85 | 77 | $html = str_replace($tmpSrc, $pixelSrc, $html); |
86 | 78 | } |
87 | 79 | } |
|
0 commit comments