diff --git a/src/media.php b/src/media.php index b0e9e81..9f7a226 100644 --- a/src/media.php +++ b/src/media.php @@ -149,23 +149,52 @@ class MediaManager { return; } - $selected = isset( $_GET[ $this->taxonomy ] ) ? $_GET[ $this->taxonomy ] : false; - $dropdown = wp_dropdown_categories([ + $filter_name = "filter_" . $this->taxonomy; + $excluded_name = "excluded_" . $this->taxonomy; + + $filter_dropdown = wp_dropdown_categories([ 'show_option_all' => 'All Tags', 'taxonomy' => $this->taxonomy, - 'name' => $this->taxonomy, + 'name' => $filter_name . "[]", 'orderby' => 'name', - 'selected' => $selected, 'hierarchical' => true, 'value_field' => 'term_id', 'depth' => 3, - 'hide_empty' => true, + 'hide_empty' => false, 'echo' => false ]); - $dropdown = str_replace('", ">", $excluded_dropdown); + foreach ( $selected as $sid ) { + $excluded_dropdown = str_replace("value=\"{$sid}\">", "value=\"{$sid}\" selected>", $excluded_dropdown); + } + + + echo ""; + echo $filter_dropdown; + echo ""; + echo $excluded_dropdown; } public function apply_list_filter( $query ) { @@ -176,27 +205,70 @@ class MediaManager { return; } - if (!empty($_GET[$this->taxonomy])) { - - $id = $_GET[$this->taxonomy]; + $filter_name = "filter_" . $this->taxonomy; + $excluded_name = "excluded_" . $this->taxonomy; - $tag = $this->tags->get_by_id((int)$id); - if ( $tag === null ) { - return; + $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); + if ( $tag === null ) { + 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); - unset($query->query_vars['ttgf_media_tags']); - $query->set('tax_query', [[ - 'taxonomy' => 'ttgf_media_tags', + $queries[] = [ + 'taxonomy' => $this->taxonomy, 'field' => 'term_id', - 'terms' => [$ids[0]], + 'terms' => $ids, '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 ) { diff --git a/src/tag.php b/src/tag.php index 0643deb..4aa9f8d 100644 --- a/src/tag.php +++ b/src/tag.php @@ -107,6 +107,10 @@ class TagTree { '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); } @@ -185,7 +189,7 @@ class TagTree { public function get_by_id( $term_id ) { foreach ( $this->root_tags as $rt ) { - if ($rt->id == $term_id) { + if ( $rt->id == $term_id ) { return $rt; } @@ -335,6 +339,4 @@ class TagTree { } } - - }