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

Correct ids relation in the degree programs overview shortcode #8

Merged
merged 3 commits into from
May 31, 2024
Merged
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
49 changes: 30 additions & 19 deletions src/Infrastructure/Repository/WpQueryArgsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,35 @@ public function build(CollectionCriteria $criteria): WpQueryArgs
$queryArgs = $this->applyFilter($filter, $queryArgs, $criteria->languageCode());
}

foreach ($criteria->hisCodes() as $hisCode) {
$queryArgs = $this->applyHisCode($hisCode, $queryArgs);
if (count($criteria->hisCodes()) > 0) {
$queryArgs = $this->applyHisCodes($criteria->hisCodes(), $queryArgs);
}

return $queryArgs;
}

public function applyHisCode(string $hisCode, WpQueryArgs $queryArgs): WpQueryArgs
/**
* @param array<string> $hisCodes
*/
public function applyHisCodes(array $hisCodes, WpQueryArgs $queryArgs): WpQueryArgs
{
$taxQueryItem = [
'relation' => 'AND',
];

try {
$taxonomyToTermMapping = $this->campoKeysRepository->taxonomyToTermsMapFromCampoKeys(
CampoKeys::fromHisCode($hisCode)
);
$hisCodesQuery = [];

foreach ($hisCodes as $hisCode) {
$taxQueryItem = [
'relation' => 'AND',
];

try {
$taxonomyToTermMapping = $this->campoKeysRepository->taxonomyToTermsMapFromCampoKeys(
CampoKeys::fromHisCode($hisCode)
);
} catch (RuntimeException) {
continue;
}

if (count($taxonomyToTermMapping) === 0) {
return $queryArgs;
continue;
}

foreach ($taxonomyToTermMapping as $taxonomy => $termId) {
Expand All @@ -119,14 +128,16 @@ public function applyHisCode(string $hisCode, WpQueryArgs $queryArgs): WpQueryAr
];
}

return $queryArgs->withTaxQueryItem($taxQueryItem);
} catch (RuntimeException) {
/*
* Return an empty result if one or more campo keys in HIS code are not matched to any terms.
* Otherwise invalid HIS codes would be matched to false results.
*/
return $queryArgs->withArg('post__in', [0]);
$hisCodesQuery[] = $taxQueryItem;
}

if (count($hisCodesQuery) === 0) {
return $queryArgs;
}

$hisCodesQuery['relation'] = 'OR';

return $queryArgs->withTaxQueryItem($hisCodesQuery);
}

private function applyOrderBy(
Expand Down