diff --git a/inc/theme-article.php b/inc/theme-article.php
index 2fd6496..8d7a875 100644
--- a/inc/theme-article.php
+++ b/inc/theme-article.php
@@ -553,46 +553,4 @@ function toc_replace_heading($content)
$toc[] = array('text' => trim(strip_tags($content[3])), 'depth' => $content[1], 'count' => $toc_count);
return "{$content[3]}";
-}
-
-function article_toc()
-{
- global $toc;
-
- $index = wp_cache_get(get_the_ID(), 'toc');
-
- if ($index === false && $toc) {
- $index = '
' . "\n";
- $prev_depth = '';
- $to_depth = 0;
- foreach ($toc as $toc_item) {
- $toc_depth = $toc_item['depth'];
- if ($prev_depth) {
- if ($toc_depth == $prev_depth) {
- $index .= '' . "\n";
- } elseif ($toc_depth > $prev_depth) {
- $to_depth++;
- $index .= '' . "\n";
- } else {
- $to_depth2 = $to_depth > $prev_depth - $toc_depth ? $prev_depth - $toc_depth : $to_depth;
- if ($to_depth2) {
- for ($i = 0; $i < $to_depth2; $i++) {
- $index .= '' . "\n" . '
' . "\n";
- $to_depth--;
- }
- }
- $index .= '';
- }
- }
- $index .= '- ' . str_replace(array('[h2title]', '[/h2title]'),array('', ''),$toc_item['text']) . '';
- $prev_depth = $toc_item['depth'];
- }
- for ($i = 0; $i <= $to_depth; $i++) {
- $index .= '
' . "\n" . '
' . "\n";
- }
- wp_cache_set(get_the_ID(), $index, 'toc', 360000);
- $index = '';
- }
-
- return $index;
}
\ No newline at end of file
diff --git a/inc/theme-options.php b/inc/theme-options.php
index fd3abfb..c37a767 100644
--- a/inc/theme-options.php
+++ b/inc/theme-options.php
@@ -3,7 +3,7 @@
* 主题选项
* @author Seaton Jiang
* @license MIT License
- * @version 2021.06.25
+ * @version 2021.06.26
*/
function getrobots()
@@ -597,14 +597,6 @@ function kratos_options()
'std' => '1',
);
- $options[] = array(
- 'name' => __('文章目录', 'kratos'),
- 'desc' => __('开启文章页面目录功能,将自动识别到 h3 的标题,并在侧边栏生成文章目录', 'kratos'),
- 'id' => 'g_post_toc',
- 'type' => 'checkbox',
- 'std' => '1',
- );
-
$options[] = array(
'name' => __('文章打赏', 'kratos'),
'desc' => __('开启文章页面打赏功能', 'kratos'),
diff --git a/inc/theme-widgets.php b/inc/theme-widgets.php
index 041eb58..7c1fcb9 100644
--- a/inc/theme-widgets.php
+++ b/inc/theme-widgets.php
@@ -535,6 +535,71 @@ class widget_comments extends WP_Widget
}
}
+class widget_toc extends WP_Widget
+{
+ public function __construct()
+ {
+ add_action('admin_enqueue_scripts', array($this, 'scripts'));
+
+ $widget_ops = array(
+ 'name' => __('文章目录', 'kratos'),
+ 'description' => __('仅在有目录规则的文章中显示目录的工具', 'kratos'),
+ );
+
+ parent::__construct(false, false, $widget_ops);
+ }
+
+ public function scripts()
+ {
+ wp_enqueue_script('media-upload');
+ wp_enqueue_media();
+ wp_enqueue_script('widget_scripts', ASSET_PATH . '/assets/js/widget.min.js', array('jquery'));
+ wp_enqueue_style('widget_css', ASSET_PATH . '/assets/css/widget.min.css', array());
+ }
+
+ public function widget($args, $instance)
+ {
+ global $toc;
+
+ $index = wp_cache_get(get_the_ID(), 'toc');
+
+ if ($index === false && $toc) {
+ $index = '' . "\n";
+ $prev_depth = '';
+ $to_depth = 0;
+ foreach ($toc as $toc_item) {
+ $toc_depth = $toc_item['depth'];
+ if ($prev_depth) {
+ if ($toc_depth == $prev_depth) {
+ $index .= '' . "\n";
+ } elseif ($toc_depth > $prev_depth) {
+ $to_depth++;
+ $index .= '' . "\n";
+ } else {
+ $to_depth2 = $to_depth > $prev_depth - $toc_depth ? $prev_depth - $toc_depth : $to_depth;
+ if ($to_depth2) {
+ for ($i = 0; $i < $to_depth2; $i++) {
+ $index .= '' . "\n" . '
' . "\n";
+ $to_depth--;
+ }
+ }
+ $index .= '';
+ }
+ }
+ $index .= '- ' . str_replace(array('[h2title]', '[/h2title]'),array('', ''),$toc_item['text']) . '';
+ $prev_depth = $toc_item['depth'];
+ }
+ for ($i = 0; $i <= $to_depth; $i++) {
+ $index .= '
' . "\n" . '
' . "\n";
+ }
+ wp_cache_set(get_the_ID(), $index, 'toc', 360000);
+ $index = '';
+ }
+
+ echo $index;
+ }
+}
+
function register_widgets()
{
register_widget('widget_ad');
@@ -543,5 +608,6 @@ function register_widgets()
register_widget('widget_search');
register_widget('widget_posts');
register_widget('widget_comments');
+ register_widget('widget_toc');
}
add_action('widgets_init', 'register_widgets');
diff --git a/single.php b/single.php
index ba5eece..5e533b7 100644
--- a/single.php
+++ b/single.php
@@ -146,7 +146,7 @@ $select_col = $col_array[kratos_option('g_article_widgets', 'two_side')];