Files

79 lines
2.9 KiB
PHP

<?php
/**
* Docs Mode: Sidebar Navigation
*
* 文档模式侧边栏:显示文档目录树,当前章节高亮。
* Uses \AKSRT\Core\DocsPresenter for tree construction.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
$current_id = get_the_ID();
$parent_id = wp_get_post_parent_id($current_id);
$root_id = $parent_id ?: $current_id;
$presenter = class_exists('\AKSRT\Core\DocsPresenter') ? \AKSRT\Core\DocsPresenter::get_instance() : null;
if ($presenter) {
$doc_pages = $presenter->get_flat_tree($root_id, 3);
} else {
$doc_pages = get_pages([
'child_of' => $root_id,
'sort_column' => 'menu_order, post_title',
'sort_order' => 'ASC',
'parent' => $root_id,
]);
}
$root_page = get_post($root_id);
?>
<aside class="aksrt-doc-sidebar" role="navigation" aria-label="<?php esc_attr_e('文档导航', 'aksrt'); ?>">
<div class="aksrt-doc-sidebar__header">
<a href="<?php echo esc_url(get_permalink($root_id)); ?>" class="aksrt-doc-sidebar__root">
<i class="fa fas fa-book aksrt-doc-sidebar__root-icon"></i>
<span><?php echo esc_html($root_page->post_title ?? __('文档', 'aksrt')); ?></span>
</a>
</div>
<div class="aksrt-doc-sidebar__search">
<input type="text" class="aksrt-doc-sidebar__search-input" placeholder="<?php esc_attr_e('搜索文档...', 'aksrt'); ?>" aria-label="<?php esc_attr_e('搜索文档', 'aksrt'); ?>">
<i class="fa fas fa-search aksrt-doc-sidebar__search-icon"></i>
</div>
<?php if (!empty($doc_pages)) : ?>
<ul class="aksrt-doc-sidebar__nav">
<?php foreach ($doc_pages as $dp) :
$depth = isset($dp->_depth) ? (int) $dp->_depth : 0;
$is_current = ((int) $dp->ID === $current_id);
$is_ancestor = ((int) $dp->ID === $parent_id);
$li_class = 'aksrt-doc-sidebar__item';
if ($is_current) $li_class .= ' is-current';
if ($is_ancestor) $li_class .= ' is-ancestor';
if ($depth > 0) $li_class .= ' aksrt-doc-sidebar__item--depth-' . $depth;
$has_children = false;
if ($presenter) {
$sub = get_pages(['parent' => $dp->ID, 'post_status' => 'publish', 'fields' => 'ids']);
$has_children = !empty($sub);
} else {
$sub_pages = get_pages(['child_of' => $dp->ID, 'sort_column' => 'menu_order', 'sort_order' => 'ASC', 'parent' => $dp->ID]);
$has_children = !empty($sub_pages);
}
$is_open = $is_current || $is_ancestor;
?>
<li class="<?php echo esc_attr($li_class); ?>" style="<?php echo $depth > 0 ? 'padding-left:' . ($depth * 16) . 'px;' : ''; ?>">
<a href="<?php echo esc_url(get_permalink($dp->ID)); ?>" class="aksrt-doc-sidebar__link<?php echo $is_current ? ' is-active' : ''; ?>">
<?php if ($has_children) : ?>
<i class="fa fas fa-chevron-<?php echo $is_open ? 'down' : 'right'; ?> aksrt-doc-sidebar__toggle" aria-hidden="true"></i>
<?php endif; ?>
<span><?php echo esc_html($dp->post_title); ?></span>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</aside>