Dateien nach "/" hochladen

This commit is contained in:
M_Viper 2025-04-20 11:05:32 +00:00
parent 41fecda632
commit 7f09b4896d

359
wp-multi-team-card.php Normal file
View File

@ -0,0 +1,359 @@
<?php
/**
* Plugin Name: WP Multi Team-Card
* Plugin URI: https://git.viper.ipv64.net/M_Viper/wp-multi
* Description: Erstellt Teamkarten mit Name, Funktion, Zuständigkeit, Bild und Kategorie. Ausgabe per Shortcode [teamcards].
* Version: 1.0
* Author: M_Viper
* Author URI: https://m-viper.de
* Requires at least: 6.7.2
* Tested up to: 6.7.2
* PHP Version: 7.2
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-multi-team-card
* Tags: team, card, shortcodes, team-management, customizable, responsive
* Support: [Microsoft Teams Support](https://teams.live.com/l/community/FEAzokphpZTJ2u6OgI)
* Support: [Telegram Support](https://t.me/M_Viper04)
*/
if (!defined('ABSPATH')) exit;
// Check if the PHP version is sufficient
if (version_compare(PHP_VERSION, '7.2', '<')) {
die('This plugin requires PHP 7.2 or higher.');
}
// CPT registrieren
function teamcard_register_post_type() {
register_post_type('teamcard', [
'labels' => [
'name' => 'Teammitglieder',
'singular_name' => 'Teammitglied',
'add_new' => 'Neues Teammitglied',
'add_new_item' => 'Teammitglied hinzufügen',
'edit_item' => 'Teammitglied bearbeiten',
],
'public' => true,
'show_ui' => false, // Standard-UI ausblenden
'show_in_menu' => false, // Nicht im Hauptmenü anzeigen
'menu_icon' => 'dashicons-groups',
'supports' => ['title'],
'has_archive' => false,
'show_in_admin_bar' => false,
]);
register_taxonomy('teamcard_kategorie', 'teamcard', [
'labels' => [
'name' => 'Kategorien',
'singular_name' => 'Kategorie',
],
'hierarchical' => true,
'public' => true,
'show_admin_column' => true,
]);
}
add_action('init', 'teamcard_register_post_type');
// Admin-Menü hinzufügen
function teamcard_add_admin_menu() {
add_menu_page(
'Teammitglieder verwalten',
'Teamkarten',
'manage_options',
'teamcard_management',
'teamcard_admin_page',
'dashicons-groups',
30
);
}
add_action('admin_menu', 'teamcard_add_admin_menu');
// Die Hauptverwaltungsseite
function teamcard_admin_page() {
?>
<div class="wrap teamcard-admin">
<h1>Teammitglieder verwalten</h1>
<!-- Formular zum Hinzufügen neuer Teammitglieder -->
<div class="teamcard-add-new-form">
<h2>Neues Teammitglied hinzufügen</h2>
<div class="form-fields">
<div class="form-field">
<label for="new-teamcard-name">Name:</label>
<input type="text" id="new-teamcard-name" placeholder="Name eingeben">
</div>
<div class="form-field">
<label for="new-teamcard-funktion">Funktion:</label>
<input type="text" id="new-teamcard-funktion" placeholder="Funktion eingeben">
</div>
<div class="form-field">
<label for="new-teamcard-zustaendigkeit">Zuständigkeit:</label>
<input type="text" id="new-teamcard-zustaendigkeit" placeholder="Zuständigkeit eingeben">
</div>
<div class="form-field">
<label>Bild:</label>
<div class="image-preview-container">
<img id="new-teamcard-bild-vorschau" src="" style="display:none; max-width:100px;">
</div>
<input type="hidden" id="new-teamcard-bild-id" value="">
<button type="button" class="button" id="new-teamcard-bild-button">Bild auswählen</button>
</div>
<div class="form-field">
<button type="button" id="add-teamcard-button" class="button button-primary">Teammitglied hinzufügen</button>
</div>
</div>
</div>
<h2>Vorhandene Teammitglieder</h2>
<p>Ziehe die Zeilen, um die Reihenfolge zu ändern. Klicke auf die Felder, um sie zu bearbeiten.</p>
<!-- Liste der vorhandenen Teammitglieder -->
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th width="10%">Bild</th>
<th width="20%">Name</th>
<th width="20%">Funktion</th>
<th width="30%">Zuständigkeit</th>
<th width="20%">Aktionen</th>
</tr>
</thead>
<tbody id="teamcard-list">
<?php
$teamcards = get_posts([
'post_type' => 'teamcard',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
]);
foreach ($teamcards as $teamcard) {
$funktion = get_post_meta($teamcard->ID, '_teamcard_funktion', true);
$zustaendigkeit = get_post_meta($teamcard->ID, '_teamcard_zustaendigkeit', true);
$bild_id = get_post_meta($teamcard->ID, '_teamcard_bild_id', true);
$bild_url = $bild_id ? wp_get_attachment_image_url($bild_id, 'thumbnail') : '';
?>
<tr data-id="<?php echo $teamcard->ID; ?>" class="teamcard-item">
<td class="teamcard-bild">
<div class="image-preview-container">
<?php if ($bild_url): ?>
<img src="<?php echo esc_url($bild_url); ?>" class="teamcard-bild-vorschau">
<?php endif; ?>
</div>
<button type="button" class="button teamcard-bild-button" data-id="<?php echo $teamcard->ID; ?>">Bild ändern</button>
</td>
<td>
<div class="editable" data-field="title" data-id="<?php echo $teamcard->ID; ?>"><?php echo esc_html($teamcard->post_title); ?></div>
</td>
<td>
<div class="editable" data-field="funktion" data-id="<?php echo $teamcard->ID; ?>"><?php echo esc_html($funktion); ?></div>
</td>
<td>
<div class="editable" data-field="zustaendigkeit" data-id="<?php echo $teamcard->ID; ?>"><?php echo esc_html($zustaendigkeit); ?></div>
</td>
<td>
<button type="button" class="button button-small teamcard-delete" data-id="<?php echo $teamcard->ID; ?>">Löschen</button>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<div id="teamcard-message" class="notice" style="display:none;"></div>
</div>
<?php
}
// AJAX-Handler zum Speichern der Teammitglieder
function teamcard_ajax_handlers() {
// Neues Teammitglied hinzufügen
add_action('wp_ajax_add_teamcard', function() {
$name = sanitize_text_field($_POST['name']);
$funktion = sanitize_text_field($_POST['funktion']);
$zustaendigkeit = sanitize_text_field($_POST['zustaendigkeit']);
$bild_id = intval($_POST['bild_id']);
// Höchste menu_order finden
$max_order = 0;
$posts = get_posts([
'post_type' => 'teamcard',
'posts_per_page' => 1,
'orderby' => 'menu_order',
'order' => 'DESC'
]);
if (!empty($posts)) {
$max_order = $posts[0]->menu_order;
}
// Neuen Post erstellen
$post_id = wp_insert_post([
'post_type' => 'teamcard',
'post_title' => $name,
'post_status' => 'publish',
'menu_order' => $max_order + 1
]);
if ($post_id) {
update_post_meta($post_id, '_teamcard_funktion', $funktion);
update_post_meta($post_id, '_teamcard_zustaendigkeit', $zustaendigkeit);
if ($bild_id > 0) {
update_post_meta($post_id, '_teamcard_bild_id', $bild_id);
}
$bild_url = $bild_id ? wp_get_attachment_image_url($bild_id, 'thumbnail') : '';
wp_send_json_success([
'id' => $post_id,
'bild_url' => $bild_url
]);
} else {
wp_send_json_error(['message' => 'Fehler beim Erstellen des Teammitglieds']);
}
});
// Teammitglied aktualisieren
add_action('wp_ajax_update_teamcard', function() {
$post_id = intval($_POST['id']);
$field = sanitize_text_field($_POST['field']);
$value = sanitize_text_field($_POST['value']);
if ($field === 'title') {
wp_update_post([
'ID' => $post_id,
'post_title' => $value
]);
} else {
update_post_meta($post_id, '_teamcard_' . $field, $value);
}
wp_send_json_success();
});
// Teammitglied löschen
add_action('wp_ajax_delete_teamcard', function() {
$post_id = intval($_POST['id']);
if (wp_delete_post($post_id, true)) {
wp_send_json_success();
} else {
wp_send_json_error(['message' => 'Fehler beim Löschen des Teammitglieds']);
}
});
// Bild aktualisieren
add_action('wp_ajax_update_teamcard_image', function() {
$post_id = intval($_POST['id']);
$bild_id = intval($_POST['bild_id']);
update_post_meta($post_id, '_teamcard_bild_id', $bild_id);
$bild_url = wp_get_attachment_image_url($bild_id, 'thumbnail');
wp_send_json_success(['bild_url' => $bild_url]);
});
// Reihenfolge aktualisieren
add_action('wp_ajax_update_teamcard_order', function() {
$order = $_POST['order'];
foreach ($order as $position => $post_id) {
wp_update_post([
'ID' => intval($post_id),
'menu_order' => $position
]);
}
wp_send_json_success();
});
}
add_action('init', 'teamcard_ajax_handlers');
// Admin-Skripte und Styles laden
function teamcard_admin_scripts($hook) {
if ($hook !== 'toplevel_page_teamcard_management') return;
wp_enqueue_media();
wp_enqueue_script('jquery-ui-sortable');
wp_enqueue_style(
'teamcard-admin-style',
plugin_dir_url(__FILE__) . 'teamcard-admin.css',
[],
'1.0'
);
wp_enqueue_script(
'teamcard-admin-script',
plugin_dir_url(__FILE__) . 'teamcard-admin.js',
['jquery', 'jquery-ui-sortable'],
'1.0',
true
);
wp_localize_script('teamcard-admin-script', 'teamcard_data', [
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('teamcard_nonce')
]);
}
add_action('admin_enqueue_scripts', 'teamcard_admin_scripts');
// Shortcode für die Frontend-Anzeige
function teamcard_shortcode($atts) {
$atts = shortcode_atts([
'kategorie' => '',
], $atts);
$args = [
'post_type' => 'teamcard',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
];
if ($atts['kategorie']) {
$args['tax_query'] = [[
'taxonomy' => 'teamcard_kategorie',
'field' => 'slug',
'terms' => $atts['kategorie'],
]];
}
$query = new WP_Query($args);
if (!$query->have_posts()) return '<p>Keine Teammitglieder gefunden.</p>';
ob_start();
echo '<div class="teamcard-grid" style="display: flex; flex-wrap: wrap; gap: 20px;">';
while ($query->have_posts()) {
$query->the_post();
$funktion = get_post_meta(get_the_ID(), '_teamcard_funktion', true);
$zustaendigkeit = get_post_meta(get_the_ID(), '_teamcard_zustaendigkeit', true);
$bild_id = get_post_meta(get_the_ID(), '_teamcard_bild_id', true);
$bild_url = $bild_id ? wp_get_attachment_url($bild_id) : '';
echo '<div class="teamcard" style="display: flex; flex-wrap: wrap; border:1px solid #ccc; border-radius:10px; padding:15px; width:48%; align-items: center;">';
// Bild links
if ($bild_url) {
echo '<div style="flex: 1; text-align: center; display: flex; justify-content: center; align-items: center; margin-right: 10px;">';
echo '<img src="' . esc_url($bild_url) . '" style="max-width:100px; border-radius:50%; margin-bottom:0;">';
echo '</div>';
}
// Text rechts
echo '<div style="flex: 2; padding-left: 10px;">';
echo '<h3>' . get_the_title() . '</h3>';
echo '<p><strong>' . esc_html($funktion) . '</strong></p>';
echo '<p>' . esc_html($zustaendigkeit) . '</p>';
echo '</div>';
echo '</div>'; // Schließt die Teamcard-Div
}
echo '</div>'; // Schließt die Teamcard-Grid-Div
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode('teamcards', 'teamcard_shortcode');