generated from alleyinteractive/create-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from alleyinteractive/feature/issue-73/add-and…
…-or-option-multiple-terms Issue-73: In Query block add AND/OR option for multiple terms
- Loading branch information
Showing
8 changed files
with
140 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
interface Types { | ||
[key: string]: { | ||
name: string; | ||
slug: string; | ||
rest_base: string; | ||
}; | ||
} | ||
|
||
/** | ||
* Builds the term query args for the WP REST API. | ||
* | ||
* @param string[] allowedTaxonomies The list of allowed taxonomies. | ||
* @param { [key: string]: any[] } terms The selected terms. | ||
* @param { [key: string]: any[] } availableTaxonomies The available taxonomies. | ||
* @param { [key: string]: string } termRelations The AND/OR relation used for each taxonomy. | ||
* @param string taxRelation The AND/OR relation used for all the terms. | ||
* @returns string The term query args. | ||
*/ | ||
export default function buildTermQueryArgs( | ||
allowedTaxonomies: string[], | ||
terms: { [key: string]: any[] }, | ||
availableTaxonomies: Types, | ||
termRelations: { [key: string]: string }, | ||
taxRelation: string, | ||
): string { | ||
const taxCount = allowedTaxonomies.filter((taxonomy: string) => terms[taxonomy]?.length > 0).length; // eslint-disable-line max-len | ||
|
||
const termQueryArgs: string[] = []; | ||
if (Object.keys(availableTaxonomies).length > 0) { | ||
allowedTaxonomies.forEach((taxonomy) => { | ||
if (terms[taxonomy]?.length > 0) { | ||
const restBase = availableTaxonomies[taxonomy].rest_base; | ||
if (restBase) { | ||
termQueryArgs.push(`${restBase}[terms]=${terms[taxonomy].map((term) => term.id).join(',')}`); | ||
if (termRelations[taxonomy] !== '' && typeof termRelations[taxonomy] !== 'undefined') { | ||
termQueryArgs.push(`${restBase}[operator]=${termRelations[taxonomy]}`); | ||
} | ||
} | ||
} | ||
}); | ||
if (taxCount > 1) { | ||
termQueryArgs.push(`tax_relation=${taxRelation}`); | ||
} | ||
} | ||
return termQueryArgs.join('&'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters