PluginProbe
MailPoet – Newsletters, Email Marketing, and Automation / 5.34.1
MailPoet – Newsletters, Email Marketing, and Automation v5.34.1
5.38.0 5.37.0 5.36.1 5.36.0 5.35.1 5.35.0 5.34.3 5.34.2 5.34.1 5.34.0 5.33.1 5.33.0 5.32.0 5.31.0 5.30.0 5.29.0 5.28.1 5.28.0 5.27.0 5.26.0 5.26.1 5.25.0 5.24.0 4.43.0 4.43.1 All 542 releases
mailpoet / lib / CustomFields / RestApi / Endpoints / CustomFieldsGetEndpoint.php
CustomFieldsGetEndpoint.php
136 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types = 1);
2
3 namespace MailPoet\CustomFields\RestApi\Endpoints;
4
5 if (!defined('ABSPATH')) exit;
6
7
8 use MailPoet\API\REST\Request;
9 use MailPoet\API\REST\Response;
10 use MailPoet\CustomFields\CustomFieldsRepository;
11 use MailPoet\CustomFields\RestApi\CustomFieldApiException;
12 use MailPoet\Entities\CustomFieldEntity;
13 use MailPoet\Validator\Builder;
14
15 class CustomFieldsGetEndpoint extends CustomFieldsEndpoint {
16 private const ALLOWED_TYPES = [
17 CustomFieldEntity::TYPE_TEXT,
18 CustomFieldEntity::TYPE_TEXTAREA,
19 CustomFieldEntity::TYPE_RADIO,
20 CustomFieldEntity::TYPE_CHECKBOX,
21 CustomFieldEntity::TYPE_SELECT,
22 CustomFieldEntity::TYPE_DATE,
23 ];
24
25 /** @var CustomFieldsRepository */
26 private $customFieldsRepository;
27
28 public function __construct(
29 CustomFieldsRepository $customFieldsRepository
30 ) {
31 $this->customFieldsRepository = $customFieldsRepository;
32 }
33
34 public function handle(Request $request): Response {
35 $search = is_string($request->getParam('search')) ? (string)$request->getParam('search') : '';
36 $orderby = is_string($request->getParam('orderby')) ? (string)$request->getParam('orderby') : 'name';
37 $order = is_string($request->getParam('order')) ? (string)$request->getParam('order') : 'asc';
38 $page = is_numeric($request->getParam('page')) ? max(1, (int)$request->getParam('page')) : 1;
39 $perPage = is_numeric($request->getParam('per_page')) ? max(1, min(100, (int)$request->getParam('per_page'))) : 25;
40 $group = is_string($request->getParam('group')) ? (string)$request->getParam('group') : 'all';
41 $filter = $this->parseFilter($request->getParam('filter'));
42
43 $result = $this->customFieldsRepository->listWithCounts([
44 'search' => $search,
45 'orderby' => $orderby,
46 'order' => $order,
47 'page' => $page,
48 'per_page' => $perPage,
49 'group' => $group,
50 'filter' => $filter,
51 ]);
52
53 $items = array_map([$this, 'buildItemFromRow'], $result['items']);
54 $pages = $result['total'] === 0 ? 0 : (int)ceil($result['total'] / max(1, $perPage));
55
56 return new Response([
57 'items' => $items,
58 'meta' => [
59 'count' => $result['total'],
60 'pages' => $pages,
61 ],
62 'groups' => $result['groups'],
63 ]);
64 }
65
66 /**
67 * @param mixed $rawFilter
68 * @return array{type?: string[]}
69 */
70 private function parseFilter($rawFilter): array {
71 if ($rawFilter === null || $rawFilter === '' || $rawFilter === []) {
72 return [];
73 }
74 if (!is_array($rawFilter)) {
75 throw new CustomFieldApiException(
76 __('Filters must be an object.', 'mailpoet'),
77 400,
78 'mailpoet_custom_fields_invalid_filter'
79 );
80 }
81
82 $allowed = ['type'];
83 foreach (array_keys($rawFilter) as $key) {
84 if (!in_array($key, $allowed, true)) {
85 throw new CustomFieldApiException(
86 __('Unsupported custom fields filter.', 'mailpoet'),
87 400,
88 'mailpoet_custom_fields_invalid_filter'
89 );
90 }
91 }
92
93 $filter = [];
94 if (array_key_exists('type', $rawFilter)) {
95 $filter['type'] = $this->parseTypeFilter($rawFilter['type']);
96 }
97
98 return $filter;
99 }
100
101 /**
102 * @param mixed $rawType
103 * @return string[]
104 */
105 private function parseTypeFilter($rawType): array {
106 if ($rawType === '' || $rawType === null) {
107 return [];
108 }
109 $values = is_array($rawType) ? $rawType : [$rawType];
110 $types = [];
111 foreach ($values as $value) {
112 if (!is_string($value) || !in_array($value, self::ALLOWED_TYPES, true)) {
113 throw new CustomFieldApiException(
114 __('Unsupported custom field type filter.', 'mailpoet'),
115 400,
116 'mailpoet_custom_fields_invalid_type'
117 );
118 }
119 $types[] = $value;
120 }
121 return array_values(array_unique($types));
122 }
123
124 public static function getRequestSchema(): array {
125 return [
126 'search' => Builder::string(),
127 'orderby' => Builder::string(),
128 'order' => Builder::string(),
129 'page' => Builder::integer(),
130 'per_page' => Builder::integer(),
131 'group' => Builder::string(),
132 'filter' => Builder::object(),
133 ];
134 }
135 }
136