Skip to content

Commit

Permalink
Merge pull request #42 from RRZE-Webteam/FAU-434
Browse files Browse the repository at this point in the history
[FAU-434] Correctly assign campo keys to multiple terms within degree program
  • Loading branch information
zhyian authored Sep 4, 2024
2 parents 6db6806 + fba35b5 commit 0e4e9c7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 22 additions & 11 deletions src/Infrastructure/Search/FilterableTermsUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,19 @@ private function setObjectTerms(
string $taxonomy,
MultilingualString|MultilingualList|MultilingualLink|MultilingualLinks|Degree|AdmissionRequirement $property
): void {

$termIds = $this->maybeCreateTerms($taxonomy, $property);
/** @var array<int, TermData> $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
Expand All @@ -179,6 +179,8 @@ private function isValidPostId(int $postId): bool
* phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong
*
* Refactoring could decrease performance.
*
* @return array<int, TermData>
*/
private function maybeCreateTerms(
string $taxonomy,
Expand Down Expand Up @@ -225,15 +227,15 @@ private function maybeCreateTerms(

if ($termData->termId()) {
// Term was persisted already
$result[] = $termData->termId();
$result[$termData->termId()] = $termData;
$this->updateTerm($termData);
continue;
}

$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(),
Expand Down Expand Up @@ -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;
$taxonomyCampoKeys = $campoKeys[$campoKeyType] ?? null;

if (is_null($campoKey)) {
if (!is_array($taxonomyCampoKeys)) {
return;
}

update_term_meta($termId, CampoKeysRepository::CAMPO_KEY_TERM_META_KEY, $campoKey);
/** @var TermData $term */
foreach ($terms as $term) {
$termCampoKey = $taxonomyCampoKeys[$term->remoteTermId()] ?? null;

if (is_null($termCampoKey)) {
continue;
}

update_term_meta($term->termId(), CampoKeysRepository::CAMPO_KEY_TERM_META_KEY, $termCampoKey);
}
}
}

0 comments on commit 0e4e9c7

Please sign in to comment.