Added exclusion of tags in media library

This commit is contained in:
Patrick 2026-04-24 20:02:02 +02:00
parent 89f2585709
commit 190a69a57d
2 changed files with 96 additions and 22 deletions

View File

@ -149,23 +149,52 @@ class MediaManager {
return; return;
} }
$selected = isset( $_GET[ $this->taxonomy ] ) ? $_GET[ $this->taxonomy ] : false; $filter_name = "filter_" . $this->taxonomy;
$dropdown = wp_dropdown_categories([ $excluded_name = "excluded_" . $this->taxonomy;
$filter_dropdown = wp_dropdown_categories([
'show_option_all' => 'All Tags', 'show_option_all' => 'All Tags',
'taxonomy' => $this->taxonomy, 'taxonomy' => $this->taxonomy,
'name' => $this->taxonomy, 'name' => $filter_name . "[]",
'orderby' => 'name', 'orderby' => 'name',
'selected' => $selected,
'hierarchical' => true, 'hierarchical' => true,
'value_field' => 'term_id', 'value_field' => 'term_id',
'depth' => 3, 'depth' => 3,
'hide_empty' => true, 'hide_empty' => false,
'echo' => false 'echo' => false
]); ]);
$dropdown = str_replace('<select ', '<select multiple ', $dropdown); $selected = isset( $_GET[ $filter_name ] ) ? $_GET[ $filter_name ] : [];
$filter_dropdown = str_replace('<select ', '<select multiple ', $filter_dropdown);
$filter_dropdown = str_replace(" selected='selected'>", ">", $filter_dropdown);
foreach ( $selected as $sid ) {
$filter_dropdown = str_replace("value=\"{$sid}\">", "value=\"{$sid}\" selected>", $filter_dropdown);
}
echo $dropdown; $excluded_dropdown = wp_dropdown_categories([
'show_option_all' => 'None',
'taxonomy' => $this->taxonomy,
'name' => $excluded_name . "[]",
'orderby' => 'name',
'hierarchical' => true,
'value_field' => 'term_id',
'depth' => 3,
'hide_empty' => false,
'echo' => false
]);
$selected = isset( $_GET[ $excluded_name ] ) ? $_GET[ $excluded_name ] : [ $this->tags->get_by_name("Generated")->id ];
$excluded_dropdown = str_replace('<select ', '<select multiple ', $excluded_dropdown);
$excluded_dropdown = str_replace(" selected='selected'>", ">", $excluded_dropdown);
foreach ( $selected as $sid ) {
$excluded_dropdown = str_replace("value=\"{$sid}\">", "value=\"{$sid}\" selected>", $excluded_dropdown);
}
echo "<label for='{$filter_name}'>Inkludierte Tags</label>";
echo $filter_dropdown;
echo "<label for='{$excluded_name}'>Excludierte Tags</label>";
echo $excluded_dropdown;
} }
public function apply_list_filter( $query ) { public function apply_list_filter( $query ) {
@ -176,27 +205,70 @@ class MediaManager {
return; return;
} }
if (!empty($_GET[$this->taxonomy])) { $filter_name = "filter_" . $this->taxonomy;
$excluded_name = "excluded_" . $this->taxonomy;
$id = $_GET[$this->taxonomy]; $queries = [];
if ( array_key_exists($filter_name, $_GET) && ! empty($_GET[$filter_name]) && array_search(0, $_GET[$filter_name]) !== 0 ) {
$tags = [];
foreach ( $_GET[$filter_name] as $id ) {
$tag = $this->tags->get_by_id((int)$id); $tag = $this->tags->get_by_id((int)$id);
if ( $tag === null ) { if ( $tag === null ) {
return; continue;
}
$tags = array_merge($tags, [$tag], $tag->get_all_children());
} }
$tags = array_merge([$tag], $tag->get_all_children());
$ids = array_map(fn ($t) => $t->id, $tags); $ids = array_map(fn ($t) => $t->id, $tags);
unset($query->query_vars['ttgf_media_tags']); $queries[] = [
$query->set('tax_query', [[ 'taxonomy' => $this->taxonomy,
'taxonomy' => 'ttgf_media_tags',
'field' => 'term_id', 'field' => 'term_id',
'terms' => [$ids[0]], 'terms' => $ids,
'include_children' => true, 'include_children' => true,
]]); 'operator' => 'IN'
];
} }
if ( ! array_key_exists($excluded_name, $_GET) ) {
$_GET[$excluded_name] = [ $this->tags->get_by_name("Generated")->id ];
}
if ( (count($_GET[$excluded_name]) == 1 && $_GET[$excluded_name][0] != 0) || count($_GET[$excluded_name]) > 1 ) {
$tags = [];
foreach ( $_GET[$excluded_name] as $id ) {
$tag = $this->tags->get_by_id((int)$id);
if ( $tag === null ) {
continue;
}
$tags = array_merge($tags, [$tag], $tag->get_all_children());
}
$ids = array_map(fn ($t) => $t->id, $tags);
$queries[] = [
'taxonomy' => $this->taxonomy,
'field' => 'term_id',
'terms' => $ids,
'include_children' => true,
'operator' => 'NOT IN'
];
}
if ( count($queries) > 1 ) {
$queries['relation'] = 'AND';
}
$query->set('tax_query', $queries);
} }
private function _build_dropdown( $name, $options, $selected, $empty = true ) { private function _build_dropdown( $name, $options, $selected, $empty = true ) {

View File

@ -107,6 +107,10 @@ class TagTree {
'update_count_callback' => '_update_generic_term_count' 'update_count_callback' => '_update_generic_term_count'
]); ]);
if ( empty(get_terms([ 'taxonomy' => $taxonomy_id, 'hide_empty' => false ])) ) {
wp_insert_term("Generated", $taxonomy_id);
}
return new TagTree($taxonomy_id); return new TagTree($taxonomy_id);
} }
@ -185,7 +189,7 @@ class TagTree {
public function get_by_id( $term_id ) { public function get_by_id( $term_id ) {
foreach ( $this->root_tags as $rt ) { foreach ( $this->root_tags as $rt ) {
if ($rt->id == $term_id) { if ( $rt->id == $term_id ) {
return $rt; return $rt;
} }
@ -335,6 +339,4 @@ class TagTree {
} }
} }
} }