Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taxonomy Builder #90

Merged
merged 39 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
32c3a3b
Basic taxonomy controls
ryanwelcher Nov 16, 2023
3444cee
Added ACF to the registeredMeta array
jvanja Sep 21, 2023
213dd80
Combine multiple custom field sources for the post meta query.
ryanwelcher Dec 29, 2023
4f96c89
Adds order by post ID and fixes a duplicate Order in label.
ryanwelcher Nov 17, 2023
ff24edb
Reset the meta_query array before processing new meta.
ryanwelcher Dec 19, 2023
546ddea
Update readmes and version bumps for release.
ryanwelcher Dec 29, 2023
2a921c7
2.1.0
ryanwelcher Dec 29, 2023
0c61633
Update readme.
ryanwelcher Dec 29, 2023
99cc3ee
Gitignore test artifacts
ryanwelcher Feb 20, 2024
59c6c71
Retrieve the query from the passed block via the filter.
ryanwelcher Mar 13, 2024
afc1bf2
Bump to 2.1.1
ryanwelcher Mar 13, 2024
4c14dc7
Add env config.
ryanwelcher Mar 14, 2024
123f9a8
Put global constants in namespace
szepeviktor Jan 13, 2023
afc6238
Revert "Put global constants in namespace"
ryanwelcher Mar 14, 2024
a48d28d
Merge branch 'trunk' into feature/taxonomy-control
ryanwelcher Aug 31, 2024
21e4023
Basic taxonomy controls
ryanwelcher Nov 16, 2023
5118bbd
Update readmes and version bumps for release.
ryanwelcher Dec 29, 2023
dc83058
Put global constants in namespace
szepeviktor Jan 13, 2023
7dc8934
Revert "Put global constants in namespace"
ryanwelcher Mar 14, 2024
d70a458
Merge branch 'feature/taxonomy-control' of github.com:ryanwelcher/adv…
ryanwelcher Nov 21, 2024
c57f072
Add controls in a modal for now
ryanwelcher Nov 21, 2024
6bc3cfc
Add the __nextHasNoMarginBottom to the console will stop yelling at me.
ryanwelcher Nov 28, 2024
80c6a34
Add the utils file and move a few functions into there.
ryanwelcher Nov 28, 2024
73e0790
Commit before removing tabs in case I change my mind.
ryanwelcher Nov 29, 2024
4210dd9
Add the trait and process for meta_query.
ryanwelcher Nov 29, 2024
855c69d
Simplify the interface. We don't really need the tabs.
ryanwelcher Nov 29, 2024
b58da73
Process booleans correctly and harden up the code. 💪
ryanwelcher Nov 29, 2024
f077841
Fix fatal when queries are not set.
ryanwelcher Nov 29, 2024
578b99d
Rename controls and start refining the interface.
ryanwelcher Nov 29, 2024
572b1fc
Name things a little better.
ryanwelcher Nov 29, 2024
8c39894
Disable the button if there are no taxonomies associated with the sel…
ryanwelcher Nov 29, 2024
8d813c0
Adding relation control to advanced mode.
ryanwelcher Nov 29, 2024
ea03a69
Clean up and remove exclude taxonomy control
ryanwelcher Nov 29, 2024
76b1926
Fix bad merge conflict fix.
ryanwelcher Nov 29, 2024
98ec88c
Remove processing the exclude_taxonomies field.
ryanwelcher Dec 2, 2024
c8c4a04
WIP moving advanced into each item
ryanwelcher Dec 5, 2024
aaaaa87
Fast refresh is to hot right now.
ryanwelcher Dec 5, 2024
e821cb1
Clean up the UI. Make the relation toggle better. Ignore Kevin and hi…
ryanwelcher Dec 5, 2024
6e656e4
Merge branch 'trunk' into feature/taxonomy-control
ryanwelcher Dec 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading