247 lines
9.9 KiB
PHP
247 lines
9.9 KiB
PHP
<?php
|
||
/**
|
||
* Plugin Name: AI Translate
|
||
* Description: Plugin zum Verwalten und Übersetzen von Beiträgen, Seiten und Custom Post Types (ohne AJAX).
|
||
* Version: 1.15
|
||
* Author: Dein Name
|
||
* License: GPL2
|
||
*/
|
||
|
||
if ( ! defined( 'ABSPATH' ) ) {
|
||
exit;
|
||
}
|
||
|
||
/**
|
||
* Verarbeitet Formular‑ und URL‑Anfragen:
|
||
* - Falls per POST ausgewählte Beiträge übermittelt werden, werden diese (ohne Duplikate) zur bestehenden Auswahl hinzugefügt.
|
||
* - Falls per GET der Parameter "remove" vorhanden ist, wird der entsprechende Eintrag entfernt.
|
||
*/
|
||
function ait_handle_form_submissions() {
|
||
// Beiträge hinzufügen
|
||
if ( isset( $_POST['selected_posts'] ) && is_array( $_POST['selected_posts'] ) ) {
|
||
// Konvertiere alle IDs in Integer
|
||
$new_posts = array_map( 'intval', $_POST['selected_posts'] );
|
||
$current = get_option( 'ait_selected_posts', [] );
|
||
$merged = array_unique( array_merge( $current, $new_posts ) );
|
||
update_option( 'ait_selected_posts', $merged );
|
||
echo '<div class="notice notice-success"><p>Auswahl hinzugefügt.</p></div>';
|
||
}
|
||
// Einzelnen Eintrag entfernen
|
||
if ( isset( $_GET['remove'] ) ) {
|
||
$remove_id = intval( $_GET['remove'] );
|
||
$current = get_option( 'ait_selected_posts', [] );
|
||
if ( ( $key = array_search( $remove_id, $current ) ) !== false ) {
|
||
unset( $current[ $key ] );
|
||
update_option( 'ait_selected_posts', $current );
|
||
echo '<div class="notice notice-success"><p>Eintrag entfernt.</p></div>';
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Fügt das Admin-Menü hinzu.
|
||
*/
|
||
function ait_add_admin_menu() {
|
||
add_menu_page( 'AI Translate', 'AI Translate', 'manage_options', 'ai-translate', 'ait_admin_page', 'dashicons-translation', 25 );
|
||
}
|
||
add_action( 'admin_menu', 'ait_add_admin_menu' );
|
||
|
||
/**
|
||
* Zeigt die Admin-Seite an.
|
||
*/
|
||
function ait_admin_page() {
|
||
// Zuerst werden alle Formular-/URL-Anfragen verarbeitet.
|
||
ait_handle_form_submissions();
|
||
|
||
$current_tab = $_GET['tab'] ?? 'post';
|
||
?>
|
||
<div class="wrap">
|
||
<h1>AI Translate</h1>
|
||
<h2 class="nav-tab-wrapper">
|
||
<a href="?page=ai-translate&tab=post" class="nav-tab <?php echo ($current_tab == 'post') ? 'nav-tab-active' : ''; ?>">Beiträge</a>
|
||
<a href="?page=ai-translate&tab=page" class="nav-tab <?php echo ($current_tab == 'page') ? 'nav-tab-active' : ''; ?>">Seiten</a>
|
||
<?php
|
||
$custom_post_types = get_post_types( [ '_builtin' => false ], 'objects' );
|
||
foreach ( $custom_post_types as $cpt ) {
|
||
echo '<a href="?page=ai-translate&tab=' . esc_attr( $cpt->name ) . '" class="nav-tab ' . (($current_tab == $cpt->name) ? 'nav-tab-active' : '') . '">' . esc_html( $cpt->label ) . '</a>';
|
||
}
|
||
?>
|
||
</h2>
|
||
|
||
<!-- Gespeicherte Auswahl anzeigen -->
|
||
<div id="selected-list-container">
|
||
<?php echo ait_get_selected_list_html( $current_tab ); ?>
|
||
</div>
|
||
|
||
<!-- Übersetzen-Button (unberührt, falls er später andere Funktionen auslösen soll) -->
|
||
<button id="translate-button" class="button button-primary">Übersetzen</button>
|
||
|
||
<?php ait_display_table( $current_tab ); ?>
|
||
</div>
|
||
<?php
|
||
}
|
||
|
||
/**
|
||
* Erzeugt den HTML-Code für die "Gespeicherte Auswahl".
|
||
* Jeder Eintrag wird als Liste mit einem [x]-Link ausgegeben, über den sich der Eintrag entfernen lässt.
|
||
*/
|
||
function ait_get_selected_list_html( $current_tab ) {
|
||
ob_start();
|
||
$selected_posts = get_option( 'ait_selected_posts', [] );
|
||
echo '<div class="selected-posts-box">';
|
||
echo '<h3>Gespeicherte Auswahl</h3>';
|
||
if ( ! empty( $selected_posts ) ) {
|
||
echo '<div id="selected-posts-list">';
|
||
$titles = [];
|
||
// Optional: Debug-Ausgabe (z.B. per error_log)
|
||
// error_log("Gespeicherte Auswahl: " . implode(', ', $selected_posts));
|
||
foreach ( $selected_posts as $post_id ) {
|
||
$post = get_post( $post_id );
|
||
if ( $post && ! empty( $post->post_title ) ) {
|
||
$remove_url = add_query_arg( [ 'remove' => $post_id, 'tab' => $current_tab ], admin_url( 'admin.php?page=ai-translate' ) );
|
||
$titles[] = '<span class="selected-item" data-id="' . esc_attr( $post_id ) . '">'
|
||
. esc_html( $post->post_title )
|
||
. ' <a class="remove-item" href="' . esc_url( $remove_url ) . '">[x]</a></span>';
|
||
}
|
||
}
|
||
echo implode( ', ', $titles );
|
||
echo '</div>';
|
||
} else {
|
||
echo '<p>Keine Einträge ausgewählt.</p>';
|
||
}
|
||
echo '</div>';
|
||
return ob_get_clean();
|
||
}
|
||
|
||
/**
|
||
* Gibt für einen Beitrag alle Begriffe aus den hierarchischen Taxonomien zurück.
|
||
*/
|
||
function ait_get_post_categories( $post_id, $post_type ) {
|
||
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
|
||
$category_names = [];
|
||
foreach ( $taxonomies as $taxonomy ) {
|
||
if ( $taxonomy->hierarchical ) {
|
||
$terms = get_the_terms( $post_id, $taxonomy->name );
|
||
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
|
||
foreach ( $terms as $term ) {
|
||
$category_names[] = $term->name;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return $category_names;
|
||
}
|
||
|
||
/**
|
||
* Ermittelt die erste hierarchische Taxonomie für den Post Type.
|
||
*/
|
||
function ait_get_category_taxonomy( $post_type ) {
|
||
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
|
||
foreach ( $taxonomies as $taxonomy ) {
|
||
if ( $taxonomy->hierarchical ) {
|
||
return $taxonomy->name;
|
||
}
|
||
}
|
||
return '';
|
||
}
|
||
|
||
/**
|
||
* Zeigt eine Tabelle mit Beiträgen, Seiten oder Custom Post Types inklusive Pagination und Kategorie‑Filter.
|
||
* Die Tabelle enthält ein Formular, das per POST abgeschickt wird, um ausgewählte Beiträge zur Auswahl hinzuzufügen.
|
||
*/
|
||
function ait_display_table( $post_type ) {
|
||
$paged = max( 1, intval( $_GET['paged'] ?? 1 ) );
|
||
$posts_per_page = 10;
|
||
$selected_category = intval( $_GET['category'] ?? 0 );
|
||
?>
|
||
<form id="ai-translate-form" method="post" action="">
|
||
<input type="hidden" name="page" value="ai-translate">
|
||
<input type="hidden" name="tab" value="<?php echo esc_attr( $post_type ); ?>">
|
||
<?php
|
||
// Kategorie-Filter nur anzeigen, wenn für den Post Type eine hierarchische Taxonomie vorhanden ist.
|
||
$category_tax = ait_get_category_taxonomy( $post_type );
|
||
if ( $category_tax ) : ?>
|
||
<select name="category" onchange="this.form.submit();">
|
||
<option value="0">Alle Kategorien</option>
|
||
<?php
|
||
$terms = get_terms( [ 'taxonomy' => $category_tax, 'hide_empty' => false ] );
|
||
if ( ! is_wp_error( $terms ) ) {
|
||
foreach ( $terms as $term ) {
|
||
echo '<option value="' . esc_attr( $term->term_id ) . '" ' . selected( $selected_category, $term->term_id, false ) . '>' . esc_html( $term->name ) . '</option>';
|
||
}
|
||
}
|
||
?>
|
||
</select>
|
||
<?php endif; ?>
|
||
<table class="wp-list-table widefat fixed striped">
|
||
<thead>
|
||
<tr>
|
||
<th class="checkbox-column"><input type="checkbox" id="select-all"></th>
|
||
<th>Titel</th>
|
||
<th>Kategorie</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<?php
|
||
$args = [
|
||
'post_type' => $post_type,
|
||
'posts_per_page' => $posts_per_page,
|
||
'paged' => $paged,
|
||
'post_status' => [ 'publish', 'private', 'draft' ],
|
||
];
|
||
if ( $selected_category && $category_tax ) {
|
||
$args['tax_query'] = [[
|
||
'taxonomy' => $category_tax,
|
||
'field' => 'term_id',
|
||
'terms' => $selected_category,
|
||
]];
|
||
}
|
||
$query = new WP_Query( $args );
|
||
if ( $query->have_posts() ) :
|
||
while ( $query->have_posts() ) : $query->the_post();
|
||
?>
|
||
<tr>
|
||
<td class="checkbox-column">
|
||
<input type="checkbox" name="selected_posts[]" value="<?php echo get_the_ID(); ?>">
|
||
</td>
|
||
<td><?php echo esc_html( get_the_title() ); ?></td>
|
||
<td><?php echo esc_html( implode( ', ', ait_get_post_categories( get_the_ID(), $post_type ) ) ); ?></td>
|
||
</tr>
|
||
<?php
|
||
endwhile;
|
||
else :
|
||
?>
|
||
<tr>
|
||
<td colspan="3">Keine Beiträge gefunden.</td>
|
||
</tr>
|
||
<?php endif;
|
||
wp_reset_postdata();
|
||
?>
|
||
</tbody>
|
||
</table>
|
||
<p>
|
||
<input type="submit" name="save_selection" class="button button-primary" value="Zur Auswahl hinzufügen">
|
||
</p>
|
||
</form>
|
||
<div class="pagination">
|
||
<?php for ( $i = 1; $i <= $query->max_num_pages; $i++ ) : ?>
|
||
<a href="?page=ai-translate&tab=<?php echo esc_attr( $post_type ); ?>&paged=<?php echo $i; ?>" class="button <?php echo ( $paged == $i ) ? 'button-primary' : ''; ?>"><?php echo $i; ?></a>
|
||
<?php endfor; ?>
|
||
</div>
|
||
<script>
|
||
// Select-All Funktionalität (rein clientseitig)
|
||
document.addEventListener('DOMContentLoaded', function() {
|
||
var selectAllCheckbox = document.getElementById('select-all');
|
||
if ( selectAllCheckbox ) {
|
||
selectAllCheckbox.addEventListener('change', function() {
|
||
var checkboxes = document.querySelectorAll('tbody input[type="checkbox"]');
|
||
checkboxes.forEach(function(cb) {
|
||
cb.checked = selectAllCheckbox.checked;
|
||
});
|
||
});
|
||
}
|
||
});
|
||
</script>
|
||
<?php
|
||
}
|
||
?>
|