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