PluginProbe ʕ •ᴥ•ʔ
FAPI Member / 2.1.18
FAPI Member v2.1.18
2.2.33 2.2.32 trunk 1.9.47 2.1.18 2.2.24 2.2.25 2.2.26 2.2.28 2.2.29 2.2.30 2.2.31
fapi-member / src / Service / EmailService.php
fapi-member / src / Service Last commit date
ApiService.php 1 year ago ElementService.php 1 year ago EmailService.php 1 year ago FormService.php 1 year ago LevelOrderService.php 1 year ago LevelService.php 1 year ago MembershipService.php 1 year ago RedirectService.php 1 year ago SanitizationService.php 1 year ago UserService.php 1 year ago
EmailService.php
214 lines
1 <?php
2
3 namespace FapiMember\Service;
4
5 use FapiMember\Api\V2\ApiController;
6 use FapiMember\Container\Container;
7 use FapiMember\FapiMemberPlugin;
8 use FapiMember\Model\Enums\Keys\OptionKey;
9 use FapiMember\Model\Enums\Types\EmailType;
10 use FapiMember\Model\MemberLevel;
11 use FapiMember\Repository\EmailRepository;
12 use FapiMember\Repository\MembershipHistoryRepository;
13 use FapiMember\Utils\EmailHelper;
14 use FapiMember\Utils\SecurityValidator;
15
16 class EmailService
17 {
18 private ApiService $apiService;
19 private MembershipHistoryRepository $membershipHistoryRepository;
20 private EmailRepository $emailRepository;
21 private ApiController $apiController;
22
23 public function __construct()
24 {
25 $this->apiService = Container::get(ApiService::class);
26 $this->membershipHistoryRepository = Container::get(MembershipHistoryRepository::class);
27 $this->emailRepository = Container::get(EmailRepository::class);
28 $this->apiController = Container::get(ApiController::class);
29 }
30
31 public function sendEmail(string $email, string $type, int $levelId, array $props): bool
32 {
33 $emails = $this->emailRepository->getTemplatesForLevel($levelId, true);
34
35 if (!isset($emails[$type])) {
36 return false;
37 }
38
39 $subject = $emails[$type]['s'];
40 $body = $emails[$type]['b'];
41 $subject = EmailHelper::replaceShortcodes($subject, $props);
42 $body = EmailHelper::replaceShortcodes($body, $props);
43
44 return wp_mail($email, $subject, $body);
45 }
46
47 /**
48 * @param array<MemberLevel> $levels
49 * @return array<array<string|MemberLevel>>
50 */
51 public function findEmailsToSend(
52 array $levels,
53 bool $wasUserCreated,
54 bool $newToMembership,
55 ): array
56 {
57 $toSend = [];
58
59 foreach ($levels as $level) {
60 if ($wasUserCreated === true) {
61 $toSend[] = [EmailType::AFTER_REGISTRATION, $level];
62
63 return $toSend;
64 }
65
66 if ($newToMembership) {
67 $toSend[] = [EmailType::AFTER_ADDING, $level];
68
69 continue;
70 }
71
72 $toSend[] = [EmailType::AFTER_MEMBERSHIP_PROLONGED, $level];
73 }
74
75 return $toSend;
76 }
77
78 public function getEmailFromValidVoucher(array $data): array
79 {
80 $voucherId = $data['voucher'];
81 $voucher = $this->apiService->getVoucher($voucherId);
82 $voucherItemTemplateCode = $voucher['item_template_code'];
83 $itemTemplate = $this->apiService->getItemTemplate($voucherItemTemplateCode);
84
85 if ($voucher === false) {
86 $this->apiController->callbackError(
87 array(
88 'class' => self::class,
89 'description' => 'Error getting voucher.',
90 'errors' => $this->apiService->getLastErrors(),
91 )
92 );
93 }
94
95 $itemTemplate = ($itemTemplate === false) ? array() : $itemTemplate;
96
97 if (
98 !FapiMemberPlugin::isDevelopment() &&
99 !SecurityValidator::isVoucherSecurityValid(
100 $voucher,
101 $itemTemplate,
102 $data['time'],
103 $data['security'],
104 )) {
105 $this->apiController->callbackError(
106 array(
107 'class' => self::class,
108 'description' => 'Voucher security is not valid.',
109 )
110 );
111 }
112
113 if (!isset($voucher['status']) || $voucher['status'] !== 'applied') {
114 $this->apiController->callbackError(
115 array(
116 'class' => self::class,
117 'description' => 'Voucher is not applied.',
118 )
119 );
120 }
121
122 if (!isset( $voucher['applicant']['email'])) {
123 $this->apiController->callbackError(
124 array(
125 'class' => self::class,
126 'description' => 'Cannot find applicant email in API response.',
127 )
128 );
129 }
130
131 return array('email' => $voucher['applicant']['email']);
132 }
133
134 public function getEmailFromPaidInvoice(array $data): array
135 {
136 $invoice = $this->apiService->getInvoice((int) $data['id']);
137
138 if ($invoice === false) {
139 $this->apiController->callbackError(
140 array(
141 'class' => self::class,
142 'description' => 'Error getting invoice.',
143 'errors' => $this->apiService->getLastErrors(),
144 )
145 );
146 }
147
148 if (!FapiMemberPlugin::isDevelopment() &&
149 !SecurityValidator::isInvoiceSecurityValid($invoice, $data['time'], $data['security'])
150 ) {
151 $this->apiController->callbackError(
152 array(
153 'class' => self::class,
154 'description' => 'Invoice security is not valid.',
155 )
156 );
157 }
158
159 if (isset($invoice['parent'])) {
160 $this->apiController->callbackError(
161 array(
162 'class' => self::class,
163 'description' => 'Invoice parent is set and not null.',
164 )
165 );
166 }
167
168 if (!isset($invoice['customer']['email'])) {
169 $this->apiController->callbackError(
170 array(
171 'class' => self::class,
172 'description' => 'Cannot find customer email in API response.',
173 )
174 );
175 }
176
177 return array(
178 'email' => $invoice['customer']['email'],
179 'first_name' => isset($invoice['customer']['first_name']) ? $invoice['customer']['first_name'] : null,
180 'last_name' => isset($invoice['customer']['last_name']) ? $invoice['customer']['last_name'] : null,
181 );
182 }
183
184 public function getEmailFromBodyWithValidToken(array $data): array
185 {
186 $token = get_option(OptionKey::TOKEN, null);
187
188 if ($data['token'] !== $token) {
189 $this->apiController->callbackError(
190 array(
191 'class' => self::class,
192 'description' => 'Invalid token provided. Check token correctness.',
193 )
194 );
195 }
196
197 if (!isset($data['email'])) {
198 $this->apiController->callbackError(
199 array(
200 'class' => self::class,
201 'description' => 'Parameter email is missing.',
202 )
203 );
204 }
205
206 return array(
207 'email' => $data['email'],
208 'first_name' => isset($data['first_name']) ? $data['first_name'] : null,
209 'last_name' => isset($data['last_name']) ? $data['last_name'] : null,
210 );
211 }
212
213 }
214