From 2ba357a50f6116a614f617818d7dc13bf348a11f Mon Sep 17 00:00:00 2001 From: Oleksandr Zhyian Date: Wed, 4 Sep 2024 11:07:45 +0300 Subject: [PATCH 1/3] feat: correctly assign campo keys to multiple terms within degree program --- .../Search/FilterableTermsUpdater.php | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/Infrastructure/Search/FilterableTermsUpdater.php b/src/Infrastructure/Search/FilterableTermsUpdater.php index 410bad3..9c81cab 100644 --- a/src/Infrastructure/Search/FilterableTermsUpdater.php +++ b/src/Infrastructure/Search/FilterableTermsUpdater.php @@ -141,19 +141,19 @@ private function setObjectTerms( string $taxonomy, MultilingualString|MultilingualList|MultilingualLink|MultilingualLinks|Degree|AdmissionRequirement $property ): void { - - $termIds = $this->maybeCreateTerms($taxonomy, $property); + /** @var array $terms */ + $terms = $this->maybeCreateTerms($taxonomy, $property); wp_set_object_terms( $rawView->id()->asInt(), - $termIds, + array_keys($terms), $taxonomy ); - if (! isset($termIds[0])) { + if (!$terms) { return; } - $this->maybeUpdateCampoKeys($rawView, $taxonomy, (int) $termIds[0]); + $this->maybeUpdateCampoKeys($rawView, $taxonomy, $terms); } private function isValidPostId(int $postId): bool @@ -179,6 +179,8 @@ private function isValidPostId(int $postId): bool * phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong * * Refactoring could decrease performance. + * + * @return array */ private function maybeCreateTerms( string $taxonomy, @@ -225,7 +227,7 @@ private function maybeCreateTerms( if ($termData->termId()) { // Term was persisted already - $result[] = $termData->termId(); + $result[$termData->termId()] = $termData; $this->updateTerm($termData); continue; } @@ -233,7 +235,7 @@ private function maybeCreateTerms( $termId = $this->createTerm($termData); if (is_int($termId)) { - $result[] = $termId; + $result[$termId] = $termData; $taxonomiesCache[$taxonomy][$termId] = [ self::TAXONOMIES_CACHE_NAME_PROPERTY => $termData->name()->inGerman(), self::TAXONOMIES_CACHE_ORIGINAL_ID_PROPERTY => $termData->remoteTermId(), @@ -540,16 +542,25 @@ private static function arraySearchBy(array $array, string $property, mixed $val return null; } - private function maybeUpdateCampoKeys(DegreeProgramViewRaw $rawView, string $taxonomy, int $termId): void + private function maybeUpdateCampoKeys(DegreeProgramViewRaw $rawView, string $taxonomy, array $terms): void { - $campoKeys = $rawView->campoKeys()->asArray(); - $campoKeyType = CampoKeysRepository::TAXONOMY_TO_CAMPO_KEY_MAP[$taxonomy] ?? ''; - $campoKey = $campoKeys[$campoKeyType] ?? null; + /** @var TermData $term */ + foreach ($terms as $term) { + $campoKeys = $rawView->campoKeys()->asArray(); + $campoKeyType = CampoKeysRepository::TAXONOMY_TO_CAMPO_KEY_MAP[$taxonomy] ?? ''; + $taxonomyCampoKeys = $campoKeys[$campoKeyType] ?? null; - if (is_null($campoKey)) { - return; - } + if (!is_array($taxonomyCampoKeys)) { + continue; + } - update_term_meta($termId, CampoKeysRepository::CAMPO_KEY_TERM_META_KEY, $campoKey); + $termCampoKey = $taxonomyCampoKeys[$term->remoteTermId()] ?? null; + + if (is_null($termCampoKey)) { + continue; + } + + update_term_meta($term->termId(), CampoKeysRepository::CAMPO_KEY_TERM_META_KEY, $termCampoKey); + } } } From bbd2b8467c125249bbc74c1e2fd26e7dc9bf20b7 Mon Sep 17 00:00:00 2001 From: Oleksandr Zhyian Date: Wed, 4 Sep 2024 14:50:26 +0300 Subject: [PATCH 2/3] refactor: improve code performance --- .../Search/FilterableTermsUpdater.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Infrastructure/Search/FilterableTermsUpdater.php b/src/Infrastructure/Search/FilterableTermsUpdater.php index 9c81cab..1a2fbce 100644 --- a/src/Infrastructure/Search/FilterableTermsUpdater.php +++ b/src/Infrastructure/Search/FilterableTermsUpdater.php @@ -544,16 +544,16 @@ private static function arraySearchBy(array $array, string $property, mixed $val private function maybeUpdateCampoKeys(DegreeProgramViewRaw $rawView, string $taxonomy, array $terms): void { - /** @var TermData $term */ - foreach ($terms as $term) { - $campoKeys = $rawView->campoKeys()->asArray(); - $campoKeyType = CampoKeysRepository::TAXONOMY_TO_CAMPO_KEY_MAP[$taxonomy] ?? ''; - $taxonomyCampoKeys = $campoKeys[$campoKeyType] ?? null; + $campoKeys = $rawView->campoKeys()->asArray(); + $campoKeyType = CampoKeysRepository::TAXONOMY_TO_CAMPO_KEY_MAP[$taxonomy] ?? ''; + $taxonomyCampoKeys = $campoKeys[$campoKeyType] ?? null; - if (!is_array($taxonomyCampoKeys)) { - continue; - } + if (!is_array($taxonomyCampoKeys)) { + return; + } + /** @var TermData $term */ + foreach ($terms as $term) { $termCampoKey = $taxonomyCampoKeys[$term->remoteTermId()] ?? null; if (is_null($termCampoKey)) { From e87c2ec69f860b3b0742991381c2bdd45a786068 Mon Sep 17 00:00:00 2001 From: Oleksandr Zhyian Date: Wed, 4 Sep 2024 16:34:25 +0300 Subject: [PATCH 3/3] docs: update changelog --- CHANGELOG.md | 4 ++++ composer.lock | 9 +++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6eef6e..a6ece97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Implemented synchronization for campo keys of child terms. + ## [2.0.0] - 2024-07-24 ### Added diff --git a/composer.lock b/composer.lock index 41d877e..ea17375 100644 --- a/composer.lock +++ b/composer.lock @@ -411,12 +411,12 @@ "source": { "type": "git", "url": "https://github.com/RRZE-Webteam/FAU-Studium-Common.git", - "reference": "059457f63e219f1fd3a0d8b3d3b063d7ea2b40db" + "reference": "2314da111571d795a3f3d3dbe194680737a26e07" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/RRZE-Webteam/FAU-Studium-Common/zipball/059457f63e219f1fd3a0d8b3d3b063d7ea2b40db", - "reference": "059457f63e219f1fd3a0d8b3d3b063d7ea2b40db", + "url": "https://api.github.com/repos/RRZE-Webteam/FAU-Studium-Common/zipball/2314da111571d795a3f3d3dbe194680737a26e07", + "reference": "2314da111571d795a3f3d3dbe194680737a26e07", "shasum": "" }, "require": { @@ -438,6 +438,7 @@ "suggest": { "inpsyde/modularity": "Modular PSR-11 implementation for WordPress Plugins, Themes or Libraries" }, + "default-branch": true, "type": "library", "autoload": { "psr-4": { @@ -488,7 +489,7 @@ "source": "https://github.com/RRZE-Webteam/FAU-Studium-Common/tree/main", "issues": "https://github.com/RRZE-Webteam/FAU-Studium-Common/issues" }, - "time": "2024-09-04T10:07:44+00:00" + "time": "2024-09-04T13:20:56+00:00" }, { "name": "webmozart/assert",