Skip to content
Permalink
Browse files

Major refactoring of ProcessWire's FieldtypeComments classes and addi…

…tion of new CommentListCustom and CommentFormCustom classes for more markup control over comment lists and forms. Plus several new API methods and other upgrades for comments.
  • Loading branch information
Ryan Cramer
Ryan Cramer committed Mar 13, 2020
1 parent 63a588b commit 550fc26300372fe92635dc3d2a76114af7cc4d83
@@ -111,7 +111,7 @@ class Comment extends WireData {
/**
* Field this comment is for
*
* @var null|Field
* @var null|Field|CommentField
*
*/
protected $field = null;
@@ -423,7 +423,7 @@ public function getPage() {
/**
* Get Field that this Comment belongs to
*
* @return null|CommentField
* @return null|Field|CommentField
*
*/
public function getField() {
@@ -487,6 +487,7 @@ public function parent() {
*
*/
public function parents() {
if(!$this->parent_id) return $this->wire(new CommentArray());
$parents = $this->getPageComments()->makeNew();
$parent = $this->parent();
while($parent && $parent->id) {
@@ -507,7 +508,7 @@ public function children() {
// $comments = $this->getPageComments();
$page = $this->getPage();
$field = $this->getField();
$comments = $this->getPage()->get($field->name);
$comments = $page->get($field->name);
$children = $comments->makeNew();
if($page) $children->setPage($this->getPage());
if($field) $children->setField($this->getField());
@@ -519,6 +520,36 @@ public function children() {
return $children;
}

/**
* Return number of children (replies) for this comment
*
* ~~~~~
* $qty = $comment->numChildren([ 'minStatus' => Comment::statusApproved ]);
* ~~~~~
*
* @param array $options Limit return value by specific properties (below):
* - `status` (int): Specify Comment::status* constant to include only this status
* - `minStatus` (int): Specify Comment::status* constant to include only comments with at least this status
* - `maxStatus` (int): Specify Comment::status* constant or include only comments up to this status
* - `minCreated` (int): Minimum created unix timestamp
* - `maxCreated` (int): Maximum created unix timestamp
* - `stars` (int): Number of stars to match (1-5)
* - `minStars` (int): Minimum number of stars to match (1-5)
* - `maxStars` (int): Maximum number of stars to match (1-5)
* @return int
* @since 3.0.153
*
*/
public function numChildren(array $options = array()) {
$options['parent'] = $this->id;
$field = $this->getField();
if(!$field) return null;
/** @var FieldtypeComments $fieldtype */
$fieldtype = $field->type;
if(!$fieldtype) return 0;
return $fieldtype->getNumComments($this->getPage(), $field, $options);
}

/**
* Does this comment have the given child comment?
*
@@ -135,7 +135,8 @@ public function renderAll(array $options = array()) {
/**
* Return instance of CommentList object
*
* @param array $options See CommentList::$options for details
* @param array $options See CommentList::$options for details, plus:
* - `className` (string): PHP class name to use for CommentList (default='CommentList')
* @return CommentList
*
*/
@@ -148,14 +149,21 @@ public function getCommentList(array $options = array()) {
'useStars' => $field->get('useStars'),
'depth' => $field->get('depth'),
'dateFormat' => $field->get('dateFormat'),
'className' => 'CommentList',
);
} else {
$defaults = array(
'dateFormat' => 'relative'
'dateFormat' => 'relative',
'className' => 'CommentList',
);
}
$options = array_merge($defaults, $options);
return $this->wire(new CommentList($this, $options));
$file = __DIR__ . '/' . $options['className'] . '.php';
$className = wireClassName($options['className'], true);
if(!class_exists($className) && is_file($file)) include_once($file);
unset($options['className']);

return $this->wire(new $className($this, $options));
}

/**
@@ -167,8 +175,20 @@ public function getCommentList(array $options = array()) {
*
*/
public function getCommentForm(array $options = array()) {
if(!$this->page) throw new WireException("You must set a page to this CommentArray before using it i.e. \$ca->setPage(\$page)");
return $this->wire(new CommentForm($this->page, $this, $options));
$defaults = array(
'className' => 'CommentForm',
'depth' => ($this->field ? (int) $this->field->get('depth') : 0)
);
$options = array_merge($defaults, $options);
if(!$this->page) {
throw new WireException("You must set a page to this CommentArray before using it i.e. \$ca->setPage(\$page)");
}
$file = __DIR__ . '/' . $options['className'] . '.php';
$className = wireClassName($options['className'], true);
if(!class_exists($className) && is_file($file)) include_once($file);
unset($options['className']);

return $this->wire(new $className($this->page, $this, $options));
}

/**
@@ -184,7 +204,7 @@ public function setPage(Page $page) {
/**
* Set the Field that these comments are on
*
* @param Field $field
* @param CommentField|Field $field
*
*/
public function setField(Field $field) {
@@ -204,7 +224,7 @@ public function getPage() {
/**
* Get the Field that these comments are on
*
* @return Field
* @return CommentField|Field
*
*/
public function getField() {
@@ -55,6 +55,31 @@ public function count($selectorString) {
return $this->getFieldtype()->count($selectorString, $this);
}

/**
* Get number of comments for Page, optionally limited by specific $options
*
* Unlike the count() method this is focused on a specific Page and may be faster in
* cases where it matches what you need to count.
*
* @param Page $page
* @param array $options
* - `status` (int): Specify Comment::status* constant to include only this status
* - `minStatus` (int): Specify Comment::status* constant to include only comments with at least this status
* - `maxStatus` (int): Specify Comment::status* constant or include only comments up to this status
* - `parent` (int|Comment): Specify parent comment ID, 0 for root-only, or omit for no parent limitation
* - `minCreated` (int): Minimum created unix timestamp
* - `maxCreated` (int): Maximum created unix timestamp
* - `stars` (int): Number of stars to match (1-5)
* - `minStars` (int): Minimum number of stars to match (1-5)
* - `maxStars` (int): Maximum number of stars to match (1-5)
* @return int
* @since 3.0.153
*
*/
public function getNumComments(Page $page, array $options = array()) {
return $this->getFieldtype()->getNumComments($page, $this, $options);
}

/**
* Given a comment code or subcode, return the associated comment ID or 0 if it doesn't exist
*
@@ -79,6 +104,41 @@ public function getCommentByID($page, $id) {
return $this->getFieldtype()->getCommentByID($page, $this, $id);
}

/**
* Get parent comments for given Comment
*
* @param Page $page
* @param Comment $comment
* @return CommentArray
* @since 3.0.153
*
*/
public function getCommentParents(Page $page, Comment $comment) {
return $this->getFieldtype()->getCommentParents($page, $this, $comment);
}

/**
* Add new comment
*
* Requires a new Comment object with no id, that has all its required field populated and validated
* and is ready to add. Note that the `sort` property is assigned automatically if not specified in Comment.
*
* The primary reason to use this method is if you want to add a comment without loading all the other
* comments on a given Page.
*
* @param Page $page Page where comments field exists
* @param Comment $comment New comment to add
* @param bool $send Send comment for automatic approval filtering and email notifications?
* - `true` if comment was just submitted now from user input and filtering should apply, notifications sent, etc.
* - `false` if you are importing comments and NO filtering should be applied, NO notifications sent, etc.
* @return bool Returns true on success, false on fail
* @since 3.0.153
*
*/
public function addComment(Page $page, Comment $comment, $send) {
return $this->getFieldtype()->addComment($page, $this, $comment, $send);
}

/**
* Update specific properties for a comment
*

0 comments on commit 550fc26

Please sign in to comment.
You can’t perform that action at this time.