taxonomies as $tax) { add_action("{$tax}_add_form_fields", [$this, 'render_add_fields']); add_action("{$tax}_edit_form_fields", [$this, 'render_edit_fields']); add_action("created_{$tax}", [$this, 'save_term_meta']); add_action("edited_{$tax}", [$this, 'save_term_meta']); } add_action('pre_get_posts', [$this, 'filter_archive_query']); } /** * Get term meta with defaults. */ public static function get_meta(int $term_id): array { $defaults = AKSRT_Domain_Model::default_term_meta(); $saved = get_term_meta($term_id, self::META_KEY, true); if (!is_array($saved)) $saved = []; return array_merge($defaults, $saved); } /** * Get a single term meta value. */ public static function get_meta_value(int $term_id, string $key, $default = '') { $meta = self::get_meta($term_id); return $meta[$key] ?? $default; } /** * Resolve global page-display settings with term-level overrides. * * @param WP_Term $term Archive term. * @return array */ public static function resolve_archive_settings(WP_Term $term): array { $taxonomy = $term->taxonomy === 'post_tag' ? 'post_tag' : 'category'; $global = $taxonomy === 'post_tag' && class_exists('AKSRT_Settings') ? AKSRT_Settings::get_tag_page_settings() : (class_exists('AKSRT_Settings') ? AKSRT_Settings::get_category_page_settings() : [ 'header_style' => 'cover', 'cover_enabled' => true, 'desc_enabled' => true, 'list_mode' => 'grid', 'filter_bar' => true, 'sidebar' => true, 'per_page' => 12, ]); $saved = get_term_meta($term->term_id, self::META_KEY, true); if (!is_array($saved)) { $saved = []; } $layout_mode = isset($saved['layout_mode']) && $saved['layout_mode'] !== '' ? sanitize_key((string) $saved['layout_mode']) : (string) ($global['list_mode'] ?? 'grid'); $layout_map = [ 'grid' => ['mode' => 'card', 'card_style' => 'standard'], 'card' => ['mode' => 'card', 'card_style' => 'standard'], 'list' => ['mode' => 'list', 'card_style' => 'standard'], 'compact' => ['mode' => 'list', 'card_style' => 'compact'], 'mini' => ['mode' => 'mini', 'card_style' => 'standard'], 'grid-cms' => ['mode' => 'grid-cms', 'card_style' => 'standard'], ]; $resolved_layout = $layout_map[$layout_mode] ?? ['mode' => 'card', 'card_style' => 'standard']; $header_style = (string) ($global['header_style'] ?? 'simple'); if (!in_array($header_style, ['cover', 'simple', 'none'], true)) { $header_style = 'simple'; } if (array_key_exists('show_header', $saved)) { $header_style = !empty($saved['show_header']) ? ($header_style === 'none' ? 'simple' : $header_style) : 'none'; } $sidebar_enabled = (bool) ($global['sidebar'] ?? true); if (($saved['show_sidebar'] ?? '') === '1') { $sidebar_enabled = true; } elseif (($saved['show_sidebar'] ?? '') === '0') { $sidebar_enabled = false; } $cover_url = isset($saved['cover_url']) ? esc_url_raw((string) $saved['cover_url']) : ''; if ($cover_url === '' && $taxonomy === 'category' && !empty($global['cover_enabled']) && class_exists('AKSRT_Settings')) { $cover_url = AKSRT_Settings::get_category_cover_url($term->term_id); } $description = isset($saved['description']) ? sanitize_textarea_field((string) $saved['description']) : ''; if ($description === '') { $description = (string) $term->description; } return [ 'taxonomy' => $taxonomy, 'layout_mode' => $resolved_layout['mode'], 'card_style' => $resolved_layout['card_style'], 'layout_preset' => $layout_mode, 'header_style' => $header_style, 'show_sidebar' => $sidebar_enabled, 'show_cover' => $header_style === 'cover' && !empty($global['cover_enabled']) && $cover_url !== '', 'show_desc' => $taxonomy === 'post_tag' ? $description !== '' : (!empty($global['desc_enabled']) && $description !== ''), 'filter_bar' => $taxonomy === 'category' && !empty($global['filter_bar']), 'per_page' => max(4, min(48, (int) ($global['per_page'] ?? 12))), 'cover_url' => $cover_url, 'description' => $description, ]; } /** * Apply archive per-page settings to the main query. */ public function filter_archive_query(WP_Query $query): void { if (is_admin() || !$query->is_main_query()) { return; } $term = null; if ($query->is_category()) { $cat_id = absint($query->get('cat')); if ($cat_id > 0) { $term = get_term($cat_id, 'category'); } else { $slug = sanitize_title((string) $query->get('category_name')); if ($slug !== '') { $term = get_term_by('slug', $slug, 'category'); } } } elseif ($query->is_tag()) { $tag_id = absint($query->get('tag_id')); if ($tag_id > 0) { $term = get_term($tag_id, 'post_tag'); } else { $slug = sanitize_title((string) $query->get('tag')); if ($slug !== '') { $term = get_term_by('slug', $slug, 'post_tag'); } } } if (!$term instanceof WP_Term) { return; } $settings = self::resolve_archive_settings($term); if (!empty($settings['per_page'])) { $query->set('posts_per_page', (int) $settings['per_page']); } } /** * Render fields for "Add new term" form. */ public function render_add_fields(string $taxonomy): void { ?>

term_id); ?>



isset($raw['cover_url']) ? esc_url_raw($raw['cover_url']) : '', 'description' => isset($raw['description']) ? sanitize_textarea_field($raw['description']) : '', 'layout_mode' => isset($raw['layout_mode']) ? sanitize_key($raw['layout_mode']) : '', 'show_sidebar' => isset($raw['show_sidebar']) ? '1' : '', 'show_header' => isset($raw['show_header']) ? true : false, 'show_featured' => isset($raw['show_featured']) ? true : false, ]; // Only allow valid layout modes $valid_modes = ['', 'card', 'list', 'mini', 'grid-cms']; if (!in_array($sanitized['layout_mode'], $valid_modes, true)) { $sanitized['layout_mode'] = ''; } update_term_meta($term_id, self::META_KEY, $sanitized); } }