AbstractSegmentsListingEndpoint.php
1 month ago
DynamicSegmentsBulkActionEndpoint.php
2 months ago
DynamicSegmentsListingEndpoint.php
1 month ago
SegmentRequestValidationTrait.php
2 months ago
SegmentsBulkActionEndpoint.php
2 months ago
SegmentsEndpoint.php
2 months ago
SegmentsListingEndpoint.php
1 month ago
index.php
2 months ago
AbstractSegmentsListingEndpoint.php
172 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Segments\RestApi\Endpoints; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\REST\AbstractListingEndpoint; |
| 9 | use MailPoet\API\REST\ApiException; |
| 10 | use MailPoet\API\REST\ListingRequestValidationTrait; |
| 11 | use MailPoet\API\REST\Request; |
| 12 | use MailPoet\API\REST\Response; |
| 13 | use MailPoet\Listing\Handler as ListingHandler; |
| 14 | use MailPoet\Validator\Builder; |
| 15 | |
| 16 | abstract class AbstractSegmentsListingEndpoint extends AbstractListingEndpoint { |
| 17 | use SegmentRequestValidationTrait; |
| 18 | // Reused only for the shared `yyyy-MM-dd` date filter validators |
| 19 | // (validateDateFilter / validateDateRange); pagination and sort use the |
| 20 | // segment-specific SegmentRequestValidationTrait above. |
| 21 | use ListingRequestValidationTrait; |
| 22 | |
| 23 | /** @var string[] */ |
| 24 | private $allowedSortFields; |
| 25 | |
| 26 | /** @var int */ |
| 27 | private $defaultPerPage; |
| 28 | |
| 29 | /** |
| 30 | * @param string[] $allowedSortFields |
| 31 | */ |
| 32 | public function __construct( |
| 33 | ListingHandler $listingHandler, |
| 34 | array $allowedSortFields, |
| 35 | int $defaultPerPage |
| 36 | ) { |
| 37 | parent::__construct($listingHandler); |
| 38 | $this->allowedSortFields = $allowedSortFields; |
| 39 | $this->defaultPerPage = $defaultPerPage; |
| 40 | } |
| 41 | |
| 42 | public function handle(Request $request): Response { |
| 43 | $this->validateListingRequest($request); |
| 44 | return parent::handle($request); |
| 45 | } |
| 46 | |
| 47 | public static function getRequestSchema(): array { |
| 48 | $schema = parent::getRequestSchema(); |
| 49 | $schema['limit'] = Builder::integer(); |
| 50 | $schema['offset'] = Builder::integer(); |
| 51 | $schema['sort_by'] = Builder::string(); |
| 52 | $schema['sort_order'] = Builder::string(); |
| 53 | return $schema; |
| 54 | } |
| 55 | |
| 56 | abstract protected function allowsSearch(): bool; |
| 57 | |
| 58 | /** |
| 59 | * Filter keys this listing accepts in the `filter` request param. Each |
| 60 | * listing declares its own so unknown keys are rejected with a 400. |
| 61 | * |
| 62 | * @return string[] |
| 63 | */ |
| 64 | abstract protected function getAllowedFilters(): array; |
| 65 | |
| 66 | protected function getListingValidationErrorPrefix(): string { |
| 67 | return 'segments'; |
| 68 | } |
| 69 | |
| 70 | protected function getDefaultGroup(): ?string { |
| 71 | return 'all'; |
| 72 | } |
| 73 | |
| 74 | protected function validateListingRequest(Request $request): void { |
| 75 | $this->validateGroup(is_string($request->getParam('group')) ? (string)$request->getParam('group') : null); |
| 76 | $orderParam = $request->getParam('order') ?? $request->getParam('sort_order'); |
| 77 | $this->validateOrder(is_string($orderParam) ? (string)$orderParam : null, $this->getDefaultSortOrder()); |
| 78 | |
| 79 | $orderbyParam = $request->getParam('orderby') ?? $request->getParam('sort_by'); |
| 80 | $orderby = is_string($orderbyParam) && $orderbyParam !== '' |
| 81 | ? (string)$orderbyParam |
| 82 | : $this->getDefaultSortBy(); |
| 83 | if (!in_array($orderby, $this->allowedSortFields, true)) { |
| 84 | throw new ApiException( |
| 85 | sprintf( |
| 86 | // translators: %s is the list of supported sort fields. |
| 87 | __('Unsupported sort field. Allowed values are: %s.', 'mailpoet'), |
| 88 | implode(', ', $this->allowedSortFields) |
| 89 | ), |
| 90 | 400, |
| 91 | 'mailpoet_segments_invalid_orderby' |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | $this->validatePage($request->getParam('page')); |
| 96 | $this->validateOffset($request->getParam('offset')); |
| 97 | $this->validatePerPage($request->getParam('per_page') ?? $request->getParam('limit'), $this->defaultPerPage); |
| 98 | |
| 99 | if (!$this->allowsSearch() && is_string($request->getParam('search')) && trim((string)$request->getParam('search')) !== '') { |
| 100 | throw new ApiException( |
| 101 | __('Search is not supported for this listing.', 'mailpoet'), |
| 102 | 400, |
| 103 | 'mailpoet_segments_search_not_supported' |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | $this->validateFilters($request); |
| 108 | } |
| 109 | |
| 110 | protected function validateFilters(Request $request): void { |
| 111 | $filters = $request->getParam('filter'); |
| 112 | if ($filters === null || $filters === []) { |
| 113 | return; |
| 114 | } |
| 115 | if (!is_array($filters)) { |
| 116 | throw new ApiException( |
| 117 | __('Filters must be an object.', 'mailpoet'), |
| 118 | 400, |
| 119 | 'mailpoet_segments_invalid_filter' |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | $allowed = $this->getAllowedFilters(); |
| 124 | // Rebuild with verified string keys so the typed validators below receive an |
| 125 | // array<string, mixed> rather than the request's array<mixed, mixed>. |
| 126 | $normalizedFilters = []; |
| 127 | foreach ($filters as $key => $value) { |
| 128 | if (!is_string($key) || !in_array($key, $allowed, true)) { |
| 129 | throw new ApiException( |
| 130 | __('Unsupported segments filter.', 'mailpoet'), |
| 131 | 400, |
| 132 | 'mailpoet_segments_invalid_filter' |
| 133 | ); |
| 134 | } |
| 135 | $normalizedFilters[$key] = $value; |
| 136 | } |
| 137 | |
| 138 | $this->validateScoreFilter($normalizedFilters); |
| 139 | |
| 140 | foreach ([['created_from', 'created_to'], ['updated_from', 'updated_to']] as [$fromKey, $toKey]) { |
| 141 | if (!in_array($fromKey, $allowed, true)) { |
| 142 | continue; |
| 143 | } |
| 144 | $from = $this->validateDateFilter($normalizedFilters, $fromKey); |
| 145 | $to = $this->validateDateFilter($normalizedFilters, $toKey); |
| 146 | $this->validateDateRange($from, $to); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @param array<string, mixed> $filters |
| 152 | */ |
| 153 | private function validateScoreFilter(array $filters): void { |
| 154 | foreach (['score_min', 'score_max'] as $key) { |
| 155 | if (!array_key_exists($key, $filters) || $filters[$key] === '') { |
| 156 | continue; |
| 157 | } |
| 158 | if (!is_numeric($filters[$key])) { |
| 159 | throw new ApiException( |
| 160 | __('The engagement score filter must be numeric.', 'mailpoet'), |
| 161 | 400, |
| 162 | 'mailpoet_segments_invalid_' . $key |
| 163 | ); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | protected function getDefaultPerPage(): int { |
| 169 | return $this->defaultPerPage; |
| 170 | } |
| 171 | } |
| 172 |