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
CustomFieldsPutEndpoint.php
134 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 CustomFieldsPutEndpoint 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 | $id = $this->getId($request); |
| 35 | $customField = $this->customFieldsRepository->findOneById($id); |
| 36 | if (!$customField instanceof CustomFieldEntity || $customField->getDeletedAt() !== null) { |
| 37 | throw new CustomFieldApiException( |
| 38 | __('The custom field does not exist.', 'mailpoet'), |
| 39 | 404, |
| 40 | 'mailpoet_custom_fields_not_found' |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | $requestData = $this->getRequestData($request); |
| 45 | if ( |
| 46 | $requestData['type'] !== '' |
| 47 | && $requestData['type'] !== $customField->getType() |
| 48 | && $this->customFieldsRepository->hasSubscriberValues($id) |
| 49 | ) { |
| 50 | throw new CustomFieldApiException( |
| 51 | __('The custom field type cannot be changed because subscribers have values stored for this field.', 'mailpoet'), |
| 52 | 409, |
| 53 | 'mailpoet_custom_fields_type_locked' |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | try { |
| 58 | $data = $this->apiDataSanitizer->sanitize($requestData); |
| 59 | } catch (InvalidArgumentException $exception) { |
| 60 | throw new CustomFieldApiException( |
| 61 | $exception->getMessage(), |
| 62 | 400, |
| 63 | 'mailpoet_custom_fields_invalid_data', |
| 64 | [], |
| 65 | $exception |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | $existing = $this->customFieldsRepository->findOneBy(['name' => $data['name']]); |
| 70 | if ($existing instanceof CustomFieldEntity && $existing->getId() !== $id) { |
| 71 | throw new CustomFieldApiException( |
| 72 | __('A custom field with this name already exists.', 'mailpoet'), |
| 73 | 409, |
| 74 | 'mailpoet_custom_fields_duplicate' |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | $data['id'] = $id; |
| 79 | try { |
| 80 | $customField = $this->customFieldsRepository->createOrUpdate($data); |
| 81 | } catch (UniqueConstraintViolationException $exception) { |
| 82 | // Concurrent request renamed another field to this name between the duplicate-name check and the update. |
| 83 | throw new CustomFieldApiException( |
| 84 | __('A custom field with this name already exists.', 'mailpoet'), |
| 85 | 409, |
| 86 | 'mailpoet_custom_fields_duplicate', |
| 87 | [], |
| 88 | $exception |
| 89 | ); |
| 90 | } |
| 91 | return new Response($this->buildItem($customField)); |
| 92 | } |
| 93 | |
| 94 | private function getId(Request $request): int { |
| 95 | $rawId = $request->getParam('id'); |
| 96 | return (int)(is_scalar($rawId) ? $rawId : 0); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @return array{name: string, type: string, params: array} |
| 101 | */ |
| 102 | private function getRequestData(Request $request): array { |
| 103 | $rawName = $request->getParam('name'); |
| 104 | $rawType = $request->getParam('type'); |
| 105 | $rawParams = $request->getParam('params'); |
| 106 | return [ |
| 107 | 'name' => sanitize_text_field(is_scalar($rawName) ? (string)$rawName : ''), |
| 108 | 'type' => sanitize_key(is_scalar($rawType) ? (string)$rawType : ''), |
| 109 | 'params' => is_array($rawParams) ? $this->sanitizeParams($rawParams) : [], |
| 110 | ]; |
| 111 | } |
| 112 | |
| 113 | private function sanitizeParams(array $params): array { |
| 114 | $sanitized = []; |
| 115 | foreach ($params as $key => $value) { |
| 116 | if (is_array($value)) { |
| 117 | $sanitized[$key] = $this->sanitizeParams($value); |
| 118 | } elseif (is_scalar($value)) { |
| 119 | $sanitized[$key] = sanitize_text_field((string)$value); |
| 120 | } |
| 121 | } |
| 122 | return $sanitized; |
| 123 | } |
| 124 | |
| 125 | public static function getRequestSchema(): array { |
| 126 | return [ |
| 127 | 'id' => Builder::integer()->required(), |
| 128 | 'name' => Builder::string()->required()->minLength(1), |
| 129 | 'type' => Builder::string()->required()->minLength(1), |
| 130 | 'params' => Builder::object(), |
| 131 | ]; |
| 132 | } |
| 133 | } |
| 134 |