From 644b0ccce8b2bcce2f098ec901725fac3ce7491c Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 16 Apr 2026 12:57:29 +0200 Subject: [PATCH] added removal of tags from posts --- src/media.css | 29 +++++++++++++++++++++++++ src/media.js | 8 +++++++ src/media.php | 60 +++++++++++++++++++++++++++++++-------------------- src/tag.php | 27 +++++++++++++++++++---- 4 files changed, 97 insertions(+), 27 deletions(-) create mode 100644 src/media.css create mode 100644 src/media.js diff --git a/src/media.css b/src/media.css new file mode 100644 index 0000000..3a8821f --- /dev/null +++ b/src/media.css @@ -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; + } +} diff --git a/src/media.js b/src/media.js new file mode 100644 index 0000000..d71299a --- /dev/null +++ b/src/media.js @@ -0,0 +1,8 @@ + +function theatergf_remove_tag_cb(event) { + + target = event.target + + form = document.querySelector('#post') + +} diff --git a/src/media.php b/src/media.php index c574021..8af9a45 100644 --- a/src/media.php +++ b/src/media.php @@ -5,10 +5,12 @@ namespace TheaterGF\Core; require_once __DIR__ . "/tag.php"; class MediaManager { + readonly public string $taxonomy; public $tags; public function __construct() { + $this->taxonomy = 'ttgf_media_tags'; add_action('init', [ $this, 'register_structure' ]); @@ -29,10 +31,12 @@ class MediaManager { add_action('restrict_manage_posts', [ $this, 'show_list_filter' ]); add_filter('pre_get_posts', [ $this, 'apply_list_filter' ]); + + add_action('admin_enqueue_scripts', [ $this, 'enqueue_admin_style' ]); } public function register_structure() { - $this->tags = TagTree::create('ttgf_media_tags'); + $this->tags = TagTree::create($this->taxonomy); } public function create_tag_management_box() { @@ -50,26 +54,10 @@ class MediaManager { ?>

Assigned

-
+
-
full_name() ?>
+
full_name() ?>

- - name ?> - term_id ?>
-
- tags->get_multiple(get_the_terms( $post, 'ttgf_media_tags') ?: []) as $term): ?> - full_name() ?> - id ?>
- -
- -

Available

-
- tags->get_all() as $tag ): ?> - full_name() ?>: id ?>
-
- 'ttgf_media_tags', 'hide_empty' => false ]) as $term): ?> - name ?> - term_id ?>
-

Add new

@@ -85,6 +73,14 @@ class MediaManager { tags->get_by_name($new_tag); if ( ! $tag ) { $tag = $this->tags->create_tag($new_tag); } + + if ( is_wp_error($tag) ) { + return; + } - wp_set_post_terms($post, [ $tag->id ], 'ttgf_media_tags', true); + 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 ) { - $terms = get_terms([ 'taxonomy' => 'ttgf_media_tags', 'hide_empty' => false ]); + /*$terms = get_terms([ 'taxonomy' => 'ttgf_media_tags', 'hide_empty' => false ]); $fields['ttgf_media_tags'] = [ 'label' => 'Tags', @@ -123,7 +137,7 @@ class MediaManager { 'label' => 'Folder Attributes', 'input' => 'html', 'html' => "" - ]; + ];*/ return $fields; } diff --git a/src/tag.php b/src/tag.php index 3e2ddec..0643deb 100644 --- a/src/tag.php +++ b/src/tag.php @@ -1,7 +1,8 @@ children = array_splice($this->children, $index, 1); } } - + } class TagTree { @@ -164,14 +165,16 @@ class TagTree { 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; }); if ( $found ) { return $found; } } - return null; + return null;*/ } public function get_from_post( $post ) { @@ -179,6 +182,22 @@ class TagTree { 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 ) { $segments = array_filter(explode('/', trim($name, '/')));