DynamicSegments
1 month ago
RestApi
1 month ago
SegmentDependencyValidator.php
3 years ago
SegmentListingRepository.php
1 month ago
SegmentSaveController.php
2 weeks ago
SegmentSubscribersRepository.php
2 weeks ago
SegmentsFinder.php
3 years ago
SegmentsRepository.php
2 weeks ago
SegmentsSimpleListRepository.php
2 months ago
SubscribersFinder.php
1 month ago
WP.php
2 weeks ago
WooCommerce.php
2 weeks ago
index.php
3 years ago
SegmentListingRepository.php
131 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Segments; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\SegmentEntity; |
| 9 | use MailPoet\Listing\ListingDateRangeFilterTrait; |
| 10 | use MailPoet\Listing\ListingDefinition; |
| 11 | use MailPoet\Listing\ListingRepository; |
| 12 | use MailPoet\Util\Helpers; |
| 13 | use MailPoetVendor\Doctrine\ORM\EntityManager; |
| 14 | use MailPoetVendor\Doctrine\ORM\QueryBuilder; |
| 15 | |
| 16 | class SegmentListingRepository extends ListingRepository { |
| 17 | use ListingDateRangeFilterTrait; |
| 18 | |
| 19 | const DEFAULT_SORT_BY = 'name'; |
| 20 | |
| 21 | /** @var WooCommerce */ |
| 22 | private $wooCommerce; |
| 23 | |
| 24 | public function __construct( |
| 25 | EntityManager $entityManager, |
| 26 | WooCommerce $wooCommerce |
| 27 | ) { |
| 28 | parent::__construct($entityManager); |
| 29 | $this->wooCommerce = $wooCommerce; |
| 30 | } |
| 31 | |
| 32 | protected function applySelectClause(QueryBuilder $queryBuilder) { |
| 33 | $queryBuilder->select("PARTIAL s.{id,name,type,description,publicDescription,createdAt,updatedAt,deletedAt,averageEngagementScore,displayInManageSubscriptionPage}"); |
| 34 | } |
| 35 | |
| 36 | protected function applyFromClause(QueryBuilder $queryBuilder) { |
| 37 | $queryBuilder->from(SegmentEntity::class, 's'); |
| 38 | } |
| 39 | |
| 40 | protected function applyGroup(QueryBuilder $queryBuilder, string $group) { |
| 41 | if ($group === 'trash') { |
| 42 | $queryBuilder->andWhere('s.deletedAt IS NOT NULL'); |
| 43 | } else { |
| 44 | $queryBuilder->andWhere('s.deletedAt IS NULL'); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | protected function applySearch(QueryBuilder $queryBuilder, string $search, array $parameters = []) { |
| 49 | $search = Helpers::escapeSearch($search); |
| 50 | $queryBuilder |
| 51 | ->andWhere('s.name LIKE :search or s.description LIKE :search') |
| 52 | ->setParameter('search', "%$search%"); |
| 53 | } |
| 54 | |
| 55 | protected function applyFilters(QueryBuilder $queryBuilder, array $filters) { |
| 56 | $this->applyDateRangeFilter($queryBuilder, 's.createdAt', $filters, 'created_from', 'created_to'); |
| 57 | $this->applyScoreFilter($queryBuilder, $filters); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param array<string, mixed> $filters |
| 62 | */ |
| 63 | private function applyScoreFilter(QueryBuilder $queryBuilder, array $filters): void { |
| 64 | $min = $filters['score_min'] ?? null; |
| 65 | if (is_numeric($min)) { |
| 66 | $queryBuilder |
| 67 | ->andWhere('s.averageEngagementScore >= :scoreMin') |
| 68 | ->setParameter('scoreMin', (float)$min); |
| 69 | } |
| 70 | $max = $filters['score_max'] ?? null; |
| 71 | if (is_numeric($max)) { |
| 72 | $queryBuilder |
| 73 | ->andWhere('s.averageEngagementScore <= :scoreMax') |
| 74 | ->setParameter('scoreMax', (float)$max); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | protected function applyParameters(QueryBuilder $queryBuilder, array $parameters) { |
| 79 | $types = [SegmentEntity::TYPE_DEFAULT, SegmentEntity::TYPE_WP_USERS]; |
| 80 | if ($this->wooCommerce->shouldShowWooCommerceSegment()) { |
| 81 | $types[] = SegmentEntity::TYPE_WC_USERS; |
| 82 | } |
| 83 | $queryBuilder |
| 84 | ->andWhere('s.type IN (:type)') |
| 85 | ->setParameter('type', $types); |
| 86 | } |
| 87 | |
| 88 | protected function applySorting(QueryBuilder $queryBuilder, string $sortBy, string $sortOrder) { |
| 89 | if (!$sortBy) { |
| 90 | $sortBy = self::DEFAULT_SORT_BY; |
| 91 | } |
| 92 | $queryBuilder->addOrderBy("s.$sortBy", $sortOrder); |
| 93 | } |
| 94 | |
| 95 | public function getGroups(ListingDefinition $definition): array { |
| 96 | $queryBuilder = clone $this->queryBuilder; |
| 97 | $this->applyFromClause($queryBuilder); |
| 98 | $this->applyParameters($queryBuilder, $definition->getParameters()); |
| 99 | |
| 100 | $queryBuilder->select('count(s.id)'); |
| 101 | |
| 102 | if (!$this->wooCommerce->shouldShowWooCommerceSegment()) { |
| 103 | $queryBuilder |
| 104 | ->andWhere('s.type != :wcUsers') |
| 105 | ->setParameter('wcUsers', SegmentEntity::TYPE_WC_USERS); |
| 106 | } |
| 107 | |
| 108 | $allQueryBuilder = clone $queryBuilder; |
| 109 | $trashedQueryBuilder = clone $queryBuilder; |
| 110 | |
| 111 | $allQueryBuilder->andWhere('s.deletedAt IS NULL'); |
| 112 | $allCount = (int)$allQueryBuilder->getQuery()->getSingleScalarResult(); |
| 113 | |
| 114 | $trashedQueryBuilder->andWhere('s.deletedAt IS NOT NULL'); |
| 115 | $trashedCount = (int)$trashedQueryBuilder->getQuery()->getSingleScalarResult(); |
| 116 | |
| 117 | return [ |
| 118 | [ |
| 119 | 'name' => 'all', |
| 120 | 'label' => __('All', 'mailpoet'), |
| 121 | 'count' => $allCount, |
| 122 | ], |
| 123 | [ |
| 124 | 'name' => 'trash', |
| 125 | 'label' => __('Trash', 'mailpoet'), |
| 126 | 'count' => $trashedCount, |
| 127 | ], |
| 128 | ]; |
| 129 | } |
| 130 | } |
| 131 |