Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FAU-434] Correctly assign campo keys to multiple terms within degree program #42

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}