@@ -30,16 +30,33 @@ class ResultPlugin
3030 */
3131 protected $ scopeConfig ;
3232
33+ /**
34+ * @var bool
35+ */
36+ protected $ allowedOnPage ;
37+
38+ /**
39+ * @var \Magento\Store\Model\StoreManagerInterface
40+ */
41+ protected $ storeManager ;
42+
3343 /**
3444 * @param \Magento\Framework\App\RequestInterface $request
3545 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
46+ * @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
3647 */
3748 public function __construct (
3849 \Magento \Framework \App \RequestInterface $ request ,
39- \Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig
50+ \Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig ,
51+ \Magento \Store \Model \StoreManagerInterface $ storeManager = null
4052 ) {
4153 $ this ->request = $ request ;
4254 $ this ->scopeConfig = $ scopeConfig ;
55+
56+ $ objectManager = \Magento \Framework \App \ObjectManager::getInstance ();
57+ $ this ->storeManager = $ storeManager ?: $ objectManager ->get (
58+ \Magento \Store \Model \StoreManagerInterface::class
59+ );
4360 }
4461
4562 /**
@@ -59,6 +76,10 @@ public function aroundRenderResult(
5976 return $ result ;
6077 }
6178
79+ if (!$ this ->isAllowedOnPage ()) {
80+ return $ result ;
81+ }
82+
6283 $ html = $ response ->getBody ();
6384 $ scripts = [];
6485
@@ -134,4 +155,76 @@ private function isEnabled()
134155
135156 return $ enabled ;
136157 }
158+
159+ /**
160+ * @return bool
161+ */
162+ private function isAllowedOnPage ()
163+ {
164+ if (null !== $ this ->allowedOnPage ) {
165+ return $ this ->allowedOnPage ;
166+ }
167+ $ this ->allowedOnPage = false ;
168+
169+ $ spPages = $ this ->scopeConfig ->getValue (
170+ 'mfrocketjavascript/general/disallowed_pages_for_deferred_js ' ,
171+ \Magento \Store \Model \ScopeInterface::SCOPE_STORE
172+ );
173+ $ spPages = explode ("\n" , str_replace ("\r" , "\n" , $ spPages ));
174+
175+ foreach ($ spPages as $ key => $ path ) {
176+ $ spPages [$ key ] = trim ($ spPages [$ key ]);
177+ if (empty ($ spPages [$ key ])) {
178+ unset($ spPages [$ key ]);
179+ }
180+ }
181+ $ baseUrl = trim ($ this ->storeManager ->getStore ()->getBaseUrl (), '/ ' );
182+ $ baseUrl = str_replace ('/index.php ' , '' , $ baseUrl );
183+
184+ $ currentUrl = $ this ->storeManager ->getStore ()->getCurrentUrl ();
185+ $ currentUrl = explode ('? ' , $ currentUrl );
186+ $ currentUrl = trim ($ currentUrl [0 ], '/ ' );
187+ foreach (['index.php ' , '.php ' , '.html ' ] as $ end ) {
188+ $ el = mb_strlen ($ end );
189+ $ cl = mb_strlen ($ currentUrl );
190+ if (mb_strrpos ($ currentUrl , $ end ) == $ cl - $ el ) {
191+ $ currentUrl = mb_substr ($ currentUrl , 0 , $ cl - $ el );
192+ }
193+ }
194+ $ currentUrl = str_replace ('/index.php ' , '' , $ currentUrl );
195+ $ currentUrl = trim ($ currentUrl , '/ ' );
196+ foreach ($ spPages as $ key => $ path ) {
197+ $ path = trim ($ path , '/ ' );
198+
199+ if (mb_strlen ($ path )) {
200+ if ('* ' == $ path {0 }) {
201+ $ subPath = trim ($ path , '*/ ' );
202+ if (mb_strlen ($ currentUrl ) - mb_strlen ($ subPath ) === mb_strrpos ($ currentUrl , $ subPath )) {
203+ $ this ->allowedOnPage = true ;
204+ break ;
205+ }
206+ }
207+
208+ if ('* ' == $ path {mb_strlen ($ path ) - 1 }) {
209+ if (0 === mb_strpos ($ currentUrl , $ baseUrl . '/ ' . trim ($ path , '*/ ' ))) {
210+ $ this ->allowedOnPage = true ;
211+ break ;
212+ }
213+ }
214+ if ($ currentUrl == $ baseUrl . '/ ' . trim ($ path , '/ ' )) {
215+ $ this ->allowedOnPage = true ;
216+ break ;
217+ }
218+ } else {
219+ //homepage
220+
221+ if ($ currentUrl == $ baseUrl ) {
222+ $ this ->allowedOnPage = true ;
223+ break ;
224+ }
225+ }
226+ }
227+
228+ return $ this ->allowedOnPage = !$ this ->allowedOnPage ;
229+ }
137230}
0 commit comments