Skip to content

Commit

Permalink
editor
Browse files Browse the repository at this point in the history
  • Loading branch information
mogmarsh committed Oct 24, 2023
1 parent 06868f4 commit 3b56676
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
20 changes: 20 additions & 0 deletions blocks/query/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@
"type": "array"
},
"type": "object"
},
"termRelations": {
"default": {},
"items": {
"default": "AND",
"enum": [
"AND",
"OR"
],
"type": "string"
},
"type": "object"
},
"taxRelation": {
"default": "AND",
"enum": [
"AND",
"OR"
],
"type": "string"
}
}
}
64 changes: 63 additions & 1 deletion blocks/query/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
PanelRow,
RadioControl,
RangeControl,
SelectControl,
TextControl,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
Expand Down Expand Up @@ -59,6 +60,8 @@ export default function Edit({
postTypes = ['post'],
searchTerm = '',
terms = {},
termRelations = {},
taxRelation = 'AND',
},
setAttributes,
}: EditProps) {
Expand Down Expand Up @@ -93,16 +96,27 @@ export default function Edit({
const [availableTaxonomies, setAvailableTaxonomies] = useState<Taxonomies>({});
const [availableTypes, setAvailableTypes] = useState<Types>({});

const taxCount = allowedTaxonomies.reduce((acc: number, taxonomy: string) => {
const hasTax = terms[taxonomy]?.length > 0 ? 1 : 0;
return acc + hasTax;
}, 0);

let termQueryArgs = '';
if (Object.keys(availableTaxonomies).length > 0) {
allowedTaxonomies.forEach((taxonomy) => {
if (terms[taxonomy]?.length > 0) {
const restBase = availableTaxonomies[taxonomy].rest_base;
if (restBase) {
termQueryArgs += `&${restBase}=${terms[taxonomy].map((term) => term.id).join(',')}`;
termQueryArgs += `&${restBase}[terms]=${terms[taxonomy].map((term) => term.id).join(',')}`;
if (termRelations[taxonomy] !== '' && typeof termRelations[taxonomy] !== 'undefined') {
termQueryArgs += `&${restBase}[operator]=${termRelations[taxonomy]}`;
}
}
}
});
if (taxCount > 1) {
termQueryArgs += `&tax_relation=${taxRelation}`;
}
}

const manualPostIds = manualPosts.map((post) => (post ?? null)).join(',');
Expand Down Expand Up @@ -200,6 +214,14 @@ export default function Edit({
setAttributes({ terms: newTermAttrs });
});

const setTermRelation = ((type: string, relation: string) => {
const newTermRelationAttrs = {
...termRelations,
[type]: relation,
};
setAttributes({ termRelations: newTermRelationAttrs });
});

const setNumberOfPosts = (newValue?: number) => {
setAttributes({
numberOfPosts: newValue,
Expand Down Expand Up @@ -319,9 +341,49 @@ export default function Edit({
onSelect={(newCategories: Term[]) => setTerms(taxonomy, newCategories)}
multiple
/>
{terms[taxonomy]?.length > 1 ? (
<SelectControl
label={sprintf(
__('%s Relation', 'wp-curate'),
availableTaxonomies[taxonomy].name || taxonomy

Check failure on line 348 in blocks/query/edit.tsx

View workflow job for this annotation

GitHub Actions / node-tests / Install, build, and test

Missing trailing comma

Check failure on line 348 in blocks/query/edit.tsx

View workflow job for this annotation

GitHub Actions / node-tests / Install, build, and test

Missing trailing comma
)}
help={__('AND: Posts must have all selected terms. OR: Posts may have one or more selected terms.', 'wp-curate')}
options={[
{
label: __('AND', 'wp-curate'),
value: 'AND',
},
{
label: __('OR', 'wp-curate'),
value: 'OR',
},
]}
onChange={(newValue) => setTermRelation(taxonomy, newValue)}
value={termRelations[taxonomy]}
/>
) : null}
<hr />
</>
))
) : null}
{taxCount > 1 ? (
<SelectControl
label={__('Taxonomy Relation', 'wp-curate')}
help={__('AND: Posts must meet all selected taxonomy requirements. OR: Posts may have meet one or more selected taxonomy requirements.', 'wp-curate')}
options={[
{
label: __('AND', 'wp-curate'),
value: 'AND',
},
{
label: __('OR', 'wp-curate'),
value: 'OR',
},
]}
onChange={(newValue) => setAttributes({ taxRelation: newValue })}
value={taxRelation}
/>
) : null }
<TextControl
label={__('Search Term', 'wp-curate')}
onChange={(next) => setAttributes({ searchTerm: next })}
Expand Down
4 changes: 4 additions & 0 deletions blocks/query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ interface EditProps {
terms?: {
[key: string]: any[];
};
termRelations?: {
[key: string]: string;
};
taxRelation?: string;
};
setAttributes: (attributes: any) => void;
}
Expand Down
4 changes: 2 additions & 2 deletions services/deduplicate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ export function mainDedupe() {
queryBlocks.forEach((queryBlock) => {
const { attributes } = queryBlock;
const {
backfillPosts = [],
backfillPosts = null,
deduplication = 'inherit',
posts = [],
numberOfPosts = 5,
postTypes = ['post'],
} = attributes;
if (!backfillPosts.length) {
if (!backfillPosts) {
return;
}
const postTypeString = postTypes.join(',');
Expand Down

0 comments on commit 3b56676

Please sign in to comment.