NewsletterLinkSubject.php
2 years ago
SegmentSubject.php
5 months ago
SubscriberSubject.php
5 months ago
index.php
3 years ago
SegmentSubject.php
93 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet\Subjects; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Field; |
| 9 | use MailPoet\Automation\Engine\Data\Subject as SubjectData; |
| 10 | use MailPoet\Automation\Engine\Integration\Payload; |
| 11 | use MailPoet\Automation\Engine\Integration\Subject; |
| 12 | use MailPoet\Automation\Integrations\MailPoet\Payloads\SegmentPayload; |
| 13 | use MailPoet\NotFoundException; |
| 14 | use MailPoet\Segments\SegmentsRepository; |
| 15 | use MailPoet\Validator\Builder; |
| 16 | use MailPoet\Validator\Schema\ObjectSchema; |
| 17 | use MailPoet\WPCOM\DotcomHelperFunctions; |
| 18 | |
| 19 | /** |
| 20 | * @implements Subject<SegmentPayload> |
| 21 | */ |
| 22 | class SegmentSubject implements Subject { |
| 23 | const KEY = 'mailpoet:segment'; |
| 24 | |
| 25 | /** @var SegmentsRepository */ |
| 26 | private $segmentsRepository; |
| 27 | |
| 28 | /** @var DotcomHelperFunctions */ |
| 29 | private $dotcomHelperFunctions; |
| 30 | |
| 31 | public function __construct( |
| 32 | SegmentsRepository $segmentsRepository, |
| 33 | DotcomHelperFunctions $dotcomHelperFunctions |
| 34 | ) { |
| 35 | $this->segmentsRepository = $segmentsRepository; |
| 36 | $this->dotcomHelperFunctions = $dotcomHelperFunctions; |
| 37 | } |
| 38 | |
| 39 | public function getKey(): string { |
| 40 | return self::KEY; |
| 41 | } |
| 42 | |
| 43 | public function getName(): string { |
| 44 | if ($this->dotcomHelperFunctions->isGarden()) { |
| 45 | // translators: automation subject (entity entering automation) title |
| 46 | return __('Segment', 'mailpoet'); |
| 47 | } |
| 48 | // translators: automation subject (entity entering automation) title |
| 49 | return __('MailPoet segment', 'mailpoet'); |
| 50 | } |
| 51 | |
| 52 | public function getArgsSchema(): ObjectSchema { |
| 53 | return Builder::object([ |
| 54 | 'segment_id' => Builder::integer()->required(), |
| 55 | ]); |
| 56 | } |
| 57 | |
| 58 | public function getPayload(SubjectData $subjectData): Payload { |
| 59 | $id = $subjectData->getArgs()['segment_id']; |
| 60 | $segment = $this->segmentsRepository->findOneById($id); |
| 61 | if (!$segment) { |
| 62 | // translators: %d is the ID. |
| 63 | throw NotFoundException::create()->withMessage(sprintf(__("Segment with ID '%d' not found.", 'mailpoet'), $id)); |
| 64 | } |
| 65 | return new SegmentPayload($segment); |
| 66 | } |
| 67 | |
| 68 | /** @return Field[] */ |
| 69 | public function getFields(): array { |
| 70 | return [ |
| 71 | // phpcs:disable Squiz.PHP.CommentedOutCode.Found -- temporarily hide those fields |
| 72 | /* |
| 73 | new Field( |
| 74 | 'mailpoet:segment:id', |
| 75 | Field::TYPE_INTEGER, |
| 76 | __('Segment ID', 'mailpoet'), |
| 77 | function (SegmentPayload $payload) { |
| 78 | return $payload->getId(); |
| 79 | } |
| 80 | ), |
| 81 | new Field( |
| 82 | 'mailpoet:segment:name', |
| 83 | Field::TYPE_STRING, |
| 84 | __('Segment name', 'mailpoet'), |
| 85 | function (SegmentPayload $payload) { |
| 86 | return $payload->getName(); |
| 87 | } |
| 88 | ), |
| 89 | */ |
| 90 | ]; |
| 91 | } |
| 92 | } |
| 93 |