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); + } } }