API.php
3 months ago
APIException.php
3 months ago
CustomFields.php
2 months ago
Segments.php
2 months ago
Subscribers.php
2 months ago
Tags.php
3 months ago
index.php
3 years ago
API.php
166 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\API\MP\v1; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\Changelog; |
| 9 | |
| 10 | /** |
| 11 | * API used by other plugins |
| 12 | * Do not add bodies of methods into this class. Use other classes. See CustomFields or Subscribers. |
| 13 | * This class is under refactor, and we are going to move most of the remaining implementations from here. |
| 14 | */ |
| 15 | class API { |
| 16 | /** @var CustomFields */ |
| 17 | private $customFields; |
| 18 | |
| 19 | /** @var Segments */ |
| 20 | private $segments; |
| 21 | |
| 22 | /** @var Subscribers */ |
| 23 | private $subscribers; |
| 24 | |
| 25 | /** @var Tags */ |
| 26 | private $tags; |
| 27 | |
| 28 | /** @var Changelog */ |
| 29 | private $changelog; |
| 30 | |
| 31 | public function __construct( |
| 32 | CustomFields $customFields, |
| 33 | Segments $segments, |
| 34 | Subscribers $subscribers, |
| 35 | Tags $tags, |
| 36 | Changelog $changelog |
| 37 | ) { |
| 38 | $this->customFields = $customFields; |
| 39 | $this->segments = $segments; |
| 40 | $this->subscribers = $subscribers; |
| 41 | $this->tags = $tags; |
| 42 | $this->changelog = $changelog; |
| 43 | } |
| 44 | |
| 45 | public function getSubscriberFields() { |
| 46 | return $this->customFields->getSubscriberFields(); |
| 47 | } |
| 48 | |
| 49 | public function addSubscriberField(array $data = []) { |
| 50 | try { |
| 51 | return $this->customFields->addSubscriberField($data); |
| 52 | } catch (\InvalidArgumentException $e) { |
| 53 | throw new APIException($e->getMessage(), $e->getCode(), $e); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @throws APIException |
| 59 | */ |
| 60 | public function subscribeToList($subscriberId, $listId, $options = []): array { |
| 61 | return $this->subscribeToLists($subscriberId, [$listId], $options); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @throws APIException |
| 66 | */ |
| 67 | public function subscribeToLists($subscriberId, array $listIds, $options = []) { |
| 68 | return $this->subscribers->subscribeToLists($subscriberId, $listIds, $options); |
| 69 | } |
| 70 | |
| 71 | public function unsubscribeFromList($subscriberId, $listId) { |
| 72 | return $this->unsubscribeFromLists($subscriberId, [$listId]); |
| 73 | } |
| 74 | |
| 75 | public function unsubscribeFromLists($subscriberId, array $listIds) { |
| 76 | return $this->subscribers->unsubscribeFromLists($subscriberId, $listIds); |
| 77 | } |
| 78 | |
| 79 | public function unsubscribe($subscriberIdOrEmail) { |
| 80 | return $this->subscribers->unsubscribe($subscriberIdOrEmail); |
| 81 | } |
| 82 | |
| 83 | public function getLists(): array { |
| 84 | return $this->segments->getAll(); |
| 85 | } |
| 86 | |
| 87 | public function addSubscriber(array $subscriber, $listIds = [], $options = []): array { |
| 88 | return $this->subscribers->addSubscriber($subscriber, $listIds, $options); |
| 89 | } |
| 90 | |
| 91 | public function updateSubscriber($subscriberIdOrEmail, array $subscriber): array { |
| 92 | return $this->subscribers->updateSubscriber($subscriberIdOrEmail, $subscriber); |
| 93 | } |
| 94 | |
| 95 | public function addList(array $list) { |
| 96 | return $this->segments->addList($list); |
| 97 | } |
| 98 | |
| 99 | public function deleteList(string $listId): bool { |
| 100 | return $this->segments->deleteList($listId); |
| 101 | } |
| 102 | |
| 103 | public function updateList(array $list): array { |
| 104 | return $this->segments->updateList($list); |
| 105 | } |
| 106 | |
| 107 | public function getSubscriber($subscriberEmail) { |
| 108 | return $this->subscribers->getSubscriber($subscriberEmail); |
| 109 | } |
| 110 | |
| 111 | public function getSubscribers(array $filter = [], int $limit = 50, int $offset = 0): array { |
| 112 | return $this->subscribers->getSubscribers($filter, $limit, $offset); |
| 113 | } |
| 114 | |
| 115 | public function getSubscribersCount(array $filter = []): int { |
| 116 | return $this->subscribers->getSubscribersCount($filter); |
| 117 | } |
| 118 | |
| 119 | public function isSetupComplete() { |
| 120 | return !( |
| 121 | $this->changelog->shouldShowWelcomeWizard() |
| 122 | || $this->changelog->shouldShowWooCommerceListImportPage() |
| 123 | || $this->changelog->shouldShowRevenueTrackingPermissionPage() |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | public function getTags(): array { |
| 128 | return $this->tags->getAll(); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @param int|string $tagIdOrName |
| 133 | */ |
| 134 | public function getTag($tagIdOrName): array { |
| 135 | return $this->tags->getTag($tagIdOrName); |
| 136 | } |
| 137 | |
| 138 | public function addTag(array $tag): array { |
| 139 | return $this->tags->addTag($tag); |
| 140 | } |
| 141 | |
| 142 | public function updateTag(array $tag): array { |
| 143 | return $this->tags->updateTag($tag); |
| 144 | } |
| 145 | |
| 146 | public function deleteTag(string $tagId): bool { |
| 147 | return $this->tags->deleteTag($tagId); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @param int|string $subscriberIdOrEmail |
| 152 | * @param int|string $tagIdOrName |
| 153 | */ |
| 154 | public function tagSubscriber($subscriberIdOrEmail, $tagIdOrName): array { |
| 155 | return $this->subscribers->tagSubscriber($subscriberIdOrEmail, $tagIdOrName); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @param int|string $subscriberIdOrEmail |
| 160 | * @param int|string $tagIdOrName |
| 161 | */ |
| 162 | public function untagSubscriber($subscriberIdOrEmail, $tagIdOrName): array { |
| 163 | return $this->subscribers->untagSubscriber($subscriberIdOrEmail, $tagIdOrName); |
| 164 | } |
| 165 | } |
| 166 |