Skip to content

Commit

Permalink
Merge pull request #84 from ryanwelcher/feature/exclude-categories
Browse files Browse the repository at this point in the history
Adds control to exclude categories
  • Loading branch information
ryanwelcher authored Oct 9, 2024
2 parents 115fa12 + ed1dc0e commit a089b26
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions includes/Query_Params_Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Query_Params_Generator {
use Traits\Include_Posts;
use Traits\Meta_Query;
use Traits\Date_Query;
use Traits\Exclude_Taxonomies;
use Traits\Disable_Pagination;


Expand All @@ -29,6 +30,7 @@ class Query_Params_Generator {
'include_posts',
'meta_query',
'date_query',
'exclude_taxonomies',
'disable_pagination',
);

Expand Down
30 changes: 30 additions & 0 deletions includes/Traits/Exclude_Taxonomies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Exclude_Taxonomies
*/

namespace AdvancedQueryLoop\Traits;

/**
* Trait
*/
trait Exclude_Taxonomies {

/**
* Main processing function.
*/
public function process_exclude_taxonomies(): void {
$taxonomies_to_exclude = $this->custom_params['exclude_taxonomies'];
if( count( $taxonomies_to_exclude ) ) {
$tax_query = [];
foreach ( $taxonomies_to_exclude as $slug ) {
$tax_query[] = [
'taxonomy' => $slug,
'operator' => 'NOT EXISTS'
];
}
$this->custom_args['tax_query'] = $tax_query;
}
}
}

93 changes: 93 additions & 0 deletions src/components/exclude-taxonomies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* WordPress dependencies
*/
/**
* WordPress dependencies
*/
import { FormTokenField, BaseControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';

/**
* A helper to retrieve the correct items to display or save in the token field
*
* @param {Array} subSet
* @param {Array} fullSet
* @param {string} lookupProperty
* @param {string} returnProperty
* @return {Array} The correct items to display or save in the token field
*/
function prepDataFromTokenField(
subSet,
fullSet,
lookupProperty,
returnProperty
) {
const subsetFullObjects = fullSet.filter( ( item ) =>
subSet.includes( item[ lookupProperty ] )
);
return subsetFullObjects.map(
( { [ returnProperty ]: returnVal } ) => returnVal
);
}

export const ExcludeTaxonomies = ( { attributes, setAttributes } ) => {
const {
query: {
multiple_posts: multiplePosts = [],
postType,
exclude_taxonomies: excludeTaxonomies = [],
} = {},
} = attributes;

const taxonomies = useSelect(
( select ) => {
const knownTaxes = select( coreDataStore ).getTaxonomies();
return knownTaxes.filter(
( { types } ) =>
types.includes( postType ) ||
types.some( ( i ) => multiplePosts.includes( i ) )
);
},
[ multiplePosts, postType ]
);

return (
<BaseControl
help={ __(
'Choose taxonomies to exclude from the query.',
'advanced-query-loop'
) }
>
<FormTokenField
label={ __( 'Exclude Taxonomies', 'advanced-query-loop' ) }
value={
prepDataFromTokenField(
excludeTaxonomies,
taxonomies,
'slug',
'name'
) || []
}
suggestions={ [ ...taxonomies?.map( ( { name } ) => name ) ] }
onChange={ ( selectedTaxonomies ) => {
setAttributes( {
query: {
...attributes.query,
exclude_taxonomies:
prepDataFromTokenField(
selectedTaxonomies,
taxonomies,
'name',
'slug'
) || [],
},
} );
} }
__experimentalExpandOnFocus
__experimentalShowHowTo={ false }
/>
</BaseControl>
);
};
2 changes: 2 additions & 0 deletions src/variations/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { MultiplePostSelect } from '../components/multiple-post-select';
import { PostOrderControls } from '../components/post-order-controls';
import { PostExcludeControls } from '../components/post-exclude-controls';
import { PostIncludeControls } from '../components/post-include-controls';
import { ExcludeTaxonomies } from '../components/exclude-taxonomies';
import { PaginationToggle } from '../components/pagination-toggle';

/**
Expand Down Expand Up @@ -63,6 +64,7 @@ const withAdvancedQueryControls = ( BlockEdit ) => ( props ) => {
<PostOffsetControls { ...props } />
<PostOrderControls { ...props } />
<PostExcludeControls { ...props } />
<ExcludeTaxonomies { ...props } />
<PostIncludeControls { ...props } />
<PostMetaQueryControls { ...props } />
<PostDateQueryControls { ...props } />
Expand Down

0 comments on commit a089b26

Please sign in to comment.