Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ indent_style = tab
space_around_brackets = true
indent_size = 4

[*.php]
indent_style = space

[{.jshintrc,*.json,*.yml}]
indent_style = space
indent_size = 2
Expand Down
28 changes: 28 additions & 0 deletions src/Base/RestAPI/Controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,32 @@ protected function defaultTaxonomyParamIsValid(WP_REST_Request $request, string

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];
}
}
8 changes: 4 additions & 4 deletions src/Base/RestAPI/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class ItemController extends BaseController
*/
public function getItems(WP_REST_Request $request): array
{
$orderBy = $request->get_param('orderby') ?? 'post_date,ID';
$order = $request->get_param('order') ?? 'DESC,DESC';

$parameters = $this->convertParameters($request->get_params());
$items = (new Item())
->query(apply_filters('owc/pdc/rest-api/items/query', $this->getPaginatorParams($request)))
->query($parameters)
->query($this->excludeInactiveItemsQuery())
->query(['orderby' => [
'post_date' => 'DESC',
'ID' => 'DESC',
]]);
->query($this->getOrderClause($orderBy, $order));

if ($this->plugin->settings->useShowOn() && $this->showOnParamIsValid($request)) {
$items->filterSource($request->get_param('source'));
Expand Down
6 changes: 4 additions & 2 deletions src/Base/RestAPI/Controllers/SubthemaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ class SubthemaController extends BaseController
*/
public function getSubthemas(WP_REST_Request $request): array
{
$orderBy = $request->get_param('orderby') ?? 'name';
$order = $request->get_param('order') ?? 'ASC';

$items = (new Subthema())
->query(apply_filters('owc/pdc/rest-api/subthemas/query', $this->getPaginatorParams($request)))
->query($this->getOrderClause($orderBy, $order))
->query([
'order' => 'ASC',
'orderby' => 'name',
'post_status' => $this->getPostStatus($request)
]);

Expand Down
3 changes: 1 addition & 2 deletions src/Base/RestAPI/Controllers/ThemaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ public function getThemas(WP_REST_Request $request): array

$items = (new Thema())
->query(apply_filters('owc/pdc/rest-api/themas/query', $this->getPaginatorParams($request)))
->query($this->getOrderClause($orderBy, $order))
->query([
'order' => $order,
'orderby' => $orderBy,
'post_status' => $this->getPostStatus($request)
])
->hide(['items']);
Expand Down