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
SegmentsSimpleListRepository.php
124 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\Entities\SubscriberEntity; |
| 10 | use MailPoet\Subscribers\SubscribersCountsController; |
| 11 | use MailPoetVendor\Doctrine\DBAL\ArrayParameterType; |
| 12 | use MailPoetVendor\Doctrine\DBAL\Result; |
| 13 | use MailPoetVendor\Doctrine\ORM\EntityManager; |
| 14 | |
| 15 | class SegmentsSimpleListRepository { |
| 16 | /** @var EntityManager */ |
| 17 | private $entityManager; |
| 18 | |
| 19 | /** @var SubscribersCountsController */ |
| 20 | private $subscribersCountsController; |
| 21 | |
| 22 | public function __construct( |
| 23 | EntityManager $entityManager, |
| 24 | SubscribersCountsController $subscribersCountsController |
| 25 | ) { |
| 26 | $this->entityManager = $entityManager; |
| 27 | $this->subscribersCountsController = $subscribersCountsController; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * This fetches list of all segments basic data and count of subscribed subscribers. |
| 32 | * @return array<array{id: string, name: string, type: string, subscribers: int}> |
| 33 | */ |
| 34 | public function getListWithSubscribedSubscribersCounts(array $segmentTypes = []): array { |
| 35 | return $this->getList( |
| 36 | $segmentTypes, |
| 37 | SubscriberEntity::STATUS_SUBSCRIBED |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * This fetches list of all segments basic data and count of subscribers associated to a segment regardless their subscription status. |
| 43 | * @return array<array{id: string, name: string, type: string, subscribers: int}> |
| 44 | */ |
| 45 | public function getListWithAssociatedSubscribersCounts(array $segmentTypes = []): array { |
| 46 | return $this->getList( |
| 47 | $segmentTypes |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Adds a virtual segment with for subscribers without list |
| 53 | * @return array<array{id: string, name: string, type: string, subscribers: int}> |
| 54 | */ |
| 55 | public function addVirtualSubscribersWithoutListSegment(array $segments): array { |
| 56 | $withoutSegmentStats = $this->subscribersCountsController->getSubscribersWithoutSegmentStatisticsCount(); |
| 57 | $segments[] = [ |
| 58 | 'id' => '0', |
| 59 | 'type' => SegmentEntity::TYPE_WITHOUT_LIST, |
| 60 | 'name' => __('Subscribers without a list', 'mailpoet'), |
| 61 | 'subscribers' => $withoutSegmentStats['all'], |
| 62 | ]; |
| 63 | return $segments; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @return array<array{id: string, name: string, type: string, subscribers: int}> |
| 68 | */ |
| 69 | private function getList( |
| 70 | array $segmentTypes = [], |
| 71 | ?string $subscriberGlobalStatus = null |
| 72 | ): array { |
| 73 | $segmentsTable = $this->entityManager->getClassMetadata(SegmentEntity::class)->getTableName(); |
| 74 | |
| 75 | $segmentsDataQuery = $this->entityManager |
| 76 | ->getConnection() |
| 77 | ->createQueryBuilder(); |
| 78 | |
| 79 | $segmentsDataQuery->select( |
| 80 | "segments.id, segments.name, segments.type" |
| 81 | )->from($segmentsTable, 'segments') |
| 82 | ->where('segments.deleted_at IS NULL') |
| 83 | ->orderBy('segments.name'); |
| 84 | |
| 85 | if (!empty($segmentTypes)) { |
| 86 | $segmentsDataQuery |
| 87 | ->andWhere('segments.type IN (:typesParam)') |
| 88 | ->setParameter('typesParam', $segmentTypes, ArrayParameterType::STRING); |
| 89 | } |
| 90 | |
| 91 | $result = $segmentsDataQuery->executeQuery(); |
| 92 | if (!$result instanceof Result) { |
| 93 | return []; |
| 94 | } |
| 95 | $segments = $result->fetchAll(); |
| 96 | |
| 97 | // Fetch subscribers counts for static and dynamic segments and correct data types |
| 98 | $statisticsKey = $subscriberGlobalStatus ?: 'all'; |
| 99 | /** @var array<array{id: string, name: string, type: string, subscribers: int}> $normalized */ |
| 100 | $normalized = []; |
| 101 | foreach ($segments as $segment) { |
| 102 | if (!is_array($segment)) { |
| 103 | continue; |
| 104 | } |
| 105 | // BC compatibility fix. PHP8.1+ returns integer but JS apps expect string |
| 106 | $id = is_scalar($segment['id'] ?? null) ? (string)$segment['id'] : ''; |
| 107 | $subscribers = 0; |
| 108 | if (is_numeric($id) && (int)$id > 0) { |
| 109 | $stats = $this->subscribersCountsController->getSegmentStatisticsCountById((int)$id); |
| 110 | $subscribers = isset($stats[$statisticsKey]) && is_numeric($stats[$statisticsKey]) ? (int)$stats[$statisticsKey] : 0; |
| 111 | } |
| 112 | $segment['id'] = $id; |
| 113 | $segment['subscribers'] = $subscribers; |
| 114 | $normalized[] = [ |
| 115 | 'id' => $id, |
| 116 | 'name' => is_string($segment['name'] ?? null) ? $segment['name'] : '', |
| 117 | 'type' => is_string($segment['type'] ?? null) ? $segment['type'] : '', |
| 118 | 'subscribers' => $subscribers, |
| 119 | ]; |
| 120 | } |
| 121 | return $normalized; |
| 122 | } |
| 123 | } |
| 124 |