added removal of tags from posts

This commit is contained in:
Patrick 2026-04-16 12:57:29 +02:00
parent a748afe1c4
commit 644b0ccce8
4 changed files with 97 additions and 27 deletions

29
src/media.css Normal file
View File

@ -0,0 +1,29 @@
.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;
}
}

8
src/media.js Normal file
View File

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

View File

@ -5,10 +5,12 @@ 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' ]);
@ -29,10 +31,12 @@ 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('ttgf_media_tags'); $this->tags = TagTree::create($this->taxonomy);
} }
public function create_tag_management_box() { public function create_tag_management_box() {
@ -50,26 +54,10 @@ class MediaManager {
?> ?>
<h3>Assigned</h3> <h3>Assigned</h3>
<div class="ttgf_tags_container"> <div class="theatergf tag-container">
<?php foreach ( $tags as $tag ): ?> <?php foreach ( $tags as $tag ): ?>
<div class="ttgf_tag"><?= $tag->full_name() ?></div> <div class="theatergf tag"><?= $tag->full_name() ?><button class="remove" name="ttgf_remove_tag" value="<?= $tag->id ?>">X</button></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>
@ -85,6 +73,14 @@ 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 );
@ -95,7 +91,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) ) { if ( isset($new_tag) && $new_tag !== '' ) {
$tag = $this->tags->get_by_name($new_tag); $tag = $this->tags->get_by_name($new_tag);
@ -103,13 +99,31 @@ class MediaManager {
$tag = $this->tags->create_tag($new_tag); $tag = $this->tags->create_tag($new_tag);
} }
wp_set_post_terms($post, [ $tag->id ], 'ttgf_media_tags', true); if ( is_wp_error($tag) ) {
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',
@ -123,7 +137,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;
} }

View File

@ -1,7 +1,8 @@
<?php <?php
namespace TheaterGF\Core; namespace TheaterGF\Core;
use \WP_Error;
class Tag { class Tag {
public $term; public $term;
@ -164,14 +165,16 @@ class TagTree {
public function get( $term ) { public function get( $term ) {
foreach ( $this->root_tags as $rt ) { return $this->get_by_id($term->term_id);
/*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 ) {
@ -179,6 +182,22 @@ 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, '/')));