init
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\PromotionPopup\Model;
|
||||
|
||||
use Esun\PromotionPopup\Model\Config\Source\Audience;
|
||||
use Esun\PromotionPopup\Model\Config\Source\Frequency;
|
||||
use Esun\PromotionPopup\Model\Config\Source\PromotionType;
|
||||
use Magento\Cms\Api\GetBlockByIdentifierInterface;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class Config
|
||||
{
|
||||
private const XML_PATH_PREFIX = 'esun_promotion_popup/general/';
|
||||
|
||||
public function __construct(
|
||||
private readonly ScopeConfigInterface $scopeConfig,
|
||||
private readonly StoreManagerInterface $storeManager,
|
||||
private readonly TimezoneInterface $timezone,
|
||||
private readonly RequestInterface $request,
|
||||
private readonly GetBlockByIdentifierInterface $blockByIdentifier
|
||||
) {
|
||||
}
|
||||
|
||||
public function canDisplay(): bool
|
||||
{
|
||||
if (!$this->isCampaignActive()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !in_array($this->request->getFullActionName(), $this->getExcludedHandles(), true);
|
||||
}
|
||||
|
||||
public function isCampaignActive(): bool
|
||||
{
|
||||
$identifier = $this->getCmsBlockIdentifier();
|
||||
$store = $this->storeManager->getStore();
|
||||
if (!$this->isEnabled()
|
||||
|| $identifier === ''
|
||||
|| !$this->timezone->isScopeDateInInterval(
|
||||
$store,
|
||||
$this->getValue('start_date') ?: null,
|
||||
$this->getValue('end_date') ?: null
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return $this->blockByIdentifier->execute($identifier, (int)$store->getId())->isActive();
|
||||
} catch (NoSuchEntityException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function isRegistrationNewsletterCouponPromotion(): bool
|
||||
{
|
||||
return $this->getValue('promotion_type') === PromotionType::REGISTRATION_NEWSLETTER_COUPON;
|
||||
}
|
||||
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
return $this->scopeConfig->isSetFlag(
|
||||
self::XML_PATH_PREFIX . 'enabled',
|
||||
ScopeInterface::SCOPE_STORE
|
||||
);
|
||||
}
|
||||
|
||||
public function getCmsBlockIdentifier(): string
|
||||
{
|
||||
return trim($this->getValue('cms_block_identifier'));
|
||||
}
|
||||
|
||||
public function getWebsiteId(): int
|
||||
{
|
||||
return (int)$this->storeManager->getStore()->getWebsiteId();
|
||||
}
|
||||
|
||||
public function getDisplayDelay(): int
|
||||
{
|
||||
return min(60000, max(0, (int)$this->getValue('display_delay')));
|
||||
}
|
||||
|
||||
public function getFrequency(): string
|
||||
{
|
||||
$frequency = $this->getValue('frequency');
|
||||
$allowed = [Frequency::ALWAYS, Frequency::SESSION, Frequency::DAILY, Frequency::CAMPAIGN];
|
||||
|
||||
return in_array($frequency, $allowed, true) ? $frequency : Frequency::DAILY;
|
||||
}
|
||||
|
||||
public function getAudience(): string
|
||||
{
|
||||
return $this->getValue('audience') === Audience::ALL ? Audience::ALL : Audience::GUEST;
|
||||
}
|
||||
|
||||
public function getCampaignVersion(): int
|
||||
{
|
||||
return max(1, (int)$this->getValue('campaign_version'));
|
||||
}
|
||||
|
||||
/** @return string[] */
|
||||
private function getExcludedHandles(): array
|
||||
{
|
||||
return array_values(array_unique(array_filter(array_map(
|
||||
static fn(string $handle): string => trim($handle),
|
||||
preg_split('/[\r\n,]+/', $this->getValue('excluded_handles')) ?: []
|
||||
))));
|
||||
}
|
||||
|
||||
private function getValue(string $field): string
|
||||
{
|
||||
return (string)$this->scopeConfig->getValue(
|
||||
self::XML_PATH_PREFIX . $field,
|
||||
ScopeInterface::SCOPE_STORE
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\PromotionPopup\Model\Config\Source;
|
||||
|
||||
use Magento\Framework\Data\OptionSourceInterface;
|
||||
|
||||
class Audience implements OptionSourceInterface
|
||||
{
|
||||
public const GUEST = 'guest';
|
||||
public const ALL = 'all';
|
||||
|
||||
public function toOptionArray(): array
|
||||
{
|
||||
return [
|
||||
['value' => self::GUEST, 'label' => __('Guests Only')],
|
||||
['value' => self::ALL, 'label' => __('All Visitors')],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\PromotionPopup\Model\Config\Source;
|
||||
|
||||
use Magento\Cms\Model\ResourceModel\Block\CollectionFactory;
|
||||
use Magento\Framework\Data\OptionSourceInterface;
|
||||
|
||||
class CmsBlock implements OptionSourceInterface
|
||||
{
|
||||
public function __construct(private readonly CollectionFactory $collectionFactory)
|
||||
{
|
||||
}
|
||||
|
||||
public function toOptionArray(): array
|
||||
{
|
||||
$collection = $this->collectionFactory->create();
|
||||
$collection->addFieldToSelect(['title', 'identifier']);
|
||||
$collection->addFieldToFilter('is_active', 1);
|
||||
$collection->setOrder('title', 'ASC');
|
||||
|
||||
$options = [['value' => '', 'label' => __('-- Please Select --')]];
|
||||
$identifiers = [];
|
||||
foreach ($collection as $block) {
|
||||
$identifier = trim((string)$block->getIdentifier());
|
||||
if ($identifier === '' || isset($identifiers[$identifier])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$identifiers[$identifier] = true;
|
||||
$options[] = [
|
||||
'value' => $identifier,
|
||||
'label' => sprintf('%s [%s]', (string)$block->getTitle(), $identifier),
|
||||
];
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\PromotionPopup\Model\Config\Source;
|
||||
|
||||
use Magento\Framework\Data\OptionSourceInterface;
|
||||
|
||||
class Frequency implements OptionSourceInterface
|
||||
{
|
||||
public const ALWAYS = 'always';
|
||||
public const SESSION = 'session';
|
||||
public const DAILY = 'daily';
|
||||
public const CAMPAIGN = 'campaign';
|
||||
|
||||
public function toOptionArray(): array
|
||||
{
|
||||
return [
|
||||
['value' => self::DAILY, 'label' => __('Once per Day')],
|
||||
['value' => self::SESSION, 'label' => __('Once per Session')],
|
||||
['value' => self::CAMPAIGN, 'label' => __('Once per Campaign Version')],
|
||||
['value' => self::ALWAYS, 'label' => __('Every Eligible Page View')],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\PromotionPopup\Model\Config\Source;
|
||||
|
||||
use Magento\Framework\Data\OptionSourceInterface;
|
||||
|
||||
class PromotionType implements OptionSourceInterface
|
||||
{
|
||||
public const CONTENT_ONLY = 'content_only';
|
||||
public const REGISTRATION_NEWSLETTER_COUPON = 'registration_newsletter_coupon';
|
||||
|
||||
public function toOptionArray(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'value' => self::REGISTRATION_NEWSLETTER_COUPON,
|
||||
'label' => __('Registration + Newsletter Coupon'),
|
||||
],
|
||||
['value' => self::CONTENT_ONLY, 'label' => __('Content Only')],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user