From c8ad8aba4601f536762fe087bdb47f329a73834c Mon Sep 17 00:00:00 2001 From: Duong Huy Toan Date: Tue, 30 Mar 2021 11:06:07 +0700 Subject: [PATCH 1/4] =?UTF-8?q?L=E1=BB=97i=20hi=E1=BB=83n=20th=E1=BB=8B=20?= =?UTF-8?q?tr=C3=AAn=20trang=20better=20maintenance.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Block/Popup.php | 374 ++++++++++++---------- Helper/Data.php | 83 +++-- view/frontend/templates/popup/popup.phtml | 2 +- 3 files changed, 248 insertions(+), 211 deletions(-) diff --git a/Block/Popup.php b/Block/Popup.php index fd3c24f..3d0723c 100644 --- a/Block/Popup.php +++ b/Block/Popup.php @@ -24,6 +24,7 @@ use Magento\Catalog\Block\Product\AbstractProduct; use Magento\Catalog\Block\Product\Context; use Magento\Framework\Phrase; +use Magento\Framework\Stdlib\DateTime\TimezoneInterface; use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory; use Magento\Widget\Block\BlockInterface; use Mageplaza\BetterPopup\Helper\Data as HelperData; @@ -42,6 +43,11 @@ class Popup extends AbstractProduct implements BlockInterface */ protected $_helperData; + /** + * @var TimezoneInterface + */ + protected $_localeDate; + /** * @var CollectionFactory */ @@ -49,54 +55,27 @@ class Popup extends AbstractProduct implements BlockInterface /** * Popup constructor. - * * @param Context $context * @param HelperData $helperData + * @param TimezoneInterface $localeDate * @param CollectionFactory $subscriberCollectionFactory * @param array $data */ public function __construct( Context $context, HelperData $helperData, + TimezoneInterface $localeDate, CollectionFactory $subscriberCollectionFactory, array $data = [] - ) { - $this->_helperData = $helperData; + ) + { + $this->_helperData = $helperData; + $this->_localeDate = $localeDate; $this->_subscriberCollectionFactory = $subscriberCollectionFactory; parent::__construct($context, $data); } - /** - * Get Width Popup Config - * - * @return array|mixed - */ - public function getWidthPopup() - { - return $this->_helperData->getWhatToShowConfig('width'); - } - - /** - * Get Height Popup Config - * - * @return array|mixed - */ - public function getHeightPopup() - { - return $this->_helperData->getWhatToShowConfig('height'); - } - - /** - * Get Background Color Popup - * - * @return array|mixed - */ - public function getBackGroundColor() - { - return $this->_helperData->getWhatToShowConfig('background_color'); - } - /** * Get Text Color in Popup * @@ -107,26 +86,6 @@ public function getTextColor() return $this->_helperData->getWhatToShowConfig('text_color'); } - /** - * Check FullScreen option - * - * @return bool - */ - public function isFullScreen() - { - return (int) $this->_helperData->getWhatToShowConfig('responsive') === Responsive::FULLSCREEN_POPUP; - } - - /** - * Check show fireworks config - * - * @return bool - */ - public function isShowFireworks() - { - return $this->_helperData->getWhatToShowConfig('popup_success/enabled_fireworks'); - } - /** * Is Enable Show Float Button * @@ -159,62 +118,6 @@ public function getFloatLabel() return $label ?: __('Subscribe'); } - /** - * Get Config Popup Appear - * - * @return array|mixed - */ - public function getPopupAppear() - { - return (int) $this->_helperData->getWhenToShowConfig('popup_appear'); - } - - /** - * Get time delay to show popup - * - * @return array|int|mixed - */ - public function getDelayConfig() - { - if ($this->getPopupAppear() === Appear::AFTER_X_SECONDS) { - return $this->_helperData->getWhenToShowConfig('delay'); - } - - return 0; - } - - /** - * is Exit Intent Config - * - * @return string - */ - public function isExitIntent() - { - return $this->getPopupAppear() === Appear::EXIT_INTENT; - } - - /** - * Get Popup show again after (days) - * - * @return array|mixed - */ - public function getCookieConfig() - { - $cookieDays = (int) $this->_helperData->getWhenToShowConfig('cookieExp'); - - return ($cookieDays !== null) ? $cookieDays : 30; - } - - /** - * Get Percentage scroll down to show Popup - * - * @return array|mixed - */ - public function getPercentageScroll() - { - return $this->_helperData->getWhenToShowConfig('after_scroll'); - } - /** * Get Html Content popup * @@ -224,7 +127,7 @@ public function getPopupContent() { $htmlConfig = $this->_helperData->getWhatToShowConfig('html_content'); - $search = [ + $search = [ '{{form_url}}', '{{url_loader}}', '{{email_icon_url}}', @@ -255,40 +158,35 @@ public function getPopupContent() } /** - * Check include pages are show Popup + * Get Url NewAction Newsletter * - * @return bool + * @return string */ - public function checkIncludePages() + public function getFormActionUrl() { - $fullActionName = $this->getRequest()->getFullActionName(); - $arrayPages = explode("\n", $this->_helperData->getWhereToShowConfig('include_pages')); - $includePages = array_map('trim', $arrayPages); - - return in_array($fullActionName, $includePages, true); + return $this->getUrl('newsletter/subscriber/new', ['_secure' => true]); } /** - * Check include paths to show popup + * check Manually Insert Config * * @return bool */ - public function checkIncludePaths() + public function isManuallyInsert() { - $currentPath = $this->getRequest()->getRequestUri(); - $pathsConfig = $this->_helperData->getWhereToShowConfig('include_pages_with_url'); - - if ($pathsConfig) { - $arrayPaths = explode("\n", $pathsConfig); - $pathsUrl = array_map('trim', $arrayPaths); - foreach ($pathsUrl as $path) { - if ($path && strpos($currentPath, $path) !== false) { - return true; - } - } - } + return $this->_helperData->isEnabled() + && (int)$this->_helperData->getWhereToShowConfig('which_page_to_show') === PageToShow::MANUALLY_INSERT + && $this->checkExclude(); + } - return false; + /** + * Check Exclude (page & path) + * + * @return bool + */ + public function checkExclude() + { + return ($this->checkExcludePages() && $this->checkExcludePaths()); } /** @@ -299,8 +197,8 @@ public function checkIncludePaths() public function checkExcludePages() { $fullActionName = $this->getRequest()->getFullActionName(); - $arrayPages = explode("\n", $this->_helperData->getWhereToShowConfig('exclude_pages')); - $includePages = array_map('trim', $arrayPages); + $arrayPages = explode("\n", $this->_helperData->getWhereToShowConfig('exclude_pages')); + $includePages = array_map('trim', $arrayPages); return !in_array($fullActionName, $includePages, true); } @@ -317,7 +215,7 @@ public function checkExcludePaths() if ($pathsConfig) { $arrayPaths = explode("\n", $pathsConfig); - $pathsUrl = array_map('trim', $arrayPaths); + $pathsUrl = array_map('trim', $arrayPaths); foreach ($pathsUrl as $path) { if (strpos($currentPath, $path) !== false) { @@ -330,60 +228,90 @@ public function checkExcludePaths() } /** - * Check Include (page & path) + * Check Pages to show popup * * @return bool */ - public function checkInclude() + public function checkPagesToShow() { - return ($this->checkIncludePages() || $this->checkIncludePaths()); + if ($this->_helperData->isEnabled()) { + $config = $this->_helperData->getWhereToShowConfig('which_page_to_show'); + + switch ($config) { + case PageToShow::SPECIFIC_PAGES: + return ($this->checkInclude() && $this->checkExclude()); + case PageToShow::ALL_PAGES: + return $this->checkExclude(); + case PageToShow::MANUALLY_INSERT: + return false; + } + } + + return false; } /** - * Check Exclude (page & path) + * Check Include (page & path) * * @return bool */ - public function checkExclude() + public function checkInclude() { - return ($this->checkExcludePages() && $this->checkExcludePaths()); + return ($this->checkIncludePages() || $this->checkIncludePaths()); } /** - * check Manually Insert Config + * Check include pages are show Popup * * @return bool */ - public function isManuallyInsert() + public function checkIncludePages() { - return $this->_helperData->isEnabled() - && (int) $this->_helperData->getWhereToShowConfig('which_page_to_show') === PageToShow::MANUALLY_INSERT - && $this->checkExclude(); + $fullActionName = $this->getRequest()->getFullActionName(); + $arrayPages = explode("\n", $this->_helperData->getWhereToShowConfig('include_pages')); + $includePages = array_map('trim', $arrayPages); + + return in_array($fullActionName, $includePages, true); } /** - * Check Pages to show popup + * Check include paths to show popup * * @return bool */ - public function checkPagesToShow() + public function checkIncludePaths() { - if ($this->_helperData->isEnabled()) { - $config = $this->_helperData->getWhereToShowConfig('which_page_to_show'); + $currentPath = $this->getRequest()->getRequestUri(); + $pathsConfig = $this->_helperData->getWhereToShowConfig('include_pages_with_url'); - switch ($config) { - case PageToShow::SPECIFIC_PAGES: - return ($this->checkInclude() && $this->checkExclude()); - case PageToShow::ALL_PAGES: - return $this->checkExclude(); - case PageToShow::MANUALLY_INSERT: - return false; + if ($pathsConfig) { + $arrayPaths = explode("\n", $pathsConfig); + $pathsUrl = array_map('trim', $arrayPaths); + foreach ($pathsUrl as $path) { + if ($path && strpos($currentPath, $path) !== false) { + return true; + } } } return false; } + /** + * @return bool + */ + public function checkBetterMaintenance() + { + if ($this->_helperData->getBetterMaintenanceConfigGeneral('enabled') === '1') { + if (strtotime($this->_localeDate->date()->format('m/d/Y H:i:s')) + < strtotime($this->_helperData->getBetterMaintenanceConfigGeneral('end_time'))) { + return false; + } + } + + return true; + } + /** * Get Ajax Data * @@ -392,24 +320,24 @@ public function checkPagesToShow() public function getAjaxData() { $params = [ - 'url' => $this->getUrl('betterpopup/ajax/success'), - 'isScroll' => $this->getPopupAppear() === Appear::AFTER_SCROLL_DOWN, - 'afterSeconds' => [ + 'url' => $this->getUrl('betterpopup/ajax/success'), + 'isScroll' => $this->getPopupAppear() === Appear::AFTER_SCROLL_DOWN, + 'afterSeconds' => [ 'isAfterSeconds' => $this->getPopupAppear() === Appear::AFTER_X_SECONDS, - 'delay' => $this->getDelayConfig() + 'delay' => $this->getDelayConfig() ], - 'percentage' => $this->getPercentageScroll(), - 'fullScreen' => [ + 'percentage' => $this->getPercentageScroll(), + 'fullScreen' => [ 'isFullScreen' => $this->isFullScreen(), - 'bgColor' => $this->getBackGroundColor() + 'bgColor' => $this->getBackGroundColor() ], - 'isExitIntent' => $this->isExitIntent(), - 'isShowFireworks' => $this->isShowFireworks(), - 'popupConfig' => [ - 'width' => $this->getWidthPopup(), - 'height' => $this->getHeightPopup(), - 'cookieExp' => $this->getCookieConfig(), - 'delay' => $this->getDelayConfig(), + 'isExitIntent' => $this->isExitIntent(), + 'isShowFireworks' => $this->isShowFireworks(), + 'popupConfig' => [ + 'width' => $this->getWidthPopup(), + 'height' => $this->getHeightPopup(), + 'cookieExp' => $this->getCookieConfig(), + 'delay' => $this->getDelayConfig(), 'showOnDelay' => true, ], 'srcCloseIconWhite' => $this->getViewFileUrl('Mageplaza_BetterPopup::images/icon-close-white.png') @@ -419,12 +347,108 @@ public function getAjaxData() } /** - * Get Url NewAction Newsletter + * Get Config Popup Appear + * + * @return array|mixed + */ + public function getPopupAppear() + { + return (int)$this->_helperData->getWhenToShowConfig('popup_appear'); + } + + /** + * Get time delay to show popup + * + * @return array|int|mixed + */ + public function getDelayConfig() + { + if ($this->getPopupAppear() === Appear::AFTER_X_SECONDS) { + return $this->_helperData->getWhenToShowConfig('delay'); + } + + return 0; + } + + /** + * Get Percentage scroll down to show Popup + * + * @return array|mixed + */ + public function getPercentageScroll() + { + return $this->_helperData->getWhenToShowConfig('after_scroll'); + } + + /** + * Check FullScreen option + * + * @return bool + */ + public function isFullScreen() + { + return (int)$this->_helperData->getWhatToShowConfig('responsive') === Responsive::FULLSCREEN_POPUP; + } + + /** + * Get Background Color Popup + * + * @return array|mixed + */ + public function getBackGroundColor() + { + return $this->_helperData->getWhatToShowConfig('background_color'); + } + + /** + * is Exit Intent Config * * @return string */ - public function getFormActionUrl() + public function isExitIntent() { - return $this->getUrl('newsletter/subscriber/new', ['_secure' => true]); + return $this->getPopupAppear() === Appear::EXIT_INTENT; + } + + /** + * Check show fireworks config + * + * @return bool + */ + public function isShowFireworks() + { + return $this->_helperData->getWhatToShowConfig('popup_success/enabled_fireworks'); + } + + /** + * Get Width Popup Config + * + * @return array|mixed + */ + public function getWidthPopup() + { + return $this->_helperData->getWhatToShowConfig('width'); + } + + /** + * Get Height Popup Config + * + * @return array|mixed + */ + public function getHeightPopup() + { + return $this->_helperData->getWhatToShowConfig('height'); + } + + /** + * Get Popup show again after (days) + * + * @return array|mixed + */ + public function getCookieConfig() + { + $cookieDays = (int)$this->_helperData->getWhenToShowConfig('cookieExp'); + + return ($cookieDays !== null) ? $cookieDays : 30; } } diff --git a/Helper/Data.php b/Helper/Data.php index 828fa25..d344c23 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -63,13 +63,26 @@ public function __construct( StoreManagerInterface $storeManager, Filesystem $filesystem, DirectoryList $directoryList - ) { - $this->_fileSystem = $filesystem; + ) + { + $this->_fileSystem = $filesystem; $this->_directoryList = $directoryList; parent::__construct($context, $objectManager, $storeManager); } + /** + * @param string $code + * @param null $storeId + * @return array|mixed + */ + public function getBetterMaintenanceConfigGeneral($code = '', $storeId = null) + { + $code = ($code !== '') ? '/' . $code : ''; + + return $this->getConfigValue('mpbettermaintenance/general' . $code, $storeId); + } + /** * @param $code * @param null $storeId @@ -104,24 +117,24 @@ public function getWhenToShowConfig($code, $storeId = null) } /** - * @param $code * @param null $storeId * * @return mixed */ - public function getSendEmailConfig($code, $storeId = null) + public function isSendEmail($storeId = null) { - return $this->getModuleConfig('send_email/' . $code, $storeId); + return $this->getSendEmailConfig('isSendEmail', $storeId); } /** + * @param $code * @param null $storeId * * @return mixed */ - public function isSendEmail($storeId = null) + public function getSendEmailConfig($code, $storeId = null) { - return $this->getSendEmailConfig('isSendEmail', $storeId); + return $this->getModuleConfig('send_email/' . $code, $storeId); } /** @@ -134,6 +147,30 @@ public function getToEmail() return $this->getSendEmailConfig('to'); } + /** + * @param $templateId + * + * @return string + * @throws FileSystemException + */ + public function getDefaultTemplateHtml($templateId) + { + return $this->readFile($this->getTemplatePath($templateId)); + } + + /** + * @param $relativePath + * + * @return string + * @throws FileSystemException + */ + public function readFile($relativePath) + { + $rootDirectory = $this->_fileSystem->getDirectoryRead(DirectoryList::ROOT); + + return $rootDirectory->readFile($relativePath); + } + /** * Get default template path * @@ -151,17 +188,17 @@ public function getTemplatePath($templateId, $type = '.html') $rootPath = $this->_directoryList->getRoot(); $currentDirArr = explode('\\', $currentDir); - $countDir = count($currentDirArr); + $countDir = count($currentDirArr); if ($countDir === 1) { $currentDirArr = explode('/', $currentDir); - $countDir = count($currentDirArr); + $countDir = count($currentDirArr); } $rootPathArr = explode('/', $rootPath); - $countPath = count($rootPathArr); + $countPath = count($rootPathArr); if ($countPath === 1) { $rootPathArr = explode('\\', $rootPath); - $countPath = count($rootPathArr); + $countPath = count($rootPathArr); } $basePath = ''; @@ -174,30 +211,6 @@ public function getTemplatePath($templateId, $type = '.html') return $templatePath . $templateId . $type; } - /** - * @param $relativePath - * - * @return string - * @throws FileSystemException - */ - public function readFile($relativePath) - { - $rootDirectory = $this->_fileSystem->getDirectoryRead(DirectoryList::ROOT); - - return $rootDirectory->readFile($relativePath); - } - - /** - * @param $templateId - * - * @return string - * @throws FileSystemException - */ - public function getDefaultTemplateHtml($templateId) - { - return $this->readFile($this->getTemplatePath($templateId)); - } - /** * @return int * @throws NoSuchEntityException diff --git a/view/frontend/templates/popup/popup.phtml b/view/frontend/templates/popup/popup.phtml index 6d11f8d..8023f1c 100644 --- a/view/frontend/templates/popup/popup.phtml +++ b/view/frontend/templates/popup/popup.phtml @@ -20,7 +20,7 @@ */ /** @var \Mageplaza\BetterPopup\Block\Popup $block */ -if ($block->checkPagesToShow()) : +if ($block->checkPagesToShow() && $block->checkBetterMaintenance()) : $floatLocation = ($block->getLocationFloatButton() == 1) ? 'left: 70px' : 'right:70px'; ?>
Date: Tue, 30 Mar 2021 11:27:42 +0700 Subject: [PATCH 2/4] =?UTF-8?q?L=E1=BB=97i=20hi=E1=BB=83n=20th=E1=BB=8B=20?= =?UTF-8?q?tr=C3=AAn=20trang=20better=20maintenance.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Block/Success.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Block/Success.php b/Block/Success.php index b970fa4..b5227ba 100644 --- a/Block/Success.php +++ b/Block/Success.php @@ -23,6 +23,7 @@ use Magento\Catalog\Block\Product\Context; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Stdlib\DateTime\TimezoneInterface; use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory; use Mageplaza\BetterPopup\Helper\Data as HelperData; use Mageplaza\BetterPopup\Model\Generate; @@ -41,9 +42,9 @@ class Success extends Popup /** * Success constructor. - * * @param Context $context * @param HelperData $helperData + * @param TimezoneInterface $localeDate * @param CollectionFactory $subscriberCollectionFactory * @param Generate $generate * @param array $data @@ -51,11 +52,12 @@ class Success extends Popup public function __construct( Context $context, HelperData $helperData, + TimezoneInterface $localeDate, CollectionFactory $subscriberCollectionFactory, Generate $generate, array $data = [] ) { - parent::__construct($context, $helperData, $subscriberCollectionFactory, $data); + parent::__construct($context, $helperData, $localeDate, $subscriberCollectionFactory, $data); $this->generate = $generate; } From c9ba07122d79696f8a83095e32419e0af23d6795 Mon Sep 17 00:00:00 2001 From: Duong Huy Toan Date: Mon, 10 May 2021 19:53:34 +0700 Subject: [PATCH 3/4] Clean code. --- composer.json | 52 +++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/composer.json b/composer.json index be8e9d2..c9ea06e 100644 --- a/composer.json +++ b/composer.json @@ -1,26 +1,26 @@ -{ - "name": "mageplaza/module-better-popup", - "description": "Magento 2 Better Popup", - "require": { - "mageplaza/module-core": "^1.4.5" - }, - "type": "magento2-module", - "version": "1.2.1", - "license": "proprietary", - "authors": [ - { - "name": "Mageplaza", - "email": "support@mageplaza.com", - "homepage": "https://www.mageplaza.com", - "role": "Technical Support" - } - ], - "autoload": { - "files": [ - "registration.php" - ], - "psr-4": { - "Mageplaza\\BetterPopup\\": "" - } - } -} +{ + "name": "mageplaza/module-better-popup", + "description": "Magento 2 Better Popup", + "require": { + "mageplaza/module-core": "^1.4.5" + }, + "type": "magento2-module", + "version": "1.3.0", + "license": "proprietary", + "authors": [ + { + "name": "Mageplaza", + "email": "support@mageplaza.com", + "homepage": "https://www.mageplaza.com", + "role": "Technical Support" + } + ], + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "Mageplaza\\BetterPopup\\": "" + } + } +} From 26da5cd8b6dba8f159de0ab39c76877c8ec0ab1d Mon Sep 17 00:00:00 2001 From: Duong Huy Toan Date: Mon, 10 May 2021 19:55:53 +0700 Subject: [PATCH 4/4] Clean code --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c9ea06e..0e0526e 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "mageplaza/module-core": "^1.4.5" }, "type": "magento2-module", - "version": "1.3.0", + "version": "1.2.2", "license": "proprietary", "authors": [ {