Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #25 from mageplaza/2.4-develop
Browse files Browse the repository at this point in the history
v4.1.0
  • Loading branch information
DHT-Uri authored May 11, 2021
2 parents ae779aa + 4ba9739 commit d8789f9
Show file tree
Hide file tree
Showing 14 changed files with 1,670 additions and 1,503 deletions.
369 changes: 197 additions & 172 deletions Block/Popup.php

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions Block/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,21 +42,22 @@ class Success extends Popup

/**
* Success constructor.
*
* @param Context $context
* @param HelperData $helperData
* @param TimezoneInterface $localeDate
* @param CollectionFactory $subscriberCollectionFactory
* @param Generate $generate
* @param array $data
*/
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;
}

Expand Down
81 changes: 47 additions & 34 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,25 @@ public function __construct(
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
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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
*
Expand All @@ -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 = '';
Expand All @@ -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
Expand Down
133 changes: 119 additions & 14 deletions Plugin/Controller/Subscriber/NewAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,19 @@
namespace Mageplaza\BetterPopup\Plugin\Controller\Subscriber;

use Exception;
use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement;
use Magento\Customer\Model\Session;
use Magento\Customer\Model\Url as CustomerUrl;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
use Magento\Framework\Validator\EmailAddress as EmailValidator;
use Magento\Newsletter\Model\Subscriber;
use Magento\Newsletter\Model\SubscriberFactory;
use Magento\Newsletter\Model\SubscriptionManagerInterface;
use Magento\Store\Model\StoreManagerInterface;
use Mageplaza\BetterPopup\Helper\Data;

/**
Expand All @@ -44,6 +53,53 @@ class NewAction extends \Magento\Newsletter\Controller\Subscriber\NewAction
*/
protected $_helperData;

/**
* @var SubscriptionManagerInterface
*/
protected $subscriberManagerInterface;

/**
* NewAction constructor.
*
* @param Context $context
* @param SubscriberFactory $subscriberFactory
* @param Session $customerSession
* @param StoreManagerInterface $storeManager
* @param CustomerUrl $customerUrl
* @param CustomerAccountManagement $customerAccountManagement
* @param SubscriptionManagerInterface $subscriptionManager
* @param JsonFactory $jsonFactory
* @param Data $helperData
* @param EmailValidator|null $emailValidator
*/
public function __construct(
Context $context,
SubscriberFactory $subscriberFactory,
Session $customerSession,
StoreManagerInterface $storeManager,
CustomerUrl $customerUrl,
CustomerAccountManagement $customerAccountManagement,
SubscriptionManagerInterface $subscriptionManager,
JsonFactory $jsonFactory,
Data $helperData,
EmailValidator $emailValidator = null
) {
$this->resultJsonFactory = $jsonFactory;
$this->_helperData = $helperData;
$this->subscriberManagerInterface = $subscriptionManager;

parent::__construct(
$context,
$subscriberFactory,
$customerSession,
$storeManager,
$customerUrl,
$customerAccountManagement,
$subscriptionManager,
$emailValidator
);
}

/**
* @param $subject
* @param $proceed
Expand All @@ -52,10 +108,7 @@ class NewAction extends \Magento\Newsletter\Controller\Subscriber\NewAction
*/
public function aroundExecute($subject, $proceed)
{
$resultJsonFactory = ObjectManager::getInstance()->get(JsonFactory::class);
$_helperData = ObjectManager::getInstance()->get(Data::class);

if (!$_helperData->isEnabled() || !$this->getRequest()->isAjax()) {
if (!$this->_helperData->isEnabled() || !$this->getRequest()->isAjax()) {
return $proceed();
}

Expand All @@ -68,25 +121,77 @@ public function aroundExecute($subject, $proceed)
$this->validateGuestSubscription();
$this->validateEmailAvailable($email);

$this->_subscriberFactory->create()->subscribe($email);
if (!$_helperData->versionCompare('2.2.0')) {
$this->_subscriberFactory->create()
->loadByEmail($email)
->setChangeStatusAt(date('Y-m-d h:i:s'))->save();
$websiteId = (int)$this->_storeManager->getStore()->getWebsiteId();
$subscriber = $this->_subscriberFactory->create()->loadBySubscriberEmail($email, $websiteId);
if ($subscriber->getId()
&& (int)$subscriber->getSubscriberStatus() === Subscriber::STATUS_SUBSCRIBED) {
$response = [
'success' => false,
'msg' => __('This email address is already subscribed.')
];

return $this->resultJsonFactory->create()->setData($response);
}
$storeId = (int)$this->_storeManager->getStore()->getId();
$currentCustomerId = $this->getSessionCustomerId($email);
$subscriber = $currentCustomerId
? $this->subscriberManagerInterface->subscribeCustomer($currentCustomerId, $storeId)
: $this->subscriberManagerInterface->subscribe($email, $storeId);
$message = $this->getSuccessMessage((int)$subscriber->getSubscriberStatus());
$response = [
'success' => true,
'msg' => $message,
];
} catch (LocalizedException $e) {
$response = [
'success' => true,
'msg' => __('There was a problem with the subscription: %1', $e->getMessage()),
'success' => false,
'msg' => __('There was a problem with the subscription: %1', $e->getMessage()),
];
} catch (Exception $e) {
$response = [
'status' => 'ERROR',
'msg' => __('Something went wrong with the subscription: %1', $e->getMessage()),
'status' => false,
'msg' => __('Something went wrong with the subscription: %1', $e->getMessage()),
];
}
}

return $resultJsonFactory->create()->setData($response);
return $this->resultJsonFactory->create()->setData($response);
}

/**
* Get customer id from session if he is owner of the email
*
* @param string $email
*
* @return int|null
*/
private function getSessionCustomerId(string $email): ?int
{
if (!$this->_customerSession->isLoggedIn()) {
return null;
}

$customer = $this->_customerSession->getCustomerDataObject();
if ($customer->getEmail() !== $email) {
return null;
}

return (int)$this->_customerSession->getId();
}

/**
* Get success message
*
* @param int $status
*
* @return Phrase
*/
private function getSuccessMessage(int $status): Phrase
{
if ($status === Subscriber::STATUS_NOT_ACTIVE) {
return __('The confirmation request has been sent.');
}

return __('Thank you for your subscription.');
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"mageplaza/module-core": "^1.4.5"
},
"type": "magento2-module",
"version": "4.0.0",
"version": "4.1.0",
"license": "proprietary",
"authors": [
{
Expand Down
Loading

0 comments on commit d8789f9

Please sign in to comment.