Skip to content

Commit 186229c

Browse files
author
Bohdan Berezhniy
committed
make code more simple
1 parent 900f558 commit 186229c

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

Plugin/Amasty/PageSpeedOptimizer/Model/Output/LazyLoadProcessorPlugin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ public function aroundReplaceWithPictureTag($subject, callable $proceed, $image,
4242
}
4343

4444
$originImagePath = $imagePath;
45+
4546
if (strpos($imagePath, 'Magefan_LazyLoad/images/pixel.jpg')) {
4647

4748
$doStr = 'data-original="';
4849
$p1 = strpos($image, $doStr);
50+
4951
if ($p1 !== false) {
5052
$p1 += strlen($doStr);
5153
$p2 = strpos($image, '"', $p1);

Plugin/Magefan/WebP/Model/WebPPlugin.php

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,79 +9,71 @@
99
namespace Magefan\LazyLoad\Plugin\Magefan\WebP\Model;
1010

1111
use Magefan\LazyLoad\Model\Config;
12-
use Magefan\WebP\Model\WebP;
13-
use Magento\Framework\Filesystem\Driver\File;
1412

1513
/**
1614
* Class WebPPlugin
1715
*/
1816
class WebPPlugin
1917
{
20-
const LAZY_TAG = '<!-- MAGEFAN_LAZY_LOAD -->';
2118

2219
/**
23-
* @var WebP
20+
* @var Config
2421
*/
25-
private $webp;
22+
private $config;
2623

2724
/**
28-
* @var Config
25+
* WebPPlugin constructor.
26+
* @param Config $config
2927
*/
30-
private $config;
31-
3228
public function __construct(
33-
WebP $webp,
34-
Config $config,
35-
File $fileDriver
29+
Config $config
3630

3731
) {
38-
$this->webp = $webp;
3932
$this->config = $config;
40-
$this->fileDriver = $fileDriver;
4133
}
4234

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)
4443
{
45-
4644
if (!$this->config->getEnabled()) {
47-
return $proceed($webpUrl, $imagePath, $htmlTag);
45+
return $proceed($imagePath, $image);
4846
}
4947

5048
$originImagePath = $imagePath;
5149

5250
if (strpos($imagePath, 'Magefan_LazyLoad/images/pixel.jpg')) {
5351

5452
$doStr = 'data-original="';
55-
$p1 = strpos($htmlTag, $doStr);
53+
$p1 = strpos($image, $doStr);
5654

5755
if ($p1 !== false) {
5856
$p1 += strlen($doStr);
59-
$p2 = strpos($htmlTag, '"', $p1);
57+
$p2 = strpos($image, '"', $p1);
6058
if ($p2 !== false) {
61-
$imagePath = substr($htmlTag, $p1, $p2 - $p1);
62-
$webpUrl = $this->webp->getWebpUrl($imagePath);
63-
59+
$imagePath = substr($image, $p1, $p2 - $p1);
6460
}
6561
}
66-
67-
} else {
68-
69-
return $proceed($webpUrl, $imagePath, $htmlTag);
70-
}
71-
72-
if (!$this->fileDriver->isExists($this->webp->getPathFromUrl($imagePath))) {
73-
return $htmlTag;
7462
}
7563

76-
$html = $proceed($webpUrl, $imagePath, $htmlTag);
64+
$html = $proceed($imagePath, $image);
7765

7866
if ($originImagePath != $imagePath) {
67+
7968
if (strpos($html, '<picture') !== false) {
8069
$tmpSrc = 'TMP_SRC';
8170
$pixelSrc = 'srcset="' . $originImagePath . '"';
71+
8272
$html = str_replace($pixelSrc, $tmpSrc, $html);
73+
8374
$html = preg_replace('#<source\s+([^>]*)(?:srcset="([^"]*)")([^>]*)?>#isU', '<source ' . $pixelSrc .
8475
' data-originalset="$2" $1 $3/>', $html);
76+
8577
$html = str_replace($tmpSrc, $pixelSrc, $html);
8678
}
8779
}

0 commit comments

Comments
 (0)