Differentiate product list/mini row sizes; custom HTML and posts-navs admin empty states via aksrt_get_widget_null.
103 lines
4.1 KiB
PHP
103 lines
4.1 KiB
PHP
<?php
|
|
/**
|
|
* In-article / topic TOC shell (zibll widget_ui_posts_navs parity).
|
|
*
|
|
* Shell matches the built-in single sidebar TOC so page-template.js can fill
|
|
* `.posts-nav-box.toc-widget > .toc-list` from `[data-nav]` headings.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Widgets;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class WidgetPostsNavs extends \WP_Widget {
|
|
|
|
public function __construct() {
|
|
parent::__construct(
|
|
'aksrt_posts_navs',
|
|
esc_html__('AKSRT 文章目录树', 'aksrt'),
|
|
[
|
|
'description' => esc_html__('显示当前文章/帖子标题目录,非详情页不输出。标题超过 2 个才填充。', 'aksrt'),
|
|
'classname' => 'widget_aksrt_posts_navs',
|
|
]
|
|
);
|
|
}
|
|
|
|
public function widget($args, $instance): void {
|
|
if (!is_singular(['post', 'page', 'aksrt_forum_topic', 'aksrt_product'])) {
|
|
if (current_user_can('manage_options')) {
|
|
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
echo function_exists('aksrt_get_widget_null')
|
|
? aksrt_get_widget_null(__('目录树仅在文章/帖子/商品详情页显示', 'aksrt')) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
: '<p class="widget-empty">' . esc_html__('目录树仅在详情页显示', 'aksrt') . '</p>';
|
|
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
}
|
|
return;
|
|
}
|
|
|
|
$title = sanitize_text_field((string) ($instance['title'] ?? __('文章目录', 'aksrt')));
|
|
$mini_title = sanitize_text_field((string) ($instance['mini_title'] ?? ''));
|
|
$in_affix = !empty($instance['in_affix']);
|
|
|
|
$data_title = $title;
|
|
if ($mini_title !== '') {
|
|
$data_title .= ($data_title !== '' ? ' · ' : '') . $mini_title;
|
|
}
|
|
if ($data_title === '') {
|
|
$data_title = __('文章目录', 'aksrt');
|
|
}
|
|
|
|
// Mirror aksrt-single-render built-in TOC shell so page-template.js binds.
|
|
$classes = 'zib-widget toc-widget posts-nav-box';
|
|
if ($in_affix) {
|
|
$classes .= ' is-affix';
|
|
}
|
|
|
|
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
printf(
|
|
'<div class="%s"%s data-title="%s" aria-label="%s">',
|
|
esc_attr($classes),
|
|
$in_affix ? ' data-affix="true"' : '',
|
|
esc_attr($data_title),
|
|
esc_attr($data_title)
|
|
);
|
|
echo '<h3 class="widget-title toc-widget__title">' . esc_html($data_title) . '</h3>';
|
|
echo '<ol class="toc-list"></ol>';
|
|
echo '</div>';
|
|
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
}
|
|
|
|
public function form($instance): void {
|
|
$title = (string) ($instance['title'] ?? __('文章目录', 'aksrt'));
|
|
$mini_title = (string) ($instance['mini_title'] ?? '');
|
|
$in_affix = !empty($instance['in_affix']);
|
|
?>
|
|
<p>
|
|
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
|
|
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($title); ?>">
|
|
</p>
|
|
<p>
|
|
<label for="<?php echo esc_attr($this->get_field_id('mini_title')); ?>"><?php esc_html_e('副标题:', 'aksrt'); ?></label>
|
|
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('mini_title')); ?>" name="<?php echo esc_attr($this->get_field_name('mini_title')); ?>" value="<?php echo esc_attr($mini_title); ?>">
|
|
</p>
|
|
<p>
|
|
<label>
|
|
<input type="checkbox" id="<?php echo esc_attr($this->get_field_id('in_affix')); ?>" name="<?php echo esc_attr($this->get_field_name('in_affix')); ?>" value="1" <?php checked($in_affix); ?>>
|
|
<?php esc_html_e('侧栏随动(仅侧边栏)', 'aksrt'); ?>
|
|
</label>
|
|
</p>
|
|
<p class="description"><?php esc_html_e('请添加到文章/帖子侧栏。正文标题 ≥3 个时由前端填充目录;无标题则自动移除。', 'aksrt'); ?></p>
|
|
<?php
|
|
}
|
|
|
|
public function update($new_instance, $old_instance): array {
|
|
return [
|
|
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
|
'mini_title' => sanitize_text_field((string) ($new_instance['mini_title'] ?? '')),
|
|
'in_affix' => !empty($new_instance['in_affix']) ? 1 : 0,
|
|
];
|
|
}
|
|
}
|