Açıklama Yok

SubscriberEntity.php 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. namespace MailPoet\Entities;
  3. if (!defined('ABSPATH')) exit;
  4. use DateTimeInterface;
  5. use MailPoet\Doctrine\EntityTraits\AutoincrementedIdTrait;
  6. use MailPoet\Doctrine\EntityTraits\CreatedAtTrait;
  7. use MailPoet\Doctrine\EntityTraits\DeletedAtTrait;
  8. use MailPoet\Doctrine\EntityTraits\UpdatedAtTrait;
  9. use MailPoet\Util\Helpers;
  10. use MailPoetVendor\Doctrine\Common\Collections\ArrayCollection;
  11. use MailPoetVendor\Doctrine\Common\Collections\Collection;
  12. use MailPoetVendor\Doctrine\ORM\Mapping as ORM;
  13. use MailPoetVendor\Symfony\Component\Validator\Constraints as Assert;
  14. /**
  15. * @ORM\Entity()
  16. * @ORM\Table(name="subscribers")
  17. * @ORM\HasLifecycleCallbacks
  18. */
  19. class SubscriberEntity {
  20. // statuses
  21. const STATUS_BOUNCED = 'bounced';
  22. const STATUS_INACTIVE = 'inactive';
  23. const STATUS_SUBSCRIBED = 'subscribed';
  24. const STATUS_UNCONFIRMED = 'unconfirmed';
  25. const STATUS_UNSUBSCRIBED = 'unsubscribed';
  26. public const OBSOLETE_LINK_TOKEN_LENGTH = 6;
  27. public const LINK_TOKEN_LENGTH = 32;
  28. use AutoincrementedIdTrait;
  29. use CreatedAtTrait;
  30. use UpdatedAtTrait;
  31. use DeletedAtTrait;
  32. /**
  33. * @ORM\Column(type="bigint", nullable=true)
  34. * @var string|null
  35. */
  36. private $wpUserId;
  37. /**
  38. * @ORM\Column(type="boolean")
  39. * @var bool
  40. */
  41. private $isWoocommerceUser = false;
  42. /**
  43. * @ORM\Column(type="string")
  44. * @var string
  45. */
  46. private $firstName = '';
  47. /**
  48. * @ORM\Column(type="string")
  49. * @var string
  50. */
  51. private $lastName = '';
  52. /**
  53. * @ORM\Column(type="string")
  54. * @Assert\Email()
  55. * @Assert\NotBlank()
  56. * @var string
  57. */
  58. private $email;
  59. /**
  60. * @ORM\Column(type="string")
  61. * @var string
  62. */
  63. private $status = self::STATUS_UNCONFIRMED;
  64. /**
  65. * @ORM\Column(type="string", nullable=true)
  66. * @var string|null
  67. */
  68. private $subscribedIp;
  69. /**
  70. * @ORM\Column(type="string", nullable=true)
  71. * @var string|null
  72. */
  73. private $confirmedIp;
  74. /**
  75. * @ORM\Column(type="datetimetz", nullable=true)
  76. * @var DateTimeInterface|null
  77. */
  78. private $confirmedAt;
  79. /**
  80. * @ORM\Column(type="datetimetz", nullable=true)
  81. * @var DateTimeInterface|null
  82. */
  83. private $lastSubscribedAt;
  84. /**
  85. * @ORM\Column(type="text", nullable=true)
  86. * @var string|null
  87. */
  88. private $unconfirmedData;
  89. /**
  90. * @ORM\Column(type="string")
  91. * @var string
  92. */
  93. private $source = 'unknown';
  94. /**
  95. * @ORM\Column(type="integer")
  96. * @var int
  97. */
  98. private $countConfirmations = 0;
  99. /**
  100. * @ORM\Column(type="string", nullable=true)
  101. * @var string|null
  102. */
  103. private $unsubscribeToken;
  104. /**
  105. * @ORM\Column(type="string", nullable=true)
  106. * @var string|null
  107. */
  108. private $linkToken;
  109. /**
  110. * @ORM\Column(type="float", nullable=true)
  111. * @var float|null
  112. */
  113. private $engagementScore;
  114. /**
  115. * @ORM\Column(type="datetimetz", nullable=true)
  116. * @var DateTimeInterface|null
  117. */
  118. private $engagementScoreUpdatedAt;
  119. /**
  120. * @ORM\Column(type="datetimetz", nullable=true)
  121. * @var DateTimeInterface|null
  122. */
  123. private $lastEngagementAt;
  124. /**
  125. * @ORM\OneToMany(targetEntity="MailPoet\Entities\SubscriberSegmentEntity", mappedBy="subscriber", orphanRemoval=true)
  126. * @var Collection<int, SubscriberSegmentEntity>
  127. */
  128. private $subscriberSegments;
  129. public function __construct() {
  130. $this->subscriberSegments = new ArrayCollection();
  131. }
  132. /**
  133. * @deprecated This is here only for backward compatibility with custom shortcodes https://kb.mailpoet.com/article/160-create-a-custom-shortcode
  134. * This can be removed after 2021-08-01
  135. */
  136. public function __get($key) {
  137. $getterName = 'get' . Helpers::underscoreToCamelCase($key, $capitaliseFirstChar = true);
  138. $callable = [$this, $getterName];
  139. if (is_callable($callable)) {
  140. return call_user_func($callable);
  141. }
  142. }
  143. /**
  144. * @return int|null
  145. */
  146. public function getWpUserId() {
  147. return $this->wpUserId ? (int)$this->wpUserId : null;
  148. }
  149. /**
  150. * @param int|null $wpUserId
  151. */
  152. public function setWpUserId($wpUserId) {
  153. $this->wpUserId = $wpUserId ? (string)$wpUserId : null;
  154. }
  155. public function isWPUser(): bool {
  156. return $this->getWpUserId() > 0;
  157. }
  158. /**
  159. * @return bool
  160. */
  161. public function getIsWoocommerceUser() {
  162. return $this->isWoocommerceUser;
  163. }
  164. /**
  165. * @param bool $isWoocommerceUser
  166. */
  167. public function setIsWoocommerceUser($isWoocommerceUser) {
  168. $this->isWoocommerceUser = $isWoocommerceUser;
  169. }
  170. /**
  171. * @return string
  172. */
  173. public function getFirstName() {
  174. return $this->firstName;
  175. }
  176. /**
  177. * @param string $firstName
  178. */
  179. public function setFirstName($firstName) {
  180. $this->firstName = $firstName;
  181. }
  182. /**
  183. * @return string
  184. */
  185. public function getLastName() {
  186. return $this->lastName;
  187. }
  188. /**
  189. * @param string $lastName
  190. */
  191. public function setLastName($lastName) {
  192. $this->lastName = $lastName;
  193. }
  194. /**
  195. * @return string
  196. */
  197. public function getEmail() {
  198. return $this->email;
  199. }
  200. /**
  201. * @param string $email
  202. */
  203. public function setEmail($email) {
  204. $this->email = $email;
  205. }
  206. /**
  207. * @return string
  208. */
  209. public function getStatus() {
  210. return $this->status;
  211. }
  212. /**
  213. * @param string $status
  214. */
  215. public function setStatus($status) {
  216. if (!in_array($status, [
  217. self::STATUS_BOUNCED,
  218. self::STATUS_INACTIVE,
  219. self::STATUS_SUBSCRIBED,
  220. self::STATUS_UNCONFIRMED,
  221. self::STATUS_UNSUBSCRIBED,
  222. ])) {
  223. throw new \InvalidArgumentException("Invalid status '{$status}' given to subscriber!");
  224. }
  225. $this->status = $status;
  226. }
  227. /**
  228. * @return string|null
  229. */
  230. public function getSubscribedIp() {
  231. return $this->subscribedIp;
  232. }
  233. /**
  234. * @param string $subscribedIp
  235. */
  236. public function setSubscribedIp($subscribedIp) {
  237. $this->subscribedIp = $subscribedIp;
  238. }
  239. /**
  240. * @return string|null
  241. */
  242. public function getConfirmedIp() {
  243. return $this->confirmedIp;
  244. }
  245. /**
  246. * @param string|null $confirmedIp
  247. */
  248. public function setConfirmedIp($confirmedIp) {
  249. $this->confirmedIp = $confirmedIp;
  250. }
  251. /**
  252. * @return DateTimeInterface|null
  253. */
  254. public function getConfirmedAt() {
  255. return $this->confirmedAt;
  256. }
  257. /**
  258. * @param DateTimeInterface|null $confirmedAt
  259. */
  260. public function setConfirmedAt($confirmedAt) {
  261. $this->confirmedAt = $confirmedAt;
  262. }
  263. /**
  264. * @return DateTimeInterface|null
  265. */
  266. public function getLastSubscribedAt() {
  267. return $this->lastSubscribedAt;
  268. }
  269. /**
  270. * @param DateTimeInterface|null $lastSubscribedAt
  271. */
  272. public function setLastSubscribedAt($lastSubscribedAt) {
  273. $this->lastSubscribedAt = $lastSubscribedAt;
  274. }
  275. /**
  276. * @return string|null
  277. */
  278. public function getUnconfirmedData() {
  279. return $this->unconfirmedData;
  280. }
  281. /**
  282. * @param string|null $unconfirmedData
  283. */
  284. public function setUnconfirmedData($unconfirmedData) {
  285. $this->unconfirmedData = $unconfirmedData;
  286. }
  287. /**
  288. * @return string
  289. */
  290. public function getSource() {
  291. return $this->source;
  292. }
  293. /**
  294. * @param string $source
  295. */
  296. public function setSource($source) {
  297. if (!in_array($source, [
  298. 'api',
  299. 'form',
  300. 'unknown',
  301. 'imported',
  302. 'administrator',
  303. 'wordpress_user',
  304. 'woocommerce_user',
  305. 'woocommerce_checkout',
  306. ])) {
  307. throw new \InvalidArgumentException("Invalid source '{$source}' given to subscriber!");
  308. }
  309. $this->source = $source;
  310. }
  311. /**
  312. * @return int
  313. */
  314. public function getConfirmationsCount() {
  315. return $this->countConfirmations;
  316. }
  317. /**
  318. * @param int $countConfirmations
  319. */
  320. public function setConfirmationsCount($countConfirmations) {
  321. $this->countConfirmations = $countConfirmations;
  322. }
  323. /**
  324. * @return string|null
  325. */
  326. public function getUnsubscribeToken() {
  327. return $this->unsubscribeToken;
  328. }
  329. /**
  330. * @param string|null $unsubscribeToken
  331. */
  332. public function setUnsubscribeToken($unsubscribeToken) {
  333. $this->unsubscribeToken = $unsubscribeToken;
  334. }
  335. /**
  336. * @return string|null
  337. */
  338. public function getLinkToken() {
  339. return $this->linkToken;
  340. }
  341. /**
  342. * @param string|null $linkToken
  343. */
  344. public function setLinkToken($linkToken) {
  345. $this->linkToken = $linkToken;
  346. }
  347. /**
  348. * @return Collection<int, SubscriberSegmentEntity>
  349. */
  350. public function getSubscriberSegments() {
  351. return $this->subscriberSegments;
  352. }
  353. public function getSegments() {
  354. return $this->subscriberSegments->map(function (SubscriberSegmentEntity $subscriberSegment) {
  355. return $subscriberSegment->getSegment();
  356. })->filter(function ($segment) {
  357. return $segment !== null;
  358. });
  359. }
  360. /**
  361. * @return float|null
  362. */
  363. public function getEngagementScore(): ?float {
  364. return $this->engagementScore;
  365. }
  366. /**
  367. * @param float|null $engagementScore
  368. */
  369. public function setEngagementScore(?float $engagementScore): void {
  370. $this->engagementScore = $engagementScore;
  371. }
  372. /**
  373. * @return DateTimeInterface|null
  374. */
  375. public function getEngagementScoreUpdatedAt(): ?DateTimeInterface {
  376. return $this->engagementScoreUpdatedAt;
  377. }
  378. /**
  379. * @param DateTimeInterface|null $engagementScoreUpdatedAt
  380. */
  381. public function setEngagementScoreUpdatedAt(?DateTimeInterface $engagementScoreUpdatedAt): void {
  382. $this->engagementScoreUpdatedAt = $engagementScoreUpdatedAt;
  383. }
  384. public function getLastEngagementAt(): ?DateTimeInterface {
  385. return $this->lastEngagementAt;
  386. }
  387. public function setLastEngagementAt(DateTimeInterface $lastEngagementAt): void {
  388. $this->lastEngagementAt = $lastEngagementAt;
  389. }
  390. /** @ORM\PreFlush */
  391. public function cleanupSubscriberSegments(): void {
  392. // Delete old orphan SubscriberSegments to avoid errors on update
  393. $this->subscriberSegments->map(function (SubscriberSegmentEntity $subscriberSegment) {
  394. if ($subscriberSegment->getSegment() === null) {
  395. $this->subscriberSegments->removeElement($subscriberSegment);
  396. }
  397. });
  398. }
  399. }