The Comment Class

Developers
Version 1.5.8+
7 min read

You are viewing the documentation for Meerkat version 1 for Statamic 2. The latest version is Meerkat 2 for Statamic 3.

Read v2 docs

While it is generally not advised to interact with Meerkat's underlying APIs and classes directly (check out the docs for the Addon API and Events if you would like to learn about extending Meerkat) we can make a special exception for the Statamic\Addons\Meerkat\Comments\Comment class.

This class is returned by many of the APIs methods (or as the items in a collection returned by API methods).

Methods

published()

The published() method can be used to determine if the comment was approved by an administrator using the Control Panel:

<?php

namespace Statamic\Addons\Awesome;

use Statamic\Extend\Tags;

public class AwesomeTags extends Tags
{

    public function index()
    {
        $comment = $this->api('Meerkat')->findOrFail('1443303351');

        if ($comment->published()) {
            // Hooray!
        }

        // You can also use the `approved()` method:
        if ($comment->approved()) {

        }
    }
}

getDate()

The getDate() method can be used to retrieve the date that the comment was submitted on. This method will return an instance of Carbon\Carbon (which is a very powerful date time abstraction for PHP, you are encouraged to read more about it here).

$comment = $this->api('Meerkat')->findOrFail('1443303351');

$date = $comment->getDate();

isSpam()

The isSpam() method can be used to determine if a comment was marked as spam or not:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

if ($comment->isSpam()) {
    // The comment is spam.
}

isReply()

The isReply() method can be used to determine if the comment is a reply to another comment:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

if ($comment->isReply()) {
    // This comment is a reply.
}

getParentID()

If the comment is a reply, you can use the getParentID() method to get the ID of the comment it was left as a reply to:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

if ($comment->isReply()) {
    $parentID = $comment->getParentID();
}

This method will return null if there is no parent comment.

getParent()

The getParent() method is similar to the getParentID() method, but, instead of returning the parent comment's ID, this method will return the Statamic\Addons\Meerkat\Comments\Comment instance for the parent comment:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

if ($comment->isReply()) {
    $parentComment = $comment->getParent();
}

This method will return null if there is no parent comment.

isRoot()

The isRoot() method can be used to determine if the method is the parent of another comment:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

if ($comment->isRoot()) {
    // I'm a root comment!
}

getReplies()

The getReplies() method will return an instance of Statamic\Addons\Meerkat\Comments\CommentCollection containing instances of Statamic\Addons\Meerkat\Comments\Comment for each reply to the current comment:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

$replies = $comment->getReplies();

hasReplies()

Now that you know about the getReplies() method, perhaps you want a simpler way to check if the comment has replies. This can be accomplished using the hasReplies() helper method:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

if ($comment->hasReplies()) {
    $replies = $comment->getReplies();
}

delete()

The delete() method will delete the comment, and any of it's replies:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

// Say goodbye, my friend.
$comment->delete();

Removing comments might seem simple, but there is a lot going on under the hood. You should check out the beforeDelete and removed sections of the Events documentation.

softDelete()

The softDelete() is similar to the delete() method, in that it will cause the comment to not appear in the response list. However, instead of removing the comment directly, it will set an is_deleted flag on the comment instead.

$comment = $this->api('Meerkat')->findOrFail('1443303351');

// 'Delete' the comment.
$comment->softDelete();

This method does not affect any of the replies to the comment, if any.

isDeleted()

The isDeleted method can be used to determine if a comment was previously soft deleted:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

if ($comment->isDeleted()) {
    // This comment was previously soft-deleted.
}

get()

The get() method can be used to retrieve information about the comment:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

// Get the email address of the comment.
$emailAddress = $comment->get('email');

You can also access comment data using PHP's array syntax like so:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

$emailAddress == $comment['email'];

getPath()

Like Statamic's content, Meerkat stores it's comments as flat-files. Wouldn't it be nice if we could quickly determine the path to the file? We can do that with the getPath() method:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

// Get the physical path to the comment.
$commentPath = $comment->getPath();

getRepliesPath()

Like the getPath() method, the getRepliesPath() method will return a path on the file-system. However, the getRepliesPath() method will return the path to the replies folder:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

// Replies path.
$repliesPath = $comment->getRepliesPath();

getOriginalMarkdown()

The content of each comment will automatically be converted from Markdown. However, it might be useful to get access to the original Markdown content; this is precisely what we can use the getOriginalMarkdown() method for:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

// Get the comment's original markdown.
$markdown = $comment->getOriginalMarkdown();

postedByAuthenticatedUser()

The postedByAuthenticatedUser() method can be used to determine if a the comment was left by a user who was signed into your site. It will return either true or false:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

if ($comment->postedByAuthenticatedUser()) {
    // A registered user left this comment.
}

user()

The user() method is similar to the postedByAuthenticatedUser() method, except it will return the user instance for the authenticated user. If the comment was not left by a registered user, or the user cannot be found, the method will return null:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

$user = $comment->user();

if ($user !== null) {
    // Comment was left by a registered user.
}

data()

The data() method will convert the comment data into an array, with the keys representing the data from the original comment:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

$data = $comment->toArray();

After the above code has executed, $data would contain similar data to the following example:

array:3 [
  "name"        => "John"
  "comment"     => "HTML comment content"
  "email"       => "user@example.org"
]

In addition to using the data() method, we can also access the comment's data using array syntax:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

// Get the comment author's name.
$name = $comment['name'];

save()

The save() method can be used to persist changes to the comment to disk. You may call the save() method after you've altered data like so:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

$comment['name'] = 'We changed it!';
$comment->save();

Only call the save() method if you've altered data using the array syntax.

getStreamName()

The getStreamName() method will return the name of the comment's stream:

$comment = $this->api('Meerkat')->findOrFail('1443303351');

$streamName = $comment->getStreamName();

This method will return an empty string if it cannot determine the stream name.