Compare commits

..

No commits in common. "190a69a57d1c4e2e4e88d1ec1eb8214f189877c8" and "a748afe1c41ad767c95437db2c3adaac699e2801" have entirely different histories.

4 changed files with 67 additions and 206 deletions

View File

@ -1,29 +0,0 @@
.theatergf.tag {
padding: 1px 1em;
border-radius: 9999px;
border-top-left-radius: 9999px;
border-bottom-left-radius: 9999px;
border-style: solid;
border-width: 1px;
position: relative;
.remove {
position: absolute;
top: 1px;
right: 10px;
display: none;
color: red;
}
}
.theatergf.tag:hover {
.remove {
display: block;
background: gray;
cursor: pointer;
}
}

View File

@ -1,8 +0,0 @@
function theatergf_remove_tag_cb(event) {
target = event.target
form = document.querySelector('#post')
}

View File

@ -5,12 +5,10 @@ namespace TheaterGF\Core;
require_once __DIR__ . "/tag.php"; require_once __DIR__ . "/tag.php";
class MediaManager { class MediaManager {
readonly public string $taxonomy;
public $tags; public $tags;
public function __construct() { public function __construct() {
$this->taxonomy = 'ttgf_media_tags';
add_action('init', [ $this, 'register_structure' ]); add_action('init', [ $this, 'register_structure' ]);
@ -31,12 +29,10 @@ class MediaManager {
add_action('restrict_manage_posts', [ $this, 'show_list_filter' ]); add_action('restrict_manage_posts', [ $this, 'show_list_filter' ]);
add_filter('pre_get_posts', [ $this, 'apply_list_filter' ]); add_filter('pre_get_posts', [ $this, 'apply_list_filter' ]);
add_action('admin_enqueue_scripts', [ $this, 'enqueue_admin_style' ]);
} }
public function register_structure() { public function register_structure() {
$this->tags = TagTree::create($this->taxonomy); $this->tags = TagTree::create('ttgf_media_tags');
} }
public function create_tag_management_box() { public function create_tag_management_box() {
@ -54,10 +50,26 @@ class MediaManager {
?> ?>
<h3>Assigned</h3> <h3>Assigned</h3>
<div class="theatergf tag-container"> <div class="ttgf_tags_container">
<?php foreach ( $tags as $tag ): ?> <?php foreach ( $tags as $tag ): ?>
<div class="theatergf tag"><?= $tag->full_name() ?><button class="remove" name="ttgf_remove_tag" value="<?= $tag->id ?>">X</button></div> <div class="ttgf_tag"><?= $tag->full_name() ?></div>
<?php endforeach ?><br/> <?php endforeach ?><br/>
<?php foreach ( get_the_terms( $post, 'ttgf_media_tags') ?: [] as $term): ?>
<?= $term->name ?> - <?= $term->term_id ?><br/>
<?php endforeach ?><br/>
<?php foreach ( $this->tags->get_multiple(get_the_terms( $post, 'ttgf_media_tags') ?: []) as $term): ?>
<?= $term->full_name() ?> - <?= $term->id ?><br/>
<?php endforeach ?>
</div>
<h3>Available</h3>
<div>
<?php foreach ( $this->tags->get_all() as $tag ): ?>
<?= $tag->full_name() ?>: <?= $tag->id ?><br/>
<?php endforeach ?><br/>
<?php foreach ( get_terms([ 'taxonomy' => 'ttgf_media_tags', 'hide_empty' => false ]) as $term): ?>
<?= $term->name ?> - <?= $term->term_id ?><br/>
<?php endforeach ?>
</div> </div>
<h3>Add new</h3> <h3>Add new</h3>
@ -73,14 +85,6 @@ class MediaManager {
<?php <?php
} }
public function enqueue_admin_style() {
global $typenow;
wp_enqueue_style('theatergf-tag-style', plugin_dir_url(__FILE__) . '/media.css');
if ( $typenow === 'post' ) {
}
}
public function save_tags( $post ) { public function save_tags( $post ) {
$is_autosave = wp_is_post_autosave( $post ); $is_autosave = wp_is_post_autosave( $post );
$is_revision = wp_is_post_revision( $post ); $is_revision = wp_is_post_revision( $post );
@ -91,7 +95,7 @@ class MediaManager {
} }
$new_tag = sanitize_text_field($_POST['ttgf_new_tag']); $new_tag = sanitize_text_field($_POST['ttgf_new_tag']);
if ( isset($new_tag) && $new_tag !== '' ) { if ( isset($new_tag) ) {
$tag = $this->tags->get_by_name($new_tag); $tag = $this->tags->get_by_name($new_tag);
@ -99,31 +103,13 @@ class MediaManager {
$tag = $this->tags->create_tag($new_tag); $tag = $this->tags->create_tag($new_tag);
} }
if ( is_wp_error($tag) ) { wp_set_post_terms($post, [ $tag->id ], 'ttgf_media_tags', true);
return;
}
wp_set_post_terms($post, [ $tag->id ], $this->taxonomy, true);
return;
}
$remove_tag = sanitize_text_field($_POST['ttgf_remove_tag']);
if ( isset($remove_tag) && $remove_tag !== '') {
$tag = $this->tags->get_by_id((int)$remove_tag);
if ( $tag !== null ) {
wp_remove_object_terms($post, $tag->id, $this->taxonomy);
}
return;
} }
} }
public function create_edit_media_control( $fields, $post ) { public function create_edit_media_control( $fields, $post ) {
/*$terms = get_terms([ 'taxonomy' => 'ttgf_media_tags', 'hide_empty' => false ]); $terms = get_terms([ 'taxonomy' => 'ttgf_media_tags', 'hide_empty' => false ]);
$fields['ttgf_media_tags'] = [ $fields['ttgf_media_tags'] = [
'label' => 'Tags', 'label' => 'Tags',
@ -137,7 +123,7 @@ class MediaManager {
'label' => 'Folder Attributes', 'label' => 'Folder Attributes',
'input' => 'html', 'input' => 'html',
'html' => "<select name='attachments[{$post->ID}][ttgf_media_hidden]'><option></option><option value='hidden'>Hidden</option></select>" 'html' => "<select name='attachments[{$post->ID}][ttgf_media_hidden]'><option></option><option value='hidden'>Hidden</option></select>"
];*/ ];
return $fields; return $fields;
} }
@ -149,126 +135,58 @@ class MediaManager {
return; return;
} }
$filter_name = "filter_" . $this->taxonomy; $selected = isset( $_GET[ 'ttgf_media_folder' ] ) ? $_GET[ 'ttgf_media_folder' ] : false;
$excluded_name = "excluded_" . $this->taxonomy; wp_dropdown_categories([
'show_option_all' => 'All folders',
$filter_dropdown = wp_dropdown_categories([ 'taxonomy' => 'ttgf_media_folder',
'show_option_all' => 'All Tags', 'name' => 'ttgf_media_folder',
'taxonomy' => $this->taxonomy,
'name' => $filter_name . "[]",
'orderby' => 'name', 'orderby' => 'name',
'selected' => $selected,
'hierarchical' => true, 'hierarchical' => true,
'value_field' => 'term_id', 'value_field' => 'name',
'depth' => 3, 'depth' => 3,
'hide_empty' => false, 'hide_empty' => true,
'echo' => false
]); ]);
$selected = isset( $_GET[ $filter_name ] ) ? $_GET[ $filter_name ] : []; echo '<select name="ttgf_media_folder_filter">';
$filter_dropdown = str_replace('<select ', '<select multiple ', $filter_dropdown); echo '<option>Select Filter</option>';
$filter_dropdown = str_replace(" selected='selected'>", ">", $filter_dropdown); echo '<option value="show_hidden">Display hidden</option>';
foreach ( $selected as $sid ) { echo '</select>';
$filter_dropdown = str_replace("value=\"{$sid}\">", "value=\"{$sid}\" selected>", $filter_dropdown);
return;
/*$terms = get_terms([ 'taxonomy' => 'ttgf_media_folder', 'hide_empty' => false ]);
echo '<select name="ttgf_media_folder_filter">';
echo '<option value="">All Folders</option>';
foreach ( $terms as $term ) {
echo "<option value='{$term->slug}'>{$term->name}</option>";
} }
$excluded_dropdown = wp_dropdown_categories([ echo '</select>';*/
'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 ) {
if ( ! is_admin() || ! $query->is_main_query() ) { if ( ! is_admin() || $query->is_main_query() ) {
return;
}
if ($query->get('post_type') !== 'attachment') {
return; return;
} }
$filter_name = "filter_" . $this->taxonomy; if (!empty($_GET['media_folder_filter'])) {
$excluded_name = "excluded_" . $this->taxonomy; $query->set('tax_query', [[
'taxonomy' => 'ttgf_media_folder',
$queries = []; 'field' => 'name',
'terms' => $_GET['ttgf_media_folder'],
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());
}
$ids = array_map(fn ($t) => $t->id, $tags);
$queries[] = [
'taxonomy' => $this->taxonomy,
'field' => 'term_id',
'terms' => $ids,
'include_children' => true,
'operator' => 'IN'
];
} }
if ( ! array_key_exists($excluded_name, $_GET) ) { $show_hidden = isset($_GET['ttgf_media_folder_filter']) && $_GET['ttgf_media_folder_filter'] != "show_hidden";
$_GET[$excluded_name] = [ $this->tags->get_by_name("Generated")->id ]; $query->set('meta_query', [[
} 'key' => 'ttgf_media_hidden',
'value' => ( ! $show_hidden),
if ( (count($_GET[$excluded_name]) == 1 && $_GET[$excluded_name][0] != 0) || count($_GET[$excluded_name]) > 1 ) { 'compare' => '!='
]]);
$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 ) {
@ -283,3 +201,4 @@ class MediaManager {
} }
new MediaManager(); new MediaManager();

View File

@ -1,7 +1,6 @@
<?php <?php
namespace TheaterGF\Core;
use \WP_Error; namespace TheaterGF\Core;
class Tag { class Tag {
@ -107,10 +106,6 @@ 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);
} }
@ -169,16 +164,14 @@ class TagTree {
public function get( $term ) { public function get( $term ) {
return $this->get_by_id($term->term_id); foreach ( $this->root_tags as $rt ) {
/*foreach ( $this->root_tags as $rt ) {
$found = array_find($rt->get_all_children(), function ($child) use ($term) { return $child->id == $term->term_id; }); $found = array_find($rt->get_all_children(), function ($child) use ($term) { return $child->id == $term->term_id; });
if ( $found ) { if ( $found ) {
return $found; return $found;
} }
} }
return null;*/ return null;
} }
public function get_from_post( $post ) { public function get_from_post( $post ) {
@ -186,22 +179,6 @@ class TagTree {
return $this->get_multiple($terms ?: []); return $this->get_multiple($terms ?: []);
} }
public function get_by_id( $term_id ) {
foreach ( $this->root_tags as $rt ) {
if ( $rt->id == $term_id ) {
return $rt;
}
$found = array_find($rt->get_all_children(), fn ($child) => $child->id == $term_id );
if ( $found ) {
return $found;
}
}
return null;
}
public function get_by_name( $name, $return_path = false ) { public function get_by_name( $name, $return_path = false ) {
$segments = array_filter(explode('/', trim($name, '/'))); $segments = array_filter(explode('/', trim($name, '/')));
@ -339,4 +316,6 @@ class TagTree {
} }
} }
} }