diff --git a/includes/comments.php b/includes/comments.php index 1c94c69..96e63d1 100644 --- a/includes/comments.php +++ b/includes/comments.php @@ -20,9 +20,9 @@ require_once __DIR__ . "/../libs/comments.php"; $parameter = array( 'parentId' => $this->hidden ? 0 : $this->cid, - 'parentContent' => $this->row, + 'parentContent' => (Cuckoo_Comments_Archive::isTypechoVersion()) ? $this : $this->row, 'respondId' => $this->respondId, - 'commentPage' => $this->request->filter('int')->commentPage, + 'commentPage' => (Cuckoo_Comments_Archive::isTypechoVersion()) ? $this->parameter->commentPage : $this->request->filter('int')->commentPage, 'allowComment' => $this->allow('comment') ); $this->widget('Cuckoo_Comments_Archive', $parameter)->to($comments); diff --git a/libs/comments.php b/libs/comments.php index f3833b0..292cb46 100644 --- a/libs/comments.php +++ b/libs/comments.php @@ -14,37 +14,39 @@ * @date 2023-12-09 */ +use Typecho\Config; +use Typecho\Cookie; +use Typecho\Db; +use Typecho\Router; +use Typecho\Widget\Exception; +use Typecho\Widget\Helper\PageNavigator\Box; +use Utils\Helper; +use Widget\Comments\Archive; + if (!defined('__TYPECHO_ROOT_DIR__')) exit; // 基本按照 Typecho 评论组件而来 -/** - * 评论归档 - * - * @category typecho - * @package Widget - * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org) - * @license GNU General Public License 2.0 - * @version $Id$ - */ - /** * 评论归档组件 * + * 由于不继承 \Widget\Base\Comments 会导致 1.3.0 和 1.2.0 版本的 ___parentContent 方法返回类型不一致 (1.3.0 返回 \Widget\Base\Contents,1.3.0 以下返回数组) + * 继承又会因为基本都要覆写 (因为有大量 protected 方法,若需调用子类 private 变量则需要进行覆写) + * 没办法了,为了兼容新老版本只能继承 + 覆写了口牙! (这样就能不管 ___parentContent 方法到底给我了啥) + * * @category typecho * @package Widget * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org) * @license GNU General Public License 2.0 */ -class Cuckoo_Comments_Archive extends Widget_Abstract_Comments -{ +class Cuckoo_Comments_Archive extends Archive { /** * 当前页 * * @access private * @var integer */ - private $_currentPage; + private int $currentPage; /** * 所有文章个数 @@ -52,7 +54,7 @@ class Cuckoo_Comments_Archive extends Widget_Abstract_Comments * @access private * @var integer */ - private $_total = false; + private int $total = 0; /** * 子父级评论关系 @@ -60,29 +62,44 @@ class Cuckoo_Comments_Archive extends Widget_Abstract_Comments * @access private * @var array */ - private $_threadedComments = array(); + private array $threadedComments = []; /** * _singleCommentOptions * - * @var mixed + * @var Config|null * @access private */ - private $_singleCommentOptions = NULL; + private ?Config $singleCommentOptions = NULL; /** - * 构造函数,初始化组件 + * @param Config $parameter + */ + protected function initParameter(Config $parameter) { + $parameter->setDefault([ + 'parentId' => 0, + 'respondId' => '', + 'commentPage' => 0, + 'commentsNum' => 0, + 'allowComment' => 1, + 'parentContent' => null, + ]); + } + + /** + * 判断 Typecho 版本号 * * @access public - * @param mixed $request request对象 - * @param mixed $response response对象 - * @param mixed $params 参数列表 - * @return void + * @return bool 提取到的版本号是否大于或等于 1.3.0 */ - public function __construct($request, $response, $params = NULL) - { - parent::__construct($request, $response, $params); - $this->parameter->setDefault('parentId=0&commentPage=0&commentsNum=0&allowComment=1'); + public static function isTypechoVersion(): bool { + $version = "1.0.0"; + + if (preg_match('/Typecho\s+([\d.]+)/i', Helper::options()->generator, $matches)) { + $version = $matches[1]; + } + + return (bool)version_compare($version, '1.3.0', '>='); } /** @@ -90,12 +107,13 @@ public function __construct($request, $response, $params = NULL) * * @access private * @return void + * @throws Db\Exception */ - private function threadedCommentsCallback() - { - $singleCommentOptions = $this->_singleCommentOptions; + private function threadedCommentsCallback(): void { + $singleCommentOptions = $this->singleCommentOptions; if (function_exists('threadedComments')) { - return threadedComments($this, $singleCommentOptions); + threadedComments($this, $singleCommentOptions); + return; } $commentClass = ''; @@ -105,23 +123,21 @@ private function threadedCommentsCallback() } else { $commentClass .= ' comment-by-user'; } - } -?> -
+