forked from adamfairholm/PyroCMS-Related-Posts-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
related_posts.php
99 lines (88 loc) · 2.33 KB
/
related_posts.php
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
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Related Posts
*
* Fetches posts that are tagged with keyword(s) specified.
*
* @package PyroCMS
* @author Rick Mills (WDWFans.com) <rick.sketchy@gmail.com>
* @copyright Copyright (c) 2012 Rick Mills
*
*/
class Plugin_Related_Posts extends Plugin
{
/**
* Posts
*
* Usage:
* {{ related_posts:posts keywords="keyword1,keyword2,keyword3,etc" maximum_posts="5" maximum_length="100" }}
*
* @return string (related posts are returned as part of a list and should be wrapped with 'ul' or 'ol' tags.
*/
function posts()
{
$keywords = $this->attribute('keywords');
$maxposts = $this->attribute('maximum_posts');
$maxlength = $this->attribute('maximum_length');
$keywordArray = explode(",", $keywords);
$this->db->select('id');
$this->db->from('keywords');
$this->db->where_in('name', $keywordArray);
$result = $this->db->get();
$keywordIds = array();
foreach($result->result() as $r)
{
$keywordIds[] = $r->id;
}
if($result->num_rows() > 0)
{
$this->db->select('*');
$this->db->from('keywords_applied');
$this->db->where_in('keyword_id', $keywordIds);
$applied = $this->db->get();
if(!empty($applied))
{
$hash_store = array();
foreach($applied->result() as $a)
{
$hash_store[] = $a->hash;
}
if(!empty($hash_store))
{
// time to search the blog posts!
$this->db->select('*');
$this->db->from('blog');
$this->db->where_in('keywords', $hash_store);
$this->db->limit($maxposts);
$this->db->order_by('created_on', 'desc');
$postQuery = $this->db->get();
$postString = '';
foreach($postQuery->result() as $post)
{
$postString .='<li>'.anchor('blog/'.date('Y/m', $post->created_on) .'/'.$post->slug, $this->neat_trim($post->title, $maxlength)).' - '.date('M jS Y', $post->created_on).'</li>';
}
return $postString;
}
}
}
else
{
$postString = '<li>No recent news available!</li>';
return $postString;
}
}
function neat_trim($str, $n, $delim='...')
{
$len = strlen($str);
if ($len > $n)
{
preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches);
return rtrim($matches[1]) . $delim;
}
else
{
return $str;
}
}
}
/* End of file related_posts.php */