-
Notifications
You must be signed in to change notification settings - Fork 593
/
Plugin.php
140 lines (128 loc) · 4.27 KB
/
Plugin.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
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
<?php
/**
* Typecho 版 PostViews
*
* @package Views
* @author Willin Kan
* @version 1.0.1
* @update: 2011.05.29
* @link http://kan.willin.org/typecho/
*/
class Views_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Archive')->beforeRender = array('Views_Plugin', 'viewsCounter');
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
// contents 表中若无 views 字段则添加
if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents'))))
$db->query('ALTER TABLE `'. $prefix .'contents` ADD `views` INT(10) DEFAULT 0;');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form){}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 加入 beforeRender
*
* @access public
* @return void
*/
public static function viewsCounter()
{
// 访问计数
if (Typecho_Widget::widget('Widget_Archive')->is('single')) {
$db = Typecho_Db::get();
$cid = Typecho_Widget::widget('Widget_Archive')->cid;
$row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid));
$db->query($db->update('table.contents')->rows(array('views' => (int)$row['views']+1))->where('cid = ?', $cid));
}
}
/**
* 输出访问次数
*
* 语法: Views_Plugin::theViews();
* 输出: '访问: xx,xxx 次'
*
* 语法: Views_Plugin::theViews('有 ', ' 次点击');
* 输出: '有 xx,xxx 次点击'
*
* @access public
* @param string $before 前字串
* @param string $after 后字串
* @param bool $echo 是否显示 (0 用于运算,不显示)
* @return string
*/
public static function theViews($before = '访问: ', $after = ' 次', $echo = 1)
{
$db = Typecho_Db::get();
$cid = Typecho_Widget::widget('Widget_Archive')->cid;
$row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid));
if ($echo)
echo $before, number_format($row['views']), $after;
else
return $row['views'];
}
/**
* 输出最受欢迎文章
*
* 语法: Views_Plugin::theMostViewed();
*
* @access public
* @param int $limit 文章数目
* @param string $before 前字串
* @param string $after 后字串
* @return string
*/
public static function theMostViewed($limit = 10, $before = '<br/> - ( 访问: ', $after = ' 次 ) ')
{
$db = Typecho_Db::get();
$options = Typecho_Widget::widget('Widget_Options');
$limit = is_numeric($limit) ? $limit : 10;
$posts = $db->fetchAll($db->select()->from('table.contents')
->where('type = ? AND status = ? AND password IS NULL', 'post', 'publish')
->order('views', Typecho_Db::SORT_DESC)
->limit($limit)
);
if ($posts) {
foreach ($posts as $post) {
$result = Typecho_Widget::widget('Widget_Abstract_Contents')->push($post);
$post_views = number_format($result['views']);
$post_title = htmlspecialchars($result['title']);
$permalink = $result['permalink'];
echo "<li><a href='$permalink' title='$post_title'>$post_title</a><span style='font-size:70%'>$before $post_views $after</span></li>\n";
}
} else {
echo "<li>N/A</li>\n";
}
}
}