'Ungültige Anfrage.' ) ); } if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'Keine Berechtigung.' ) ); } } public function save_wiki() { $this->verify_nonce(); $id = absint( $_POST['id'] ?? 0 ); $title = sanitize_text_field( $_POST['title'] ?? '' ); $desc = wp_kses_post( $_POST['description'] ?? '' ); $icon = sanitize_text_field( $_POST['icon'] ?? '📖' ); $color = sanitize_hex_color( $_POST['color'] ?? '#2271b1' ); $ver = sanitize_text_field( $_POST['version'] ?? '1.0.0' ); if ( empty( $title ) ) { wp_send_json_error( array( 'message' => 'Titel darf nicht leer sein.' ) ); } $data = array( 'post_type' => 'wmw_wiki', 'post_title' => $title, 'post_content' => $desc, 'post_status' => 'publish', ); if ( $id ) { $data['ID'] = $id; $result = wp_update_post( $data, true ); } else { $result = wp_insert_post( $data, true ); } if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } update_post_meta( $result, '_wmw_icon', $icon ); update_post_meta( $result, '_wmw_color', $color ); update_post_meta( $result, '_wmw_version', $ver ); wp_send_json_success( array( 'id' => $result, 'title' => $title, 'url' => get_permalink( $result ), ) ); } public function delete_wiki() { $this->verify_nonce(); $id = absint( $_POST['id'] ?? 0 ); if ( ! $id ) wp_send_json_error(); // Delete all articles in this wiki $articles = wmw_get_articles( $id ); foreach ( $articles as $article ) { wp_delete_post( $article->ID, true ); } wp_delete_post( $id, true ); wp_send_json_success(); } public function save_article() { $this->verify_nonce(); $id = absint( $_POST['id'] ?? 0 ); $title = sanitize_text_field( $_POST['title'] ?? '' ); $content = wp_kses_post( $_POST['content'] ?? '' ); $excerpt = sanitize_textarea_field( $_POST['excerpt'] ?? '' ); $wiki_id = absint( $_POST['wiki_id'] ?? 0 ); $order = absint( $_POST['order'] ?? 0 ); $cats = array_map( 'absint', (array) ( $_POST['categories'] ?? array() ) ); $tags = sanitize_text_field( $_POST['tags'] ?? '' ); $status = in_array( $_POST['status'] ?? 'publish', array( 'publish', 'draft' ) ) ? $_POST['status'] : 'publish'; if ( empty( $title ) ) { wp_send_json_error( array( 'message' => 'Titel darf nicht leer sein.' ) ); } $data = array( 'post_type' => 'wmw_article', 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt, 'post_status' => $status, ); if ( $id ) { $data['ID'] = $id; $result = wp_update_post( $data, true ); } else { $result = wp_insert_post( $data, true ); } if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } update_post_meta( $result, '_wmw_wiki_id', $wiki_id ); update_post_meta( $result, '_wmw_order', $order ); if ( ! empty( $cats ) ) { wp_set_object_terms( $result, $cats, 'wmw_category' ); } if ( ! empty( $tags ) ) { $tag_arr = array_map( 'trim', explode( ',', $tags ) ); wp_set_object_terms( $result, $tag_arr, 'wmw_tag' ); } wp_send_json_success( array( 'id' => $result, 'title' => $title, 'url' => get_permalink( $result ), ) ); } public function delete_article() { $this->verify_nonce(); $id = absint( $_POST['id'] ?? 0 ); if ( ! $id ) wp_send_json_error(); wp_delete_post( $id, true ); wp_send_json_success(); } public function reorder_articles() { $this->verify_nonce(); $order = (array) ( $_POST['order'] ?? array() ); foreach ( $order as $position => $article_id ) { update_post_meta( absint( $article_id ), '_wmw_order', absint( $position ) ); } wp_send_json_success(); } public function ajax_search() { $query = sanitize_text_field( $_POST['query'] ?? $_GET['query'] ?? '' ); $wiki_id = absint( $_POST['wiki_id'] ?? $_GET['wiki_id'] ?? 0 ); if ( strlen( $query ) < 2 ) { wp_send_json_success( array( 'results' => array(), 'count' => 0 ) ); } $results = WMW_Search::search( $query, $wiki_id ); $output = array(); foreach ( $results as $post ) { $wiki = wmw_get_article_wiki( $post->ID ); $output[] = array( 'id' => $post->ID, 'title' => $post->wmw_title, 'excerpt' => $post->wmw_excerpt, 'url' => get_permalink( $post->ID ), 'wiki' => $wiki ? $wiki->post_title : '', 'icon' => $wiki ? wmw_get_wiki_icon( $wiki->ID ) : '📄', ); } wp_send_json_success( array( 'results' => $output, 'count' => count( $output ) ) ); } public function reindex() { $this->verify_nonce(); $count = WMW_Search::reindex_all(); wp_send_json_success( array( 'count' => $count, 'message' => $count . ' Artikel neu indiziert.' ) ); } public function track_view() { global $wpdb; $article_id = absint( $_POST['article_id'] ?? 0 ); if ( ! $article_id ) wp_send_json_error(); $table = $wpdb->prefix . 'wmw_views'; $existing = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $table WHERE article_id = %d", $article_id ) ); if ( $existing ) { $wpdb->query( $wpdb->prepare( "UPDATE $table SET view_count = view_count + 1 WHERE article_id = %d", $article_id ) ); } else { $wpdb->insert( $table, array( 'article_id' => $article_id, 'view_count' => 1 ), array( '%d', '%d' ) ); } wp_send_json_success(); } }