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
CustomFields.php
88 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\API\MP\v1; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\CustomFields\ApiDataSanitizer; |
| 9 | use MailPoet\CustomFields\CustomFieldsRepository; |
| 10 | |
| 11 | class CustomFields { |
| 12 | /** @var ApiDataSanitizer */ |
| 13 | private $customFieldsDataSanitizer; |
| 14 | |
| 15 | /** @var CustomFieldsRepository */ |
| 16 | private $customFieldsRepository; |
| 17 | |
| 18 | public function __construct( |
| 19 | ApiDataSanitizer $customFieldsDataSanitizer, |
| 20 | CustomFieldsRepository $customFieldsRepository |
| 21 | ) { |
| 22 | $this->customFieldsDataSanitizer = $customFieldsDataSanitizer; |
| 23 | $this->customFieldsRepository = $customFieldsRepository; |
| 24 | } |
| 25 | |
| 26 | public function getSubscriberFields(): array { |
| 27 | $data = [ |
| 28 | [ |
| 29 | 'id' => 'email', |
| 30 | 'name' => __('Email', 'mailpoet'), |
| 31 | 'type' => 'text', |
| 32 | 'params' => [ |
| 33 | 'required' => '1', |
| 34 | ], |
| 35 | ], |
| 36 | [ |
| 37 | 'id' => 'first_name', |
| 38 | 'name' => __('First name', 'mailpoet'), |
| 39 | 'type' => 'text', |
| 40 | 'params' => [ |
| 41 | 'required' => '', |
| 42 | ], |
| 43 | ], |
| 44 | [ |
| 45 | 'id' => 'last_name', |
| 46 | 'name' => __('Last name', 'mailpoet'), |
| 47 | 'type' => 'text', |
| 48 | 'params' => [ |
| 49 | 'required' => '', |
| 50 | ], |
| 51 | ], |
| 52 | ]; |
| 53 | |
| 54 | $customFields = $this->customFieldsRepository->findAllActive(); |
| 55 | foreach ($customFields as $customField) { |
| 56 | $result = [ |
| 57 | 'id' => 'cf_' . $customField->getId(), |
| 58 | 'name' => $customField->getName(), |
| 59 | 'type' => $customField->getType(), |
| 60 | 'params' => $customField->getParams(), |
| 61 | ]; |
| 62 | $data[] = $result; |
| 63 | } |
| 64 | |
| 65 | return $data; |
| 66 | } |
| 67 | |
| 68 | public function addSubscriberField(array $data = []): array { |
| 69 | // Run sanitize() OUTSIDE the try/catch so its InvalidArgumentException |
| 70 | // propagates to API::addSubscriberField, which maps it to an APIException |
| 71 | // carrying the original sanitizer code (1001-1010). Wrapping it here would |
| 72 | // collapse every validation error into FAILED_TO_SAVE_SUBSCRIBER_FIELD (1) |
| 73 | // and make the documented sanitizer codes unreachable. |
| 74 | $sanitized = $this->customFieldsDataSanitizer->sanitize($data); |
| 75 | try { |
| 76 | $customField = $this->customFieldsRepository->createOrUpdate($sanitized); |
| 77 | } catch (\Exception $e) { |
| 78 | throw new APIException('Failed to save a new subscriber field ' . $e->getMessage(), APIException::FAILED_TO_SAVE_SUBSCRIBER_FIELD); |
| 79 | } |
| 80 | return [ |
| 81 | 'id' => 'cf_' . $customField->getId(), |
| 82 | 'name' => $customField->getName(), |
| 83 | 'type' => $customField->getType(), |
| 84 | 'params' => $customField->getParams(), |
| 85 | ]; |
| 86 | } |
| 87 | } |
| 88 |