FormsBulkActionEndpoint.php
2 months ago
FormsEndpoint.php
2 months ago
FormsListingEndpoint.php
1 month ago
index.php
2 months ago
FormsListingEndpoint.php
153 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Form\RestApi\Endpoints; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\JSON\ResponseBuilders\FormsResponseBuilder; |
| 9 | use MailPoet\API\REST\AbstractListingEndpoint; |
| 10 | use MailPoet\API\REST\ApiException; |
| 11 | use MailPoet\API\REST\ListingRequestValidationTrait; |
| 12 | use MailPoet\API\REST\Request; |
| 13 | use MailPoet\API\REST\Response; |
| 14 | use MailPoet\Config\AccessControl; |
| 15 | use MailPoet\Entities\FormEntity; |
| 16 | use MailPoet\Form\Listing\FormListingRepository; |
| 17 | use MailPoet\Listing\Handler as ListingHandler; |
| 18 | use MailPoet\Listing\ListingDefinition; |
| 19 | use MailPoet\Listing\ListingRepository; |
| 20 | use MailPoet\Validator\Builder; |
| 21 | use MailPoet\WP\Functions as WPFunctions; |
| 22 | |
| 23 | class FormsListingEndpoint extends AbstractListingEndpoint { |
| 24 | use ListingRequestValidationTrait; |
| 25 | |
| 26 | private const ALLOWED_SORT_FIELDS = ['name', 'created_at', 'updated_at']; |
| 27 | private const ALLOWED_FILTERS = ['status', 'created_from', 'created_to', 'updated_from', 'updated_to']; |
| 28 | |
| 29 | /** @var FormListingRepository */ |
| 30 | private $formListingRepository; |
| 31 | |
| 32 | /** @var FormsResponseBuilder */ |
| 33 | private $formsResponseBuilder; |
| 34 | |
| 35 | public function __construct( |
| 36 | ListingHandler $listingHandler, |
| 37 | FormListingRepository $formListingRepository, |
| 38 | FormsResponseBuilder $formsResponseBuilder |
| 39 | ) { |
| 40 | parent::__construct($listingHandler); |
| 41 | $this->formListingRepository = $formListingRepository; |
| 42 | $this->formsResponseBuilder = $formsResponseBuilder; |
| 43 | } |
| 44 | |
| 45 | public function handle(Request $request): Response { |
| 46 | $this->validateRequest($request); |
| 47 | return parent::handle($request); |
| 48 | } |
| 49 | |
| 50 | public function checkPermissions(): bool { |
| 51 | return WPFunctions::get()->currentUserCan(AccessControl::PERMISSION_MANAGE_FORMS); |
| 52 | } |
| 53 | |
| 54 | public static function getRequestSchema(): array { |
| 55 | $schema = parent::getRequestSchema(); |
| 56 | $schema['limit'] = Builder::integer(); |
| 57 | $schema['offset'] = Builder::integer(); |
| 58 | $schema['filter'] = Builder::object(); |
| 59 | return $schema; |
| 60 | } |
| 61 | |
| 62 | protected function getListingRepository(): ListingRepository { |
| 63 | return $this->formListingRepository; |
| 64 | } |
| 65 | |
| 66 | protected function buildItems(array $rows, ListingDefinition $definition): array { |
| 67 | return $this->formsResponseBuilder->buildForListing($rows); |
| 68 | } |
| 69 | |
| 70 | protected function getDefaultSortBy(): string { |
| 71 | return 'updated_at'; |
| 72 | } |
| 73 | |
| 74 | protected function getDefaultSortOrder(): string { |
| 75 | return 'desc'; |
| 76 | } |
| 77 | |
| 78 | protected function getDefaultGroup(): ?string { |
| 79 | return 'all'; |
| 80 | } |
| 81 | |
| 82 | protected function getListingValidationErrorPrefix(): string { |
| 83 | return 'forms'; |
| 84 | } |
| 85 | |
| 86 | private function validateRequest(Request $request): void { |
| 87 | $this->validateSortField($request->getParam('orderby'), self::ALLOWED_SORT_FIELDS); |
| 88 | $this->validateSortField($request->getParam('sort_by'), self::ALLOWED_SORT_FIELDS); |
| 89 | $this->validateSortOrder($request->getParam('order')); |
| 90 | $this->validateSortOrder($request->getParam('sort_order')); |
| 91 | $this->validatePagination($request); |
| 92 | $this->validateFilters($request->getParam('filter')); |
| 93 | } |
| 94 | |
| 95 | /** @param mixed $filters */ |
| 96 | private function validateFilters($filters): void { |
| 97 | if ($filters === null || $filters === []) { |
| 98 | return; |
| 99 | } |
| 100 | if (!is_array($filters)) { |
| 101 | throw new ApiException( |
| 102 | __('Filters must be an object.', 'mailpoet'), |
| 103 | 400, |
| 104 | 'mailpoet_forms_invalid_filter' |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | $normalizedFilters = []; |
| 109 | foreach ($filters as $filter => $value) { |
| 110 | if (!is_string($filter) || !in_array($filter, self::ALLOWED_FILTERS, true)) { |
| 111 | throw new ApiException( |
| 112 | __('Unsupported forms filter.', 'mailpoet'), |
| 113 | 400, |
| 114 | 'mailpoet_forms_invalid_filter' |
| 115 | ); |
| 116 | } |
| 117 | $normalizedFilters[$filter] = $value; |
| 118 | } |
| 119 | |
| 120 | $this->validateStatusFilter($normalizedFilters); |
| 121 | $this->validateDateRange( |
| 122 | $this->validateDateFilter($normalizedFilters, 'created_from'), |
| 123 | $this->validateDateFilter($normalizedFilters, 'created_to') |
| 124 | ); |
| 125 | $this->validateDateRange( |
| 126 | $this->validateDateFilter($normalizedFilters, 'updated_from'), |
| 127 | $this->validateDateFilter($normalizedFilters, 'updated_to') |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | /** @param array<string, mixed> $filters */ |
| 132 | private function validateStatusFilter(array $filters): void { |
| 133 | if (!array_key_exists('status', $filters) || $filters['status'] === '' || $filters['status'] === []) { |
| 134 | return; |
| 135 | } |
| 136 | $statuses = is_array($filters['status']) ? $filters['status'] : [$filters['status']]; |
| 137 | $allowed = [FormEntity::STATUS_ENABLED, FormEntity::STATUS_DISABLED]; |
| 138 | foreach ($statuses as $status) { |
| 139 | if (!is_string($status) || !in_array($status, $allowed, true)) { |
| 140 | throw new ApiException( |
| 141 | sprintf( |
| 142 | // translators: %s is a comma-separated list of allowed form statuses. |
| 143 | __('Unsupported status filter. Allowed values are: %s.', 'mailpoet'), |
| 144 | implode(', ', $allowed) |
| 145 | ), |
| 146 | 400, |
| 147 | 'mailpoet_forms_invalid_status' |
| 148 | ); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 |