Compare commits
2 Commits
3c151eb62b
...
a748afe1c4
| Author | SHA1 | Date |
|---|---|---|
|
|
a748afe1c4 | |
|
|
6ee5474955 |
115
src/media.php
115
src/media.php
|
|
@ -2,43 +2,19 @@
|
||||||
|
|
||||||
namespace TheaterGF\Core;
|
namespace TheaterGF\Core;
|
||||||
|
|
||||||
require_once __DIR__ . "/media-meta-box-html.php";
|
require_once __DIR__ . "/tag.php";
|
||||||
|
|
||||||
function build_tag_name( $term ) {
|
|
||||||
|
|
||||||
if (is_array( $term )) {
|
|
||||||
$out = [];
|
|
||||||
|
|
||||||
foreach ( $term as $t ) {
|
|
||||||
$out[] = build_tag_name($t);
|
|
||||||
}
|
|
||||||
sort($out);
|
|
||||||
|
|
||||||
return $out;
|
|
||||||
}
|
|
||||||
|
|
||||||
$parts = [];
|
|
||||||
|
|
||||||
while ( $term && $term->parent != 0 ) {
|
|
||||||
$parts[] = $term->name;
|
|
||||||
$term = get_term($term->parent, 'ttgf_media_tags');
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( $term ) {
|
|
||||||
$parts[] = $term->name;
|
|
||||||
}
|
|
||||||
|
|
||||||
return implode('/', array_reverse($parts));
|
|
||||||
}
|
|
||||||
|
|
||||||
class MediaManager {
|
class MediaManager {
|
||||||
|
|
||||||
|
public $tags;
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
|
|
||||||
add_action('init', [ $this, 'register_structure' ]);
|
add_action('init', [ $this, 'register_structure' ]);
|
||||||
|
|
||||||
add_filter('attachment_fields_to_edit', [ $this, 'create_edit_media_control' ], 10, 2);
|
add_filter('attachment_fields_to_edit', [ $this, 'create_edit_media_control' ], 10, 2);
|
||||||
add_action('add_meta_boxes', [ $this, 'create_tag_management_box' ]);
|
add_action('add_meta_boxes', [ $this, 'create_tag_management_box' ]);
|
||||||
|
add_action('edit_attachment', [ $this, 'save_tags' ]);
|
||||||
|
|
||||||
// Dont know what it does
|
// Dont know what it does
|
||||||
add_filter( 'update_post_term_count_statuses', function( $statuses, $taxonomy ) {
|
add_filter( 'update_post_term_count_statuses', function( $statuses, $taxonomy ) {
|
||||||
|
|
@ -56,26 +32,7 @@ class MediaManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function register_structure() {
|
public function register_structure() {
|
||||||
register_taxonomy('ttgf_media_tags', 'attachment', [
|
$this->tags = TagTree::create('ttgf_media_tags');
|
||||||
'labels' => [
|
|
||||||
'name' => 'Tags',
|
|
||||||
'singular_name' => 'Tag',
|
|
||||||
'search_items' => 'Search tags',
|
|
||||||
'all_items' => 'All tags',
|
|
||||||
'parent_item' => 'Parent tag',
|
|
||||||
'edit_item' => 'Edit tags',
|
|
||||||
'update_item' => 'Update tags',
|
|
||||||
'add_new_item' => 'Add new tag',
|
|
||||||
'new_item_name' => 'New tag name',
|
|
||||||
'menu_name' => 'Tags',
|
|
||||||
],
|
|
||||||
'hierarchical' => true,
|
|
||||||
'public' => false,
|
|
||||||
'show_ui' => false,
|
|
||||||
'show_admin_column' => true,
|
|
||||||
'rewrite' => false,
|
|
||||||
'update_count_callback' => '_update_generic_term_count'
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create_tag_management_box() {
|
public function create_tag_management_box() {
|
||||||
|
|
@ -89,25 +46,67 @@ class MediaManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tag_management_meta_box_html( $post ) {
|
public function tag_management_meta_box_html( $post ) {
|
||||||
$all_terms = get_terms([
|
$tags = $this->tags->get_from_post($post);
|
||||||
'taxonomy' => 'ttgf_media_tags',
|
|
||||||
'hide_empty' => false,
|
|
||||||
]);
|
|
||||||
$all_tags = build_tag_name($all_terms);
|
|
||||||
|
|
||||||
$terms = get_the_terms($post, 'ttgf_media_tags');
|
|
||||||
$tags = build_tag_name($terms);
|
|
||||||
?>
|
?>
|
||||||
<h3>Assigned</h3>
|
<h3>Assigned</h3>
|
||||||
|
|
||||||
<div class="ttgf_tags_container">
|
<div class="ttgf_tags_container">
|
||||||
<?php foreach ( $tags as $tag ): ?>
|
<?php foreach ( $tags as $tag ): ?>
|
||||||
<div class="ttgf_tag"><?= $tag ?></div>
|
<div class="ttgf_tag"><?= $tag->full_name() ?></div>
|
||||||
<?php endforeach; ?>
|
<?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>
|
||||||
|
|
||||||
|
<h3>Add new</h3>
|
||||||
|
<div class="ttgf_tag_add">
|
||||||
|
<?php wp_nonce_field(basename(__FILE__), 'ttgf_core_admin_nonce'); ?>
|
||||||
|
<input type="text" list="ttgf_available_tags" name="ttgf_new_tag">
|
||||||
|
<datalist id="ttgf_available_tags">
|
||||||
|
<?php foreach ( $this->tags->get_all() as $tag ): ?>
|
||||||
|
<option value="<?= $tag->full_name() ?>">
|
||||||
|
<?php endforeach ?>
|
||||||
|
</datalist>
|
||||||
</div>
|
</div>
|
||||||
<?php
|
<?php
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function save_tags( $post ) {
|
||||||
|
$is_autosave = wp_is_post_autosave( $post );
|
||||||
|
$is_revision = wp_is_post_revision( $post );
|
||||||
|
$is_valid_nonce = isset($_POST['ttgf_core_admin_nonce']) && wp_verify_nonce($_POST['ttgf_core_admin_nonce'], basename(__FILE__));
|
||||||
|
|
||||||
|
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$new_tag = sanitize_text_field($_POST['ttgf_new_tag']);
|
||||||
|
if ( isset($new_tag) ) {
|
||||||
|
|
||||||
|
$tag = $this->tags->get_by_name($new_tag);
|
||||||
|
|
||||||
|
if ( ! $tag ) {
|
||||||
|
$tag = $this->tags->create_tag($new_tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_set_post_terms($post, [ $tag->id ], 'ttgf_media_tags', true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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 ]);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,321 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace TheaterGF\Core;
|
||||||
|
|
||||||
|
class Tag {
|
||||||
|
|
||||||
|
public $term;
|
||||||
|
|
||||||
|
public $id;
|
||||||
|
public $name;
|
||||||
|
|
||||||
|
public $parent;
|
||||||
|
public $children;
|
||||||
|
|
||||||
|
public function __construct( $term ) {
|
||||||
|
$this->term = $term;
|
||||||
|
$this->id = $term->term_id;
|
||||||
|
$this->name = $term->name;
|
||||||
|
$this->parent = null;
|
||||||
|
$this->children = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function full_name() {
|
||||||
|
return implode('/', array_map(function ($segment) { return $segment->term->name; }, $this->path()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function path() {
|
||||||
|
$path = [ $this ];
|
||||||
|
|
||||||
|
$parent = $this->parent;
|
||||||
|
while ( $parent ) {
|
||||||
|
$path[] = $parent;
|
||||||
|
$parent = $parent->parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_reverse($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_all_children() {
|
||||||
|
$child_list = $this->children;
|
||||||
|
|
||||||
|
foreach ( $this->children as $child ) {
|
||||||
|
$child_list = array_merge($child_list, $child->get_all_children());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $child_list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function move( $new_term, $new_parent ) {
|
||||||
|
$this->term = $new_term;
|
||||||
|
$this->id = $new_term->term_id;
|
||||||
|
|
||||||
|
if ( $this->parent == $new_parent ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->parent->remove_child($this);
|
||||||
|
$this->parent->add_child($this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update_term( $new_term ) {
|
||||||
|
$this->term = $new_term;
|
||||||
|
$this->id = $new_term->term_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function add_child( $child ) {
|
||||||
|
$this->children[] = $child;
|
||||||
|
$child->parent = $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function remove() {
|
||||||
|
$this->parent->remove_child($this);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function remove_child( $child ) {
|
||||||
|
$index = array_find($this->children, function ($c) use ($child) { return $c == $child; });
|
||||||
|
if ( $index ) {
|
||||||
|
$this->children = array_splice($this->children, $index, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class TagTree {
|
||||||
|
|
||||||
|
static public function create( $taxonomy_id ) {
|
||||||
|
$taxonomy = register_taxonomy($taxonomy_id, 'attachment', [
|
||||||
|
'labels' => [
|
||||||
|
'name' => 'Tags',
|
||||||
|
'singular_name' => 'Tag',
|
||||||
|
'search_items' => 'Search tags',
|
||||||
|
'all_items' => 'All tags',
|
||||||
|
'parent_item' => 'Parent tag',
|
||||||
|
'edit_item' => 'Edit tags',
|
||||||
|
'update_item' => 'Update tags',
|
||||||
|
'add_new_item' => 'Add new tag',
|
||||||
|
'new_item_name' => 'New tag name',
|
||||||
|
'menu_name' => 'Tags',
|
||||||
|
],
|
||||||
|
'hierarchical' => true,
|
||||||
|
'public' => false,
|
||||||
|
'show_ui' => false,
|
||||||
|
'show_admin_column' => true,
|
||||||
|
'rewrite' => false,
|
||||||
|
'update_count_callback' => '_update_generic_term_count'
|
||||||
|
]);
|
||||||
|
|
||||||
|
return new TagTree($taxonomy_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public $taxonomy;
|
||||||
|
public $root_tags;
|
||||||
|
|
||||||
|
public function __construct ( $taxonomy ) {
|
||||||
|
$this->taxonomy = $taxonomy;
|
||||||
|
$this->root_tags = [];
|
||||||
|
|
||||||
|
$this->rebuild();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_all() {
|
||||||
|
$all = $this->root_tags;
|
||||||
|
|
||||||
|
foreach ( $this->root_tags as $tag ) {
|
||||||
|
$all = array_merge($all, $tag->get_all_children());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $all;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_multiple( $terms ) {
|
||||||
|
|
||||||
|
$num_terms = count($terms);
|
||||||
|
if ( $num_terms == 0 ) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$term_ids = array_map(function ($term) { return $term->term_id; }, $terms);
|
||||||
|
$term_ids_set = array_flip($term_ids);
|
||||||
|
|
||||||
|
$tags = [];
|
||||||
|
|
||||||
|
foreach ( $this->root_tags as $rt ) {
|
||||||
|
if ( isset($term_ids_set[$rt->id]) ) {
|
||||||
|
$tags[] = $rt;
|
||||||
|
}
|
||||||
|
if ( count($tags) == $num_terms ) {
|
||||||
|
return $tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ( $rt->get_all_children() as $child ) {
|
||||||
|
if ( isset($term_ids_set[$child->id]) ) {
|
||||||
|
$tags[] = $child;
|
||||||
|
}
|
||||||
|
if ( count($tags) == $num_terms ) {
|
||||||
|
return $tags;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get( $term ) {
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_from_post( $post ) {
|
||||||
|
$terms = get_the_terms($post, $this->taxonomy);
|
||||||
|
return $this->get_multiple($terms ?: []);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_by_name( $name, $return_path = false ) {
|
||||||
|
$segments = array_filter(explode('/', trim($name, '/')));
|
||||||
|
|
||||||
|
$node = null;
|
||||||
|
$path = [];
|
||||||
|
|
||||||
|
foreach ( $segments as $segment ) {
|
||||||
|
$current_nodes = $node?->children ?: $this->root_tags;
|
||||||
|
|
||||||
|
$node = array_find($current_nodes, fn ($tag) => $tag->name === $segment);
|
||||||
|
|
||||||
|
if ( ! $node ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$path[] = $node;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( $return_path ) {
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( count($path) === count($segments) ) {
|
||||||
|
return $node;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create_tag( $tag_name, $description = '') {
|
||||||
|
$segments = array_filter(explode('/', trim($tag_name, '/')));
|
||||||
|
|
||||||
|
if ( empty($segments) ) {
|
||||||
|
return new WP_Error('invalid_tag', 'Tag name is empty');
|
||||||
|
}
|
||||||
|
|
||||||
|
$path = $this->get_by_name($tag_name, true);
|
||||||
|
|
||||||
|
if ( count($segments) === count($path) ) {
|
||||||
|
return end($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
$parent_tag = ( ! empty($path) ) ? end($path) : null;
|
||||||
|
|
||||||
|
for ( $i = count($path); $i < count($segments); ++$i ) {
|
||||||
|
$segment = $segments[$i];
|
||||||
|
|
||||||
|
$args = [];
|
||||||
|
|
||||||
|
if ( $parent_tag ) {
|
||||||
|
$args['parent'] = $parent_tag->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( $description && $i === count($segments) - 1 ) {
|
||||||
|
$args['description'] = $description;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = wp_insert_term($segment, $this->taxonomy, $args);
|
||||||
|
|
||||||
|
if ( is_wp_error($result) ) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
$term = get_term($result['term_id'], $this->taxonomy);
|
||||||
|
|
||||||
|
if ( is_wp_error($term) || !$term ) {
|
||||||
|
return new WP_Error('term_fetch_failed', 'Failed to retrieve created term');
|
||||||
|
}
|
||||||
|
|
||||||
|
$tag = new Tag($term);
|
||||||
|
|
||||||
|
if ( $parent_tag ) {
|
||||||
|
$parent_tag->add_child($tag);
|
||||||
|
} else {
|
||||||
|
$this->root_tags[] = $tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
$parent_tag = $tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $parent_tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function move_tag( $tag, $new_parent ) {
|
||||||
|
|
||||||
|
$result = wp_update_term($tag->id, $this->taxonomy, [ 'parent' => $new_parent->id ]);
|
||||||
|
|
||||||
|
if ( is_wp_error($result) ) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
$new_term = get_term($result->term_id, $this->taxonomy);
|
||||||
|
|
||||||
|
$tag->update_term( $new_term, $new_parent );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update_tag( $tag, $new_description ) {
|
||||||
|
|
||||||
|
$result = wp_update_term($tag->id, $this->taxonomy, [ 'parent' => $tag->parent->id, 'description' => $new_description ]);
|
||||||
|
|
||||||
|
if ( is_wp_error($result) ) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
$new_term = get_term($result->term_id, $this->taxonomy);
|
||||||
|
|
||||||
|
$tag->update_term($new_term);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete_tag( $tag ) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rebuild() {
|
||||||
|
$root_terms = get_terms([ 'taxonomy' => $this->taxonomy, 'hide_empty' => false, 'parent' => 0 ]);
|
||||||
|
|
||||||
|
foreach ( $root_terms as $term ) {
|
||||||
|
$this->root_tags[] = new Tag($term);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ( $this->root_tags as $tag ) {
|
||||||
|
$this->rebuild_children($tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function rebuild_children( $child ) {
|
||||||
|
$child_terms = get_terms([ 'taxonomy' => $this->taxonomy, 'hide_empty' => false, 'parent' => $child->term->term_id ]);
|
||||||
|
|
||||||
|
foreach ( $child_terms as $child_term ) {
|
||||||
|
$child->add_child(new Tag($child_term));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ( $child->children as $grandchild ) {
|
||||||
|
$this->rebuild_children($grandchild);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue