Display a message when WooCommerce is disabled
[MAILPOET-3477]
This commit is contained in:
@ -91,6 +91,21 @@ const itemActions = [
|
||||
{MailPoet.I18n.t('edit')}
|
||||
</Link>
|
||||
),
|
||||
display: (item) => (
|
||||
!item.is_plugin_missing
|
||||
),
|
||||
},
|
||||
{
|
||||
name: 'edit_disabled',
|
||||
className: 'mailpoet-hide-on-mobile mailpoet-disabled',
|
||||
link: (item) => (
|
||||
<Link to={`/edit-segment/${item.id}`}>
|
||||
{MailPoet.I18n.t('edit')}
|
||||
</Link>
|
||||
),
|
||||
display: (item) => (
|
||||
item.is_plugin_missing
|
||||
),
|
||||
},
|
||||
{
|
||||
name: 'view_subscribers',
|
||||
@ -126,12 +141,22 @@ function renderItem(item, actions) {
|
||||
<td data-colname={MailPoet.I18n.t('description')}>
|
||||
<abbr>{ item.description }</abbr>
|
||||
</td>
|
||||
{ item.is_plugin_missing
|
||||
? (
|
||||
<td colSpan="2" className="column mailpoet-hide-on-mobile" data-colname={MailPoet.I18n.t('missingPluginMessageColumn')}>
|
||||
{ item.missing_plugin_message }
|
||||
</td>
|
||||
)
|
||||
: (
|
||||
<>
|
||||
<td className="column mailpoet-hide-on-mobile" data-colname={MailPoet.I18n.t('subscribersCountColumn')}>
|
||||
{ parseInt(item.count_all, 10).toLocaleString() }
|
||||
</td>
|
||||
<td className="column mailpoet-hide-on-mobile" data-colname={MailPoet.I18n.t('subscribed')}>
|
||||
{ parseInt(item.count_subscribed, 10).toLocaleString() }
|
||||
</td>
|
||||
</>
|
||||
)}
|
||||
<td className="column-date mailpoet-hide-on-mobile" data-colname={MailPoet.I18n.t('updatedAtColumn')}>
|
||||
{ MailPoet.Date.short(item.created_at) }
|
||||
<br />
|
||||
|
@ -5,6 +5,7 @@ namespace MailPoet\API\JSON\ResponseBuilders;
|
||||
use MailPoet\Entities\DynamicSegmentFilterEntity;
|
||||
use MailPoet\Entities\SegmentEntity;
|
||||
use MailPoet\Entities\SubscriberEntity;
|
||||
use MailPoet\Segments\SegmentDependencyValidator;
|
||||
use MailPoet\Segments\SegmentSubscribersRepository;
|
||||
use MailPoet\WP\Functions;
|
||||
|
||||
@ -20,18 +21,24 @@ class DynamicSegmentsResponseBuilder {
|
||||
/** @var SegmentSubscribersRepository */
|
||||
private $segmentSubscriberRepository;
|
||||
|
||||
/** @var SegmentDependencyValidator */
|
||||
private $segmentDependencyValidator;
|
||||
|
||||
public function __construct(
|
||||
Functions $wp,
|
||||
SegmentSubscribersRepository $segmentSubscriberRepository,
|
||||
SegmentsResponseBuilder $segmentsResponseBuilder
|
||||
SegmentsResponseBuilder $segmentsResponseBuilder,
|
||||
SegmentDependencyValidator $segmentDependencyValidator
|
||||
) {
|
||||
$this->segmentsResponseBuilder = $segmentsResponseBuilder;
|
||||
$this->segmentSubscriberRepository = $segmentSubscriberRepository;
|
||||
$this->wp = $wp;
|
||||
$this->segmentDependencyValidator = $segmentDependencyValidator;
|
||||
}
|
||||
|
||||
public function build(SegmentEntity $segmentEntity) {
|
||||
$data = $this->segmentsResponseBuilder->build($segmentEntity);
|
||||
$data = $this->addMissingPluginProperties($segmentEntity, $data);
|
||||
// So far we allow dynamic segments to have only one filter
|
||||
$filter = $segmentEntity->getDynamicFilters()->first();
|
||||
if (!$filter instanceof DynamicSegmentFilterEntity) {
|
||||
@ -48,11 +55,29 @@ class DynamicSegmentsResponseBuilder {
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function addMissingPluginProperties(SegmentEntity $segment, array $data): array {
|
||||
$missingPlugins = $this->segmentDependencyValidator->getMissingPluginsBySegment($segment);
|
||||
if ($missingPlugins) {
|
||||
$missingPlugin = reset($missingPlugins);
|
||||
$data['is_plugin_missing'] = true;
|
||||
$data['missing_plugin_message'] = sprintf(
|
||||
__('Activate the %s plugin to see the number of subscribers and enable the editing of this segment.', 'mailpoet'),
|
||||
$missingPlugin
|
||||
);
|
||||
} else {
|
||||
$data['is_plugin_missing'] = false;
|
||||
$data['missing_plugin_message'] = null;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function buildListingItem(SegmentEntity $segment): array {
|
||||
$data = $this->segmentsResponseBuilder->build($segment);
|
||||
$data = $this->addMissingPluginProperties($segment, $data);
|
||||
$data['subscribers_url'] = $this->wp->adminUrl(
|
||||
'admin.php?page=mailpoet-subscribers#/filter[segment=' . $segment->getId() . ']'
|
||||
);
|
||||
|
||||
$data['count_all'] = $this->segmentSubscriberRepository->getSubscribersCount((int)$segment->getId());
|
||||
$data['count_subscribed'] = $this->segmentSubscriberRepository->getSubscribersCount((int)$segment->getId(), SubscriberEntity::STATUS_SUBSCRIBED);
|
||||
return $data;
|
||||
|
@ -261,6 +261,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
$container->autowire(\MailPoet\Segments\SegmentListingRepository::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Segments\SegmentsSimpleListRepository::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Segments\SegmentSaveController::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Segments\SegmentDependencyValidator::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Segments\DynamicSegments\DynamicSegmentsListingRepository::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Segments\DynamicSegments\FilterHandler::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Segments\DynamicSegments\Filters\EmailAction::class)->setPublic(true);
|
||||
|
@ -11,6 +11,7 @@ use MailPoet\Segments\DynamicSegments\Filters\EmailAction;
|
||||
use MailPoet\Segments\DynamicSegments\Filters\UserRole;
|
||||
use MailPoet\Segments\DynamicSegments\Filters\WooCommerceCategory;
|
||||
use MailPoet\Segments\DynamicSegments\Filters\WooCommerceProduct;
|
||||
use MailPoet\Segments\SegmentDependencyValidator;
|
||||
use MailPoetVendor\Doctrine\DBAL\Query\QueryBuilder;
|
||||
use MailPoetVendor\Doctrine\ORM\EntityManager;
|
||||
|
||||
@ -30,18 +31,23 @@ class FilterHandler {
|
||||
/** @var EntityManager */
|
||||
private $entityManager;
|
||||
|
||||
/** @var SegmentDependencyValidator */
|
||||
private $segmentDependencyValidator;
|
||||
|
||||
public function __construct(
|
||||
EntityManager $entityManager,
|
||||
EmailAction $emailAction,
|
||||
UserRole $userRole,
|
||||
WooCommerceProduct $wooCommerceProduct,
|
||||
WooCommerceCategory $wooCommerceCategory
|
||||
WooCommerceCategory $wooCommerceCategory,
|
||||
SegmentDependencyValidator $segmentDependencyValidator
|
||||
) {
|
||||
$this->emailAction = $emailAction;
|
||||
$this->userRole = $userRole;
|
||||
$this->wooCommerceProduct = $wooCommerceProduct;
|
||||
$this->wooCommerceCategory = $wooCommerceCategory;
|
||||
$this->entityManager = $entityManager;
|
||||
$this->segmentDependencyValidator = $segmentDependencyValidator;
|
||||
}
|
||||
|
||||
public function apply(QueryBuilder $queryBuilder, SegmentEntity $segment): QueryBuilder {
|
||||
@ -54,7 +60,12 @@ class FilterHandler {
|
||||
->createQueryBuilder()
|
||||
->select("DISTINCT $subscribersTable.id as inner_subscriber_id")
|
||||
->from($subscribersTable);
|
||||
// When a required plugin is missing we want to return empty result
|
||||
if ($this->segmentDependencyValidator->getMissingPluginByFilter($filter)) {
|
||||
$subscribersIdsQuery->andWhere('1 = 0');
|
||||
} else {
|
||||
$this->applyFilter($subscribersIdsQuery, $filter);
|
||||
}
|
||||
$filterSelects[] = $subscribersIdsQuery->getSQL();
|
||||
$queryBuilder->setParameters(array_merge(
|
||||
$subscribersIdsQuery->getParameters(),
|
||||
|
55
lib/Segments/SegmentDependencyValidator.php
Normal file
55
lib/Segments/SegmentDependencyValidator.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Segments;
|
||||
|
||||
use MailPoet\Entities\DynamicSegmentFilterData;
|
||||
use MailPoet\Entities\DynamicSegmentFilterEntity;
|
||||
use MailPoet\Entities\SegmentEntity;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class SegmentDependencyValidator {
|
||||
private const REQUIRED_PLUGINS_BY_TYPE = [
|
||||
DynamicSegmentFilterData::TYPE_WOOCOMMERCE => [
|
||||
'id' => 'woocommerce/woocommerce.php',
|
||||
'name' => 'WooCommerce',
|
||||
],
|
||||
];
|
||||
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
public function __construct(WPFunctions $wp) {
|
||||
$this->wp = $wp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getMissingPluginsBySegment(SegmentEntity $segment): array {
|
||||
$missingPlugins = [];
|
||||
foreach ($segment->getDynamicFilters() as $dynamicFilter) {
|
||||
$missingPlugin = $this->getMissingPluginByFilter($dynamicFilter);
|
||||
if (!$missingPlugin) {
|
||||
continue;
|
||||
}
|
||||
$missingPlugins[] = $missingPlugin['name'];
|
||||
}
|
||||
return $missingPlugins;
|
||||
}
|
||||
|
||||
public function getMissingPluginByFilter(DynamicSegmentFilterEntity $dynamicSegmentFilter): ?array {
|
||||
$requiredPlugin = $this->getRequiredPluginName($dynamicSegmentFilter);
|
||||
if (isset($requiredPlugin['id']) && !$this->wp->isPluginActive($requiredPlugin['id'])) {
|
||||
return $requiredPlugin;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private function getRequiredPluginName(DynamicSegmentFilterEntity $dynamicSegmentFilter): ?array {
|
||||
if (isset(self::REQUIRED_PLUGINS_BY_TYPE[$dynamicSegmentFilter->getFilterData()->getFilterType()])) {
|
||||
return self::REQUIRED_PLUGINS_BY_TYPE[$dynamicSegmentFilter->getFilterData()->getFilterType()];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -117,6 +117,7 @@
|
||||
'nameColumn': __('Name'),
|
||||
'subscribersCountColumn': __('Number of subscribers'),
|
||||
'updatedAtColumn': __('Modified on'),
|
||||
'missingPluginMessageColumn': __('Missing plugin message'),
|
||||
'loadingDynamicSegmentItems': __('Loading data…'),
|
||||
'noDynamicSegmentItemsFound': __('No segments found'),
|
||||
'numberOfItemsSingular': __('1 item'),
|
||||
|
Reference in New Issue
Block a user