created plugin and started tag implementation

This commit is contained in:
Patrick 2026-04-01 01:57:39 +02:00
commit 3c151eb62b
3 changed files with 261 additions and 0 deletions

29
src/admin/admin.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace TheaterGF\Core;
function add_admin_menu() {
add_menu_page(
'Theatergruppe',
'Theatergruppe',
'manage_options',
'theatergf',
'TheaterGF\Core\main_settings_page_callback',
);
add_submenu_page(
'theatergf',
'General Settings',
'General',
'manage_options',
'theatergf',
'TheaterGF\Core\main_settings_page_callback'
);
}
function main_settings_page_callback() {
echo '<div class="wrap"><h1>Theatergruppe Gänserndorf</h1></div>';
}
add_action('admin_menu', 'TheaterGF\Core\add_admin_menu');

205
src/media.php Normal file
View File

@ -0,0 +1,205 @@
<?php
namespace TheaterGF\Core;
require_once __DIR__ . "/media-meta-box-html.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 {
public function __construct() {
add_action('init', [ $this, 'register_structure' ]);
add_filter('attachment_fields_to_edit', [ $this, 'create_edit_media_control' ], 10, 2);
add_action('add_meta_boxes', [ $this, 'create_tag_management_box' ]);
// Dont know what it does
add_filter( 'update_post_term_count_statuses', function( $statuses, $taxonomy ) {
if( 'folders' === $taxonomy->name ) {
$statuses[] = 'inherit';
$statuses = array_unique( $statuses );
}
return $statuses;
}, 25, 2 );
add_action('restrict_manage_posts', [ $this, 'show_list_filter' ]);
add_filter('pre_get_posts', [ $this, 'apply_list_filter' ]);
}
public function register_structure() {
register_taxonomy('ttgf_media_tags', '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'
]);
}
public function create_tag_management_box() {
add_meta_box(
'ttgf_media_tags_box',
'Tags',
[ $this, 'tag_management_meta_box_html' ],
'attachment',
'side'
);
}
public function tag_management_meta_box_html( $post ) {
$all_terms = get_terms([
'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>
<div class="ttgf_tags_container">
<?php foreach ( $tags as $tag ): ?>
<div class="ttgf_tag"><?= $tag ?></div>
<?php endforeach; ?>
</div>
<?php
}
public function create_edit_media_control( $fields, $post ) {
$terms = get_terms([ 'taxonomy' => 'ttgf_media_tags', 'hide_empty' => false ]);
$fields['ttgf_media_tags'] = [
'label' => 'Tags',
'input' => 'html',
'html' => $this->_build_dropdown("attachments[{$post->ID}][ttgf_media_tags]",
array_map(function ($term) { return [ 'value' => $term->id, 'name' => $term->name ]; }, $terms),
$post->ID)
];
$fields['ttgf_media_hidden'] = [
'label' => 'Folder Attributes',
'input' => 'html',
'html' => "<select name='attachments[{$post->ID}][ttgf_media_hidden]'><option></option><option value='hidden'>Hidden</option></select>"
];
return $fields;
}
public function show_list_filter() {
global $typenow;
if ( $typenow !== 'attachment' ) {
return;
}
$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' => 'name',
'depth' => 3,
'hide_empty' => true,
]);
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>";
}
echo '</select>';*/
}
public function apply_list_filter( $query ) {
if ( ! is_admin() || $query->is_main_query() ) {
return;
}
if (!empty($_GET['media_folder_filter'])) {
$query->set('tax_query', [[
'taxonomy' => 'ttgf_media_folder',
'field' => 'name',
'terms' => $_GET['ttgf_media_folder'],
]]);
}
$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 ) {
$html_options = $empty ? '<option></option>' : '';
foreach ( $options as $option ) {
$html_options .= "<option value='{$option['value']}'" . $selected == $option['value'] ? 'selected' : '' . ">{$option['name']}</option>";
}
return "<select name='$name'>$html_options</select>";
}
}
new MediaManager();

27
theatergf-core.php Normal file
View File

@ -0,0 +1,27 @@
<?php
/*
* Plugin Name: TheaterGF Core
* Description: Gemeinsame Funktionalität für alle TheaterGF Plugins
* Author: Patrick Maschek
*/
namespace TheaterGF\Core;
// protect against direct access
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function plugin_activation_function() {}
function plugin_deactivation_function() {}
register_activation_hook(__FILE__, 'TheaterGF\Core\plugin_activation_function');
register_deactivation_hook(__FILE__, 'TheaterGF\Core\plugin_deactivation_function');
if ( is_admin() ) {
require_once __DIR__ . '/src/admin/admin.php';
}
require_once __DIR__ . '/src/media.php';