Compare commits
No commits in common. "190a69a57d1c4e2e4e88d1ec1eb8214f189877c8" and "a748afe1c41ad767c95437db2c3adaac699e2801" have entirely different histories.
190a69a57d
...
a748afe1c4
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
|
||||
function theatergf_remove_tag_cb(event) {
|
||||
|
||||
target = event.target
|
||||
|
||||
form = document.querySelector('#post')
|
||||
|
||||
}
|
||||
203
src/media.php
203
src/media.php
|
|
@ -5,12 +5,10 @@ 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' ]);
|
||||
|
||||
|
|
@ -31,12 +29,10 @@ 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($this->taxonomy);
|
||||
$this->tags = TagTree::create('ttgf_media_tags');
|
||||
}
|
||||
|
||||
public function create_tag_management_box() {
|
||||
|
|
@ -54,10 +50,26 @@ class MediaManager {
|
|||
?>
|
||||
<h3>Assigned</h3>
|
||||
|
||||
<div class="theatergf tag-container">
|
||||
<div class="ttgf_tags_container">
|
||||
<?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 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>
|
||||
|
|
@ -73,14 +85,6 @@ class MediaManager {
|
|||
<?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 ) {
|
||||
$is_autosave = wp_is_post_autosave( $post );
|
||||
$is_revision = wp_is_post_revision( $post );
|
||||
|
|
@ -91,7 +95,7 @@ class MediaManager {
|
|||
}
|
||||
|
||||
$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);
|
||||
|
||||
|
|
@ -99,31 +103,13 @@ class MediaManager {
|
|||
$tag = $this->tags->create_tag($new_tag);
|
||||
}
|
||||
|
||||
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;
|
||||
wp_set_post_terms($post, [ $tag->id ], 'ttgf_media_tags', true);
|
||||
}
|
||||
}
|
||||
|
||||
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',
|
||||
|
|
@ -137,7 +123,7 @@ class MediaManager {
|
|||
'label' => 'Folder Attributes',
|
||||
'input' => 'html',
|
||||
'html' => "<select name='attachments[{$post->ID}][ttgf_media_hidden]'><option></option><option value='hidden'>Hidden</option></select>"
|
||||
];*/
|
||||
];
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
|
@ -149,126 +135,58 @@ class MediaManager {
|
|||
return;
|
||||
}
|
||||
|
||||
$filter_name = "filter_" . $this->taxonomy;
|
||||
$excluded_name = "excluded_" . $this->taxonomy;
|
||||
|
||||
$filter_dropdown = wp_dropdown_categories([
|
||||
'show_option_all' => 'All Tags',
|
||||
'taxonomy' => $this->taxonomy,
|
||||
'name' => $filter_name . "[]",
|
||||
$selected = isset( $_GET[ 'ttgf_media_folder' ] ) ? $_GET[ 'ttgf_media_folder' ] : false;
|
||||
wp_dropdown_categories([
|
||||
'show_option_all' => 'All folders',
|
||||
'taxonomy' => 'ttgf_media_folder',
|
||||
'name' => 'ttgf_media_folder',
|
||||
'orderby' => 'name',
|
||||
'selected' => $selected,
|
||||
'hierarchical' => true,
|
||||
'value_field' => 'term_id',
|
||||
'value_field' => 'name',
|
||||
'depth' => 3,
|
||||
'hide_empty' => false,
|
||||
'echo' => false
|
||||
'hide_empty' => true,
|
||||
]);
|
||||
|
||||
$selected = isset( $_GET[ $filter_name ] ) ? $_GET[ $filter_name ] : [];
|
||||
$filter_dropdown = str_replace('<select ', '<select multiple ', $filter_dropdown);
|
||||
$filter_dropdown = str_replace(" selected='selected'>", ">", $filter_dropdown);
|
||||
foreach ( $selected as $sid ) {
|
||||
$filter_dropdown = str_replace("value=\"{$sid}\">", "value=\"{$sid}\" selected>", $filter_dropdown);
|
||||
echo '<select name="ttgf_media_folder_filter">';
|
||||
echo '<option>Select Filter</option>';
|
||||
echo '<option value="show_hidden">Display hidden</option>';
|
||||
echo '</select>';
|
||||
|
||||
|
||||
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([
|
||||
'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;
|
||||
echo '</select>';*/
|
||||
}
|
||||
|
||||
public function apply_list_filter( $query ) {
|
||||
if ( ! is_admin() || ! $query->is_main_query() ) {
|
||||
return;
|
||||
}
|
||||
if ($query->get('post_type') !== 'attachment') {
|
||||
if ( ! is_admin() || $query->is_main_query() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$filter_name = "filter_" . $this->taxonomy;
|
||||
$excluded_name = "excluded_" . $this->taxonomy;
|
||||
|
||||
$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;
|
||||
if (!empty($_GET['media_folder_filter'])) {
|
||||
$query->set('tax_query', [[
|
||||
'taxonomy' => 'ttgf_media_folder',
|
||||
'field' => 'name',
|
||||
'terms' => $_GET['ttgf_media_folder'],
|
||||
]]);
|
||||
}
|
||||
|
||||
$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) ) {
|
||||
$_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);
|
||||
|
||||
$show_hidden = isset($_GET['ttgf_media_folder_filter']) && $_GET['ttgf_media_folder_filter'] != "show_hidden";
|
||||
$query->set('meta_query', [[
|
||||
'key' => 'ttgf_media_hidden',
|
||||
'value' => ( ! $show_hidden),
|
||||
'compare' => '!='
|
||||
]]);
|
||||
}
|
||||
|
||||
private function _build_dropdown( $name, $options, $selected, $empty = true ) {
|
||||
|
|
@ -283,3 +201,4 @@ class MediaManager {
|
|||
}
|
||||
|
||||
new MediaManager();
|
||||
|
||||
|
|
|
|||
31
src/tag.php
31
src/tag.php
|
|
@ -1,7 +1,6 @@
|
|||
<?php
|
||||
namespace TheaterGF\Core;
|
||||
|
||||
use \WP_Error;
|
||||
namespace TheaterGF\Core;
|
||||
|
||||
class Tag {
|
||||
|
||||
|
|
@ -107,10 +106,6 @@ 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);
|
||||
}
|
||||
|
||||
|
|
@ -169,16 +164,14 @@ class TagTree {
|
|||
|
||||
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; });
|
||||
if ( $found ) {
|
||||
return $found;
|
||||
}
|
||||
}
|
||||
|
||||
return null;*/
|
||||
return null;
|
||||
}
|
||||
|
||||
public function get_from_post( $post ) {
|
||||
|
|
@ -186,22 +179,6 @@ 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, '/')));
|
||||
|
||||
|
|
@ -339,4 +316,6 @@ class TagTree {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue