From ebe352d5ccb74a90ae39506fc98fc03f747ff035 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Thu, 14 May 2020 15:11:11 -0400 Subject: [PATCH] Attempt fix for processwire/processwire-issues#1165 --- .../FieldtypeComments/CommentList.php | 23 +++++++++++++++++-- .../FieldtypeComments/CommentListCustom.php | 1 + 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/wire/modules/Fieldtype/FieldtypeComments/CommentList.php b/wire/modules/Fieldtype/FieldtypeComments/CommentList.php index 65fd42af..753c2ddf 100644 --- a/wire/modules/Fieldtype/FieldtypeComments/CommentList.php +++ b/wire/modules/Fieldtype/FieldtypeComments/CommentList.php @@ -60,6 +60,12 @@ class CommentList extends Wire implements CommentListInterface { */ protected $field; + /** + * @var bool|null + * + */ + protected $editable = null; + /** * Default options that may be overridden from constructor * @@ -175,7 +181,6 @@ class CommentList extends Wire implements CommentListInterface { public function getReplies($commentID) { if(is_object($commentID)) $commentID = $commentID->id; $commentID = (int) $commentID; - $admin = $this->options['admin']; $replies = array(); $comments = $this->comments; if($commentID && $comments->data('selectors') && $this->comment && $this->comment->id == $commentID) { @@ -188,7 +193,7 @@ class CommentList extends Wire implements CommentListInterface { } foreach($comments as $c) { if($c->parent_id != $commentID) continue; - if(!$admin && $c->status < Comment::statusApproved) continue; + if(!$this->allowRenderItem($c)) continue; $replies[] = $c; } $this->numReplies[$commentID] = count($replies); @@ -261,6 +266,7 @@ class CommentList extends Wire implements CommentListInterface { if(!count($comments)) return $out; foreach($comments as $comment) { + if(!$this->allowRenderItem($comment)) continue; $this->comment = $comment; $out .= $this->renderItem($comment, array('depth' => $depth)); } @@ -334,6 +340,19 @@ class CommentList extends Wire implements CommentListInterface { return $out; } + + /** + * Allow comment to be rendered in list? + * + * @param Comment $comment + * @return bool + * + */ + protected function allowRenderItem(Comment $comment) { + if($this->editable === null) $this->editable = $this->options['admin'] && $this->page->editable(); + if($this->editable || $comment->status >= Comment::statusApproved) return true; + return false; + } /** * Render the comment diff --git a/wire/modules/Fieldtype/FieldtypeComments/CommentListCustom.php b/wire/modules/Fieldtype/FieldtypeComments/CommentListCustom.php index 43c4a7d3..c053edfb 100644 --- a/wire/modules/Fieldtype/FieldtypeComments/CommentListCustom.php +++ b/wire/modules/Fieldtype/FieldtypeComments/CommentListCustom.php @@ -131,6 +131,7 @@ class CommentListCustom extends CommentList { $out = $parent_id ? '' : $this->renderCheckActions(); $comments = $this->options['depth'] > 0 ? $this->getReplies($parent_id) : $this->comments; foreach($comments as $comment) { + if(!$this->allowRenderItem($comment)) continue; $this->comment = $comment; $out .= $this->renderItem($comment, array('depth' => $depth)); }