From 53f201e187cf8053b70ea51fb4cd936235b421a4 Mon Sep 17 00:00:00 2001 From: XIGE <710062962@qq.com> Date: Mon, 23 Feb 2026 20:00:41 +0800 Subject: [PATCH] 1.0 --- Plugin.php | 1471 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1471 insertions(+) create mode 100644 Plugin.php diff --git a/Plugin.php b/Plugin.php new file mode 100644 index 0000000..a3aca4c --- /dev/null +++ b/Plugin.php @@ -0,0 +1,1471 @@ +getPrefix(); + + $sql = "CREATE TABLE IF NOT EXISTS `{$prefix}thoughts` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `cid` int(10) unsigned NOT NULL COMMENT '文章ID', + `authorId` int(10) unsigned NOT NULL COMMENT '用户ID', + `created` int(10) unsigned DEFAULT 0 COMMENT '创建时间', + `text` text COMMENT '感想内容', + `status` varchar(16) DEFAULT 'approved' COMMENT '状态', + PRIMARY KEY (`id`), + KEY `cid` (`cid`), + KEY `authorId` (`authorId`), + KEY `created` (`created`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"; + + try { + $db->query($sql); + } catch (Exception $e) { + throw new Typecho_Plugin_Exception('创建感想表失败: ' . $e->getMessage()); + } + + // 挂载hook + Typecho_Plugin::factory('Widget_Feedback')->comment = array(__CLASS__, 'processComment'); + Typecho_Plugin::factory('Widget_Archive')->footer = array(__CLASS__, 'footer'); + Typecho_Plugin::factory('Widget_Archive')->header = array(__CLASS__, 'header'); + Typecho_Plugin::factory('Widget_Feedback')->content = array(__CLASS__, 'renderCheckbox'); + + return _t('感想插件已激活,请进行配置'); + } + + /** + * 禁用插件方法 + */ + public static function deactivate() + { + return _t('感想插件已禁用'); + } + + /** + * 插件配置方法 + */ + public static function config(Typecho_Widget_Helper_Form $form) + { + $label = new Typecho_Widget_Helper_Form_Element_Text( + 'label_text', + NULL, + '发布为感想', + _t('勾选框标签文字'), + _t('显示在勾选框旁边的文字') + ); + $form->addInput($label->addRule('required', _t('标签文字不能为空'))); + + // 添加前端卡片默认状态设置 + $default_state = new Typecho_Widget_Helper_Form_Element_Radio( + 'default_state', + array( + 'collapsed' => '默认收起', + 'expanded' => '默认展开' + ), + 'collapsed', + _t('感想卡片默认状态'), + _t('选择感想列表在前端的默认显示状态') + ); + $form->addInput($default_state); + + // 独立页面配置 + $page_per_page = new Typecho_Widget_Helper_Form_Element_Text( + 'page_per_page', + NULL, + '20', + _t('独立页面每页显示数量'), + _t('独立页面每页显示的感想数量') + ); + $form->addInput($page_per_page->addRule('isInteger', _t('请输入整数'))); + } + + /** + * 个人用户的配置方法 + */ + public static function personalConfig(Typecho_Widget_Helper_Form $form){} + + /** + * 处理评论提交 - 彻底拦截版 + */ + public static function processComment($comment, $post) + { + $request = Typecho_Request::getInstance(); + + // 检查是否勾选了发布为感想 + if ($request->isPost() && $request->get('thoughts') == '1') { + // 只有管理员可以发布感想 + $user = Typecho_Widget::widget('Widget_User'); + if ($user->hasLogin() && $user->pass('administrator', true)) { + + // 保存到感想表 + $db = Typecho_Db::get(); + $insert = $db->insert('table.thoughts') + ->rows(array( + 'cid' => $comment['cid'], + 'authorId' => $comment['authorId'], + 'created' => $comment['created'], + 'text' => $comment['text'], + 'status' => 'approved' + )); + + $insertId = $db->query($insert); + + // 关键:直接输出结果并终止,完全拦截Typecho的评论处理 + self::outputSuccessResponse($post); + } + } + + return $comment; + } + + /** + * 输出成功响应并终止 + */ + private static function outputSuccessResponse($post) + { + // 清空可能的输出缓冲区 + if (ob_get_level()) { + ob_end_clean(); + } + + // 设置正确的Content-Type + header('Content-Type: text/html; charset=utf-8'); + + // 构建返回URL + $returnUrl = $post->permalink . '#thoughts'; + + // 输出包含JavaScript的简单HTML页面 + echo ' + +
+ +感想发布成功,正在返回文章页面...
+如果页面没有自动跳转,请点击这里
+ + '; + + // 立即终止执行,防止Typecho继续处理 + exit; + } + + /** + * 在评论表单中渲染勾选框 + */ + public static function renderCheckbox($content, $class) + { + // 只在文章页面且是管理员显示 + if ($class->request->is('post') && self::isAdmin()) { + $options = Helper::options()->plugin('ThoughtsPlugin'); + $labelText = $options->label_text ?: '发布为感想'; + + $checkbox = '