From 20001b8e329e2ae3fb9084cef25f8b74656909d9 Mon Sep 17 00:00:00 2001 From: Mike van den Hoek Date: Thu, 14 Mar 2024 14:09:35 +0100 Subject: [PATCH] (feat): support extra filter options in items endpoint --- CHANGELOG.md | 6 + pdc-base.php | 2 +- src/Base/Foundation/Plugin.php | 4 +- src/Base/Repositories/AbstractRepository.php | 43 +++++- .../RestAPI/Controllers/BaseController.php | 42 +++++- .../RestAPI/Controllers/ItemController.php | 18 ++- src/Base/Support/Traits/QueryHelpers.php | 130 ++++++++++++------ 7 files changed, 186 insertions(+), 59 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0eb65a6..665f34c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # CHANGELOG +## Version [3.12] (2024-03-14) + +### Feat + +- Support extra default taxonomy filter options in items endpoint. + ## Version [3.11.1] (2024-02-19) ### Feat diff --git a/pdc-base.php b/pdc-base.php index 4f86dbc..2b0a114 100644 --- a/pdc-base.php +++ b/pdc-base.php @@ -4,7 +4,7 @@ * Plugin Name: Yard | PDC Base * Plugin URI: https://www.openwebconcept.nl/ * Description: Acts as foundation for other PDC related content plugins. This plugin implements actions to allow for other plugins to add and/or change Custom Posttypes, Metaboxes, Taxonomies, en Posts 2 posts relations. - * Version: 3.11.1 + * Version: 3.12 * Author: Yard | Digital Agency * Author URI: https://www.yard.nl/ * License: GPL-3.0 diff --git a/src/Base/Foundation/Plugin.php b/src/Base/Foundation/Plugin.php index 2a932cd..6b5ad8d 100644 --- a/src/Base/Foundation/Plugin.php +++ b/src/Base/Foundation/Plugin.php @@ -19,7 +19,7 @@ class Plugin * * @var string */ - public const VERSION = '3.11.1'; + public const VERSION = '3.12'; /** * Path to the root of the plugin. @@ -135,7 +135,7 @@ public function filterPlugin(): void */ public function callServiceProviders(string $method, string $key = ''): void { - $offset = $key ? "core.providers.{$key}" : 'core.providers'; + $offset = $key ? "core.providers.{$key}" : 'core.providers'; $services = $this->config->get($offset); foreach ($services as $service) { diff --git a/src/Base/Repositories/AbstractRepository.php b/src/Base/Repositories/AbstractRepository.php index 4a04dc5..2035d9c 100644 --- a/src/Base/Repositories/AbstractRepository.php +++ b/src/Base/Repositories/AbstractRepository.php @@ -7,11 +7,11 @@ namespace OWC\PDC\Base\Repositories; use Closure; -use WP_Post; -use WP_Query; +use OWC\PDC\Base\Exceptions\PropertyNotExistsException; use OWC\PDC\Base\Support\CreatesFields; use OWC\PDC\Base\Support\Traits\QueryHelpers; -use OWC\PDC\Base\Exceptions\PropertyNotExistsException; +use WP_Post; +use WP_Query; /** * PDC item object with default quering and methods. @@ -276,7 +276,7 @@ public function transform(WP_Post $post) 'excerpt' => $this->isAllowed($post) ? $post->post_excerpt : "", 'date' => $post->post_date, 'post_status' => $post->post_status, - 'protected' => ! $this->isAllowed($post) + 'protected' => ! $this->isAllowed($post), ]; $data = $this->assignFields($data, $post); @@ -396,6 +396,39 @@ public function filterSource(int $source): self return $this; } + public function filterTargetAudience($audiences): self + { + if (is_string($audiences)) { + $audiences = [$audiences]; + } + + $this->query($this->filterTargetAudienceQuery($audiences)); + + return $this; + } + + public function filterAspect($aspects): self + { + if (is_string($aspects)) { + $aspects = [$aspects]; + } + + $this->query($this->filterAspectQuery($aspects)); + + return $this; + } + + public function filterUsage($audiences): self + { + if (is_string($audiences)) { + $audiences = [$audiences]; + } + + $this->query($this->filterUsageQuery($audiences)); + + return $this; + } + public function shouldFilterSource(): bool { return 0 !== $this->source; @@ -410,6 +443,6 @@ public function filterLanguage(string $language): self public function shouldFilterLanguage(): bool { - return !empty($this->language); + return ! empty($this->language); } } diff --git a/src/Base/RestAPI/Controllers/BaseController.php b/src/Base/RestAPI/Controllers/BaseController.php index ec0b5b8..927d785 100644 --- a/src/Base/RestAPI/Controllers/BaseController.php +++ b/src/Base/RestAPI/Controllers/BaseController.php @@ -6,9 +6,9 @@ namespace OWC\PDC\Base\RestAPI\Controllers; +use OWC\PDC\Base\Foundation\Plugin; use WP_Query; use WP_REST_Request; -use OWC\PDC\Base\Foundation\Plugin; /** * Controller which handels general quering, such as pagination. @@ -39,14 +39,14 @@ protected function addPaginator(array $data, WP_Query $query): array $page = 0 == $page ? 1 : $page; return array_merge([ - 'data' => $data + 'data' => $data, ], [ 'pagination' => [ 'total_count' => (int) $query->found_posts, 'total_pages' => $query->max_num_pages, 'current_page' => $page, - 'limit' => $query->get('posts_per_page') - ] + 'limit' => $query->get('posts_per_page'), + ], ]); } @@ -57,14 +57,14 @@ protected function getPaginatorParams(WP_REST_Request $request, int $limit = 10) { return [ 'posts_per_page' => $request->get_param('limit') ?: $limit, - 'paged' => $request->get_param('page') ?: 0 + 'paged' => $request->get_param('page') ?: 0, ]; } /** * Return the post status to query on. */ - protected function getPostStatus(\WP_REST_Request $request): array + protected function getPostStatus(WP_REST_Request $request): array { $preview = filter_var($request->get_param('draft-preview'), FILTER_VALIDATE_BOOLEAN); @@ -78,7 +78,7 @@ protected function getPostStatus(\WP_REST_Request $request): array /** * Check if the source parameter is valid. */ - protected function showOnParamIsValid(\WP_REST_Request $request): bool + protected function showOnParamIsValid(WP_REST_Request $request): bool { if (empty($request->get_param('source'))) { return false; @@ -90,4 +90,32 @@ protected function showOnParamIsValid(\WP_REST_Request $request): bool 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; + } } diff --git a/src/Base/RestAPI/Controllers/ItemController.php b/src/Base/RestAPI/Controllers/ItemController.php index b4fa406..efbdd2c 100644 --- a/src/Base/RestAPI/Controllers/ItemController.php +++ b/src/Base/RestAPI/Controllers/ItemController.php @@ -6,11 +6,11 @@ namespace OWC\PDC\Base\RestAPI\Controllers; -use WP_Error; -use WP_REST_Request; use OWC\PDC\Base\Repositories\Item; -use OWC\PDC\Base\Support\Traits\QueryHelpers; use OWC\PDC\Base\Support\Traits\CheckPluginActive; +use OWC\PDC\Base\Support\Traits\QueryHelpers; +use WP_Error; +use WP_REST_Request; /** * Controller which handles the (requested) pdc-item(s). @@ -35,6 +35,18 @@ public function getItems(WP_REST_Request $request): array $items->filterSource($request->get_param('source')); }; + if ($this->targetAudienceParamIsValid($request)) { + $items->filterTargetAudience($request->get_param('pdc-doelgroep')); + } + + if ($this->aspectParamIsValid($request)) { + $items->filterAspect($request->get_param('pdc-aspect')); + } + + if ($this->usageParamIsValid($request)) { + $items->filterUsage($request->get_param('pdc-usage')); + } + if (false === $parameters['include-connected']) { $items->hide(['connected']); } diff --git a/src/Base/Support/Traits/QueryHelpers.php b/src/Base/Support/Traits/QueryHelpers.php index e3c73f6..135609b 100644 --- a/src/Base/Support/Traits/QueryHelpers.php +++ b/src/Base/Support/Traits/QueryHelpers.php @@ -13,7 +13,7 @@ public function excludeInactiveItemsQuery(): array 'value' => '1', 'compare' => '=', ], - ] + ], ]; } @@ -33,8 +33,8 @@ public function excludeInternalItemsQuery(): array 'field' => 'id', 'operator' => 'NOT EXISTS', ], - ] - ] + ], + ], ]; } @@ -46,46 +46,94 @@ public function filterShowOnTaxonomyQuery(int $termID): array 'taxonomy' => 'pdc-show-on', 'terms' => sanitize_text_field($termID), 'field' => 'slug', - 'operator' => 'IN' - ] - ] + 'operator' => 'IN', + ], + ], + ]; + } + + public function filterLanguageQuery(string $language): array + { + if ('nl' === $language) { + return [ + 'meta_query' => [ + [ + 'relation' => 'OR', + [ + 'key' => '_owc_pdc-item-language', + 'value' => $language, + 'compare' => '=', + ], + [ + 'key' => '_owc_pdc-item-language', + 'value' => '', + 'compare' => '=', + ], + [ + 'key' => '_owc_pdc-item-language', + 'compare' => 'NOT EXISTS', + ], + ], + ], + ]; + } + + return [ + 'meta_query' => [ + [ + 'key' => '_owc_pdc-item-language', + 'value' => $language, + 'compare' => '=', + ], + ], + ]; + } + + public function filterTargetAudienceQuery(array $audiences): array + { + return [ + 'tax_query' => [ + [ + 'taxonomy' => 'pdc-doelgroep', + 'terms' => array_map(function ($audience) { + return sanitize_text_field($audience); + }, $audiences), + 'field' => 'slug', + 'operator' => 'IN', + ], + ], ]; } - public function filterLanguageQuery(string $language): array - { - if ($language === 'nl') { - return [ - 'meta_query' => [ - [ - 'relation' => 'OR', - [ - 'key' => '_owc_pdc-item-language', - 'value' => $language, - 'compare' => '=', - ], - [ - 'key' => '_owc_pdc-item-language', - 'value' => '', - 'compare' => '=', - ], - [ - 'key' => '_owc_pdc-item-language', - 'compare' => 'NOT EXISTS', - ], - ] - ] - ]; - } + public function filterAspectQuery(array $aspects): array + { + return [ + 'tax_query' => [ + [ + 'taxonomy' => 'pdc-aspect', + 'terms' => array_map(function ($aspect) { + return sanitize_text_field($aspect); + }, $aspects), + 'field' => 'slug', + 'operator' => 'IN', + ], + ], + ]; + } - return [ - 'meta_query' => [ - [ - 'key' => '_owc_pdc-item-language', - 'value' => $language, - 'compare' => '=', - ], - ] - ]; - } + public function filterUsageQuery(array $usages): array + { + return [ + 'tax_query' => [ + [ + 'taxonomy' => 'pdc-usage', + 'terms' => array_map(function ($usage) { + return sanitize_text_field($usage); + }, $usages), + 'field' => 'slug', + 'operator' => 'IN', + ], + ], + ]; + } }