Skip to content

Commit

Permalink
(feat): support extra filter options in items endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike van den Hoek committed Mar 14, 2024
1 parent 3ae332e commit 20001b8
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 59 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion pdc-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Base/Foundation/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
43 changes: 38 additions & 5 deletions src/Base/Repositories/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -410,6 +443,6 @@ public function filterLanguage(string $language): self

public function shouldFilterLanguage(): bool
{
return !empty($this->language);
return ! empty($this->language);
}
}
42 changes: 35 additions & 7 deletions src/Base/RestAPI/Controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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'),
],
]);
}

Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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;
}
}
18 changes: 15 additions & 3 deletions src/Base/RestAPI/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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']);
}
Expand Down
130 changes: 89 additions & 41 deletions src/Base/Support/Traits/QueryHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function excludeInactiveItemsQuery(): array
'value' => '1',
'compare' => '=',
],
]
],
];
}

Expand All @@ -33,8 +33,8 @@ public function excludeInternalItemsQuery(): array
'field' => 'id',
'operator' => 'NOT EXISTS',
],
]
]
],
],
];
}

Expand All @@ -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',
],
],
];
}
}

0 comments on commit 20001b8

Please sign in to comment.