9
9
namespace Magefan \RocketJavaScript \Model \Controller ;
10
10
11
11
use Magento \Framework \App \Response \Http as ResponseHttp ;
12
+ use Magento \Store \Model \ScopeInterface ;
12
13
13
14
/**
14
15
* Plugin for processing relocation of javascript
@@ -30,16 +31,33 @@ class ResultPlugin
30
31
*/
31
32
protected $ scopeConfig ;
32
33
34
+ /**
35
+ * @var bool
36
+ */
37
+ protected $ allowedOnPage ;
38
+
39
+ /**
40
+ * @var \Magento\Store\Model\StoreManagerInterface
41
+ */
42
+ protected $ storeManager ;
43
+
33
44
/**
34
45
* @param \Magento\Framework\App\RequestInterface $request
35
46
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
47
+ * @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
36
48
*/
37
49
public function __construct (
38
50
\Magento \Framework \App \RequestInterface $ request ,
39
- \Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig
51
+ \Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig ,
52
+ \Magento \Store \Model \StoreManagerInterface $ storeManager = null
40
53
) {
41
54
$ this ->request = $ request ;
42
55
$ this ->scopeConfig = $ scopeConfig ;
56
+
57
+ $ objectManager = \Magento \Framework \App \ObjectManager::getInstance ();
58
+ $ this ->storeManager = $ storeManager ?: $ objectManager ->get (
59
+ \Magento \Store \Model \StoreManagerInterface::class
60
+ );
43
61
}
44
62
45
63
/**
@@ -59,6 +77,10 @@ public function aroundRenderResult(
59
77
return $ result ;
60
78
}
61
79
80
+ if (!$ this ->isAllowedOnPage ()) {
81
+ return $ result ;
82
+ }
83
+
62
84
$ html = $ response ->getBody ();
63
85
$ scripts = [];
64
86
@@ -134,4 +156,76 @@ private function isEnabled()
134
156
135
157
return $ enabled ;
136
158
}
159
+
160
+ /**
161
+ * @return bool
162
+ */
163
+ private function isAllowedOnPage ()
164
+ {
165
+ if (null !== $ this ->allowedOnPage ) {
166
+ return $ this ->allowedOnPage ;
167
+ }
168
+ $ this ->allowedOnPage = false ;
169
+
170
+ $ spPages = $ this ->scopeConfig ->getValue (
171
+ 'mfrocketjavascript/general/disallowed_pages_for_deferred_js ' ,
172
+ \Magento \Store \Model \ScopeInterface::SCOPE_STORE
173
+ );
174
+ $ spPages = explode ("\n" , str_replace ("\r" , "\n" , $ spPages ));
175
+
176
+ foreach ($ spPages as $ key => $ path ) {
177
+ $ spPages [$ key ] = trim ($ spPages [$ key ]);
178
+ if (empty ($ spPages [$ key ])) {
179
+ unset($ spPages [$ key ]);
180
+ }
181
+ }
182
+ $ baseUrl = trim ($ this ->storeManager ->getStore ()->getBaseUrl (), '/ ' );
183
+ $ baseUrl = str_replace ('/index.php ' , '' , $ baseUrl );
184
+
185
+ $ currentUrl = $ this ->storeManager ->getStore ()->getCurrentUrl ();
186
+ $ currentUrl = explode ('? ' , $ currentUrl );
187
+ $ currentUrl = trim ($ currentUrl [0 ], '/ ' );
188
+ foreach (['index.php ' , '.php ' , '.html ' ] as $ end ) {
189
+ $ el = mb_strlen ($ end );
190
+ $ cl = mb_strlen ($ currentUrl );
191
+ if (mb_strrpos ($ currentUrl , $ end ) == $ cl - $ el ) {
192
+ $ currentUrl = mb_substr ($ currentUrl , 0 , $ cl - $ el );
193
+ }
194
+ }
195
+ $ currentUrl = str_replace ('/index.php ' , '' , $ currentUrl );
196
+ $ currentUrl = trim ($ currentUrl , '/ ' );
197
+ foreach ($ spPages as $ key => $ path ) {
198
+ $ path = trim ($ path , '/ ' );
199
+
200
+ if (mb_strlen ($ path )) {
201
+ if ('* ' == $ path {0 }) {
202
+ $ subPath = trim ($ path , '*/ ' );
203
+ if (mb_strlen ($ currentUrl ) - mb_strlen ($ subPath ) === mb_strrpos ($ currentUrl , $ subPath )) {
204
+ $ this ->allowedOnPage = true ;
205
+ break ;
206
+ }
207
+ }
208
+
209
+ if ('* ' == $ path {mb_strlen ($ path ) - 1 }) {
210
+ if (0 === mb_strpos ($ currentUrl , $ baseUrl . '/ ' . trim ($ path , '*/ ' ))) {
211
+ $ this ->allowedOnPage = true ;
212
+ break ;
213
+ }
214
+ }
215
+ if ($ currentUrl == $ baseUrl . '/ ' . trim ($ path , '/ ' )) {
216
+ $ this ->allowedOnPage = true ;
217
+ break ;
218
+ }
219
+ } else {
220
+ //homepage
221
+
222
+ if ($ currentUrl == $ baseUrl ) {
223
+ $ this ->allowedOnPage = true ;
224
+ break ;
225
+ }
226
+ }
227
+ }
228
+
229
+ return $ this ->allowedOnPage = !$ this ->allowedOnPage ;
230
+ }
137
231
}
0 commit comments