1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 08:17:12 +02:00

Improvements to Comment and CommentList class to support hookable modification of individual comments during rendering

This commit is contained in:
Ryan Cramer
2019-08-16 14:10:25 -04:00
parent 0392e9babf
commit 742ed9c479
3 changed files with 98 additions and 18 deletions

View File

@@ -784,7 +784,7 @@ class MarkupQA extends Wire {
if(file_exists($this->page->filesManager()->path() . basename($src))) {
// file exists, but we just don't know what it is - leave it alone
} else {
$this->error("Image file no longer exists: " . basename($src) . ")");
$this->error("Image file no longer exists: $src");
if($this->page->of()) $value = str_replace($img, '', $value);
$info['img_unresolved']++;
}
@@ -912,7 +912,7 @@ class MarkupQA extends Wire {
*
*/
public function error($text, $flags = 0) {
$logText = "$text (page={$this->page->path}, field={$this->field->name})";
$logText = "$text (field={$this->field->name}, id={$this->page->id}, path={$this->page->path})";
$this->wire('log')->save(self::errorLogName, $logText);
/*
if($this->wire('modules')->isInstalled('SystemNotifications')) {

View File

@@ -11,6 +11,7 @@
* @property int $id
* @property int $parent_id
* @property string $text
* @property string|null $textFormatted Text value formatted for output (runtime use only, must be set manually)
* @property int $sort
* @property int $status
* @property int|null $prevStatus
@@ -140,6 +141,12 @@ class Comment extends WireData {
*/
protected $pageComments = null;
/**
* @var string|null
*
*/
protected $textFormatted = null;
/**
* Construct a Comment and set defaults
*
@@ -197,6 +204,9 @@ class Comment extends WireData {
} else if($key == 'prevStatus') {
return $this->prevStatus;
} else if($key == 'textFormatted') {
return $this->textFormatted;
}
return parent::get($key);
@@ -216,6 +226,8 @@ class Comment extends WireData {
$value = $this->get($key);
if($key == 'text') {
if($this->textFormatted !== null) return $this->textFormatted;
$textformatters = null;
// $textformatters = $this->field ? $this->field->textformatters : null; // @todo
if(is_array($textformatters) && count($textformatters)) {
@@ -231,8 +243,6 @@ class Comment extends WireData {
$value = str_replace("\n", "<br />", $value);
}
} else if(in_array($key, array('cite', 'email', 'user_agent', 'website'))) {
$value = $this->wire('sanitizer')->entities(trim($value));
}
@@ -242,14 +252,27 @@ class Comment extends WireData {
public function set($key, $value) {
if(in_array($key, array('id', 'parent_id', 'status', 'flags', 'pages_id', 'created', 'created_users_id'))) $value = (int) $value;
else if($key == 'text') $value = $this->cleanCommentString($value);
else if($key == 'cite') $value = str_replace(array("\r", "\n", "\t"), ' ', substr(strip_tags($value), 0, 128));
else if($key == 'email') $value = $this->sanitizer->email($value);
else if($key == 'ip') $value = filter_var($value, FILTER_VALIDATE_IP);
else if($key == 'user_agent') $value = str_replace(array("\r", "\n", "\t"), ' ', substr(strip_tags($value), 0, 255));
else if($key == 'website') $value = $this->wire('sanitizer')->url($value, array('allowRelative' => false, 'allowQuerystring' => false));
else if($key == 'upvotes' || $key == 'downvotes') $value = (int) $value;
if(in_array($key, array('id', 'parent_id', 'status', 'flags', 'pages_id', 'created', 'created_users_id'))) {
$value = (int) $value;
} else if($key === 'text') {
$value = $this->cleanCommentString($value);
$this->textFormatted = null;
} else if($key === 'cite') {
$value = str_replace(array("\r", "\n", "\t"), ' ', substr(strip_tags($value), 0, 128));
} else if($key === 'email') {
$value = $this->sanitizer->email($value);
} else if($key === 'ip') {
$value = filter_var($value, FILTER_VALIDATE_IP);
} else if($key === 'user_agent') {
$value = str_replace(array("\r", "\n", "\t"), ' ', substr(strip_tags($value), 0, 255));
} else if($key === 'website') {
$value = $this->wire('sanitizer')->url($value, array('allowRelative' => false, 'allowQuerystring' => false));
} else if($key === 'upvotes' || $key === 'downvotes') {
$value = (int) $value;
} else if($key === 'textFormatted') {
$this->textFormatted = $value;
return $this;
}
// save the state so that modules can identify when a comment that was identified as spam
// is then set to not-spam, or when a misidentified 'approved' comment is actually spam

View File

@@ -38,7 +38,16 @@ class CommentList extends Wire implements CommentListInterface {
*/
protected $comments = null;
/**
* @var Page
*
*/
protected $page;
/**
* @var Field
*
*/
protected $field;
/**
@@ -61,7 +70,7 @@ class CommentList extends Wire implements CommentListInterface {
'downvoteFormat' => '&darr;{cnt}',
'depth' => 0,
'replyLabel' => 'Reply',
);
);
/**
* Construct the CommentList
@@ -94,6 +103,35 @@ class CommentList extends Wire implements CommentListInterface {
$this->options = array_merge($this->options, $options);
}
/**
* Get or set options
*
* @param string|null|array $key Use one of the following:
* - Omit to get array of all options
* - Specify option name to get (and omit $value argument)
* - Specify option name to set and provide a non-null $value argument
* - Specify array of one or more [ 'option' => 'value' ] to set and omit $value argument
* @param string|int|bool|null $value When setting an individual option, value should be specified here, otherwise omit
* @return array|string|int|bool|null When getting singe option, value is returned, otherwise array of all options is returned.
* @since 3.0.138
*
*/
public function options($key = null, $value = null) {
if($key === null) {
return $this->options;
} else if(is_array($key)) {
$this->options = array_merge($this->options, $key);
return $this->options;
} else if($value !== null) {
$this->options[$key] = $value;
return $this->options;
} else if(isset($this->options[$key])) {
return $this->options[$key];
} else {
return null;
}
}
/**
* Get replies to the given comment ID, or 0 for root level comments
*
@@ -212,6 +250,25 @@ class CommentList extends Wire implements CommentListInterface {
*
*/
public function renderItem(Comment $comment, $depth = 0) {
if($this->wire('hooks')->isHooked("CommentList::renderItem()")) {
return $this->__call('renderItem', array($comment, $depth));
} else {
return $this->___renderItem($comment, $depth);
}
}
/**
* Render the comment (hookable version)
*
* Hookable since 3.0.138
*
* @param Comment $comment
* @param int $depth Default=0
* @return string
* @see CommentArray::render()
*
*/
protected function ___renderItem(Comment $comment, $depth = 0) {
$text = $comment->getFormatted('text');
$cite = $comment->getFormatted('cite');