register_post_type(); $this->register_taxonomy(); flush_rewrite_rules(); } public function deactivate() { flush_rewrite_rules(); } public function register_post_type() { register_post_type(self::POST_TYPE, [ 'labels' => [ 'name' => 'FAQ', 'singular_name' => 'FAQ', 'add_new' => 'Neu hinzufügen', 'add_new_item' => 'Neue Frage hinzufügen', 'edit_item' => 'Frage bearbeiten', 'new_item' => 'Neue Frage', 'view_item' => 'Frage ansehen', 'search_items' => 'Fragen durchsuchen', 'not_found' => 'Keine Fragen gefunden', 'not_found_in_trash' => 'Keine Fragen im Papierkorb gefunden', 'menu_name' => 'FAQ', ], 'public' => false, 'publicly_queryable' => false, 'exclude_from_search' => true, 'has_archive' => false, 'rewrite' => false, 'query_var' => false, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => false, 'show_in_admin_bar' => true, 'show_in_rest' => true, 'menu_icon' => 'dashicons-editor-help', 'supports' => ['title', 'editor', 'excerpt', 'page-attributes'], ]); } public function register_taxonomy() { register_taxonomy(self::TAXONOMY, [self::POST_TYPE], [ 'labels' => [ 'name' => 'FAQ-Kategorien', 'singular_name' => 'FAQ-Kategorie', 'search_items' => 'Kategorien durchsuchen', 'all_items' => 'Alle Kategorien', 'edit_item' => 'Kategorie bearbeiten', 'update_item' => 'Kategorie aktualisieren', 'add_new_item' => 'Neue Kategorie hinzufügen', 'new_item_name' => 'Neuer Kategoriename', 'menu_name' => 'Kategorien', ], 'public' => false, 'publicly_queryable' => false, 'hierarchical' => true, 'show_ui' => true, 'show_admin_column' => true, 'rewrite' => false, 'query_var' => false, 'show_in_rest' => true, ]); } public function enqueue_admin_assets($hook) { global $post_type; $allowed = ['post.php', 'post-new.php', 'edit.php', 'term.php', 'edit-tags.php']; if (!in_array($hook, $allowed, true)) { return; } if ($post_type !== self::POST_TYPE && (!isset($_GET['taxonomy']) || sanitize_key(wp_unslash($_GET['taxonomy'])) !== self::TAXONOMY)) { return; } wp_enqueue_style( 'kgv-faq-admin', plugin_dir_url(__FILE__) . 'assets/admin.css', [], self::VERSION ); } public function enqueue_front_assets() { wp_enqueue_style( 'kgv-faq-front', plugin_dir_url(__FILE__) . 'assets/front.css', [], self::VERSION ); wp_enqueue_script( 'kgv-faq-front', plugin_dir_url(__FILE__) . 'assets/front.js', [], self::VERSION, true ); } public function admin_columns($columns) { $new_columns = []; foreach ($columns as $key => $label) { if ($key === 'date') { continue; } $new_columns[$key] = $label; if ($key === 'title') { $new_columns['kgv_faq_order'] = 'Reihenfolge'; $new_columns['kgv_faq_answer'] = 'Antwort'; } } $new_columns['date'] = 'Datum'; return $new_columns; } public function admin_column_content($column, $post_id) { if ($column === 'kgv_faq_order') { echo (int) get_post_field('menu_order', $post_id); return; } if ($column === 'kgv_faq_answer') { $content = get_post_field('post_content', $post_id); $text = wp_strip_all_tags($content); $text = mb_substr($text, 0, 120); echo esc_html($text ? $text . '…' : '—'); } } public function sortable_columns($columns) { $columns['kgv_faq_order'] = 'menu_order'; return $columns; } public function faq_shortcode($atts) { $atts = shortcode_atts([ 'category' => '', 'limit' => 100, 'show_filter' => '1', 'open_first' => '0', 'search' => '0', ], $atts, 'kgv_faq'); $selected_category = ''; if (!empty($_GET['faq_cat'])) { $selected_category = sanitize_title(wp_unslash($_GET['faq_cat'])); } elseif (!empty($atts['category'])) { $selected_category = sanitize_title($atts['category']); } $query_args = [ 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'posts_per_page' => max(1, (int) $atts['limit']), 'orderby' => ['menu_order' => 'ASC', 'title' => 'ASC'], 'order' => 'ASC', ]; if ($selected_category) { $query_args['tax_query'] = [[ 'taxonomy' => self::TAXONOMY, 'field' => 'slug', 'terms' => $selected_category, ]]; } $faq_query = new WP_Query($query_args); $terms = get_terms([ 'taxonomy' => self::TAXONOMY, 'hide_empty' => true, ]); $uid = wp_unique_id('kgv-faq-'); $open_first = $atts['open_first'] === '1'; $show_search = $atts['search'] === '1'; ob_start(); echo '
Keine Fragen gefunden.
'; } echo '