diff --git a/Model/Controller/ResultPlugin.php b/Model/Controller/ResultPlugin.php index 8d30236..5e15dd7 100644 --- a/Model/Controller/ResultPlugin.php +++ b/Model/Controller/ResultPlugin.php @@ -30,16 +30,33 @@ class ResultPlugin */ protected $scopeConfig; + /** + * @var bool + */ + protected $allowedOnPage; + + /** + * @var \Magento\Store\Model\StoreManagerInterface + */ + protected $storeManager; + /** * @param \Magento\Framework\App\RequestInterface $request * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig + * @param \Magento\Store\Model\StoreManagerInterface|null $storeManager */ public function __construct( \Magento\Framework\App\RequestInterface $request, - \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, + \Magento\Store\Model\StoreManagerInterface $storeManager = null ) { $this->request = $request; $this->scopeConfig = $scopeConfig; + + $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); + $this->storeManager = $storeManager ?: $objectManager->get( + \Magento\Store\Model\StoreManagerInterface::class + ); } /** @@ -59,6 +76,10 @@ public function aroundRenderResult( return $result; } + if (!$this->isAllowedOnPage()) { + return $result; + } + $html = $response->getBody(); $scripts = []; @@ -134,4 +155,76 @@ private function isEnabled() return $enabled; } + + /** + * @return bool + */ + private function isAllowedOnPage() + { + if (null !== $this->allowedOnPage) { + return $this->allowedOnPage; + } + $this->allowedOnPage = false; + + $spPages = $this->scopeConfig->getValue( + 'mfrocketjavascript/general/disallowed_pages_for_deferred_js', + \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ); + $spPages = explode("\n", str_replace("\r", "\n", $spPages)); + + foreach ($spPages as $key => $path) { + $spPages[$key] = trim($spPages[$key]); + if (empty($spPages[$key])) { + unset($spPages[$key]); + } + } + $baseUrl = trim($this->storeManager->getStore()->getBaseUrl(), '/'); + $baseUrl = str_replace('/index.php', '', $baseUrl); + + $currentUrl = $this->storeManager->getStore()->getCurrentUrl(); + $currentUrl = explode('?', $currentUrl); + $currentUrl = trim($currentUrl[0], '/'); + foreach (['index.php', '.php', '.html'] as $end) { + $el = mb_strlen($end); + $cl = mb_strlen($currentUrl); + if (mb_strrpos($currentUrl, $end) == $cl - $el) { + $currentUrl = mb_substr($currentUrl, 0, $cl - $el); + } + } + $currentUrl = str_replace('/index.php', '', $currentUrl); + $currentUrl = trim($currentUrl, '/'); + foreach ($spPages as $key => $path) { + $path = trim($path, '/'); + + if (mb_strlen($path)) { + if ('*' == $path{0}) { + $subPath = trim($path, '*/'); + if (mb_strlen($currentUrl) - mb_strlen($subPath) === mb_strrpos($currentUrl, $subPath)) { + $this->allowedOnPage = true; + break; + } + } + + if ('*' == $path{mb_strlen($path) - 1}) { + if (0 === mb_strpos($currentUrl, $baseUrl . '/' . trim($path, '*/'))) { + $this->allowedOnPage = true; + break; + } + } + if ($currentUrl == $baseUrl . '/' . trim($path, '/')) { + $this->allowedOnPage = true; + break; + } + } else { + //homepage + + if ($currentUrl == $baseUrl) { + $this->allowedOnPage = true; + break; + } + } + } + + return $this->allowedOnPage = !$this->allowedOnPage; + } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 86bc239..b6be563 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -32,6 +32,13 @@ If enabled all JavaScript on storefront will be moved to the end of the page. Magento\Config\Model\Config\Source\Yesno + + + + 1 + + Enter page patches each in a new line. "*" means any path, you can use it at the beginning or end. + Please note that this option only works with enabled JavaScript Bundling (Configuration > Advanced > Developer > JavaScript Settings > Enable JavaScript Bundling). diff --git a/etc/config.xml b/etc/config.xml index f4e4b07..c48c938 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -11,6 +11,9 @@ + checkout/* +onestepcheckout/* + jquery/jquery.mobile.custom.min.js mage/dataPost.min.js mage/bootstrap.min.js