NewsletterLinkFieldsFactory.php
2 years ago
SubscriberAutomationFieldsFactory.php
1 year ago
SubscriberCustomFieldsFactory.php
2 months ago
SubscriberFieldsFactory.php
2 months ago
SubscriberStatisticFieldsFactory.php
2 years ago
index.php
3 years ago
SubscriberFieldsFactory.php
264 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet\Fields; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Field; |
| 9 | use MailPoet\Automation\Integrations\MailPoet\Payloads\SubscriberPayload; |
| 10 | use MailPoet\Entities\SegmentEntity; |
| 11 | use MailPoet\Entities\SubscriberEntity; |
| 12 | use MailPoet\Segments\SegmentsFinder; |
| 13 | use MailPoet\Segments\SegmentsRepository; |
| 14 | use MailPoet\Tags\TagRepository; |
| 15 | |
| 16 | class SubscriberFieldsFactory { |
| 17 | /** @var SegmentsFinder */ |
| 18 | private $segmentsFinder; |
| 19 | |
| 20 | /** @var SegmentsRepository */ |
| 21 | private $segmentsRepository; |
| 22 | |
| 23 | /** @var SubscriberAutomationFieldsFactory */ |
| 24 | private $automationFieldsFactory; |
| 25 | |
| 26 | /** @var SubscriberCustomFieldsFactory */ |
| 27 | private $customFieldsFactory; |
| 28 | |
| 29 | /** @var TagRepository */ |
| 30 | private $tagRepository; |
| 31 | |
| 32 | /** @var SubscriberStatisticFieldsFactory */ |
| 33 | private $statisticFieldsFactory; |
| 34 | |
| 35 | public function __construct( |
| 36 | SegmentsFinder $segmentsFinder, |
| 37 | SegmentsRepository $segmentsRepository, |
| 38 | SubscriberAutomationFieldsFactory $automationFieldsFactory, |
| 39 | SubscriberCustomFieldsFactory $customFieldsFactory, |
| 40 | SubscriberStatisticFieldsFactory $statisticFieldsFactory, |
| 41 | TagRepository $tagRepository |
| 42 | ) { |
| 43 | $this->segmentsFinder = $segmentsFinder; |
| 44 | $this->segmentsRepository = $segmentsRepository; |
| 45 | $this->automationFieldsFactory = $automationFieldsFactory; |
| 46 | $this->customFieldsFactory = $customFieldsFactory; |
| 47 | $this->statisticFieldsFactory = $statisticFieldsFactory; |
| 48 | $this->tagRepository = $tagRepository; |
| 49 | } |
| 50 | |
| 51 | /** @return Field[] */ |
| 52 | public function getFields(): array { |
| 53 | return array_merge( |
| 54 | $this->customFieldsFactory->getFields(), |
| 55 | [ |
| 56 | new Field( |
| 57 | 'mailpoet:subscriber:email', |
| 58 | Field::TYPE_STRING, |
| 59 | __('Email address', 'mailpoet'), |
| 60 | function (SubscriberPayload $payload) { |
| 61 | return $payload->getEmail(); |
| 62 | } |
| 63 | ), |
| 64 | new Field( |
| 65 | 'mailpoet:subscriber:engagement-score', |
| 66 | Field::TYPE_NUMBER, |
| 67 | __('Engagement score', 'mailpoet'), |
| 68 | function (SubscriberPayload $payload) { |
| 69 | return $payload->getSubscriber()->getEngagementScore(); |
| 70 | } |
| 71 | ), |
| 72 | new Field( |
| 73 | 'mailpoet:subscriber:first-name', |
| 74 | Field::TYPE_STRING, |
| 75 | __('First name', 'mailpoet'), |
| 76 | function (SubscriberPayload $payload) { |
| 77 | return $payload->getSubscriber()->getFirstName(); |
| 78 | } |
| 79 | ), |
| 80 | new Field( |
| 81 | 'mailpoet:subscriber:last-name', |
| 82 | Field::TYPE_STRING, |
| 83 | __('Last name', 'mailpoet'), |
| 84 | function (SubscriberPayload $payload) { |
| 85 | return $payload->getSubscriber()->getLastName(); |
| 86 | } |
| 87 | ), |
| 88 | new Field( |
| 89 | 'mailpoet:subscriber:is-globally-subscribed', |
| 90 | Field::TYPE_BOOLEAN, |
| 91 | __('Is globally subscribed', 'mailpoet'), |
| 92 | function (SubscriberPayload $payload) { |
| 93 | return $payload->getStatus() === SubscriberEntity::STATUS_SUBSCRIBED; |
| 94 | } |
| 95 | ), |
| 96 | new Field( |
| 97 | 'mailpoet:subscriber:last-engagement-at', |
| 98 | Field::TYPE_DATETIME, |
| 99 | __('Last engaged', 'mailpoet'), |
| 100 | function (SubscriberPayload $payload) { |
| 101 | return $payload->getSubscriber()->getLastEngagementAt(); |
| 102 | } |
| 103 | ), |
| 104 | new Field( |
| 105 | 'mailpoet:subscriber:status', |
| 106 | Field::TYPE_ENUM, |
| 107 | __('Status', 'mailpoet'), |
| 108 | function (SubscriberPayload $payload) { |
| 109 | return $payload->getStatus(); |
| 110 | }, |
| 111 | [ |
| 112 | 'options' => [ |
| 113 | [ |
| 114 | 'id' => SubscriberEntity::STATUS_SUBSCRIBED, |
| 115 | 'name' => __('Subscribed', 'mailpoet'), |
| 116 | ], |
| 117 | [ |
| 118 | 'id' => SubscriberEntity::STATUS_UNCONFIRMED, |
| 119 | 'name' => __('Unconfirmed', 'mailpoet'), |
| 120 | ], |
| 121 | [ |
| 122 | 'id' => SubscriberEntity::STATUS_UNSUBSCRIBED, |
| 123 | 'name' => __('Unsubscribed', 'mailpoet'), |
| 124 | ], |
| 125 | [ |
| 126 | 'id' => SubscriberEntity::STATUS_INACTIVE, |
| 127 | 'name' => __('Inactive', 'mailpoet'), |
| 128 | ], |
| 129 | [ |
| 130 | 'id' => SubscriberEntity::STATUS_BOUNCED, |
| 131 | 'name' => __('Bounced', 'mailpoet'), |
| 132 | ], |
| 133 | ], |
| 134 | ] |
| 135 | ), |
| 136 | new Field( |
| 137 | 'mailpoet:subscriber:subscription-source', |
| 138 | Field::TYPE_ENUM, |
| 139 | __('Subscription source', 'mailpoet'), |
| 140 | function (SubscriberPayload $payload) { |
| 141 | return $payload->getSubscriber()->getSource(); |
| 142 | }, |
| 143 | [ |
| 144 | 'options' => [ |
| 145 | [ |
| 146 | 'id' => 'api', |
| 147 | 'name' => __('API', 'mailpoet'), |
| 148 | ], |
| 149 | [ |
| 150 | 'id' => 'form', |
| 151 | 'name' => __('Form', 'mailpoet'), |
| 152 | ], |
| 153 | [ |
| 154 | 'id' => 'unknown', |
| 155 | 'name' => __('Unknown', 'mailpoet'), |
| 156 | ], |
| 157 | [ |
| 158 | 'id' => 'imported', |
| 159 | 'name' => __('Imported', 'mailpoet'), |
| 160 | ], |
| 161 | [ |
| 162 | 'id' => 'administrator', |
| 163 | 'name' => __('Administrator', 'mailpoet'), |
| 164 | ], |
| 165 | [ |
| 166 | 'id' => 'wordpress_user', |
| 167 | 'name' => __('WordPress user', 'mailpoet'), |
| 168 | ], |
| 169 | [ |
| 170 | 'id' => 'wordpress_user_deleted', |
| 171 | 'name' => __('WordPress user (deleted)', 'mailpoet'), |
| 172 | ], |
| 173 | [ |
| 174 | 'id' => 'woocommerce_user', |
| 175 | 'name' => __('WooCommerce user', 'mailpoet'), |
| 176 | ], |
| 177 | [ |
| 178 | 'id' => 'woocommerce_checkout', |
| 179 | 'name' => __('WooCommerce checkout', 'mailpoet'), |
| 180 | ], |
| 181 | ], |
| 182 | ] |
| 183 | ), |
| 184 | new Field( |
| 185 | 'mailpoet:subscriber:last-subscribed-at', |
| 186 | Field::TYPE_DATETIME, |
| 187 | __('Subscribed date', 'mailpoet'), |
| 188 | function (SubscriberPayload $payload) { |
| 189 | return $payload->getSubscriber()->getLastSubscribedAt(); |
| 190 | } |
| 191 | ), |
| 192 | new Field( |
| 193 | 'mailpoet:subscriber:lists', |
| 194 | Field::TYPE_ENUM_ARRAY, |
| 195 | __('Subscribed lists', 'mailpoet'), |
| 196 | function (SubscriberPayload $payload) { |
| 197 | $value = []; |
| 198 | foreach ($payload->getSubscriber()->getSegments() as $list) { |
| 199 | if ($list->getType() !== SegmentEntity::TYPE_DYNAMIC) { |
| 200 | $value[] = $list->getId(); |
| 201 | } |
| 202 | } |
| 203 | return $value; |
| 204 | }, |
| 205 | [ |
| 206 | 'options' => array_map(function ($segment) { |
| 207 | return [ |
| 208 | 'id' => $segment->getId(), |
| 209 | 'name' => $segment->getName(), |
| 210 | ]; |
| 211 | }, $this->segmentsRepository->findByTypeNotIn([SegmentEntity::TYPE_DYNAMIC])), |
| 212 | ] |
| 213 | ), |
| 214 | new Field( |
| 215 | 'mailpoet:subscriber:tags', |
| 216 | Field::TYPE_ENUM_ARRAY, |
| 217 | __('Tags', 'mailpoet'), |
| 218 | function (SubscriberPayload $payload) { |
| 219 | $value = []; |
| 220 | foreach ($payload->getSubscriber()->getSubscriberTags() as $subscriberTag) { |
| 221 | $tag = $subscriberTag->getTag(); |
| 222 | if ($tag) { |
| 223 | $value[] = $tag->getId(); |
| 224 | } |
| 225 | } |
| 226 | return $value; |
| 227 | }, |
| 228 | [ |
| 229 | 'options' => array_map(function ($tag) { |
| 230 | return [ |
| 231 | 'id' => $tag->getId(), |
| 232 | 'name' => $tag->getName(), |
| 233 | ]; |
| 234 | }, $this->tagRepository->findAll()), |
| 235 | ] |
| 236 | ), |
| 237 | new Field( |
| 238 | 'mailpoet:subscriber:segments', |
| 239 | Field::TYPE_ENUM_ARRAY, |
| 240 | __('Segments', 'mailpoet'), |
| 241 | function (SubscriberPayload $payload) { |
| 242 | $segments = $this->segmentsFinder->findDynamicSegments($payload->getSubscriber()); |
| 243 | $value = []; |
| 244 | foreach ($segments as $segment) { |
| 245 | $value[] = $segment->getId(); |
| 246 | } |
| 247 | return $value; |
| 248 | }, |
| 249 | [ |
| 250 | 'options' => array_map(function ($segment) { |
| 251 | return [ |
| 252 | 'id' => $segment->getId(), |
| 253 | 'name' => $segment->getName(), |
| 254 | ]; |
| 255 | }, $this->segmentsRepository->findBy(['type' => SegmentEntity::TYPE_DYNAMIC])), |
| 256 | ] |
| 257 | ), |
| 258 | ], |
| 259 | $this->statisticFieldsFactory->getFields(), |
| 260 | $this->automationFieldsFactory->getFields() |
| 261 | ); |
| 262 | } |
| 263 | } |
| 264 |