From f3a5336c1abec3b3279536d9b5eadfe23b58a223 Mon Sep 17 00:00:00 2001 From: joyqi <59437+joyqi@users.noreply.github.com> Date: Thu, 25 Jan 2024 14:24:47 +0800 Subject: [PATCH] Remove comment link if status is not approved (#1712) * remove comment link if status is not approved * fix stack * fix: remove link in admin panel when comment status is not approved * fix: comments page nav * fix comment redirect url * fix comment anchor * fix waiting comment display * fix: convert waiting to unapproved --- var/Typecho/Widget/Helper/PageNavigator.php | 2 +- var/Widget/Base/Comments.php | 38 +++++++++++---- var/Widget/Comments/Admin.php | 12 +++++ var/Widget/Comments/Archive.php | 52 +++++++++------------ var/Widget/Feedback.php | 6 ++- 5 files changed, 70 insertions(+), 40 deletions(-) diff --git a/var/Typecho/Widget/Helper/PageNavigator.php b/var/Typecho/Widget/Helper/PageNavigator.php index 15855105..87e4ef63 100644 --- a/var/Typecho/Widget/Helper/PageNavigator.php +++ b/var/Typecho/Widget/Helper/PageNavigator.php @@ -110,6 +110,6 @@ abstract class PageNavigator */ public function render() { - throw new Exception(get_class($this) . ':' . __METHOD__, 500); + throw new Exception('Method Not Implemented', 500); } } diff --git a/var/Widget/Base/Comments.php b/var/Widget/Base/Comments.php index f38a6367..10a4948c 100644 --- a/var/Widget/Base/Comments.php +++ b/var/Widget/Base/Comments.php @@ -34,6 +34,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) { * @property string $type * @property string status * @property int $parent + * @property int $commentPage * @property Date $date * @property string $dateWord * @property string $theId @@ -61,6 +62,8 @@ class Comments extends Base implements QueryInterface, RowFilterInterface, Prima switch ($key) { case 'permalink': return $this->parentContent->path; + case 'commentPage': + return $this->commentPage; default: return '{' . $key . '}'; } @@ -396,14 +399,13 @@ class Comments extends Base implements QueryInterface, RowFilterInterface, Prima } /** - * 获取当前评论链接 + * 获取当前评论页码 * - * @return string - * @throws Exception + * @return int */ - protected function ___permalink(): string + protected function ___commentPage(): int { - if ($this->options->commentsPageBreak && 'approved' == $this->status) { + if ($this->options->commentsPageBreak) { $coid = $this->coid; $parent = $this->parent; @@ -420,7 +422,13 @@ class Comments extends Base implements QueryInterface, RowFilterInterface, Prima } $select = $this->db->select('coid', 'parent') - ->from('table.comments')->where('cid = ? AND status = ?', $this->cid, 'approved') + ->from('table.comments') + ->where( + 'cid = ? AND (status = ? OR coid = ?)', + $this->cid, + 'approved', + $this->status !== 'approved' ? $this->coid : 0 + ) ->where('coid ' . ('DESC' == $this->options->commentsOrder ? '>=' : '<=') . ' ?', $coid) ->order('coid'); @@ -441,12 +449,24 @@ class Comments extends Base implements QueryInterface, RowFilterInterface, Prima } } - $currentPage = ceil($total / $this->options->commentsPageSize); + return ceil($total / $this->options->commentsPageSize); + } - $pageRow = ['permalink' => $this->parentContent->path, 'commentPage' => $currentPage]; + return 0; + } + + /** + * 获取当前评论链接 + * + * @return string + * @throws Exception + */ + protected function ___permalink(): string + { + if ($this->options->commentsPageBreak) { return Router::url( 'comment_page', - $pageRow, + $this, $this->options->index ) . '#' . $this->theId; } diff --git a/var/Widget/Comments/Admin.php b/var/Widget/Comments/Admin.php index d7ee258d..a63a0b77 100644 --- a/var/Widget/Comments/Admin.php +++ b/var/Widget/Comments/Admin.php @@ -151,4 +151,16 @@ class Admin extends Comments $cid = $this->request->is('cid') ? $this->request->filter('int')->get('cid') : $this->cid; return From::allocWithAlias($cid, ['cid' => $cid]); } + + /** + * @return string + */ + protected function ___permalink(): string + { + if ('approved' === $this->status) { + return parent::___permalink(); + } + + return '#' . $this->theId; + } } diff --git a/var/Widget/Comments/Archive.php b/var/Widget/Comments/Archive.php index 2e2a8b65..e9c8b0ba 100644 --- a/var/Widget/Comments/Archive.php +++ b/var/Widget/Comments/Archive.php @@ -99,16 +99,13 @@ class Archive extends Comments return; } - $commentsAuthor = Cookie::get('__typecho_remember_author'); - $commentsMail = Cookie::get('__typecho_remember_mail'); - $select = $this->select()->where('table.comments.cid = ?', $this->parameter->parentId) + $unapprovedCommentId = intval(Cookie::get('__typecho_unapproved_comment', 0)); + $select = $this->select()->where('cid = ?', $this->parameter->parentId) ->where( - 'table.comments.status = ? OR (table.comments.author = ?' - . ' AND table.comments.mail = ? AND table.comments.status = ?)', + 'status = ? OR (coid = ? AND status <> ?)', 'approved', - $commentsAuthor, - $commentsMail, - 'waiting' + $unapprovedCommentId, + 'approved' ); if ($this->options->commentsShowCommentOnly) { @@ -176,12 +173,11 @@ class Archive extends Comments ($this->currentPage - 1) * $this->options->commentsPageSize, $this->options->commentsPageSize ); - - /** 评论置位 */ - $this->length = count($this->stack); - $this->row = $this->length > 0 ? current($this->stack) : []; } + /** 评论置位 */ + $this->length = count($this->stack); + $this->row = $this->length > 0 ? current($this->stack) : []; reset($this->stack); } @@ -241,7 +237,10 @@ class Archive extends Comments } $template = array_merge($default, $config); - $query = Router::url('comment_page', $this, $this->options->index); + $query = Router::url('comment_page', [ + 'permalink' => $this->parameter->parentContent->path, + 'commentPage' => '{commentPage}' + ], $this->options->index); self::pluginHandle()->trigger($hasNav)->call( 'pageNav', @@ -360,7 +359,7 @@ class Archive extends Comments $singleCommentOptions->afterDate(); ?> - status) { ?> + status) { ?> commentStatus(); ?> @@ -465,21 +464,6 @@ class Archive extends Comments } } - /** - * 获取当前评论链接 - * - * @return string - */ - protected function ___permalink(): string - { - if ($this->options->commentsPageBreak) { - $pageRow = ['permalink' => $this->parentContent->path, 'commentPage' => $this->currentPage]; - return Router::url('comment_page', $pageRow, $this->options->index) . '#' . $this->theId; - } - - return $this->parentContent->permalink . '#' . $this->theId; - } - /** * 子评论 * @@ -501,6 +485,16 @@ class Archive extends Comments return $this->levels > $this->options->commentsMaxNestingLevels - 2; } + /** + * 重载评论页码获取 + * + * @return int + */ + protected function ___commentPage(): int + { + return $this->currentPage; + } + /** * 重载内容获取 * diff --git a/var/Widget/Feedback.php b/var/Widget/Feedback.php index d3f77e2b..291a2ed6 100644 --- a/var/Widget/Feedback.php +++ b/var/Widget/Feedback.php @@ -270,7 +270,11 @@ class Feedback extends Comments implements ActionInterface /** 评论完成接口 */ self::pluginHandle()->call('finishComment', $this); - $this->response->goBack('#' . $this->theId); + if ($this->status !== 'approved') { + Cookie::set('__typecho_unapproved_comment', $commentId); + } + + $this->response->redirect($this->permalink); } /**