-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblog.php
More file actions
260 lines (231 loc) · 10.9 KB
/
blog.php
File metadata and controls
260 lines (231 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
require_once 'includes/dbh.inc.php';
require_once 'includes/cache-manager.inc.php';
// Set session cookie parameters to make it accessible across all directories
session_set_cookie_params(0, '/');
session_start();
// Pagination settings
$postsPerPage = 10;
$currentPage = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$offset = ($currentPage - 1) * $postsPerPage;
try {
// Check for search query
$searchTerm = $_GET['q'] ?? '';
if (!empty($searchTerm)) {
// Use the search handler instead of regular post query
require_once 'includes/search-handler.inc.php';
$searchResults = getSearchResults($pdo, $searchTerm, $currentPage, $postsPerPage);
$posts = $searchResults['posts'];
$totalPosts = $searchResults['total'];
$totalPages = $searchResults['pages'];
} else {
// Original post query code
$stmt = $pdo->query("SELECT COUNT(*) FROM Posts WHERE Status = 'published'");
$totalPosts = $stmt->fetchColumn();
$totalPages = ceil($totalPosts / $postsPerPage);
// Get posts for current page
$stmt = $pdo->prepare("
SELECT
p.PostID,
p.Title,
p.CreatedAt,
p.FeaturedImage,
p.FeaturedImageCaption,
LEFT(p.Content, 300) as Excerpt,
u.Username as AuthorName,
GROUP_CONCAT(DISTINCT c.Name) as Categories
FROM Posts p
LEFT JOIN Users u ON p.UserID = u.UserID
LEFT JOIN PostCategories pc ON p.PostID = pc.PostID
LEFT JOIN Categories c ON pc.CategoryID = c.CategoryID
WHERE p.Status = 'published'
GROUP BY p.PostID
ORDER BY p.CreatedAt DESC
LIMIT ? OFFSET ?
");
$stmt->execute([$postsPerPage, $offset]);
$posts = $stmt->fetchAll();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog</title>
<link rel="stylesheet" href="/css/blog-styles.css">
<link rel="stylesheet" href="/style.css">
</head>
<body>
<?php
include_once 'header.php';
?>
<script src="/js/breadcrumbs.js"></script>
<div class="blog-header-container">
<div class="blog-header">
<h1>Blog Posts</h1>
</div>
</div>
<div class="blog-container">
<!-- Blog Posts -->
<div class="posts-grid">
<?php foreach ($posts as $post):
// Create URL-friendly slug from title
$slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $post['Title'])));
?>
<article class="post-card">
<?php if ($post['FeaturedImage']): ?>
<a href="/blog/post/<?php echo $post['PostID']; ?>/<?php echo $slug; ?>" class="post-thumbnail-link">
<img src="<?php echo htmlspecialchars($post['FeaturedImage']); ?>"
alt="<?php echo htmlspecialchars($post['Title']); ?>"
class="post-thumbnail">
</a>
<?php endif; ?>
<div class="post-content">
<h2>
<a href="/blog/post/<?php echo $post['PostID']; ?>/<?php echo $slug; ?>">
<?php echo htmlspecialchars($post['Title']); ?>
</a>
</h2>
<div class="post-meta">
<span class="post-date">
<?php echo date('F j, Y', strtotime($post['CreatedAt'])); ?>
</span>
<span class="post-author">
by <?php echo htmlspecialchars($post['AuthorName']); ?>
</span>
<?php if ($post['Categories']): ?>
<span class="post-categories">
<?php
$categories = explode(',', $post['Categories']);
foreach ($categories as $index => $category):
$categorySlug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $category)));
?>
<a class="article-category-tag" href="/category/<?php echo $categorySlug; ?>">
<?php echo htmlspecialchars(trim($category)); ?>
</a><?php echo $index < count($categories) - 1 ? ' ' : ''; ?>
<?php endforeach; ?>
</span>
<?php endif; ?>
</div>
<div class="post-excerpt">
<?php
// Clean the excerpt and add ellipsis
$content = $post['Content'] ?? ''; // Use null coalescing operator to handle missing Content
if (!empty($content)) {
// Add spaces around block-level elements
$content = preg_replace('/<\/?(?:p|div|h[1-6]|ul|ol|li|blockquote|pre|table|tr|th|td)[^>]*>/i', ' $0 ', $content);
// Replace <br>, <br/>, and <br /> tags with a space
$content = preg_replace('/<br\s*\/?>/i', ' ', $content);
// Remove all HTML tags
$content = strip_tags($content);
// Replace multiple spaces (including newlines and tabs) with a single space
$content = preg_replace('/\s+/', ' ', $content);
// Trim whitespace
$content = trim($content);
// Check if the excerpt ends with a complete sentence
$endsWithSentence = preg_match('/[.!?]\s*$/', $content);
// Check if the last word is complete (not cut off)
$lastChar = substr($content, -1);
$endsWithCompleteWord = preg_match('/\s/', $lastChar) || preg_match('/[.!?,]/', $lastChar);
// Truncate to 300 characters and add ellipsis if needed
if (strlen($content) > 300) {
$content = substr($content, 0, 300);
// Find the last complete word
$pos = strrpos($content, ' ');
if ($pos !== false) {
$content = substr($content, 0, $pos) . '...';
} else {
$content .= '...';
}
}
// Add ellipsis if the excerpt doesn't end naturally
elseif (!$endsWithSentence || !$endsWithCompleteWord) {
$content .= '...';
}
echo $content;
}
?>
</div>
<a href="/blog/post/<?php echo $post['PostID']; ?>/<?php echo $slug; ?>" class="read-more">
Read More
</a>
</div>
</article>
<?php endforeach; ?>
</div>
<!-- Sidebar -->
<div class="blog-sidebar">
<!-- Search -->
<div class="blog-search-container">
<input type="text" id="search-bar" placeholder="Search...">
</div>
<!-- Categories -->
<div class="blog-categories">
<h2>Categories</h2>
<ul class="blog-categories-list">
<?php
require_once 'includes/category-functions.inc.php';
$categories = getCategoriesWithPostCount($pdo);
foreach ($categories as $category):
$slug = htmlspecialchars($category['Slug']);
$name = htmlspecialchars($category['Name']);
$count = (int)$category['post_count'];
?>
<li class="blog-category">
<a class="category-link" href="/category/<?php echo $slug; ?>">
<span class="category-name"><?php echo $name; ?></span> <span class="category-count"><?php echo $count; ?></span>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<!-- Ad Box -->
<div class="sidebar-sponsor-container">
<img src="/img/sponsor_spot.png" alt="Ads support us so much!">
</div>
</div>
</div>
<?php if ($totalPages > 1): ?>
<nav class="pagination">
<?php if ($currentPage > 1): ?>
<a href="/blog/page/<?php echo ($currentPage - 1); ?>" class="page-link">
Previous
</a>
<?php endif; ?>
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
<?php if (
$i == 1 ||
$i == $totalPages ||
($i >= $currentPage - 2 && $i <= $currentPage + 2)
): ?>
<a href="/blog/page/<?php echo $i; ?>"
class="page-link <?php echo $i === $currentPage ? 'active' : ''; ?>">
<?php echo $i; ?>
</a>
<?php elseif (
$i == $currentPage - 3 ||
$i == $currentPage + 3
): ?>
<span class="page-ellipsis">...</span>
<?php endif; ?>
<?php endfor; ?>
<?php if ($currentPage < $totalPages): ?>
<a href="/blog/page/<?php echo ($currentPage + 1); ?>" class="page-link">
Next
</a>
<?php endif; ?>
</nav>
<?php endif; ?>
<?php
include_once 'footer.php';
?>
<script src="/js/search.js"></script>
</body>
</html>
<?php
} catch (PDOException $e) {
error_log("Error in blog.php: " . $e->getMessage());
echo "An error occurred while loading the blog posts.";
}
?>