includes/posts.php
78 lines · 4301 bytes
1<?php
2// Post rendering shared by the dashboard, blog, and search pages.
3
4// Set up a function to check if variables are blank later (this function accepts 0, 0.0, and "0" as valid)
5function not_blank($value) {
6 return !empty($value) && isset($value) && $value !== '';
7}
8
9// The photo or video shown above a post's body.
10function post_media($post) {
11 if ($post->type == 'photo') {
12 return '<a href="' . $post->photos[0]->original_size->url . '"><img src="' . $post->photos[0]->original_size->url . '" class="card-img" alt="..."></a>';
13 }
14 if ($post->type == 'video') {
15 return '<video controls poster="'.$post->thumbnail_url.'" class="card-img"><source src="'.$post->video_url.'"></source></video>';
16 }
17 return '';
18}
19
20// A post's type-specific body.
21function post_content($post) {
22 switch ($post->type) {
23 case 'photo':
24 return not_blank($post->caption) ? '<p class="card-text post-caption">' . $post->caption . '</p>' : '';
25 case 'text':
26 $out = '';
27 if (not_blank($post->title)) {
28 $out .= '<h5 class="card-title post-title">' . $post->title . '</h5>';
29 }
30 if (not_blank($post->body)) {
31 $out .= '<p class="card-text post-body">' . $post->body . '</p>';
32 }
33 return $out;
34 case 'answer':
35 return '<p class="card-text"><b>' . $post->asking_name . '</b>: ' . $post->question . '</p><hr>' . $post->answer;
36 case 'quote':
37 return '<blockquote class="card-text post-quote">' . $post->summary . '<br><br><cite>' . $post->reblog->comment . '</cite></blockquote>';
38 case 'chat':
39 $out = '';
40 for ($i = 0; $i < count($post->dialogue); $i++) {
41 $out .= '<blockquote class="card-text post-chat-' . $i . '">' . (not_blank($post->dialogue[$i]->name) ? ('<b>' . $post->dialogue[$i]->name . '</b>: ') : "") . $post->dialogue[$i]->phrase . '</blockquote>';
42 }
43 return $out;
44 }
45 return '';
46}
47
48// The card's opening and header rows: the posting blog, where it was reblogged
49// from, and the root blog, with follow buttons for blogs not yet followed.
50function post_header($client, $post) {
51 $out = '<div class="card" data-type="' . $post->type . '" data-id="' . $post->id_string . '">';
52 $out .= '<div class="card-header d-flex justify-content-between"><div class="card-header-blog"><a href="' . $post->blog->url . '" target="_blank"><img class="avatar" src="' . $client->getBlogAvatar($post->blog_name, 32) . '"></a>';
53 $out .= '<a href="' . $post->blog->url . '">' . $post->blog_name . '</a>';
54 if (not_blank($post->reblogged_from_name)) {
55 $out .= '<i data-feather="repeat"></i><a href="' . $post->reblogged_from_url . '">' . $post->reblogged_from_name . '</a>';
56 if (!$post->reblogged_from_following) {
57 $out .= '<a href="javascript:void(0);" onclick="follow(\'' . $post->reblogged_from_name . '\', \'' . $post->reblogged_from_uuid . '\');" data-follow-id="' . $post->reblogged_from_uuid . '"><span class="badge badge-secondary ml-2">Follow</span></a>';
58 }
59 }
60 $out .= '</div>';
61
62 // Add 'follow user' button if not following blog
63 if ($post->followed) {
64 $out .= '</div>';
65 } else {
66 $out .= '<a href="javascript:void(0);" onclick="follow(\'' . $post->blog_name . '\', \'' . $post->blog->uuid . '\');" title="Follow" data-id="' . $post->blog->uuid . '"><i data-feather="user-plus"></i></a></div>';
67 }
68
69 // Add root blog (original poster)
70 $out .= '<div class="card-header d-flex justify-content-between"><div class="card-header-blog">';
71 $out .= '<a href="' . $post->reblogged_root_url . '" target="_blank"><img class="avatar" src="' . $client->getBlogAvatar($post->reblogged_root_name, 32) . '"></a>';
72 $out .= '<a href="' . $post->reblogged_root_url . '">' . $post->reblogged_root_name . '</a></div>';
73 if (!$post->reblogged_root_following && strpos($post->reblogged_root_name, 'deactivated') === false) {
74 $out .= '<a href="javascript:void(0);" onclick="follow(\'' . $post->reblogged_root_name . '\');" title="Follow" data-id="' . $post->reblogged_root_uuid . '"><i data-feather="user-plus"></i></a>';
75 }
76 $out .= '</div>';
77 return $out;
78}