Нет описания

NewsletterEntity.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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\SafeToOneAssociationLoadTrait;
  9. use MailPoet\Doctrine\EntityTraits\UpdatedAtTrait;
  10. use MailPoet\Util\Helpers;
  11. use MailPoetVendor\Carbon\Carbon;
  12. use MailPoetVendor\Doctrine\Common\Collections\ArrayCollection;
  13. use MailPoetVendor\Doctrine\Common\Collections\Collection;
  14. use MailPoetVendor\Doctrine\Common\Collections\Criteria;
  15. use MailPoetVendor\Doctrine\ORM\Mapping as ORM;
  16. use MailPoetVendor\Symfony\Component\Validator\Constraints as Assert;
  17. /**
  18. * @ORM\Entity()
  19. * @ORM\Table(name="newsletters")
  20. */
  21. class NewsletterEntity {
  22. // types
  23. const TYPE_AUTOMATIC = 'automatic';
  24. const TYPE_STANDARD = 'standard';
  25. const TYPE_WELCOME = 'welcome';
  26. const TYPE_NOTIFICATION = 'notification';
  27. const TYPE_NOTIFICATION_HISTORY = 'notification_history';
  28. const TYPE_WC_TRANSACTIONAL_EMAIL = 'wc_transactional';
  29. const TYPE_RE_ENGAGEMENT = 're_engagement';
  30. // standard newsletters
  31. const STATUS_DRAFT = 'draft';
  32. const STATUS_SCHEDULED = 'scheduled';
  33. const STATUS_SENDING = 'sending';
  34. const STATUS_SENT = 'sent';
  35. // automatic newsletters status
  36. const STATUS_ACTIVE = 'active';
  37. use AutoincrementedIdTrait;
  38. use CreatedAtTrait;
  39. use UpdatedAtTrait;
  40. use DeletedAtTrait;
  41. use SafeToOneAssociationLoadTrait;
  42. /**
  43. * @ORM\Column(type="string", nullable=true)
  44. * @var string|null
  45. */
  46. private $hash;
  47. /**
  48. * @ORM\Column(type="string")
  49. * @var string
  50. */
  51. private $subject;
  52. /**
  53. * @ORM\Column(type="string")
  54. * @Assert\NotBlank()
  55. * @var string
  56. */
  57. private $type;
  58. /**
  59. * @ORM\Column(type="string")
  60. * @var string
  61. */
  62. private $senderAddress = '';
  63. /**
  64. * @ORM\Column(type="string")
  65. * @var string
  66. */
  67. private $senderName = '';
  68. /**
  69. * @ORM\Column(type="string")
  70. * @var string
  71. */
  72. private $status = self::STATUS_DRAFT;
  73. /**
  74. * @ORM\Column(type="string")
  75. * @var string
  76. */
  77. private $replyToAddress = '';
  78. /**
  79. * @ORM\Column(type="string")
  80. * @var string
  81. */
  82. private $replyToName = '';
  83. /**
  84. * @ORM\Column(type="string")
  85. * @var string
  86. */
  87. private $preheader = '';
  88. /**
  89. * @ORM\Column(type="json", nullable=true)
  90. * @var array|null
  91. */
  92. private $body;
  93. /**
  94. * @ORM\Column(type="datetimetz", nullable=true)
  95. * @var DateTimeInterface|null
  96. */
  97. private $sentAt;
  98. /**
  99. * @ORM\Column(type="string", nullable=true)
  100. * @var string|null
  101. */
  102. private $unsubscribeToken;
  103. /**
  104. * @ORM\Column(type="string")
  105. * @var string
  106. */
  107. private $gaCampaign = '';
  108. /**
  109. * @ORM\ManyToOne(targetEntity="MailPoet\Entities\NewsletterEntity")
  110. * @var NewsletterEntity|null
  111. */
  112. private $parent;
  113. /**
  114. * @ORM\OneToMany(targetEntity="MailPoet\Entities\NewsletterEntity", mappedBy="parent")
  115. * @var ArrayCollection<int, NewsletterEntity>
  116. */
  117. private $children;
  118. /**
  119. * @ORM\OneToMany(targetEntity="MailPoet\Entities\NewsletterSegmentEntity", mappedBy="newsletter", orphanRemoval=true)
  120. * @var ArrayCollection<int, NewsletterSegmentEntity>
  121. */
  122. private $newsletterSegments;
  123. /**
  124. * @ORM\OneToMany(targetEntity="MailPoet\Entities\NewsletterOptionEntity", mappedBy="newsletter", orphanRemoval=true)
  125. * @var ArrayCollection<int, NewsletterOptionEntity>
  126. */
  127. private $options;
  128. /**
  129. * @ORM\OneToMany(targetEntity="MailPoet\Entities\SendingQueueEntity", mappedBy="newsletter")
  130. * @var ArrayCollection<int, SendingQueueEntity>
  131. */
  132. private $queues;
  133. public function __construct() {
  134. $this->children = new ArrayCollection();
  135. $this->newsletterSegments = new ArrayCollection();
  136. $this->options = new ArrayCollection();
  137. $this->queues = new ArrayCollection();
  138. }
  139. /**
  140. * @deprecated This is here only for backward compatibility with custom shortcodes https://kb.mailpoet.com/article/160-create-a-custom-shortcode
  141. * This can be removed after 2021-08-01
  142. */
  143. public function __get($key) {
  144. $getterName = 'get' . Helpers::underscoreToCamelCase($key, $capitaliseFirstChar = true);
  145. $callable = [$this, $getterName];
  146. if (is_callable($callable)) {
  147. return call_user_func($callable);
  148. }
  149. }
  150. public function __clone() {
  151. // reset ID
  152. $this->id = null;
  153. }
  154. /**
  155. * @return string|null
  156. */
  157. public function getHash() {
  158. return $this->hash;
  159. }
  160. /**
  161. * @param string|null $hash
  162. */
  163. public function setHash($hash) {
  164. $this->hash = $hash;
  165. }
  166. /**
  167. * @return string
  168. */
  169. public function getSubject() {
  170. return $this->subject;
  171. }
  172. /**
  173. * @param string $subject
  174. */
  175. public function setSubject($subject) {
  176. $this->subject = $subject;
  177. }
  178. /**
  179. * @return string
  180. */
  181. public function getType() {
  182. return $this->type;
  183. }
  184. /**
  185. * @param string $type
  186. */
  187. public function setType($type) {
  188. $this->type = $type;
  189. }
  190. /**
  191. * @return string
  192. */
  193. public function getSenderAddress() {
  194. return $this->senderAddress;
  195. }
  196. /**
  197. * @param string $senderAddress
  198. */
  199. public function setSenderAddress($senderAddress) {
  200. $this->senderAddress = $senderAddress;
  201. }
  202. /**
  203. * @return string
  204. */
  205. public function getSenderName() {
  206. return $this->senderName;
  207. }
  208. /**
  209. * @param string $senderName
  210. */
  211. public function setSenderName($senderName) {
  212. $this->senderName = $senderName;
  213. }
  214. /**
  215. * @return string
  216. */
  217. public function getStatus() {
  218. return $this->status;
  219. }
  220. /**
  221. * @param string $status
  222. */
  223. public function setStatus($status) {
  224. $this->status = $status;
  225. // activate/deactivate unfinished tasks
  226. $newTaskStatus = null;
  227. $typesWithActivation = [self::TYPE_NOTIFICATION, self::TYPE_WELCOME, self::TYPE_AUTOMATIC];
  228. if (($status === self::STATUS_DRAFT) && in_array($this->type, $typesWithActivation)) {
  229. $newTaskStatus = ScheduledTaskEntity::STATUS_PAUSED;
  230. }
  231. if (($status === self::STATUS_ACTIVE) && in_array($this->type, $typesWithActivation)) {
  232. $newTaskStatus = ScheduledTaskEntity::STATUS_SCHEDULED;
  233. }
  234. if (!$newTaskStatus) return;
  235. $queues = $this->getUnfinishedQueues();
  236. foreach ($queues as $queue) {
  237. /** @var SendingQueueEntity $queue */
  238. $task = $queue->getTask();
  239. if ($task === null) continue;
  240. $scheduled = new Carbon($task->getScheduledAt());
  241. if ($scheduled < (new Carbon())->subDays(30)) continue;
  242. if (($status === self::STATUS_DRAFT) && ($task->getStatus() !== ScheduledTaskEntity::STATUS_SCHEDULED)) continue;
  243. if (($status === self::STATUS_ACTIVE) && ($task->getStatus() !== ScheduledTaskEntity::STATUS_PAUSED)) continue;
  244. $task->setStatus($newTaskStatus);
  245. }
  246. }
  247. /**
  248. * @return string
  249. */
  250. public function getReplyToAddress() {
  251. return $this->replyToAddress;
  252. }
  253. /**
  254. * @param string $replyToAddress
  255. */
  256. public function setReplyToAddress($replyToAddress) {
  257. $this->replyToAddress = $replyToAddress;
  258. }
  259. /**
  260. * @return string
  261. */
  262. public function getReplyToName() {
  263. return $this->replyToName;
  264. }
  265. /**
  266. * @param string $replyToName
  267. */
  268. public function setReplyToName($replyToName) {
  269. $this->replyToName = $replyToName;
  270. }
  271. /**
  272. * @return string
  273. */
  274. public function getPreheader() {
  275. return $this->preheader;
  276. }
  277. /**
  278. * @param string $preheader
  279. */
  280. public function setPreheader($preheader) {
  281. $this->preheader = $preheader;
  282. }
  283. /**
  284. * @return array|null
  285. */
  286. public function getBody() {
  287. return $this->body;
  288. }
  289. /**
  290. * @param array|null $body
  291. */
  292. public function setBody($body) {
  293. $this->body = $body;
  294. }
  295. /**
  296. * @return DateTimeInterface|null
  297. */
  298. public function getSentAt() {
  299. return $this->sentAt;
  300. }
  301. /**
  302. * @param DateTimeInterface|null $sentAt
  303. */
  304. public function setSentAt($sentAt) {
  305. $this->sentAt = $sentAt;
  306. }
  307. /**
  308. * @return string|null
  309. */
  310. public function getUnsubscribeToken() {
  311. return $this->unsubscribeToken;
  312. }
  313. /**
  314. * @return string
  315. */
  316. public function getGaCampaign() {
  317. return $this->gaCampaign;
  318. }
  319. /**
  320. * @param string $gaCampaign
  321. */
  322. public function setGaCampaign($gaCampaign) {
  323. $this->gaCampaign = $gaCampaign;
  324. }
  325. /**
  326. * @param string|null $unsubscribeToken
  327. */
  328. public function setUnsubscribeToken($unsubscribeToken) {
  329. $this->unsubscribeToken = $unsubscribeToken;
  330. }
  331. /**
  332. * @return NewsletterEntity|null
  333. */
  334. public function getParent() {
  335. $this->safelyLoadToOneAssociation('parent');
  336. return $this->parent;
  337. }
  338. /**
  339. * @param NewsletterEntity|null $parent
  340. */
  341. public function setParent($parent) {
  342. $this->parent = $parent;
  343. }
  344. /**
  345. * @return ArrayCollection<int, NewsletterEntity>
  346. */
  347. public function getChildren() {
  348. return $this->children;
  349. }
  350. /**
  351. * @return ArrayCollection<int, NewsletterSegmentEntity>
  352. */
  353. public function getNewsletterSegments() {
  354. return $this->newsletterSegments;
  355. }
  356. /**
  357. * @return ArrayCollection<int, NewsletterOptionEntity>
  358. */
  359. public function getOptions() {
  360. return $this->options;
  361. }
  362. public function getOption(string $name): ?NewsletterOptionEntity {
  363. $option = $this->options->filter(function (NewsletterOptionEntity $option) use ($name): bool {
  364. return ($field = $option->getOptionField()) ? $field->getName() === $name : false;
  365. })->first();
  366. return $option ?: null;
  367. }
  368. public function getOptionValue(string $name) {
  369. $option = $this->getOption($name);
  370. return $option ? $option->getValue() : null;
  371. }
  372. /**
  373. * @return ArrayCollection<int, SendingQueueEntity>
  374. */
  375. public function getQueues() {
  376. return $this->queues;
  377. }
  378. /**
  379. * @return SendingQueueEntity|null
  380. */
  381. public function getLatestQueue() {
  382. $criteria = new Criteria();
  383. $criteria->orderBy(['id' => Criteria::DESC]);
  384. $criteria->setMaxResults(1);
  385. return $this->queues->matching($criteria)->first() ?: null;
  386. }
  387. /**
  388. * @return Collection<int, SendingQueueEntity>
  389. */
  390. private function getUnfinishedQueues(): Collection {
  391. $criteria = new Criteria();
  392. $expr = Criteria::expr();
  393. $criteria->where($expr->neq('countToProcess', 0));
  394. return $this->queues->matching($criteria);
  395. }
  396. public function getGlobalStyle(string $category, string $style): ?string {
  397. $body = $this->getBody();
  398. if ($body === null) {
  399. return null;
  400. }
  401. return $body['globalStyles'][$category][$style] ?? null;
  402. }
  403. public function getProcessedAt(): ?DateTimeInterface {
  404. $processedAt = null;
  405. $queue = $this->getLatestQueue();
  406. if ($queue instanceof SendingQueueEntity) {
  407. $task = $queue->getTask();
  408. if ($task instanceof ScheduledTaskEntity) {
  409. $processedAt = $task->getProcessedAt();
  410. }
  411. }
  412. return $processedAt;
  413. }
  414. }