-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBaseController.php
More file actions
149 lines (124 loc) · 3.97 KB
/
BaseController.php
File metadata and controls
149 lines (124 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* Controller which handels general quering, such as pagination.
*/
namespace OWC\PDC\Base\RestAPI\Controllers;
use WP_Query;
use WP_REST_Request;
use OWC\PDC\Base\Foundation\Plugin;
/**
* Controller which handels general quering, such as pagination.
*/
abstract class BaseController
{
/**
* Instance of the plugin.
*/
protected Plugin $plugin;
/**
* Construction, with dependency injection of the BasePlugin.
*
* @return void
*/
public function __construct(Plugin $plugin)
{
$this->plugin = $plugin;
}
/**
* Merges a paginator, based on a WP_Query, inside a data arary.
*/
protected function addPaginator(array $data, WP_Query $query): array
{
$page = $query->get('paged');
$page = 0 == $page ? 1 : $page;
return array_merge([
'data' => $data,
], [
'pagination' => [
'total_count' => (int) $query->found_posts,
'total_pages' => $query->max_num_pages,
'current_page' => $page,
'limit' => $query->get('posts_per_page'),
],
]);
}
/**
* Get the paginator query params for a given query.
*/
protected function getPaginatorParams(WP_REST_Request $request, int $limit = 10): array
{
return [
'posts_per_page' => $request->get_param('limit') ?: $limit,
'paged' => $request->get_param('page') ?: 0,
];
}
/**
* Return the post status to query on.
*/
protected function getPostStatus(WP_REST_Request $request): array
{
$preview = filter_var($request->get_param('draft-preview'), FILTER_VALIDATE_BOOLEAN);
if (! \is_user_logged_in()) {
$preview = false;
}
return $preview ? ['publish', 'draft', 'future'] : ['publish'];
}
/**
* Check if the source parameter is valid.
*/
protected function showOnParamIsValid(WP_REST_Request $request): bool
{
if (empty($request->get_param('source'))) {
return false;
}
if (! is_numeric($request->get_param('source'))) {
return false;
}
return true;
}
protected function targetAudienceParamIsValid(WP_REST_Request $request): bool
{
return $this->defaultTaxonomyParamIsValid($request, 'pdc-doelgroep');
}
protected function aspectParamIsValid(WP_REST_Request $request): bool
{
return $this->defaultTaxonomyParamIsValid($request, 'pdc-aspect');
}
protected function usageParamIsValid(WP_REST_Request $request): bool
{
return $this->defaultTaxonomyParamIsValid($request, 'pdc-usage');
}
protected function defaultTaxonomyParamIsValid(WP_REST_Request $request, string $param): bool
{
if (empty($request->get_param($param))) {
return false;
}
if (! is_array($request->get_param($param)) && ! is_string($request->get_param($param))) {
return false;
}
return true;
}
protected function getOrderClause(string $orderBy, string $order): array
{
$orderArray = [];
$orderByParts = explode(',', $orderBy);
$orderParts = explode(',', $order);
// Empty string results in array with one empty value, we ignore that.
if (!array_filter($orderByParts)) {
return [];
}
// Single orderby value, return simple array.
if (count($orderByParts) === 1) {
return [
'orderby' => trim($orderByParts[0]),
'order' => strtoupper(trim($orderParts[0] ?? 'ASC')),
];
}
// Multiple orderby values, return associative array.
foreach ($orderByParts as $index => $orderByPart) {
$orderValue = $orderParts[$index] ?? $orderParts[0] ?? 'ASC';
$orderArray[trim($orderByPart)] = strtoupper(trim($orderValue));
}
return ['orderby' => $orderArray];
}
}