-
Notifications
You must be signed in to change notification settings - Fork 10
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 ryanwelcher/feature/exclude-categories
Adds control to exclude categories
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 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
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; | ||
} | ||
} | ||
} | ||
|
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,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> | ||
); | ||
}; |
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