303 lines
14 KiB
PHP
303 lines
14 KiB
PHP
<?php
|
|
if ( ! defined( 'ABSPATH' ) ) exit;
|
|
|
|
class WMW_Admin {
|
|
|
|
public function init() {
|
|
add_action( 'admin_menu', array( $this, 'add_menus' ) );
|
|
add_action( 'admin_enqueue_scripts',array( $this, 'enqueue_scripts' ) );
|
|
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
|
|
add_action( 'save_post', array( $this, 'save_meta_boxes' ) );
|
|
add_filter( 'manage_wmw_article_posts_columns', array( $this, 'article_columns' ) );
|
|
add_action( 'manage_wmw_article_posts_custom_column', array( $this, 'article_column_data' ), 10, 2 );
|
|
add_filter( 'manage_wmw_wiki_posts_columns', array( $this, 'wiki_columns' ) );
|
|
add_action( 'manage_wmw_wiki_posts_custom_column', array( $this, 'wiki_column_data' ), 10, 2 );
|
|
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
|
|
}
|
|
|
|
// ── Admin Menu ────────────────────────────────────────────────────────────
|
|
|
|
public function add_menus() {
|
|
add_menu_page(
|
|
'WP Multi Wiki',
|
|
'WP Multi Wiki',
|
|
'manage_options',
|
|
'wp-multi-wiki',
|
|
array( $this, 'page_dashboard' ),
|
|
'dashicons-book-alt',
|
|
30
|
|
);
|
|
|
|
add_submenu_page( 'wp-multi-wiki', 'Alle Wikis', 'Alle Wikis', 'manage_options', 'wp-multi-wiki', array( $this, 'page_dashboard' ) );
|
|
add_submenu_page( 'wp-multi-wiki', 'Neues Wiki', 'Neues Wiki', 'manage_options', 'wmw-new-wiki', array( $this, 'page_new_wiki' ) );
|
|
add_submenu_page( 'wp-multi-wiki', 'Alle Artikel', 'Alle Artikel', 'manage_options', 'wmw-articles', array( $this, 'page_articles' ) );
|
|
add_submenu_page( 'wp-multi-wiki', 'Neuer Artikel', 'Neuer Artikel', 'manage_options', 'wmw-new-article', array( $this, 'page_new_article' ) );
|
|
add_submenu_page( 'wp-multi-wiki', 'Kategorien', 'Kategorien', 'manage_options', 'wmw-categories', array( $this, 'page_categories' ) );
|
|
add_submenu_page( 'wp-multi-wiki', 'Einstellungen', 'Einstellungen', 'manage_options', 'wmw-settings', array( $this, 'page_settings' ) );
|
|
add_submenu_page( 'wp-multi-wiki', '🐙 Gitea Import', '🐙 Gitea Import', 'manage_options', 'wmw-gitea-import', array( $this, 'page_gitea_import' ) );
|
|
|
|
// Hidden edit pages
|
|
add_submenu_page( 'wp-multi-wiki', 'Wiki bearbeiten', '', 'manage_options', 'wmw-edit-wiki', array( $this, 'page_edit_wiki' ) );
|
|
add_submenu_page( 'wp-multi-wiki', 'Artikel bearbeiten','', 'manage_options', 'wmw-edit-article', array( $this, 'page_edit_article' ) );
|
|
}
|
|
|
|
// ── Scripts & Styles ──────────────────────────────────────────────────────
|
|
|
|
public function enqueue_scripts( $hook ) {
|
|
if ( strpos( $hook, 'wp-multi-wiki' ) === false && strpos( $hook, 'wmw-' ) === false ) return;
|
|
|
|
wp_enqueue_style(
|
|
'wmw-admin',
|
|
WMW_PLUGIN_URL . 'admin/css/wmw-admin.css',
|
|
array(),
|
|
WMW_VERSION
|
|
);
|
|
wp_enqueue_style( 'wp-color-picker' );
|
|
wp_enqueue_script(
|
|
'wmw-admin',
|
|
WMW_PLUGIN_URL . 'admin/js/wmw-admin.js',
|
|
array( 'jquery', 'wp-color-picker' ),
|
|
WMW_VERSION,
|
|
true
|
|
);
|
|
wp_localize_script( 'wmw-admin', 'wmwAdmin', array(
|
|
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
|
|
'nonce' => wp_create_nonce( 'wmw_admin_nonce' ),
|
|
'confirm_delete' => __( 'Wirklich löschen?', 'wp-multi-wiki' ),
|
|
) );
|
|
|
|
wp_enqueue_editor();
|
|
}
|
|
|
|
// ── Meta Boxes ────────────────────────────────────────────────────────────
|
|
|
|
public function add_meta_boxes() {
|
|
add_meta_box( 'wmw_wiki_options', 'Wiki-Optionen', array( $this, 'mb_wiki_options' ), 'wmw_wiki', 'side' );
|
|
add_meta_box( 'wmw_article_meta', 'Artikel-Einstellungen', array( $this, 'mb_article_meta' ), 'wmw_article', 'side' );
|
|
}
|
|
|
|
public function mb_wiki_options( $post ) {
|
|
wp_nonce_field( 'wmw_save_wiki', 'wmw_wiki_nonce' );
|
|
$icon = get_post_meta( $post->ID, '_wmw_icon', true ) ?: '📖';
|
|
$color = get_post_meta( $post->ID, '_wmw_color', true ) ?: '#2271b1';
|
|
$ver = get_post_meta( $post->ID, '_wmw_version', true ) ?: '1.0.0';
|
|
?>
|
|
<p>
|
|
<label><strong>Icon (Emoji)</strong></label><br>
|
|
<input type="text" name="wmw_icon" value="<?php echo esc_attr( $icon ); ?>" style="width:100%;font-size:20px;">
|
|
</p>
|
|
<p>
|
|
<label><strong>Akzentfarbe</strong></label><br>
|
|
<input type="text" name="wmw_color" value="<?php echo esc_attr( $color ); ?>" class="wmw-color-picker">
|
|
</p>
|
|
<p>
|
|
<label><strong>Plugin-Version</strong></label><br>
|
|
<input type="text" name="wmw_version" value="<?php echo esc_attr( $ver ); ?>" style="width:100%;">
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
public function mb_article_meta( $post ) {
|
|
wp_nonce_field( 'wmw_save_article', 'wmw_article_nonce' );
|
|
$wiki_id = (int) get_post_meta( $post->ID, '_wmw_wiki_id', true );
|
|
$order = (int) get_post_meta( $post->ID, '_wmw_order', true );
|
|
$wikis = wmw_get_wikis();
|
|
?>
|
|
<p>
|
|
<label><strong>Gehört zu Wiki</strong></label><br>
|
|
<select name="wmw_wiki_id" style="width:100%">
|
|
<option value="">— Kein Wiki —</option>
|
|
<?php foreach ( $wikis as $wiki ) : ?>
|
|
<option value="<?php echo $wiki->ID; ?>" <?php selected( $wiki_id, $wiki->ID ); ?>>
|
|
<?php echo esc_html( $wiki->post_title ); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</p>
|
|
<p>
|
|
<label><strong>Sortierreihenfolge</strong></label><br>
|
|
<input type="number" name="wmw_order" value="<?php echo esc_attr( $order ); ?>" style="width:100%;" min="0">
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
public function save_meta_boxes( $post_id ) {
|
|
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
|
|
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
|
|
|
|
// Save wiki options
|
|
if ( isset( $_POST['wmw_wiki_nonce'] ) && wp_verify_nonce( $_POST['wmw_wiki_nonce'], 'wmw_save_wiki' ) ) {
|
|
update_post_meta( $post_id, '_wmw_icon', sanitize_text_field( $_POST['wmw_icon'] ?? '📖' ) );
|
|
update_post_meta( $post_id, '_wmw_color', sanitize_hex_color( $_POST['wmw_color'] ?? '#2271b1' ) );
|
|
update_post_meta( $post_id, '_wmw_version', sanitize_text_field( $_POST['wmw_version'] ?? '1.0.0' ) );
|
|
}
|
|
|
|
// Save article meta
|
|
if ( isset( $_POST['wmw_article_nonce'] ) && wp_verify_nonce( $_POST['wmw_article_nonce'], 'wmw_save_article' ) ) {
|
|
$wiki_id = absint( $_POST['wmw_wiki_id'] ?? 0 );
|
|
update_post_meta( $post_id, '_wmw_wiki_id', $wiki_id );
|
|
update_post_meta( $post_id, '_wmw_order', absint( $_POST['wmw_order'] ?? 0 ) );
|
|
|
|
// Update search index
|
|
$this->update_search_index( $post_id, $wiki_id );
|
|
}
|
|
}
|
|
|
|
private function update_search_index( $article_id, $wiki_id ) {
|
|
global $wpdb;
|
|
$post = get_post( $article_id );
|
|
$keywords = wp_strip_all_tags( $post->post_title . ' ' . $post->post_content . ' ' . $post->post_excerpt );
|
|
$table = $wpdb->prefix . 'wmw_search_index';
|
|
$wpdb->replace( $table, array(
|
|
'article_id' => $article_id,
|
|
'wiki_id' => $wiki_id,
|
|
'keywords' => $keywords,
|
|
) );
|
|
}
|
|
|
|
// ── Column Overrides ─────────────────────────────────────────────────────
|
|
|
|
public function article_columns( $columns ) {
|
|
unset( $columns['date'] );
|
|
$columns['wmw_wiki'] = 'Wiki';
|
|
$columns['wmw_category'] = 'Kategorie';
|
|
$columns['date'] = 'Datum';
|
|
return $columns;
|
|
}
|
|
|
|
public function article_column_data( $column, $post_id ) {
|
|
if ( 'wmw_wiki' === $column ) {
|
|
$wiki = wmw_get_article_wiki( $post_id );
|
|
echo $wiki ? '<a href="' . admin_url( 'admin.php?page=wmw-edit-wiki&id=' . $wiki->ID ) . '">' . esc_html( $wiki->post_title ) . '</a>' : '—';
|
|
}
|
|
if ( 'wmw_category' === $column ) {
|
|
$terms = get_the_terms( $post_id, 'wmw_category' );
|
|
echo $terms ? implode( ', ', wp_list_pluck( $terms, 'name' ) ) : '—';
|
|
}
|
|
}
|
|
|
|
public function wiki_columns( $columns ) {
|
|
unset( $columns['date'] );
|
|
$columns['wmw_articles'] = 'Artikel';
|
|
$columns['wmw_icon'] = 'Icon';
|
|
$columns['wmw_version'] = 'Version';
|
|
$columns['date'] = 'Datum';
|
|
return $columns;
|
|
}
|
|
|
|
public function wiki_column_data( $column, $post_id ) {
|
|
if ( 'wmw_articles' === $column ) {
|
|
$count = count( wmw_get_articles( $post_id ) );
|
|
echo '<strong>' . $count . '</strong>';
|
|
}
|
|
if ( 'wmw_icon' === $column ) {
|
|
echo '<span style="font-size:20px">' . wmw_get_wiki_icon( $post_id ) . '</span>';
|
|
}
|
|
if ( 'wmw_version' === $column ) {
|
|
echo esc_html( get_post_meta( $post_id, '_wmw_version', true ) ?: '1.0.0' );
|
|
}
|
|
}
|
|
|
|
// ── Admin Notices ─────────────────────────────────────────────────────────
|
|
|
|
public function admin_notices() {
|
|
if ( isset( $_GET['wmw_saved'] ) ) {
|
|
echo '<div class="notice notice-success is-dismissible"><p>✅ Gespeichert!</p></div>';
|
|
}
|
|
if ( isset( $_GET['wmw_deleted'] ) ) {
|
|
echo '<div class="notice notice-success is-dismissible"><p>🗑️ Gelöscht!</p></div>';
|
|
}
|
|
if ( isset( $_GET['wmw_error'] ) ) {
|
|
echo '<div class="notice notice-error is-dismissible"><p>❌ Fehler: ' . esc_html( $_GET['wmw_error'] ) . '</p></div>';
|
|
}
|
|
}
|
|
|
|
// ── Pages ─────────────────────────────────────────────────────────────────
|
|
|
|
public function page_dashboard() {
|
|
$wikis = get_posts( array( 'post_type' => 'wmw_wiki', 'post_status' => 'any', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC' ) );
|
|
$total_articles = count( get_posts( array( 'post_type' => 'wmw_article', 'post_status' => 'any', 'posts_per_page' => -1 ) ) );
|
|
include WMW_PLUGIN_DIR . 'templates/admin/dashboard.php';
|
|
}
|
|
|
|
public function page_new_wiki() {
|
|
include WMW_PLUGIN_DIR . 'templates/admin/wiki-form.php';
|
|
}
|
|
|
|
public function page_edit_wiki() {
|
|
$wiki_id = absint( $_GET['id'] ?? 0 );
|
|
$wiki = $wiki_id ? get_post( $wiki_id ) : null;
|
|
if ( ! $wiki || $wiki->post_type !== 'wmw_wiki' ) {
|
|
wp_die( 'Wiki nicht gefunden.' );
|
|
}
|
|
include WMW_PLUGIN_DIR . 'templates/admin/wiki-form.php';
|
|
}
|
|
|
|
public function page_articles() {
|
|
$wiki_filter = absint( $_GET['wiki_id'] ?? 0 );
|
|
$args = array( 'post_type' => 'wmw_article', 'post_status' => 'any', 'posts_per_page' => 50, 'orderby' => 'title', 'order' => 'ASC' );
|
|
if ( $wiki_filter ) {
|
|
$args['meta_query'] = array( array( 'key' => '_wmw_wiki_id', 'value' => $wiki_filter ) );
|
|
}
|
|
$articles = get_posts( $args );
|
|
$wikis = wmw_get_wikis();
|
|
include WMW_PLUGIN_DIR . 'templates/admin/article-list.php';
|
|
}
|
|
|
|
public function page_new_article() {
|
|
$wiki_id = absint( $_GET['wiki_id'] ?? 0 );
|
|
$wikis = wmw_get_wikis();
|
|
include WMW_PLUGIN_DIR . 'templates/admin/article-form.php';
|
|
}
|
|
|
|
public function page_edit_article() {
|
|
$article_id = absint( $_GET['id'] ?? 0 );
|
|
$article = $article_id ? get_post( $article_id ) : null;
|
|
if ( ! $article || $article->post_type !== 'wmw_article' ) wp_die( 'Artikel nicht gefunden.' );
|
|
$wikis = wmw_get_wikis();
|
|
include WMW_PLUGIN_DIR . 'templates/admin/article-form.php';
|
|
}
|
|
|
|
public function page_categories() {
|
|
include WMW_PLUGIN_DIR . 'templates/admin/categories.php';
|
|
}
|
|
|
|
public function page_gitea_import() {
|
|
include WMW_PLUGIN_DIR . 'templates/admin/gitea-importer.php';
|
|
}
|
|
|
|
public function page_settings() {
|
|
|
|
$saved = false;
|
|
|
|
// ── Speichern ────────────────────────────────────────────────────
|
|
if ( isset( $_POST['wmw_settings_nonce'] )
|
|
&& wp_verify_nonce( $_POST['wmw_settings_nonce'], 'wmw_save_settings' )
|
|
&& current_user_can( 'manage_options' ) ) {
|
|
|
|
$settings = array(
|
|
'show_toc' => isset( $_POST['show_toc'] ) ? 1 : 0,
|
|
'show_breadcrumbs' => isset( $_POST['show_breadcrumbs'] ) ? 1 : 0,
|
|
'show_related' => isset( $_POST['show_related'] ) ? 1 : 0,
|
|
'show_search' => isset( $_POST['show_search'] ) ? 1 : 0,
|
|
'articles_per_page' => absint( $_POST['articles_per_page'] ?? 20 ),
|
|
'sidebar_position' => sanitize_key( $_POST['sidebar_position'] ?? 'left' ),
|
|
'color_scheme' => sanitize_key( $_POST['color_scheme'] ?? 'auto' ),
|
|
'custom_css' => wp_strip_all_tags( $_POST['custom_css'] ?? '' ),
|
|
);
|
|
|
|
update_option( 'wmw_settings', $settings );
|
|
$saved = true;
|
|
}
|
|
|
|
// ── Seite anzeigen (nach Speichern ODER normalem Aufruf) ─────────
|
|
$settings = get_option( 'wmw_settings', array() );
|
|
|
|
// Erfolgsmeldung direkt hier ausgeben, kein Redirect nötig
|
|
if ( $saved ) {
|
|
echo '<div class="notice notice-success is-dismissible"><p>✅ Einstellungen gespeichert!</p></div>';
|
|
}
|
|
|
|
include WMW_PLUGIN_DIR . 'templates/admin/settings.php';
|
|
}
|
|
}
|