Popular Posts Widget

Adding popular posts widget.
This commit is contained in:
Danang Probo Sayekti 2015-07-30 07:50:46 +07:00
commit 7c28567d1c
7 changed files with 236 additions and 29 deletions

View file

@ -0,0 +1,39 @@
<h2 class="post-index"><?php echo $heading ?></h2>
<?php if (!empty($posts)) { ?>
<table class="post-list">
<tr class="head">
<th>Title</th>
<th>Published</th><?php if (config("views.counter") == "true"): ?>
<th>Views</th><?php endif; ?>
<th>Author</th>
<th>Tag</th>
<th>Operations</th>
</tr>
<?php $i = 0;
$len = count($posts); ?>
<?php foreach ($posts as $p): ?>
<?php
if ($i == 0) {
$class = 'item first';
} elseif ($i == $len - 1) {
$class = 'item last';
} else {
$class = 'item';
}
$i++;
?>
<tr class="<?php echo $class ?>">
<td><a target="_blank" href="<?php echo $p->url ?>"><?php echo $p->title ?></a></td>
<td><?php echo date('d F Y', $p->date) ?></td>
<?php if (config("views.counter") == "true"): ?>
<td><?php echo $p->views ?></td><?php endif; ?>
<td><a target="_blank" href="<?php echo $p->authorUrl ?>"><?php echo $p->author ?></a></td>
<td><?php echo $p->tag ?></td>
<td><a href="<?php echo $p->url ?>/edit?destination=admin/posts">Edit</a> <a
href="<?php echo $p->url ?>/delete?destination=admin/posts">Delete</a></td>
</tr>
<?php endforeach; ?>
</table>
<?php } else {
echo 'No posts found!';
} ?>

View file

@ -352,8 +352,8 @@ post('/add/image', function () {
$message['error'] .= '<li>CSRF Token not correct.</li>';
}
config('views.root', 'system/admin/views');
render('add-post', array(
'title' => 'Add post- ' . blog_title(),
render('add-image', array(
'title' => 'Add image - ' . blog_title(),
'description' => blog_description(),
'canonical' => site_url(),
'error' => '<ul>' . $message['error'] . '</ul>',
@ -363,7 +363,7 @@ post('/add/image', function () {
'postUrl' => $url,
'postContent' => $content,
'bodyclass' => 'addpost',
'breadcrumb' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; Add post'
'breadcrumb' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; Add image'
));
}
});
@ -427,7 +427,7 @@ post('/add/video', function () {
}
config('views.root', 'system/admin/views');
render('add-video', array(
'title' => 'Add post- ' . blog_title(),
'title' => 'Add video - ' . blog_title(),
'description' => blog_description(),
'canonical' => site_url(),
'error' => '<ul>' . $message['error'] . '</ul>',
@ -792,6 +792,72 @@ get('/admin/posts', function () {
}
});
// Show admin/popular
get('/admin/popular', function () {
$user = $_SESSION[config("site.url")]['user'];
$role = user('role', $user);
if (login()) {
config('views.root', 'system/admin/views');
if ($role === 'admin') {
config('views.root', 'system/admin/views');
$page = from($_GET, 'page');
$page = $page ? (int)$page : 1;
$perpage = 20;
$posts = popular_posts(true,$perpage);
$total = '';
if (empty($posts) || $page < 1) {
// a non-existing page
render('no-posts', array(
'title' => 'Popular posts - ' . blog_title(),
'description' => blog_description(),
'canonical' => site_url(),
'bodyclass' => 'noposts',
));
die;
}
$tl = blog_tagline();
if ($tl) {
$tagline = ' - ' . $tl;
} else {
$tagline = '';
}
render('popular-posts', array(
'title' => 'Popular posts - ' . blog_title(),
'description' => blog_description(),
'canonical' => site_url(),
'heading' => 'Popular posts',
'page' => $page,
'posts' => $posts,
'bodyclass' => 'popular-posts',
'breadcrumb' => '',
'pagination' => has_pagination($total, $perpage, $page)
));
} else {
render('denied', array(
'title' => 'Popular posts - ' . blog_title(),
'description' => blog_description(),
'canonical' => site_url(),
'bodyclass' => 'denied',
'breadcrumb' => '',
));
}
} else {
$login = site_url() . 'login';
header("location: $login");
}
});
// Show admin/mine
get('/admin/mine', function () {

View file

@ -138,12 +138,6 @@ function sortdate($a, $b)
return $a->date == $b->date ? 0 : ($a->date < $b->date) ? 1 : -1;
}
// usort function. Sort by views.
function sortviews($a, $b)
{
return $a[$page] == $b[$page] ? 0 : ($a[$page] < $b[$page]) ? 1 : -1;
}
// Rebuilt cache index
function rebuilt_cache($type)
{
@ -776,16 +770,78 @@ function recent_posts($custom = null, $count = null)
return $posts;
} else {
$str = '<ul>';
echo '<ul>';
foreach ($posts as $post) {
$str .= '<li><a href="' . $post->url . '">' . $post->title . '</a></li>';
echo '<li><a href="' . $post->url . '">' . $post->title . '</a></li>';
}
if (empty($posts)) {
$str .= '<li>No recent posts found</li>';
echo '<li>No recent posts found</li>';
}
$str .= '</ul>';
return $str;
echo '</ul>';
}
}
// Return popular posts lists
function popular_posts($custom = null, $count = null)
{
static $_views = array();
$tmp = array();
if (empty($count)) {
$count = config('popular.count');
if (empty($count)) {
$count = 5;
}
}
if (config('views.counter') == 'true') {
if (empty($_views)) {
$filename = 'content/views.json';
if (file_exists($filename)) {
$_views = json_decode(file_get_contents($filename), true);
if(is_array($_views)) {
arsort($_views);
foreach ($_views as $key => $val) {
if (file_exists($key)) {
if (strpos($key, 'blog') !== false) {
$tmp[] = pathinfo($key);
}
}
}
$posts = get_posts($tmp, 1, $count);
if (empty($custom)) {
echo '<ul>';
foreach ($posts as $post) {
echo '<li><a href="' . $post->url . '">' . $post->title . '</a></li>';
}
echo '</ul>';
}
else {
return $posts;
}
} else {
if(empty($custom)) {
echo '<ul><li>No popular posts found</li></ul>';
} else {
echo '<ul><li>No popular posts found</li></ul>';
return $tmp;
}
}
} else {
if (empty($custom)) {
echo '<ul><li>No popular posts found</li></ul>';
} else {
return $tmp;
}
}
}
} else {
if (empty($custom)) {
echo '<ul><li>No popular posts found</li></ul>';
} else {
return $tmp;
}
}
}
@ -895,6 +951,9 @@ function tag_cloud($custom = null)
} else {
return $tag_collection;
}
} else {
if(empty($custom)) return;
return $tags;
}
}
@ -945,13 +1004,13 @@ function get_description($string, $char = null)
}
}
if (strlen(strip_tags($string)) < $char) {
$string = remove_accent($string);
$string = preg_replace('/[^A-Za-z0-9 !@#$%^&*(),.-]/u', ' ', strip_tags($string));
$string = preg_replace('/\s\s+/', ' ', $string);
$string = ltrim(rtrim($string));
return $string;
} else {
$string = remove_accent($string);
$string = preg_replace('/[^A-Za-z0-9 !@#$%^&*(),.-]/u', ' ', strip_tags($string));
$string = preg_replace('/\s\s+/', ' ', $string);
$string = ltrim(rtrim($string));
$string = substr($string, 0, $char);
$string = substr($string, 0, strrpos($string, ' '));
@ -1893,6 +1952,7 @@ EOF;
echo '<li><a href="' . $base . 'admin">Admin</a></li>';
if ($role === 'admin') {
echo '<li><a href="' . $base . 'admin/posts">Posts</a></li>';
echo '<li><a href="' . $base . 'admin/popular">Popular</a></li>';
}
echo '<li><a href="' . $base . 'admin/mine">Mine</a></li>';
echo '<li><a href="' . $base . 'admin/draft">Draft</a></li>';
@ -1950,7 +2010,7 @@ function is_csrf_proper($csrf_token)
// Add page views count
function add_view($page)
{
$filename = "cache/count.json";
$filename = "content/views.json";
$views = array();
if (file_exists($filename)) {
$views = json_decode(file_get_contents($filename), true);
@ -1969,7 +2029,7 @@ function get_views($page)
static $_views = array();
if (empty($_views)) {
$filename = "cache/count.json";
$filename = "content/views.json";
if (file_exists($filename)) {
$_views = json_decode(file_get_contents($filename), true);
}
@ -2056,4 +2116,4 @@ function shorten($string = null, $char = null)
return $string;
}
}
}