Skip to content

Commit

Permalink
Merge pull request #90 from ryanwelcher/feature/taxonomy-control
Browse files Browse the repository at this point in the history
Taxonomy Builder
  • Loading branch information
ryanwelcher authored Dec 5, 2024
2 parents 98031d4 + 6e656e4 commit a89c891
Show file tree
Hide file tree
Showing 20 changed files with 686 additions and 34 deletions.
4 changes: 2 additions & 2 deletions includes/Query_Params_Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Query_Params_Generator {
use Traits\Include_Posts;
use Traits\Meta_Query;
use Traits\Date_Query;
use Traits\Exclude_Taxonomies;
use Traits\Disable_Pagination;
use Traits\Tax_Query;
use Traits\Post_Parent;


Expand All @@ -31,8 +31,8 @@ class Query_Params_Generator {
'include_posts',
'meta_query',
'date_query',
'exclude_taxonomies',
'disable_pagination',
'tax_query',
'post_parent',
);

Expand Down
64 changes: 64 additions & 0 deletions includes/Traits/Tax_Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Manage parsing the meta query information
*/

namespace AdvancedQueryLoop\Traits;

trait Tax_Query {

public function process_tax_query() {
$this->custom_args['tax_query'] = $this->parse_tax_query( $this->custom_params['tax_query'] );
}

public function parse_tax_query( $queries ) {
$tax_query = [];
// Don't process empty array of queries.
if ( isset( $queries['queries'] ) && count( $queries['queries'] ) > 0 ) {
// Handle the relation parameter.
if ( isset( $queries['relation'] ) && count( $queries['queries'] ) > 1 ) {
$tax_query['relation'] = $queries['relation'];
}
// Loop the queries
foreach ( $queries['queries'] as $query ) {
if ( isset( $query['taxonomy'] ) && isset( $query['terms'] ) && count( $query['terms'] ) > 0 ) {
$processed_query = array_filter( $query, fn( $key ) => 'id' !== $key, ARRAY_FILTER_USE_KEY );
$processed_query['include_children'] = filter_var( $query['include_children'], FILTER_VALIDATE_BOOLEAN );
$processed_query['terms'] = [ ...array_map( fn( $term ) => get_term_by( 'name', $term, $query['taxonomy'] )->term_id, $query['terms'] ) ];
$tax_query[] = $processed_query;
}
}
}
return $tax_query;
}
}

/**
* Example complex query:
* $tax_query = array(
* 'relation' => 'OR',
* array(
* 'taxonomy' => 'category',
* 'field' => 'slug',
* 'terms' => array( 'quotes' ),
* ),
* array(
* 'taxonomy' => 'tag',
* 'field' => 'slug',
* 'terms' => array( 2 ),
* ),
* array(
* 'relation' => 'AND',
* array(
* 'taxonomy' => 'post_format',
* 'field' => 'slug',
* 'terms' => array( 'post-format-quote' ),
* ),
* array(
* 'taxonomy' => 'category',
* 'field' => 'slug',
* 'terms' => array( 'wisdom' ),
* ),
* ),
* );
*/
8 changes: 6 additions & 2 deletions includes/query-loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ function add_more_sort_by( $query_params ) {
$query_params['orderby']['enum'][] = 'post__in';
$query_params['orderby']['enum'][] = 'comment_count';
$query_params['orderby']['enum'][] = 'name';
// die( '<pre>' .print_r( $query_params , 1 ) .'</pre>' );
return $query_params;
}

Expand All @@ -151,10 +152,13 @@ function add_custom_query_params( $args, $request ) {
$request->get_params(),
false,
);

// Merge all queries.
return array_merge(
$merged = array_merge(
$args,
array_filter( $filtered_query_args )
);

// die( var_dump( $request->get_params() ) );

return $merged;
}
30 changes: 30 additions & 0 deletions includes/taxonomy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Taxonomy relation functions
*
* @package AdvancedQueryLoop\Taxonomy
*/

namespace AdvancedQueryLoop\Taxonomy;

function convert_names_to_ids( $names, $tax ) {
$rtn = [];
foreach ( $names as $name ) {
$term = get_term_by( 'name', $name, $tax );
if ( $term ) {
$rtn[] = $term->term_id;
}
}
return $rtn;
}

function parse_taxonomy_query( $tax_query_data ) {
return [
[
'taxonomy' => $tax_query_data['taxonomy'],
'terms' => convert_names_to_ids( $tax_query_data['terms'], $tax_query_data['taxonomy'] ),
'include_children' => ( ! isset( $tax_query_data['include_children'] ) || 'true' === $tax_query_data['include_children'] ) ? true : false,
'operator' => $tax_query_data['operator'],
],
];
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"setup": "composer run dev",
"start": "wp-scripts start",
"start:hot": "wp-scripts start --hot",
"build": "wp-scripts build",
"plugin-zip": "wp-scripts plugin-zip",
"format": "wp-scripts format",
Expand Down
27 changes: 4 additions & 23 deletions src/components/exclude-taxonomies.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
* WordPress dependencies
*/
/**
* WordPress dependencies
*/
Expand All @@ -10,27 +7,9 @@ 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
* Internal dependencies
*/
function prepDataFromTokenField(
subSet,
fullSet,
lookupProperty,
returnProperty
) {
const subsetFullObjects = fullSet.filter( ( item ) =>
subSet.includes( item[ lookupProperty ] )
);
return subsetFullObjects.map(
( { [ returnProperty ]: returnVal } ) => returnVal
);
}
import { prepDataFromTokenField } from '../utils';

export const ExcludeTaxonomies = ( { attributes, setAttributes } ) => {
const {
Expand Down Expand Up @@ -59,6 +38,7 @@ export const ExcludeTaxonomies = ( { attributes, setAttributes } ) => {
'Choose taxonomies to exclude from the query.',
'advanced-query-loop'
) }
__nextHasNoMarginBottom
>
<FormTokenField
label={ __( 'Exclude Taxonomies', 'advanced-query-loop' ) }
Expand Down Expand Up @@ -87,6 +67,7 @@ export const ExcludeTaxonomies = ( { attributes, setAttributes } ) => {
} }
__experimentalExpandOnFocus
__experimentalShowHowTo={ false }
__nextHasNoMarginBottom
/>
</BaseControl>
);
Expand Down
2 changes: 2 additions & 0 deletions src/components/multiple-post-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const MultiplePostSelect = ( { attributes, setAttributes } ) => {
'These post types will be queried in addition to the main post type.',
'advanced-query-loop'
) }
__nextHasNoMarginBottom
>
<FormTokenField
label={ __( 'Additional Post Types', 'advanced-query-loop' ) }
Expand All @@ -46,6 +47,7 @@ export const MultiplePostSelect = ( { attributes, setAttributes } ) => {
} }
__experimentalExpandOnFocus
__experimentalShowHowTo={ false }
__nextHasNoMarginBottom
/>
</BaseControl>
);
Expand Down
1 change: 1 addition & 0 deletions src/components/pagination-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const PaginationToggle = ( { attributes, setAttributes } ) => {
},
} );
} }
__nextHasNoMarginBottom
/>
);
};
2 changes: 2 additions & 0 deletions src/components/post-date-query-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const PostDateQueryControls = ( { attributes, setAttributes } ) => {
},
} );
} }
__nextHasNoMarginBottom
/>
{ range !== '' && (
<CheckboxControl
Expand Down Expand Up @@ -156,6 +157,7 @@ export const PostDateQueryControls = ( { attributes, setAttributes } ) => {
},
} );
} }
__nextHasNoMarginBottom
/>
{ relationFromQuery !== '' &&
! relationFromQuery.includes( 'current' ) && (
Expand Down
1 change: 1 addition & 0 deletions src/components/post-exclude-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const PostExcludeControls = ( { attributes, setAttributes } ) => {
'advanced-query-loop'
)
}
__nextHasNoMarginBottom
/>
</>
);
Expand Down
2 changes: 2 additions & 0 deletions src/components/post-include-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const PostIncludeControls = ( { attributes, setAttributes } ) => {
'Start typing to search for a post title or manually enter one.',
'advanced-query-loop'
) }
__nextHasNoMarginBottom
>
<FormTokenField
label={ __( 'Posts', 'advanced-query-loop' ) }
Expand All @@ -118,6 +119,7 @@ export const PostIncludeControls = ( { attributes, setAttributes } ) => {
} }
__experimentalExpandOnFocus
__experimentalShowHowTo={ false }
__nextHasNoMarginBottom
/>
</BaseControl>
</>
Expand Down
5 changes: 4 additions & 1 deletion src/components/post-meta-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const PostMetaControl = ( {
'Start typing to search for a meta key or manually enter one.',
'advanced-query-loop'
) }
__nextHasNoMarginBottom
>
<FormTokenField
label={ __( 'Meta Key', 'advanced-query-loop' ) }
Expand Down Expand Up @@ -94,6 +95,7 @@ export const PostMetaControl = ( {
},
} );
} }
__nextHasNoMarginBottom
/>
</BaseControl>
<TextControl
Expand Down Expand Up @@ -140,9 +142,10 @@ export const PostMetaControl = ( {
},
} );
} }
__nextHasNoMarginBottom
/>
<Button
isSmall
size="small"
variant="secondary"
isDestructive
onClick={ () => {
Expand Down
1 change: 1 addition & 0 deletions src/components/post-meta-query-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const PostMetaQueryControls = ( { attributes, setAttributes } ) => {
},
} )
}
__nextHasNoMarginBottom
/>
) }

Expand Down
2 changes: 2 additions & 0 deletions src/components/post-order-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const PostOrderControls = ( { attributes, setAttributes } ) => {
},
} );
} }
__nextHasNoMarginBottom
/>
<ToggleControl
__nextHasNoMarginBottom
Expand All @@ -100,6 +101,7 @@ export const PostOrderControls = ( { attributes, setAttributes } ) => {
},
} );
} }
__nextHasNoMarginBottom
/>
</>
);
Expand Down
Loading

0 comments on commit a89c891

Please sign in to comment.