|
3 | 3 | * @Author: nguyen |
4 | 4 | * @Date: 2020-02-12 14:01:01 |
5 | 5 | * @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 |
7 | 7 | */ |
8 | 8 |
|
9 | 9 | namespace Magepow\SpeedOptimizer\Plugin; |
|
17 | 17 |
|
18 | 18 | class SpeedOptimizer extends \Magento\Framework\View\Element\Template |
19 | 19 | { |
20 | | - public $request; |
| 20 | + protected $request; |
21 | 21 |
|
22 | | - public $helper; |
| 22 | + protected $helper; |
23 | 23 |
|
24 | | - public $content; |
| 24 | + protected $content; |
25 | 25 |
|
26 | | - public $isJson; |
| 26 | + protected $isJson; |
27 | 27 |
|
28 | | - public $exclude = []; |
| 28 | + protected $exclude = []; |
| 29 | + |
| 30 | + protected $scripts = []; |
29 | 31 |
|
30 | 32 | protected $storeManager; |
31 | 33 |
|
32 | 34 | protected $themeProvider; |
33 | 35 |
|
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'; |
35 | 37 |
|
36 | 38 | public function __construct( |
37 | 39 | \Magento\Framework\View\Element\Template\Context $context, |
@@ -74,34 +76,54 @@ public function beforeSendResponse(Http $response) |
74 | 76 | $body_includes = $this->helper->getConfigModule('general/body_includes'); |
75 | 77 | if($body_includes) $body = $this->addToBottomBody($body, $body_includes); |
76 | 78 |
|
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'); |
79 | 81 |
|
80 | | - $minifyHtml = $this->helper->getConfigModule('general/minify_html'); |
| 82 | + $body = $this->processExcludeJs($body, $minifyHtml, $deferJs); |
81 | 83 | if($minifyHtml) $body = $this->minifyHtml($body); |
82 | 84 |
|
83 | | - $bodyClass = 'loading_img'; |
| 85 | + $bodyClass = ''; |
84 | 86 | $loadingBody = $this->helper->getConfigModule('general/loading_body'); |
85 | 87 | if($loadingBody){ |
86 | 88 | $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 | + } |
89 | 91 |
|
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 | + } |
91 | 107 |
|
92 | | - if(!$this->helper->getConfigModule('general/loading_img')) return; |
| 108 | + $body = $this->addBodyClass($body, $bodyClass); |
93 | 109 |
|
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 | + ); |
99 | 126 | } |
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 ); |
105 | 127 |
|
106 | 128 | $response->setBody($body); |
107 | 129 | } |
@@ -265,41 +287,36 @@ public function addLazyLoadJs( $content, $selector='img', $exclude='.loaded' ) |
265 | 287 | return $this->addToBottomBody($content, $script); |
266 | 288 | } |
267 | 289 |
|
268 | | - public function minifyJs($content, $deferJs=false) |
| 290 | + public function processExcludeJs($content, $minify=true, $deferJs=true) |
269 | 291 | { |
270 | 292 | $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 | + ); |
289 | 318 |
|
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; |
303 | 320 | } |
304 | 321 |
|
305 | 322 | public function minifyHtml($content) |
|
0 commit comments