Skip to content

Commit

Permalink
Items op taal filteren (#26)
Browse files Browse the repository at this point in the history
* (fix): remove duplicate line

* add "filterLanguageQuery()" query-helper

* add property AbstractRepository::language

* add property ItemsField::language

* enable filtering the connected items of a theme/subtheme by language

* add "language" of connected items in the API output

* bump version to 3.10; update change log

---------

Co-authored-by: Colin <colin@DESKTOP-IAN8NPS>
  • Loading branch information
colin-yard-nl and Colin authored Jan 17, 2024
1 parent 54dd130 commit da2cd76
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## Version [3.10] (2024-01-10)

### Feat

- Enable filtering the items of categories and sub-categories by language.
- Add the language of connected PDC items in the API response data.


## Version [3.9.1] (2024-01-02)

### Refactor
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.9.1
* Version: 3.10
* Author: Yard | Digital Agency
* Author URI: https://www.yard.nl/
* License: GPL-3.0
Expand Down
2 changes: 1 addition & 1 deletion 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.9.1';
public const VERSION = '3.10';

/**
* Path to the root of the plugin.
Expand Down
25 changes: 23 additions & 2 deletions src/Base/Repositories/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@ abstract class AbstractRepository
*/
protected $password = '';


/**
* Source for filtering the 'show_on' taxonomy
*/
protected int $source = 0;

/**
* Language for filtering
*/
protected ?string $language = null;

/**
* Additional fields that needs to be added to an item.
*
Expand Down Expand Up @@ -271,7 +275,6 @@ public function transform(WP_Post $post)
'content' => $this->isAllowed($post) ? apply_filters('the_content', $post->post_content) : "",
'excerpt' => $this->isAllowed($post) ? $post->post_excerpt : "",
'date' => $post->post_date,
'slug' => $post->post_name,
'post_status' => $post->post_status,
'protected' => ! $this->isAllowed($post)
];
Expand Down Expand Up @@ -347,6 +350,12 @@ protected function assignFields(array $data, WP_Post $post)
}
}

if ($this->shouldFilterLanguage()) {
if (method_exists($field['creator'], 'setLanguage')) {
$field['creator']->setLanguage($this->language);
}
}

if (is_null($field['conditional'])) {
// If the field has no conditional set we will add it
$data[$field['key']] = $field['creator']->create($post);
Expand Down Expand Up @@ -391,4 +400,16 @@ public function shouldFilterSource(): bool
{
return 0 !== $this->source;
}

public function filterLanguage(string $language): self
{
$this->language = $language;

return $this;
}

public function shouldFilterLanguage(): bool
{
return !empty($this->language);
}
}
12 changes: 12 additions & 0 deletions src/Base/RestAPI/Controllers/SubthemaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public function getSubthemas(WP_REST_Request $request): array
$items->filterSource($request->get_param('source'));
}

if ($language = $request->get_param('language')) {
$items->filterLanguage((string) $language);
}

$data = $items->all();
$query = $items->getQuery();

Expand All @@ -55,6 +59,10 @@ public function getSubthema(WP_REST_Request $request)
$thema->filterSource($request->get_param('source'));
}

if ($language = $request->get_param('language')) {
$thema->filterLanguage((string) $language);
}

$thema = $thema->find($id);

if (! $thema) {
Expand Down Expand Up @@ -83,6 +91,10 @@ public function getSubthemeBySlug(WP_REST_Request $request)
$subtheme->filterSource($request->get_param('source'));
}

if ($language = $request->get_param('language')) {
$subtheme->filterLanguage((string) $language);
}

$subtheme = $subtheme->findBySlug($slug);

if (! $subtheme) {
Expand Down
14 changes: 13 additions & 1 deletion src/Base/RestAPI/Controllers/ThemaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function getThemas(WP_REST_Request $request): array
$items->filterSource($request->get_param('source'));
}

if ($language = $request->get_param('language')) {
$items->filterLanguage((string) $language);
}

$data = $items->all();
$query = $items->getQuery();

Expand All @@ -59,7 +63,11 @@ public function getThema(WP_REST_Request $request)
$thema->filterSource($request->get_param('source'));
}

$thema = $thema->find($id);
if ($language = $request->get_param('language')) {
$thema->filterLanguage((string) $language);
}

$thema = $thema->find($id);

if (! $thema) {
return new WP_Error('no_item_found', sprintf('Thema with ID [%d] not found', $id), [
Expand Down Expand Up @@ -87,6 +95,10 @@ public function getThemeBySlug(WP_REST_Request $request)
$theme->filterSource($request->get_param('source'));
}

if ($language = $request->get_param('language')) {
$theme->filterLanguage((string) $language);
}

$theme = $theme->findBySlug($slug);

if (! $theme) {
Expand Down
1 change: 1 addition & 0 deletions src/Base/RestAPI/ItemFields/ConnectedField.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ protected function getConnectedItems(int $postID, string $type, array $extraQuer
'slug' => $post->post_name,
'excerpt' => $post->post_excerpt,
'date' => $post->post_date,
'language' => get_post_meta($post->ID, '_owc_pdc-item-language', true) ?: 'nl',
];

if ($type === 'pdc-item_to_pdc-item') {
Expand Down
27 changes: 24 additions & 3 deletions src/Base/RestAPI/SharedFields/ItemsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class ItemsField extends ConnectedField
use CheckPluginActive;
use QueryHelpers;

/**
* Language for filtering
*/
protected ?string $language = null;

/**
* Creates an array of connected posts.
*/
Expand All @@ -33,20 +38,36 @@ protected function extraQueryArgs(string $type): array
{
$query = [];

$query = array_merge_recursive($query, $this->excludeInactiveItemsQuery());
$query = array_merge_recursive($query, $this->excludeInactiveItemsQuery());

if ($this->isPluginPDCInternalProductsActive()) {
if ($this->isPluginPDCInternalProductsActive()) {
$query = array_merge_recursive($query, $this->excludeInternalItemsQuery());
}

if ($this->shouldFilterSource()) {
$query = array_merge_recursive($query, $this->filterShowOnTaxonomyQuery($this->source));
}

$query['connected_query'] = [
if ($this->shouldFilterLanguage()) {
$query = array_merge_recursive($query, $this->filterLanguageQuery($this->language));
}

$query['connected_query'] = [
'post_status' => ['publish', 'draft'],
];

return $query;
}

public function setLanguage(string $language): self
{
$this->language = $language;

return $this;
}

protected function shouldFilterLanguage(): bool
{
return !empty($this->language);
}
}
37 changes: 37 additions & 0 deletions src/Base/Support/Traits/QueryHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,41 @@ public function filterShowOnTaxonomyQuery(int $termID): array
]
];
}

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

return [
'meta_query' => [
[
'key' => '_owc_pdc-item-language',
'value' => $language,
'compare' => '=',
],
]
];
}
}

0 comments on commit da2cd76

Please sign in to comment.