CustomFieldEntity.php
2 months ago
DynamicSegmentFilterData.php
2 months ago
DynamicSegmentFilterEntity.php
3 years ago
FeatureFlagEntity.php
3 years ago
FormEntity.php
1 week ago
LogEntity.php
3 years ago
NewsletterEntity.php
2 months ago
NewsletterLinkEntity.php
3 years ago
NewsletterOptionEntity.php
3 years ago
NewsletterOptionFieldEntity.php
1 month ago
NewsletterPostEntity.php
3 years ago
NewsletterSegmentEntity.php
3 years ago
NewsletterTemplateEntity.php
3 years ago
ScheduledTaskEntity.php
1 month ago
ScheduledTaskSubscriberEntity.php
1 year ago
SegmentEntity.php
2 months ago
SendingQueueEntity.php
2 months ago
SettingEntity.php
3 years ago
StatisticsBounceEntity.php
3 years ago
StatisticsClickEntity.php
3 years ago
StatisticsFormEntity.php
3 years ago
StatisticsNewsletterEntity.php
1 year ago
StatisticsOpenEntity.php
3 years ago
StatisticsUnsubscribeEntity.php
2 months ago
StatisticsWooCommercePurchaseEntity.php
2 years ago
StatsNotificationEntity.php
3 years ago
SubscriberCustomFieldEntity.php
3 years ago
SubscriberEntity.php
4 days ago
SubscriberIPEntity.php
3 years ago
SubscriberSegmentEntity.php
3 years ago
SubscriberTagEntity.php
4 years ago
TagEntity.php
3 years ago
UserAgentEntity.php
3 years ago
UserFlagEntity.php
3 years ago
WpPostEntity.php
2 years ago
index.php
3 years ago
DynamicSegmentFilterData.php
172 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Entities; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\InvalidStateException; |
| 9 | use MailPoet\Segments\DynamicSegments\Filters\UserRole; |
| 10 | use MailPoet\Segments\DynamicSegments\Filters\WooCommerceProduct; |
| 11 | use MailPoetVendor\Doctrine\ORM\Mapping as ORM; |
| 12 | |
| 13 | /** |
| 14 | * @ORM\Embeddable() |
| 15 | */ |
| 16 | class DynamicSegmentFilterData { |
| 17 | const TYPE_AUTOMATIONS = 'automations'; |
| 18 | const TYPE_USER_ROLE = 'userRole'; |
| 19 | const TYPE_EMAIL = 'email'; |
| 20 | const TYPE_WOOCOMMERCE = 'woocommerce'; |
| 21 | const TYPE_WOOCOMMERCE_MEMBERSHIP = 'woocommerceMembership'; |
| 22 | const TYPE_WOOCOMMERCE_SUBSCRIPTION = 'woocommerceSubscription'; |
| 23 | |
| 24 | public const CONNECT_TYPE_AND = 'and'; |
| 25 | public const CONNECT_TYPE_OR = 'or'; |
| 26 | public const CONNECT_TYPE_NONE = 'none'; |
| 27 | |
| 28 | public const OPERATOR_ALL = 'all'; |
| 29 | public const OPERATOR_ANY = 'any'; |
| 30 | public const OPERATOR_NONE = 'none'; |
| 31 | |
| 32 | public const OPERATOR_STARTS_WITH = 'startsWith'; |
| 33 | public const OPERATOR_NOT_ENDS_WITH = 'notEndsWith'; |
| 34 | public const OPERATOR_IS = 'is'; |
| 35 | public const OPERATOR_CONTAINS = 'contains'; |
| 36 | public const OPERATOR_NOT_CONTAINS = 'notContains'; |
| 37 | public const OPERATOR_NOT_STARTS_WITH = 'notStartsWith'; |
| 38 | public const OPERATOR_IS_NOT = 'isNot'; |
| 39 | public const OPERATOR_ENDS_WITH = 'endsWith'; |
| 40 | public const TEXT_FIELD_OPERATORS = [ |
| 41 | DynamicSegmentFilterData::OPERATOR_IS, |
| 42 | DynamicSegmentFilterData::OPERATOR_IS_NOT, |
| 43 | DynamicSegmentFilterData::OPERATOR_CONTAINS, |
| 44 | DynamicSegmentFilterData::OPERATOR_NOT_CONTAINS, |
| 45 | DynamicSegmentFilterData::OPERATOR_STARTS_WITH, |
| 46 | DynamicSegmentFilterData::OPERATOR_NOT_STARTS_WITH, |
| 47 | DynamicSegmentFilterData::OPERATOR_ENDS_WITH, |
| 48 | DynamicSegmentFilterData::OPERATOR_NOT_ENDS_WITH, |
| 49 | ]; |
| 50 | public const IS_NOT_BLANK = 'is_not_blank'; |
| 51 | public const IS_BLANK = 'is_blank'; |
| 52 | |
| 53 | public const TIMEFRAME_ALL_TIME = 'allTime'; |
| 54 | public const TIMEFRAME_IN_THE_LAST = 'inTheLast'; |
| 55 | public const TIMEFRAME_BEFORE = 'before'; |
| 56 | public const TIMEFRAME_AFTER = 'after'; |
| 57 | public const TIMEFRAME_ON = 'on'; |
| 58 | public const TIMEFRAME_BETWEEN = 'between'; |
| 59 | public const TIMEFRAMES = [ |
| 60 | self::TIMEFRAME_ALL_TIME, |
| 61 | self::TIMEFRAME_IN_THE_LAST, |
| 62 | self::TIMEFRAME_BEFORE, |
| 63 | self::TIMEFRAME_AFTER, |
| 64 | self::TIMEFRAME_ON, |
| 65 | self::TIMEFRAME_BETWEEN, |
| 66 | ]; |
| 67 | |
| 68 | /** |
| 69 | * @ORM\Column(type="serialized_array") |
| 70 | * @var array|null |
| 71 | */ |
| 72 | private $filterData; |
| 73 | |
| 74 | /** |
| 75 | * @ORM\Column(type="string", nullable=true) |
| 76 | * @var string|null |
| 77 | */ |
| 78 | private $filterType; |
| 79 | |
| 80 | /** |
| 81 | * @ORM\Column(type="string", nullable=true) |
| 82 | * @var string|null |
| 83 | */ |
| 84 | private $action; |
| 85 | |
| 86 | public function __construct( |
| 87 | string $filterType, |
| 88 | string $action, |
| 89 | array $filterData = [] |
| 90 | ) { |
| 91 | $this->filterType = $filterType; |
| 92 | $this->action = $action; |
| 93 | $this->filterData = $filterData; |
| 94 | } |
| 95 | |
| 96 | public function getData(): ?array { |
| 97 | return $this->filterData; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @return mixed|null |
| 102 | */ |
| 103 | public function getParam(string $name) { |
| 104 | return $this->filterData[$name] ?? null; |
| 105 | } |
| 106 | |
| 107 | public function getStringParam(string $name): string { |
| 108 | $value = $this->filterData[$name] ?? null; |
| 109 | if (!is_string($value)) { |
| 110 | throw new InvalidStateException("No string value found in filter data for param $name."); |
| 111 | } |
| 112 | return $value; |
| 113 | } |
| 114 | |
| 115 | public function getIntParam(string $name): int { |
| 116 | $value = $this->filterData[$name] ?? null; |
| 117 | if (is_int($value)) { |
| 118 | return $value; |
| 119 | } |
| 120 | |
| 121 | if (is_string($value)) { |
| 122 | return (int)($value); |
| 123 | } |
| 124 | |
| 125 | throw new InvalidStateException("No compatible integer value found in filter data for param $name."); |
| 126 | } |
| 127 | |
| 128 | public function getArrayParam(string $name): array { |
| 129 | $value = $this->getParam($name); |
| 130 | if (!is_array($value)) { |
| 131 | throw new InvalidStateException("No array value found in filter data for param $name."); |
| 132 | } |
| 133 | return $value; |
| 134 | } |
| 135 | |
| 136 | public function getFilterType(): ?string { |
| 137 | if ($this->filterType) { |
| 138 | return $this->filterType; |
| 139 | } |
| 140 | // When a new column is empty, we try to get the value from serialized data |
| 141 | return $this->filterData['segmentType'] ?? null; |
| 142 | } |
| 143 | |
| 144 | public function getAction(): ?string { |
| 145 | if ($this->action) { |
| 146 | return $this->action; |
| 147 | } |
| 148 | // When a new column is empty, we try to get the value from serialized data |
| 149 | // BC compatibility, the wordpress user role segment didn't have action |
| 150 | if ($this->getFilterType() === self::TYPE_USER_ROLE && !isset($this->filterData['action'])) { |
| 151 | return UserRole::TYPE; |
| 152 | } |
| 153 | return $this->filterData['action'] ?? null; |
| 154 | } |
| 155 | |
| 156 | public function getOperator(): ?string { |
| 157 | $operator = $this->filterData['operator'] ?? null; |
| 158 | if (!$operator) { |
| 159 | return $this->getDefaultOperator(); |
| 160 | } |
| 161 | |
| 162 | return $operator; |
| 163 | } |
| 164 | |
| 165 | private function getDefaultOperator(): ?string { |
| 166 | if ($this->getFilterType() === self::TYPE_WOOCOMMERCE && $this->getAction() === WooCommerceProduct::ACTION_PRODUCT) { |
| 167 | return self::OPERATOR_ANY; |
| 168 | } |
| 169 | return null; |
| 170 | } |
| 171 | } |
| 172 |