CustomFieldsBulkActionEndpoint.php
2 months ago
CustomFieldsDuplicateEndpoint.php
2 months ago
CustomFieldsEndpoint.php
1 month ago
CustomFieldsGetEndpoint.php
1 month ago
CustomFieldsPostEndpoint.php
2 months ago
CustomFieldsPutEndpoint.php
2 months ago
index.php
2 months ago
CustomFieldsPostEndpoint.php
104 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\CustomFields\RestApi\Endpoints; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use InvalidArgumentException; |
| 9 | use MailPoet\API\REST\Request; |
| 10 | use MailPoet\API\REST\Response; |
| 11 | use MailPoet\CustomFields\ApiDataSanitizer; |
| 12 | use MailPoet\CustomFields\CustomFieldsRepository; |
| 13 | use MailPoet\CustomFields\RestApi\CustomFieldApiException; |
| 14 | use MailPoet\Entities\CustomFieldEntity; |
| 15 | use MailPoet\Validator\Builder; |
| 16 | use MailPoetVendor\Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
| 17 | |
| 18 | class CustomFieldsPostEndpoint extends CustomFieldsEndpoint { |
| 19 | /** @var CustomFieldsRepository */ |
| 20 | private $customFieldsRepository; |
| 21 | |
| 22 | /** @var ApiDataSanitizer */ |
| 23 | private $apiDataSanitizer; |
| 24 | |
| 25 | public function __construct( |
| 26 | CustomFieldsRepository $customFieldsRepository, |
| 27 | ApiDataSanitizer $apiDataSanitizer |
| 28 | ) { |
| 29 | $this->customFieldsRepository = $customFieldsRepository; |
| 30 | $this->apiDataSanitizer = $apiDataSanitizer; |
| 31 | } |
| 32 | |
| 33 | public function handle(Request $request): Response { |
| 34 | try { |
| 35 | $data = $this->apiDataSanitizer->sanitize($this->getRequestData($request)); |
| 36 | } catch (InvalidArgumentException $exception) { |
| 37 | throw new CustomFieldApiException( |
| 38 | $exception->getMessage(), |
| 39 | 400, |
| 40 | 'mailpoet_custom_fields_invalid_data', |
| 41 | [], |
| 42 | $exception |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | $existing = $this->customFieldsRepository->findOneBy(['name' => $data['name']]); |
| 47 | if ($existing instanceof CustomFieldEntity) { |
| 48 | throw new CustomFieldApiException( |
| 49 | __('A custom field with this name already exists.', 'mailpoet'), |
| 50 | 409, |
| 51 | 'mailpoet_custom_fields_duplicate' |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | try { |
| 56 | $customField = $this->customFieldsRepository->createOrUpdate($data); |
| 57 | } catch (UniqueConstraintViolationException $exception) { |
| 58 | // Concurrent request created a field with the same name between the duplicate-name check and the insert. |
| 59 | throw new CustomFieldApiException( |
| 60 | __('A custom field with this name already exists.', 'mailpoet'), |
| 61 | 409, |
| 62 | 'mailpoet_custom_fields_duplicate', |
| 63 | [], |
| 64 | $exception |
| 65 | ); |
| 66 | } |
| 67 | return new Response($this->buildItem($customField), 201); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @return array{name: string, type: string, params: array} |
| 72 | */ |
| 73 | private function getRequestData(Request $request): array { |
| 74 | $rawName = $request->getParam('name'); |
| 75 | $rawType = $request->getParam('type'); |
| 76 | $rawParams = $request->getParam('params'); |
| 77 | return [ |
| 78 | 'name' => sanitize_text_field(is_scalar($rawName) ? (string)$rawName : ''), |
| 79 | 'type' => sanitize_key(is_scalar($rawType) ? (string)$rawType : ''), |
| 80 | 'params' => is_array($rawParams) ? $this->sanitizeParams($rawParams) : [], |
| 81 | ]; |
| 82 | } |
| 83 | |
| 84 | private function sanitizeParams(array $params): array { |
| 85 | $sanitized = []; |
| 86 | foreach ($params as $key => $value) { |
| 87 | if (is_array($value)) { |
| 88 | $sanitized[$key] = $this->sanitizeParams($value); |
| 89 | } elseif (is_scalar($value)) { |
| 90 | $sanitized[$key] = sanitize_text_field((string)$value); |
| 91 | } |
| 92 | } |
| 93 | return $sanitized; |
| 94 | } |
| 95 | |
| 96 | public static function getRequestSchema(): array { |
| 97 | return [ |
| 98 | 'name' => Builder::string()->required()->minLength(1), |
| 99 | 'type' => Builder::string()->required()->minLength(1), |
| 100 | 'params' => Builder::object(), |
| 101 | ]; |
| 102 | } |
| 103 | } |
| 104 |