diff --git a/.editorconfig b/.editorconfig index 76ff616..35ba4d8 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 diff --git a/src/Base/RestAPI/Controllers/BaseController.php b/src/Base/RestAPI/Controllers/BaseController.php index f887ac8..a199ba1 100644 --- a/src/Base/RestAPI/Controllers/BaseController.php +++ b/src/Base/RestAPI/Controllers/BaseController.php @@ -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]; + } } diff --git a/src/Base/RestAPI/Controllers/ItemController.php b/src/Base/RestAPI/Controllers/ItemController.php index a606b5d..afe3ec0 100644 --- a/src/Base/RestAPI/Controllers/ItemController.php +++ b/src/Base/RestAPI/Controllers/ItemController.php @@ -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')); diff --git a/src/Base/RestAPI/Controllers/SubthemaController.php b/src/Base/RestAPI/Controllers/SubthemaController.php index cb0cb4c..629ff46 100644 --- a/src/Base/RestAPI/Controllers/SubthemaController.php +++ b/src/Base/RestAPI/Controllers/SubthemaController.php @@ -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) ]); diff --git a/src/Base/RestAPI/Controllers/ThemaController.php b/src/Base/RestAPI/Controllers/ThemaController.php index ef20ef5..71f924e 100644 --- a/src/Base/RestAPI/Controllers/ThemaController.php +++ b/src/Base/RestAPI/Controllers/ThemaController.php @@ -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']);