From caf69a6413d8e44425c0db4ae8c8f30938c37238 Mon Sep 17 00:00:00 2001 From: Seaton Jiang Date: Sat, 26 Jun 2021 10:12:43 +0800 Subject: [PATCH] feat: add toc widgets (#378) --- inc/theme-article.php | 42 --------------------------- inc/theme-options.php | 10 +------ inc/theme-widgets.php | 66 +++++++++++++++++++++++++++++++++++++++++++ single.php | 2 +- 4 files changed, 68 insertions(+), 52 deletions(-) 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"; - } - wp_cache_set(get_the_ID(), $index, 'toc', 360000); - $index = '
' . "\n" . '
文章目录
' . "\n" . '
' . $index . '
' . "\n" . '
'; - } - - 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"; + } + wp_cache_set(get_the_ID(), $index, 'toc', 360000); + $index = '
' . "\n" . '
文章目录
' . "\n" . '
' . $index . '
' . "\n" . '
'; + } + + 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')];