Skip to content

Commit 9b76503

Browse files
author
Yura
committed
3753-disallowed-pages-for-deferred
1 parent 40c387a commit 9b76503

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

Model/Controller/ResultPlugin.php

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Magefan\RocketJavaScript\Model\Controller;
1010

1111
use 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
}

etc/adminhtml/system.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
<comment>If enabled all JavaScript on storefront will be moved to the end of the page.</comment>
3333
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
3434
</field>
35+
<field id="disallowed_pages_for_deferred_js" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="textarea">
36+
<label>Disallowed Pages for Deferred JavaScript</label>
37+
<depends>
38+
<field id="enable_deferred_javascript">1</field>
39+
</depends>
40+
<comment>Enter page patches each in a new line. "*" means any path, you can use it at the beginning or end.</comment>
41+
</field>
3542
<field id="enable_js_bundling_optimization" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="30" translate="label" type="select">
3643
<label>Enable JavaScript Bundling Optimization</label>
3744
<comment>Please note that this option only works with enabled JavaScript Bundling (Configuration > Advanced > Developer > JavaScript Settings > Enable JavaScript Bundling).</comment>

etc/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
<default>
1212
<mfrocketjavascript>
1313
<general>
14+
<disallowed_pages_for_deferred_js>checkout/*
15+
onestepcheckout/*
16+
</disallowed_pages_for_deferred_js>
1417
<included_in_bundling>jquery/jquery.mobile.custom.min.js
1518
mage/dataPost.min.js
1619
mage/bootstrap.min.js

0 commit comments

Comments
 (0)