Browse Source

Popular Posts Widget

Adding popular posts widget.
pull/189/head
Danang Probo Sayekti 10 years ago
parent
commit
7c28567d1c
7 changed files with 236 additions and 29 deletions
  1. +39
    -0
      system/admin/views/popular-posts.html.php
  2. +70
    -4
      system/htmly.php
  3. +76
    -16
      system/includes/functions.php
  4. +18
    -0
      themes/blog/css/styles.css
  5. +24
    -6
      themes/blog/layout.html.php
  6. +3
    -3
      themes/logs/css/style.css
  7. +6
    -0
      themes/logs/layout.html.php

+ 39
- 0
system/admin/views/popular-posts.html.php 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!';
} ?>

+ 70
- 4
system/htmly.php View File

@ -352,8 +352,8 @@ post('/add/image', function () {
$message['error'] .= '<li>CSRF Token not correct.</li>'; $message['error'] .= '<li>CSRF Token not correct.</li>';
} }
config('views.root', 'system/admin/views'); 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(), 'description' => blog_description(),
'canonical' => site_url(), 'canonical' => site_url(),
'error' => '<ul>' . $message['error'] . '</ul>', 'error' => '<ul>' . $message['error'] . '</ul>',
@ -363,7 +363,7 @@ post('/add/image', function () {
'postUrl' => $url, 'postUrl' => $url,
'postContent' => $content, 'postContent' => $content,
'bodyclass' => 'addpost', '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'); config('views.root', 'system/admin/views');
render('add-video', array( render('add-video', array(
'title' => 'Add post- ' . blog_title(),
'title' => 'Add video - ' . blog_title(),
'description' => blog_description(), 'description' => blog_description(),
'canonical' => site_url(), 'canonical' => site_url(),
'error' => '<ul>' . $message['error'] . '</ul>', '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 // Show admin/mine
get('/admin/mine', function () { get('/admin/mine', function () {


+ 76
- 16
system/includes/functions.php View File

@ -138,12 +138,6 @@ function sortdate($a, $b)
return $a->date == $b->date ? 0 : ($a->date < $b->date) ? 1 : -1; 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 // Rebuilt cache index
function rebuilt_cache($type) function rebuilt_cache($type)
{ {
@ -776,16 +770,78 @@ function recent_posts($custom = null, $count = null)
return $posts; return $posts;
} else { } else {
$str = '<ul>';
echo '<ul>';
foreach ($posts as $post) { 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)) { 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 { } else {
return $tag_collection; 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) { if (strlen(strip_tags($string)) < $char) {
$string = remove_accent($string);
$string = preg_replace('/[^A-Za-z0-9 !@#$%^&*(),.-]/u', ' ', strip_tags($string)); $string = preg_replace('/[^A-Za-z0-9 !@#$%^&*(),.-]/u', ' ', strip_tags($string));
$string = preg_replace('/\s\s+/', ' ', $string);
$string = ltrim(rtrim($string)); $string = ltrim(rtrim($string));
return $string; return $string;
} else { } else {
$string = remove_accent($string);
$string = preg_replace('/[^A-Za-z0-9 !@#$%^&*(),.-]/u', ' ', strip_tags($string)); $string = preg_replace('/[^A-Za-z0-9 !@#$%^&*(),.-]/u', ' ', strip_tags($string));
$string = preg_replace('/\s\s+/', ' ', $string);
$string = ltrim(rtrim($string)); $string = ltrim(rtrim($string));
$string = substr($string, 0, $char); $string = substr($string, 0, $char);
$string = substr($string, 0, strrpos($string, ' ')); $string = substr($string, 0, strrpos($string, ' '));
@ -1893,6 +1952,7 @@ EOF;
echo '<li><a href="' . $base . 'admin">Admin</a></li>'; echo '<li><a href="' . $base . 'admin">Admin</a></li>';
if ($role === 'admin') { if ($role === 'admin') {
echo '<li><a href="' . $base . 'admin/posts">Posts</a></li>'; 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/mine">Mine</a></li>';
echo '<li><a href="' . $base . 'admin/draft">Draft</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 // Add page views count
function add_view($page) function add_view($page)
{ {
$filename = "cache/count.json";
$filename = "content/views.json";
$views = array(); $views = array();
if (file_exists($filename)) { if (file_exists($filename)) {
$views = json_decode(file_get_contents($filename), true); $views = json_decode(file_get_contents($filename), true);
@ -1969,7 +2029,7 @@ function get_views($page)
static $_views = array(); static $_views = array();
if (empty($_views)) { if (empty($_views)) {
$filename = "cache/count.json";
$filename = "content/views.json";
if (file_exists($filename)) { if (file_exists($filename)) {
$_views = json_decode(file_get_contents($filename), true); $_views = json_decode(file_get_contents($filename), true);
} }
@ -2056,4 +2116,4 @@ function shorten($string = null, $char = null)
return $string; return $string;
} }
}
}

+ 18
- 0
themes/blog/css/styles.css View File

@ -456,6 +456,24 @@ iframe {
margin-bottom: 0; margin-bottom: 0;
} }
/* popular-posts Section */
.popular-posts .item {
margin-bottom: 20px;
}
.popular-posts .item .title {
font-size: 16px;
line-height: 1.3;
}
.popular-posts .item .title a {
color: #778492;
}
.popular-posts .item .title a:hover {
color: #5f6b77;
}
.popular-posts .item:last-child {
margin-bottom: 0;
}
/* archive Section */ /* archive Section */
ul.archivegroup { ul.archivegroup {
margin: 0; margin: 0;


+ 24
- 6
themes/blog/layout.html.php View File

@ -115,6 +115,25 @@
<?php endforeach;?> <?php endforeach;?>
</div><!--//section-inner--> </div><!--//section-inner-->
</aside><!--//section--> </aside><!--//section-->
<?php if (config('views.counter') === 'true') :?>
<aside class="popular-posts aside section">
<div class="section-inner">
<h2 class="heading">Popular Blog Posts</h2>
<?php $lists = popular_posts(true);?>
<?php $char = 60;?>
<?php foreach ($lists as $l):?>
<?php if (strlen(strip_tags($l->title)) > $char) { $recentTitle = shorten($l->title, $char) . '...';} else {$recentTitle = $l->title;}?>
<div class="item">
<h3 class="title"><a href="<?php echo $l->url;?>"><?php echo $recentTitle;?></a></h3>
<div class="content">
<p><?php echo shorten($l->body, 75); ?>...</p>
<a class="more-link" href="<?php echo $l->url;?>"><i class="fa fa-link"></i> Read more</a>
</div><!--//content-->
</div>
<?php endforeach;?>
</div><!--//section-inner-->
</aside><!--//section-->
<?php endif;?>
<?php if (disqus()): ?> <?php if (disqus()): ?>
<aside class="comments aside section"> <aside class="comments aside section">
<div class="section-inner"> <div class="section-inner">
@ -139,11 +158,10 @@
<div class="section-inner"> <div class="section-inner">
<h2 class="heading">Tags</h2> <h2 class="heading">Tags</h2>
<div class="tag-cloud"> <div class="tag-cloud">
<?php $tags = tag_cloud(true); if(is_array($tags)) { ?>
<?php foreach ($tags as $tag => $count):?>
<a class="more-link" href="<?php echo site_url();?>tag/<?php echo $tag;?>"><?php echo $tag;?></a>
<?php endforeach;?>
<?php } ?>
<?php $tags = tag_cloud(true);?>
<?php foreach ($tags as $tag => $count):?>
<a class="more-link" href="<?php echo site_url();?>tag/<?php echo $tag;?>"><?php echo $tag;?></a>
<?php endforeach;?>
</div><!--//content--> </div><!--//content-->
</div><!--//section-inner--> </div><!--//section-inner-->
</aside><!--//section--> </aside><!--//section-->
@ -161,4 +179,4 @@
<script type="text/javascript" src="<?php echo site_url();?>themes/blog/js/bootstrap.min.js"></script> <script type="text/javascript" src="<?php echo site_url();?>themes/blog/js/bootstrap.min.js"></script>
<?php if (analytics()): ?><?php echo analytics() ?><?php endif; ?> <?php if (analytics()): ?><?php echo analytics() ?><?php endif; ?>
</body> </body>
</html>
</html>

+ 3
- 3
themes/logs/css/style.css View File

@ -608,15 +608,15 @@ ul li, ol li {
margin: 0; margin: 0;
} }
.about, .social, .archive, .comments, .tagcloud, .recent {
.about, .social, .archive, .comments, .tagcloud, .recent, .popular {
margin-bottom: 2em; margin-bottom: 2em;
} }
#sidebar .recent ul {
#sidebar .recent ul, #sidebar .popular ul {
margin: 0.25em 0 0.25em 1.3em; margin: 0.25em 0 0.25em 1.3em;
} }
#sidebar .recent li {
#sidebar .recent li, #sidebar .popular li {
list-style-type: circle; list-style-type: circle;
} }


+ 6
- 0
themes/logs/layout.html.php View File

@ -57,6 +57,12 @@
<h3>Recent Posts</h3> <h3>Recent Posts</h3>
<?php echo recent_posts() ?> <?php echo recent_posts() ?>
</div> </div>
<?php if(config('views.counter') === 'true') :?>
<div class="popular">
<h3>Popular Posts</h3>
<?php echo popular_posts() ?>
</div>
<?php endif;?>
<div class="archive"> <div class="archive">
<h3>Archive</h3> <h3>Archive</h3>
<?php echo archive_list() ?> <?php echo archive_list() ?>


Loading…
Cancel
Save