Skip to content

Commit 61c739b

Browse files
committed
Fixed error translate inline
Exclude Js, type template js or type="text/x-magento*" before replace image lazyload.
1 parent f9c8e21 commit 61c739b

File tree

1 file changed

+60
-7
lines changed

1 file changed

+60
-7
lines changed

Plugin/LazyResponse.php

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @Author: nguyen
44
* @Date: 2020-02-12 14:01:01
55
* @Last Modified by: Alex Dong
6-
* @Last Modified time: 2020-05-14 10:00:26
6+
* @Last Modified time: 2020-05-21 15:12:08
77
*/
88

99
namespace Magepow\Lazyload\Plugin;
@@ -14,17 +14,19 @@
1414

1515
class LazyResponse
1616
{
17-
public $request;
17+
protected $request;
1818

19-
public $helper;
19+
protected $helper;
2020

21-
public $content;
21+
protected $content;
2222

23-
public $isJson;
23+
protected $isJson;
2424

25-
public $exclude = [];
25+
protected $exclude = [];
2626

27-
public $placeholder = 'data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22' . '$width' . '%22%20height%3D%22' . '$height' . '%22%20viewBox%3D%220%200%20225%20265%22%3E%3C%2Fsvg%3E';
27+
protected $scripts = [];
28+
29+
protected $placeholder = 'data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22' . '$width' . '%22%20height%3D%22' . '$height' . '%22%20viewBox%3D%220%200%20225%20265%22%3E%3C%2Fsvg%3E';
2830

2931
public function __construct(
3032
RequestInterface $request,
@@ -65,6 +67,8 @@ public function beforeSendResponse(Http $response)
6567

6668
if(!$this->helper->getConfigModule('general/loading_img')) return;
6769

70+
$body = $this->processExcludeJs($body);
71+
6872
$exclude = $this->helper->getConfigModule('general/exclude_img');
6973
// $exclude = 'product-image-photo';
7074
if($exclude){
@@ -78,6 +82,19 @@ public function beforeSendResponse(Http $response)
7882
// $regex_block = '';
7983
$body = $this->addLazyload($body, $placeholder, $regex_block );
8084

85+
$body = preg_replace_callback(
86+
'~<\s*\bscript\b[^>]*>(.*?)<\s*\/\s*script\s*>~is',
87+
function($match){
88+
$scriptId = trim($match[1], ' ');
89+
if($scriptId && isset($this->scripts[$scriptId])){
90+
return $this->scripts[$scriptId];
91+
}else {
92+
return $match[0];
93+
}
94+
},
95+
$body
96+
);
97+
8198
$body_includes = $this->helper->getConfigModule('general/body_includes');
8299
if($body_includes) $body = $this->addToBottomBody($body, $body_includes);
83100

@@ -157,6 +174,42 @@ public function isExclude($class)
157174
return !empty($excludeExist);
158175
}
159176

177+
public function processExcludeJs($content, $minify=false, $deferJs=false)
178+
{
179+
$content = preg_replace_callback(
180+
'~<\s*\bscript\b[^>]*>(.*?)<\s*\/\s*script\s*>~is',
181+
function($match) use($minify, $deferJs){
182+
// if(stripos($match[0], 'type="text/x-magento') !== false) return $match[0];
183+
$scriptId = 'script_' . uniqid();
184+
if ($minify && trim($match[1], ' ')){
185+
$this->scripts[$scriptId] = $this->minifyJs( $match[0] );
186+
}else {
187+
$this->scripts[$scriptId] = $match[0];
188+
}
189+
if (!$deferJs) return '<script>' . $scriptId . '</script>';
190+
return '';
191+
},
192+
$content
193+
);
194+
195+
return $content;
196+
}
197+
198+
public function minifyJs($script)
199+
{
200+
$regex = '~//?\s*\*[\s\S]*?\*\s*//?~'; // RegEx to remove /** */ and // ** **// php comments
201+
$search = array(
202+
'/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\')\/\/.*))/',
203+
'/(\s)+/s', // shorten multiple whitespace sequences
204+
);
205+
206+
$replace = array(
207+
'',
208+
'\\1',
209+
);
210+
return preg_replace($search, $replace, $script);
211+
}
212+
160213
public function addLazyloadImage($content, $placeholder)
161214
{
162215
if($this->isJson) return $this->addLazyloadImageJson($content, $placeholder);

0 commit comments

Comments
 (0)