Files
WP-Multi-Wiki/includes/class-wmw-frontend.php
2026-03-18 21:56:45 +01:00

190 lines
6.8 KiB
PHP

<?php
if ( ! defined( 'ABSPATH' ) ) exit;
class WMW_Frontend {
public function init() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );
add_filter( 'the_content', array( $this, 'filter_content' ) );
add_filter( 'the_title', array( $this, 'filter_title' ), 10, 2 );
add_action( 'wp_head', array( $this, 'inject_css_vars' ) );
add_filter( 'template_include', array( $this, 'load_template' ) );
}
public function enqueue() {
$settings = get_option( 'wmw_settings', array() );
wp_enqueue_style(
'wmw-public',
WMW_PLUGIN_URL . 'public/css/wmw-public.css',
array(),
WMW_VERSION
);
// Custom CSS from settings
if ( ! empty( $settings['custom_css'] ) ) {
wp_add_inline_style( 'wmw-public', $settings['custom_css'] );
}
wp_enqueue_script(
'wmw-public',
WMW_PLUGIN_URL . 'public/js/wmw-public.js',
array( 'jquery' ),
WMW_VERSION,
true
);
wp_localize_script( 'wmw-public', 'wmwPublic', array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'wmw_public_nonce' ),
'settings' => array(
'show_toc' => $settings['show_toc'] ?? 1,
),
) );
}
public function inject_css_vars() {
global $post;
if ( ! $post ) return;
$wiki = null;
if ( $post->post_type === 'wmw_wiki' ) {
$wiki = $post;
} elseif ( $post->post_type === 'wmw_article' ) {
$wiki = wmw_get_article_wiki( $post->ID );
}
if ( $wiki ) {
$color = wmw_get_wiki_color( $wiki->ID );
echo "<style>:root { --wmw-accent: {$color}; --wmw-accent-light: {$color}22; }</style>\n";
}
}
public function filter_content( $content ) {
global $post;
if ( ! $post || ! is_singular() ) return $content;
$settings = get_option( 'wmw_settings', array() );
if ( $post->post_type === 'wmw_article' && ( $settings['show_toc'] ?? 1 ) ) {
$processed = WMW_TOC::process( $content );
if ( ! empty( $processed['toc'] ) ) {
$content = $processed['toc'] . $processed['content'];
}
}
return $content;
}
public function filter_title( $title, $id = null ) {
return $title;
}
public function load_template( $template ) {
global $post;
if ( ! $post ) return $template;
if ( $post->post_type === 'wmw_wiki' ) {
$custom = WMW_PLUGIN_DIR . 'templates/single-wmw-wiki.php';
if ( file_exists( $custom ) ) return $custom;
}
if ( $post->post_type === 'wmw_article' ) {
$custom = WMW_PLUGIN_DIR . 'templates/single-wmw-article.php';
if ( file_exists( $custom ) ) return $custom;
}
return $template;
}
// ── Static Render Methods ─────────────────────────────────────────────────
public static function render_wiki_index( $wiki, $layout = 'grid' ) {
$settings = get_option( 'wmw_settings', array() );
$articles = wmw_get_articles( $wiki->ID );
$categories = get_terms( array( 'taxonomy' => 'wmw_category', 'hide_empty' => true ) );
$color = wmw_get_wiki_color( $wiki->ID );
$icon = wmw_get_wiki_icon( $wiki->ID );
// Group by category
$grouped = array( 'uncategorized' => array() );
$cat_map = array();
foreach ( (array) $categories as $cat ) {
$grouped[ $cat->term_id ] = array();
$cat_map[ $cat->term_id ] = $cat;
}
foreach ( $articles as $article ) {
$cats = get_the_terms( $article->ID, 'wmw_category' );
if ( $cats && ! is_wp_error( $cats ) ) {
foreach ( $cats as $cat ) {
$grouped[ $cat->term_id ][] = $article;
}
} else {
$grouped['uncategorized'][] = $article;
}
}
include WMW_PLUGIN_DIR . 'templates/wiki-index.php';
}
public static function render_article( $article ) {
$wiki = wmw_get_article_wiki( $article->ID );
$settings = get_option( 'wmw_settings', array() );
$content = apply_filters( 'the_content', $article->post_content );
// Related articles
$related = array();
if ( $settings['show_related'] ?? 1 ) {
$cats = get_the_terms( $article->ID, 'wmw_category' );
if ( $cats && ! is_wp_error( $cats ) ) {
$cat_ids = wp_list_pluck( $cats, 'term_id' );
$related = get_posts( array(
'post_type' => 'wmw_article',
'post_status' => 'publish',
'posts_per_page' => 5,
'post__not_in' => array( $article->ID ),
'tax_query' => array( array( 'taxonomy' => 'wmw_category', 'field' => 'term_id', 'terms' => $cat_ids ) ),
) );
}
}
include WMW_PLUGIN_DIR . 'templates/article-single.php';
}
public static function render_search_box( $wiki_id = 0, $placeholder = '' ) {
$placeholder = $placeholder ?: 'Wiki durchsuchen…';
include WMW_PLUGIN_DIR . 'templates/search-box.php';
}
public static function render_wiki_list( $wikis, $columns = 3 ) {
include WMW_PLUGIN_DIR . 'templates/wiki-list.php';
}
public static function render_breadcrumb( $post ) {
$items = array();
$items[] = array( 'label' => 'Home', 'url' => home_url() );
if ( $post->post_type === 'wmw_article' ) {
$wiki = wmw_get_article_wiki( $post->ID );
if ( $wiki ) {
$items[] = array( 'label' => 'Wiki', 'url' => get_post_type_archive_link( 'wmw_wiki' ) );
$items[] = array( 'label' => $wiki->post_title, 'url' => get_permalink( $wiki->ID ) );
}
$items[] = array( 'label' => $post->post_title, 'url' => '' );
} elseif ( $post->post_type === 'wmw_wiki' ) {
$items[] = array( 'label' => 'Wiki', 'url' => get_post_type_archive_link( 'wmw_wiki' ) );
$items[] = array( 'label' => $post->post_title, 'url' => '' );
}
include WMW_PLUGIN_DIR . 'templates/breadcrumb.php';
}
public static function get_wiki_sidebar( $wiki ) {
$articles = wmw_get_articles( $wiki->ID );
$categories = get_terms( array( 'taxonomy' => 'wmw_category', 'hide_empty' => false ) );
ob_start();
include WMW_PLUGIN_DIR . 'templates/sidebar.php';
return ob_get_clean();
}
}