-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper.php
More file actions
executable file
·195 lines (173 loc) · 6 KB
/
Copy pathHelper.php
File metadata and controls
executable file
·195 lines (173 loc) · 6 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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* RecentViewsCounter 插件辅助函数
* 用于在主题中方便地调用插件功能
*/
class RecentViewsCounter_Helper
{
/**
* 获取当前文章的月度访问量
* 在文章页面中调用
*/
public static function getCurrentMonthlyViews()
{
$widget = Typecho_Widget::widget('Widget_Archive');
if ($widget->is('single')) {
return RecentViewsCounter_Plugin::getMonthlyViews($widget->cid);
}
return 0;
}
/**
* 显示当前文章的月度访问量
* 在主题中直接输出
*/
public static function showCurrentMonthlyViews($format = '本月访问: %d 次')
{
$views = self::getCurrentMonthlyViews();
printf($format, $views);
}
/**
* 获取热门文章列表
* 用于在侧边栏或其他位置显示热门文章
*/
public static function getPopularPosts($limit = 5)
{
return RecentViewsCounter_Plugin::getTopArticlesThisMonth($limit);
}
/**
* 显示热门文章列表的HTML
*/
public static function showPopularPosts($limit = 5, $title = '本月热门文章')
{
$articles = self::getPopularPosts($limit);
$options = Typecho_Widget::widget('Widget_Options');
if (!empty($articles)) {
echo '<div class="popular-posts">';
if ($title) {
echo '<h3>' . htmlspecialchars($title) . '</h3>';
}
echo '<ul>';
foreach ($articles as $article) {
$url = Typecho_Common::url($article['slug'], $options->index);
echo '<li>';
echo '<a href="' . $url . '" title="访问量: ' . $article['visit_count'] . '">';
echo htmlspecialchars($article['title']);
echo '</a>';
echo '<span class="views-count">(' . $article['visit_count'] . ')</span>';
echo '</li>';
}
echo '</ul>';
echo '</div>';
}
}
/**
* 获取指定文章的月度访问量
*/
public static function getPostMonthlyViews($cid)
{
return RecentViewsCounter_Plugin::getMonthlyViews($cid);
}
/**
* 在文章列表中显示月度访问量
*/
public static function showPostMonthlyViews($cid, $format = '%d')
{
$views = self::getPostMonthlyViews($cid);
printf($format, $views);
}
/**
* 获取网站总体统计信息
*/
public static function getSiteStats()
{
$db = Typecho_Db::get();
$one_month_ago = time() - (30 * 24 * 3600);
$today_start = strtotime(date('Y-m-d 00:00:00'));
// 本月总访问量
try {
$monthly_total = $db->fetchRow($db->select('COUNT(*) as count')
->from('table.views_records')
->where('visit_time > ?', $one_month_ago));
} catch (Exception $e) {
$monthly_total = array('count' => 0);
}
// 本月独立访客
try {
$monthly_unique = $db->fetchRow($db->select('COUNT(DISTINCT ip) as count')
->from('table.views_records')
->where('visit_time > ?', $one_month_ago));
} catch (Exception $e) {
$monthly_unique = array('count' => 0);
}
// 今日访问量
try {
$today_total = $db->fetchRow($db->select('COUNT(*) as count')
->from('table.views_records')
->where('visit_time > ?', $today_start));
} catch (Exception $e) {
$today_total = array('count' => 0);
}
return array(
'monthly_total' => $monthly_total ? intval($monthly_total['count']) : 0,
'monthly_unique' => $monthly_unique ? intval($monthly_unique['count']) : 0,
'today_total' => $today_total ? intval($today_total['count']) : 0
);
}
/**
* 显示网站统计信息
*/
public static function showSiteStats($template = null)
{
$stats = self::getSiteStats();
if ($template) {
printf($template, $stats['monthly_total'], $stats['monthly_unique'], $stats['today_total']);
} else {
echo '<div class="site-stats">';
echo '<span>本月访问: ' . $stats['monthly_total'] . '</span> | ';
echo '<span>独立访客: ' . $stats['monthly_unique'] . '</span> | ';
echo '<span>今日访问: ' . $stats['today_total'] . '</span>';
echo '</div>';
}
}
/**
* 检查插件是否已激活
*/
public static function isPluginActive()
{
$plugins = Typecho_Plugin::export();
return isset($plugins['activated']['RecentViewsCounter']);
}
}
/**
* 便捷函数,用于在主题中快速调用
*/
// 显示当前文章月度访问量
function showMonthlyViews($format = '本月访问: %d 次') {
if (RecentViewsCounter_Helper::isPluginActive()) {
RecentViewsCounter_Helper::showCurrentMonthlyViews($format);
}
}
// 显示热门文章
function showPopularPosts($limit = 5, $title = '本月热门文章') {
if (RecentViewsCounter_Helper::isPluginActive()) {
RecentViewsCounter_Helper::showPopularPosts($limit, $title);
}
}
// 显示网站统计
function showSiteStats($template = null) {
if (RecentViewsCounter_Helper::isPluginActive()) {
RecentViewsCounter_Helper::showSiteStats($template);
}
}
// 获取文章月度访问量
function getMonthlyViews($cid = null) {
if (RecentViewsCounter_Helper::isPluginActive()) {
if ($cid) {
return RecentViewsCounter_Helper::getPostMonthlyViews($cid);
} else {
return RecentViewsCounter_Helper::getCurrentMonthlyViews();
}
}
return 0;
}