Skip to content

Commit f4e4c39

Browse files
committed
Fix error translate inline.
Exclude Js, type template js or type="text/x-magento*" before replace image lazyload.
1 parent 4ff2db8 commit f4e4c39

File tree

1 file changed

+74
-57
lines changed

1 file changed

+74
-57
lines changed

Plugin/SpeedOptimizer.php

Lines changed: 74 additions & 57 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 09:55:34
6+
* @Last Modified time: 2020-05-21 14:43:02
77
*/
88

99
namespace Magepow\SpeedOptimizer\Plugin;
@@ -17,21 +17,23 @@
1717

1818
class SpeedOptimizer extends \Magento\Framework\View\Element\Template
1919
{
20-
public $request;
20+
protected $request;
2121

22-
public $helper;
22+
protected $helper;
2323

24-
public $content;
24+
protected $content;
2525

26-
public $isJson;
26+
protected $isJson;
2727

28-
public $exclude = [];
28+
protected $exclude = [];
29+
30+
protected $scripts = [];
2931

3032
protected $storeManager;
3133

3234
protected $themeProvider;
3335

34-
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';
36+
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';
3537

3638
public function __construct(
3739
\Magento\Framework\View\Element\Template\Context $context,
@@ -74,34 +76,54 @@ public function beforeSendResponse(Http $response)
7476
$body_includes = $this->helper->getConfigModule('general/body_includes');
7577
if($body_includes) $body = $this->addToBottomBody($body, $body_includes);
7678

77-
$deferJs = $this->helper->getConfigModule('general/defer_js');
78-
$body = $this->minifyJs($body, $deferJs);
79+
$minifyHtml = $this->helper->getConfigModule('general/minify_html');
80+
$deferJs = $this->helper->getConfigModule('general/defer_js');
7981

80-
$minifyHtml = $this->helper->getConfigModule('general/minify_html');
82+
$body = $this->processExcludeJs($body, $minifyHtml, $deferJs);
8183
if($minifyHtml) $body = $this->minifyHtml($body);
8284

83-
$bodyClass = 'loading_img';
85+
$bodyClass = '';
8486
$loadingBody = $this->helper->getConfigModule('general/loading_body');
8587
if($loadingBody){
8688
$bodyClass .= ' loading_body';
87-
$body = $this->addToTopBody($body, '<div class="preloading"><div class="loading"></div></div>');
88-
}
89+
$body = $this->addToTopBody($body, '<div class="preloading"><div class="loading"></div></div>');
90+
}
8991

90-
$body = $this->addBodyClass($body, $bodyClass);
92+
$loadingImg = $this->helper->getConfigModule('general/loading_img');
93+
if($loadingImg){
94+
$bodyClass .= ' loading_img';
95+
$exclude = $this->helper->getConfigModule('general/exclude_img');
96+
// $exclude = 'product-image-photo';
97+
if($exclude){
98+
$exclude = str_replace(' ', '', $exclude);
99+
$this->exclude = explode(',', $exclude);
100+
}
101+
$placeholder = $this->helper->getConfigModule('general/placeholder');
102+
// $placeholder = false;
103+
$regex_block = $this->helper->getConfigModule('general/regex_block');
104+
// $regex_block = '';
105+
$body = $this->addLazyload($body, $placeholder, $regex_block );
106+
}
91107

92-
if(!$this->helper->getConfigModule('general/loading_img')) return;
108+
$body = $this->addBodyClass($body, $bodyClass);
93109

94-
$exclude = $this->helper->getConfigModule('general/exclude_img');
95-
// $exclude = 'product-image-photo';
96-
if($exclude){
97-
$exclude = str_replace(' ', '', $exclude);
98-
$this->exclude = explode(',', $exclude);
110+
if ($deferJs){
111+
$scripts = implode('', $this->scripts);
112+
$body = $this->addToBottomBody($body, $scripts);
113+
} else {
114+
$body = preg_replace_callback(
115+
'~<\s*\bscript\b[^>]*>(.*?)<\s*\/\s*script\s*>~is',
116+
function($match) use($pattern, &$scripts){
117+
$scriptId = trim($match[1], ' ');
118+
if($scriptId && isset($this->scripts[$scriptId])){
119+
return $this->scripts[$scriptId];
120+
}else {
121+
return $match[0];
122+
}
123+
},
124+
$body
125+
);
99126
}
100-
$placeholder = $this->helper->getConfigModule('general/placeholder');
101-
// $placeholder = false;
102-
$regex_block = $this->helper->getConfigModule('general/regex_block');
103-
// $regex_block = '';
104-
$body = $this->addLazyload($body, $placeholder, $regex_block );
105127

106128
$response->setBody($body);
107129
}
@@ -265,41 +287,36 @@ public function addLazyLoadJs( $content, $selector='img', $exclude='.loaded' )
265287
return $this->addToBottomBody($content, $script);
266288
}
267289

268-
public function minifyJs($content, $deferJs=false)
290+
public function processExcludeJs($content, $minify=true, $deferJs=true)
269291
{
270292
$regex = '~//?\s*\*[\s\S]*?\*\s*//?~'; // RegEx to remove /** */ and // ** **// php comments
271-
$pattern = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\')\/\/.*))/';
272-
if( $deferJs && (stripos($content, "</body>") === false) ){
273-
$scripts = '';
274-
$content = preg_replace_callback(
275-
'~<\s*\bscript\b[^>]*>(.*?)<\s*\/\s*script\s*>~is',
276-
function($match) use($pattern, &$scripts){
277-
if(trim($match[1], ' ')){
278-
$scripts .= preg_replace($pattern, '', $match[0]);
279-
}else {
280-
$scripts .= $match[0];
281-
}
282-
283-
return '';
284-
},
285-
$content
286-
);
287-
288-
return str_ireplace("</body>", "$scripts</body>", $content);
293+
$content = preg_replace_callback(
294+
'~<\s*\bscript\b[^>]*>(.*?)<\s*\/\s*script\s*>~is',
295+
function($match) use($minify, $deferJs){
296+
// if(stripos($match[0], 'type="text/x-magento') !== false) return $match[0];
297+
$scriptId = 'script_' . uniqid();
298+
if ($minify && trim($match[1], ' ')){
299+
$search = array(
300+
'/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\')\/\/.*))/',
301+
'/(\s)+/s', // shorten multiple whitespace sequences
302+
);
303+
304+
$replace = array(
305+
'',
306+
'\\1',
307+
);
308+
309+
$this->scripts[$scriptId] = preg_replace($search, $replace, $match[0]);
310+
}else {
311+
$this->scripts[$scriptId] = $match[0];
312+
}
313+
if (!$deferJs) return '<script>' . $scriptId . '</script>';
314+
return '';
315+
},
316+
$content
317+
);
289318

290-
} else {
291-
return preg_replace_callback(
292-
'~<\s*\bscript\b[^>]*>(.*?)<\s*\/\s*script\s*>~is',
293-
function($match) use($pattern){
294-
if(trim($match[1], ' ')){
295-
return preg_replace($pattern, '', $match[0]);
296-
} else {
297-
return $match[0];
298-
}
299-
},
300-
$content
301-
);
302-
}
319+
return $content;
303320
}
304321

305322
public function minifyHtml($content)

0 commit comments

Comments
 (0)