From da2cd76f10490a7f9a20594509e58c1d50ea964f Mon Sep 17 00:00:00 2001 From: Colin <47478535+colin-yard-nl@users.noreply.github.com> Date: Wed, 17 Jan 2024 16:37:46 +0100 Subject: [PATCH] Items op taal filteren (#26) * (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 --- CHANGELOG.md | 8 ++++ pdc-base.php | 2 +- src/Base/Foundation/Plugin.php | 2 +- src/Base/Repositories/AbstractRepository.php | 25 ++++++++++++- .../Controllers/SubthemaController.php | 12 ++++++ .../RestAPI/Controllers/ThemaController.php | 14 ++++++- .../RestAPI/ItemFields/ConnectedField.php | 1 + src/Base/RestAPI/SharedFields/ItemsField.php | 27 ++++++++++++-- src/Base/Support/Traits/QueryHelpers.php | 37 +++++++++++++++++++ 9 files changed, 120 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 809a14b..65c4386 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/pdc-base.php b/pdc-base.php index 3dcd71e..a31e8fd 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.9.1 + * Version: 3.10 * 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 f79e98c..b5ca00d 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.9.1'; + public const VERSION = '3.10'; /** * Path to the root of the plugin. diff --git a/src/Base/Repositories/AbstractRepository.php b/src/Base/Repositories/AbstractRepository.php index 2fa8e71..4a04dc5 100644 --- a/src/Base/Repositories/AbstractRepository.php +++ b/src/Base/Repositories/AbstractRepository.php @@ -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. * @@ -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) ]; @@ -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); @@ -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); + } } diff --git a/src/Base/RestAPI/Controllers/SubthemaController.php b/src/Base/RestAPI/Controllers/SubthemaController.php index 47acce4..df7556d 100644 --- a/src/Base/RestAPI/Controllers/SubthemaController.php +++ b/src/Base/RestAPI/Controllers/SubthemaController.php @@ -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(); @@ -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) { @@ -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) { diff --git a/src/Base/RestAPI/Controllers/ThemaController.php b/src/Base/RestAPI/Controllers/ThemaController.php index 2f06a0c..53a9e8a 100644 --- a/src/Base/RestAPI/Controllers/ThemaController.php +++ b/src/Base/RestAPI/Controllers/ThemaController.php @@ -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(); @@ -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), [ @@ -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) { diff --git a/src/Base/RestAPI/ItemFields/ConnectedField.php b/src/Base/RestAPI/ItemFields/ConnectedField.php index ddcd2a7..16d2f0c 100644 --- a/src/Base/RestAPI/ItemFields/ConnectedField.php +++ b/src/Base/RestAPI/ItemFields/ConnectedField.php @@ -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') { diff --git a/src/Base/RestAPI/SharedFields/ItemsField.php b/src/Base/RestAPI/SharedFields/ItemsField.php index 22d36bd..a2cc499 100644 --- a/src/Base/RestAPI/SharedFields/ItemsField.php +++ b/src/Base/RestAPI/SharedFields/ItemsField.php @@ -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. */ @@ -33,9 +38,9 @@ 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()); } @@ -43,10 +48,26 @@ protected function extraQueryArgs(string $type): array $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); + } } diff --git a/src/Base/Support/Traits/QueryHelpers.php b/src/Base/Support/Traits/QueryHelpers.php index fd4ceee..e3c73f6 100644 --- a/src/Base/Support/Traits/QueryHelpers.php +++ b/src/Base/Support/Traits/QueryHelpers.php @@ -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' => '=', + ], + ] + ]; + } }