99namespace Magefan \RocketJavaScript \Model \Controller ;
1010
1111use Magento \Framework \App \Response \Http as ResponseHttp ;
12+ use Magento \Store \Model \ScopeInterface ;
1213
1314/**
1415 * Plugin for processing relocation of javascript
@@ -30,16 +31,33 @@ class ResultPlugin
3031 */
3132 protected $ scopeConfig ;
3233
34+ /**
35+ * @var bool
36+ */
37+ protected $ allowedOnPage ;
38+
39+ /**
40+ * @var \Magento\Store\Model\StoreManagerInterface
41+ */
42+ protected $ storeManager ;
43+
3344 /**
3445 * @param \Magento\Framework\App\RequestInterface $request
3546 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
47+ * @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
3648 */
3749 public function __construct (
3850 \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
4053 ) {
4154 $ this ->request = $ request ;
4255 $ this ->scopeConfig = $ scopeConfig ;
56+
57+ $ objectManager = \Magento \Framework \App \ObjectManager::getInstance ();
58+ $ this ->storeManager = $ storeManager ?: $ objectManager ->get (
59+ \Magento \Store \Model \StoreManagerInterface::class
60+ );
4361 }
4462
4563 /**
@@ -59,6 +77,10 @@ public function aroundRenderResult(
5977 return $ result ;
6078 }
6179
80+ if (!$ this ->isAllowedOnPage ()) {
81+ return $ result ;
82+ }
83+
6284 $ html = $ response ->getBody ();
6385 $ scripts = [];
6486
@@ -134,4 +156,76 @@ private function isEnabled()
134156
135157 return $ enabled ;
136158 }
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+ }
137231}
0 commit comments