ApiService.php
11 months ago
ElementService.php
11 months ago
EmailService.php
11 months ago
FormService.php
11 months ago
LevelOrderService.php
11 months ago
LevelService.php
11 months ago
MembershipService.php
11 months ago
RedirectService.php
11 months ago
SanitizationService.php
11 months ago
StatisticsService.php
11 months ago
UserService.php
11 months ago
EmailService.php
191 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 (!isset($voucher['status']) || $voucher['status'] !== 'applied') { |
| 91 | $this->apiController->callbackError( |
| 92 | array( |
| 93 | 'class' => self::class, |
| 94 | 'description' => 'Voucher is not applied.', |
| 95 | ) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | if (!isset($voucher['applicant']['email'])) { |
| 100 | $this->apiController->callbackError( |
| 101 | array( |
| 102 | 'class' => self::class, |
| 103 | 'description' => 'Cannot find applicant email in API response.', |
| 104 | ) |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | return array('email' => $voucher['applicant']['email']); |
| 109 | } |
| 110 | |
| 111 | public function getEmailFromPaidInvoice(array $data): array |
| 112 | { |
| 113 | $invoice = $this->apiService->getInvoice((int) $data['id']); |
| 114 | |
| 115 | if ($invoice === false) { |
| 116 | $this->apiController->callbackError( |
| 117 | array( |
| 118 | 'class' => self::class, |
| 119 | 'description' => 'Error getting invoice.', |
| 120 | 'errors' => $this->apiService->getLastErrors(), |
| 121 | ) |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | if (!FapiMemberPlugin::isDevelopment() && |
| 126 | !SecurityValidator::isInvoiceSecurityValid($invoice, $data['time'], $data['security']) |
| 127 | ) { |
| 128 | $this->apiController->callbackError( |
| 129 | array( |
| 130 | 'class' => self::class, |
| 131 | 'description' => 'Invoice security is not valid.', |
| 132 | ) |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | if (isset($invoice['parent'])) { |
| 137 | $this->apiController->callbackError( |
| 138 | array( |
| 139 | 'class' => self::class, |
| 140 | 'description' => 'Invoice parent is set and not null.', |
| 141 | ) |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | if (!isset($invoice['customer']['email'])) { |
| 146 | $this->apiController->callbackError( |
| 147 | array( |
| 148 | 'class' => self::class, |
| 149 | 'description' => 'Cannot find customer email in API response.', |
| 150 | ) |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | return array( |
| 155 | 'email' => $invoice['customer']['email'], |
| 156 | 'first_name' => isset($invoice['customer']['first_name']) ? $invoice['customer']['first_name'] : null, |
| 157 | 'last_name' => isset($invoice['customer']['last_name']) ? $invoice['customer']['last_name'] : null, |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | public function getEmailFromBodyWithValidToken(array $data): array |
| 162 | { |
| 163 | $token = get_option(OptionKey::TOKEN, null); |
| 164 | |
| 165 | if ($data['token'] !== $token) { |
| 166 | $this->apiController->callbackError( |
| 167 | array( |
| 168 | 'class' => self::class, |
| 169 | 'description' => 'Invalid token provided. Check token correctness.', |
| 170 | ) |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | if (!isset($data['email'])) { |
| 175 | $this->apiController->callbackError( |
| 176 | array( |
| 177 | 'class' => self::class, |
| 178 | 'description' => 'Parameter email is missing.', |
| 179 | ) |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | return array( |
| 184 | 'email' => $data['email'], |
| 185 | 'first_name' => isset($data['first_name']) ? $data['first_name'] : null, |
| 186 | 'last_name' => isset($data['last_name']) ? $data['last_name'] : null, |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | } |
| 191 |