The Post model represents an individual post/message in a topic. It extends DataModel and provides properties and methods for managing post content, permissions, and user interactions.
Location
app/Models/Post/Post.php
Properties
ID of the user who created the post
Username of the poster (stored for guests)
Email of the poster (for guests)
Raw post content (BBCode or markdown)
Timestamp when post was created
Timestamp of last edit (0 if never edited)
ID of user who last edited the post
Whether to hide smilies (0=show, 1=hide)
Computed Properties
Parent topic object. Returns null if topic is invalid or moved.
User object for the post author. Returns guest user if poster_id < 1.
Permission Properties
Returns true if current user can report this post
Returns true if current user can delete this post
Returns true if current user can edit this post
Returns true if current user can quote this post (same as canReply on parent topic)
Returns true if current user can react to this post
Link Properties
URL to edit author and date (admin/moderator only)
URL to view IP/host information (admin only)
URL to mark/unmark post as solution
Methods
html()
Returns the parsed HTML content of the post.
public function html(): string
Returns: HTML-formatted, censored post content
Example:
$post = $container->posts->load(123);
echo $post->html();
reactionData()
Generates reaction form data for this post.
public function reactionData(bool $allow = true): ?array
Whether to allow reactions
Returns: Array of reaction data or null
Example:
$post = $container->posts->load(123);
$reactions = $post->reactionData();
if ($reactions) {
// Display reaction options
foreach ($reactions['available'] as $reaction) {
echo "<button>{$reaction['emoji']}</button>";
}
}
Usage Example
// Load a post
$post = $container->posts->load(456);
// Display post content
echo "<div class='post' id='p{$post->id}'>";
// Display author info
$author = $post->user;
echo "<div class='author'>";
if (!$author->isGuest && $author->avatar) {
echo "<img src=\"{$author->avatar}\" alt='Avatar'>";
}
echo "<strong>{$author->username}</strong><br>";
echo $author->title();
echo "</div>";
// Display post message
echo "<div class='message'>";
echo $post->html();
echo "</div>";
// Display post metadata
echo "<div class='meta'>";
echo "Posted: " . date('Y-m-d H:i:s', $post->posted);
if ($post->edited > 0) {
echo " (Edited: " . date('Y-m-d H:i:s', $post->edited) . ")";
}
echo "</div>";
// Display action buttons
echo "<div class='actions'>";
if ($post->canEdit) {
echo "<a href=\"{$post->linkEdit}\">Edit</a> ";
}
if ($post->canDelete) {
echo "<a href=\"{$post->linkDelete}\">Delete</a> ";
}
if ($post->canQuote) {
echo "<a href=\"{$post->linkQuote}\">Quote</a> ";
}
if ($post->canReport) {
echo "<a href=\"{$post->linkReport}\">Report</a> ";
}
// Admin/moderator actions
if ($container->user->isAdmMod) {
echo "<a href=\"{$post->linkGetHost}\">IP: {$post->poster_ip}</a> ";
echo "<a href=\"{$post->linkAnD}\">Edit Author/Date</a> ";
}
echo "</div>";
// Display reactions if enabled
if ($post->useReaction) {
$reactions = $post->reactionData();
if ($reactions) {
echo "<div class='reactions'>";
// Render reaction interface
echo "</div>";
}
}
echo "</div>";
Post Editing Example
// Load post and check edit permission
$post = $container->posts->load(456);
if (!$post->canEdit) {
throw new Exception('You cannot edit this post');
}
// Update post content
$post->message = 'Updated post content with [b]BBCode[/b]';
$post->edited = time();
$post->editor_id = $container->user->id;
// Save changes
$container->posts->update($post);
// Update topic table of contents if needed
if ($post->parent->poster_id === $post->poster_id) {
// Parse the message to extract headings
$container->Parser->parseMessage($post->message);
$post->parent->addPostToToc($post);
$container->topics->update($post->parent);
}
Permission Checking Example
$post = $container->posts->load(456);
$currentUser = $container->user;
// Check various permissions
if ($post->canEdit) {
echo "You can edit this post\n";
}
if ($post->canDelete) {
echo "You can delete this post\n";
}
if ($currentUser->isAdmin) {
// Admins can always edit/delete
echo "Admin can see IP: {$post->poster_ip}\n";
}
if ($currentUser->isModerator($post)) {
// Moderators have special permissions
echo "You moderate this post's forum\n";
}
// Check if user is the author
if ($post->poster_id === $currentUser->id) {
echo "You are the author of this post\n";
// Check edit time limits
$editInterval = $currentUser->g_deledit_interval;
if ($editInterval > 0 && time() - $post->posted > $editInterval) {
echo "Edit time limit expired\n";
}
}