Skip to content

Commit 9b0fc04

Browse files
author
Mike van den Hoek
committed
(feat): validate passed query params before initiating WP_Query
1 parent 7ed52d9 commit 9b0fc04

File tree

3 files changed

+87
-31
lines changed

3 files changed

+87
-31
lines changed

Diff for: src/Base/Repositories/AbstractRepository.php

+56-19
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
namespace OWC\PDC\Base\Repositories;
88

99
use Closure;
10-
use WP_Post;
11-
use WP_Query;
10+
use OWC\PDC\Base\Exceptions\PropertyNotExistsException;
1211
use OWC\PDC\Base\Support\CreatesFields;
1312
use OWC\PDC\Base\Support\Traits\QueryHelpers;
14-
use OWC\PDC\Base\Exceptions\PropertyNotExistsException;
13+
use WP_Post;
14+
use WP_Query;
1515

1616
/**
1717
* PDC item object with default quering and methods.
@@ -98,35 +98,29 @@ public function __construct()
9898

9999
/**
100100
* Get all the items from the database.
101-
*
102-
* @return array
103101
*/
104102
public function all(): array
105103
{
106104
$args = array_merge($this->queryArgs, [
107105
'post_type' => [$this->posttype],
108106
]);
109107

110-
$this->query = new WP_Query($args);
108+
$this->query = new WP_Query($this->cleanParams($args));
111109

112110
return array_map([$this, 'transform'], $this->getQuery()->posts);
113111
}
114112

115113
/**
116114
* Find a particular pdc item by ID.
117-
*
118-
* @param int $id
119-
*
120-
* @return array
121115
*/
122-
public function find(int $id)
116+
public function find(int $id): ?array
123117
{
124118
$args = array_merge($this->queryArgs, [
125119
'p' => $id,
126120
'post_type' => [$this->posttype],
127121
]);
128122

129-
$this->query = new WP_Query($args);
123+
$this->query = new WP_Query($this->cleanParams($args));
130124

131125
if (empty($this->getQuery()->posts)) {
132126
return null;
@@ -137,19 +131,15 @@ public function find(int $id)
137131

138132
/**
139133
* Find a particular pdc item by slug.
140-
*
141-
* @param string $slug
142-
*
143-
* @return array|null
144134
*/
145-
public function findBySlug(string $slug)
135+
public function findBySlug(string $slug): ?array
146136
{
147137
$args = array_merge($this->queryArgs, [
148138
'name' => $slug,
149139
'post_type' => [$this->posttype],
150140
]);
151141

152-
$this->query = new WP_Query($args);
142+
$this->query = new WP_Query($this->cleanParams($args));
153143

154144
if (empty($this->getQuery()->posts)) {
155145
return null;
@@ -158,6 +148,53 @@ public function findBySlug(string $slug)
158148
return $this->transform(reset($this->getQuery()->posts));
159149
}
160150

151+
protected function cleanParams(array $args): array
152+
{
153+
$args = $this->validatePostStatusParam($args);
154+
$args = $this->cleanWronglyNestedQueryParams($args, 'tax_query');
155+
$args = $this->cleanWronglyNestedQueryParams($args, 'meta_query');
156+
157+
return $args;
158+
}
159+
160+
protected function validatePostStatusParam(array $args): array
161+
{
162+
if (empty($args['post_status'])) {
163+
return $args;
164+
}
165+
166+
if (! is_string($args['post_status']) && ! is_array($args['post_status'])) {
167+
unset($args['post_status']);
168+
169+
return $args;
170+
}
171+
172+
if (is_string($args['post_status'])) {
173+
$args['post_status'] = [$args['post_status']];
174+
}
175+
176+
if (! \is_user_logged_in()) {
177+
$args['post_status'] = ['publish'];
178+
}
179+
180+
return $args;
181+
}
182+
183+
protected function cleanWronglyNestedQueryParams(array $args, string $key): array
184+
{
185+
if (empty($args[$key]) || ! is_array($args[$key])) {
186+
return $args;
187+
}
188+
189+
foreach ($args[$key] as &$query) {
190+
if (is_array($query) && ! empty($query[0])) {
191+
$query = call_user_func_array('array_merge', $query);
192+
}
193+
}
194+
195+
return $args;
196+
}
197+
161198
/**
162199
* Get the WP_Query object.
163200
*
@@ -273,7 +310,7 @@ public function transform(WP_Post $post)
273310
'date' => $post->post_date,
274311
'slug' => $post->post_name,
275312
'post_status' => $post->post_status,
276-
'protected' => ! $this->isAllowed($post)
313+
'protected' => ! $this->isAllowed($post),
277314
];
278315

279316
$data = $this->assignFields($data, $post);

Diff for: src/Base/RestAPI/Controllers/BaseController.php

+27-8
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
namespace OWC\PDC\Base\RestAPI\Controllers;
88

9+
use OWC\PDC\Base\Foundation\Plugin;
910
use WP_Query;
1011
use WP_REST_Request;
11-
use OWC\PDC\Base\Foundation\Plugin;
1212

1313
/**
1414
* Controller which handels general quering, such as pagination.
@@ -39,14 +39,14 @@ protected function addPaginator(array $data, WP_Query $query): array
3939
$page = 0 == $page ? 1 : $page;
4040

4141
return array_merge([
42-
'data' => $data
42+
'data' => $data,
4343
], [
4444
'pagination' => [
45-
'total_count' => (int) $query->found_posts,
46-
'total_pages' => $query->max_num_pages,
45+
'total_count' => (int) $query->found_posts,
46+
'total_pages' => $query->max_num_pages,
4747
'current_page' => $page,
48-
'limit' => $query->get('posts_per_page')
49-
]
48+
'limit' => $query->get('posts_per_page'),
49+
],
5050
]);
5151
}
5252

@@ -55,10 +55,29 @@ protected function addPaginator(array $data, WP_Query $query): array
5555
*/
5656
protected function getPaginatorParams(WP_REST_Request $request, int $limit = 10): array
5757
{
58-
return array_merge($request->get_params(), [
58+
$params = array_merge($request->get_params(), [
5959
'posts_per_page' => $request->get_param('limit') ?: $limit,
60-
'paged' => $request->get_param('page') ?: 0
60+
'paged' => $request->get_param('page') ?: 0,
6161
]);
62+
63+
return $this->validateQueryParams($params);
64+
}
65+
66+
protected function validateQueryParams(array $params): array
67+
{
68+
$allowedQueryParams = [
69+
'include-connected',
70+
'tax_query',
71+
'meta_query',
72+
'posts_per_page',
73+
'paged',
74+
'post_type',
75+
'post_status',
76+
];
77+
78+
return array_filter($params, function ($param) use ($allowedQueryParams) {
79+
return in_array($param, $allowedQueryParams);
80+
}, ARRAY_FILTER_USE_KEY);
6281
}
6382

6483
/**

Diff for: src/Base/RestAPI/SharedFields/ItemsField.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
namespace OWC\PDC\Base\RestAPI\SharedFields;
88

9-
use WP_Post;
10-
use OWC\PDC\Base\Support\Traits\QueryHelpers;
11-
use OWC\PDC\Base\Support\Traits\CheckPluginActive;
129
use OWC\PDC\Base\RestAPI\ItemFields\ConnectedField;
10+
use OWC\PDC\Base\Support\Traits\CheckPluginActive;
11+
use OWC\PDC\Base\Support\Traits\QueryHelpers;
12+
use WP_Post;
1313

1414
/**
1515
* Adds connected fields to item in API.
@@ -44,7 +44,7 @@ protected function extraQueryArgs(string $type): array
4444
}
4545

4646
$query['connected_query'] = [
47-
'post_status' => ['publish', 'draft'],
47+
'post_status' => ['publish', 'draft'], // Draft only for logged in users?
4848
];
4949

5050
return $query;

0 commit comments

Comments
 (0)