-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
291 lines (250 loc) · 10.1 KB
/
Copy pathPlugin.php
File metadata and controls
291 lines (250 loc) · 10.1 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* Typecho 热门搜索插件
* 插件用于在 Typecho 站点前台展示热门搜索词。
* @package HotSearch
* @author Astrsource
* @version 1.0.0
* @link https://github.com/Astrsource/HotSearch-For-Typecho_Plugin/
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
class HotSearch_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件:创建数据表并挂载到 header 钩子
*/
public static function activate()
{
self::initTable();
Typecho_Plugin::factory('Widget_Archive')->header = array('HotSearch_Plugin', 'track');
return _t('热门搜索插件已激活,数据表已创建。请进入插件设置调整显示数量与模板。');
}
/**
* 禁用插件:根据设置决定是否删除数据表
*/
public static function deactivate()
{
$cfg = Helper::options()->plugin('HotSearch');
if ($cfg && $cfg->deleteOnDeactivate == '1') {
$db = Typecho_Db::get();
$table = $db->getPrefix() . 'search_log';
try {
$db->query("DROP TABLE IF EXISTS `" . $table . "`");
return _t('热门搜索插件已禁用,数据表已删除。');
} catch (Exception $e) {
return _t('热门搜索插件已禁用,但数据表删除失败:') . $e->getMessage();
}
}
return _t('热门搜索插件已禁用,数据表未删除。');
}
/**
* 插件配置面板
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$limit = new Typecho_Widget_Helper_Form_Element_Text(
'limit',
NULL,
'10',
_t('前台显示数量'),
_t('默认展示多少个热门搜索词')
);
$limit->input->setAttribute('class', 'mini');
$form->addInput($limit->addRule('isInteger', _t('请填写数字')));
$autoTrack = new Typecho_Widget_Helper_Form_Element_Radio(
'autoTrack',
array('1' => _t('开启'), '0' => _t('关闭')),
'1',
_t('自动记录搜索'),
_t('开启后插件会自动记录搜索关键词;若你的主题使用了自定义搜索逻辑导致无法自动记录,可关闭此项并在搜索模板中手动调用 HotSearch_Plugin::log($keyword)')
);
$form->addInput($autoTrack);
$deleteOnDeactivate = new Typecho_Widget_Helper_Form_Element_Radio(
'deleteOnDeactivate',
array('1' => _t('是'), '0' => _t('否')),
'0',
_t('禁用插件时删除数据表'),
_t('开启后,禁用插件时会自动删除搜索记录数据表。<br><strong style="color:#c00;">警告:开启后禁用插件将永久删除所有搜索记录数据,无法恢复!</strong>')
);
$form->addInput($deleteOnDeactivate);
// 外层容器模板
$wrapperTemplate = new Typecho_Widget_Helper_Form_Element_Textarea(
'wrapperTemplate',
NULL,
'<ul class="hot-search-list">{items}</ul>',
_t('外层容器模板'),
_t('可用占位符:<code>{items}</code> — 将被替换为渲染后的列表项内容。留空则默认使用 <ul> 包裹。')
);
$form->addInput($wrapperTemplate);
// 单项模板
$itemTemplate = new Typecho_Widget_Helper_Form_Element_Textarea(
'itemTemplate',
NULL,
'<li><a href="{url}" title="{keyword}">{keyword}<span class="hot-search-count">({count})</span></a></li>',
_t('单项模板'),
_t('可用占位符:<br><code>{keyword}</code> — 搜索词<br><code>{count}</code> — 搜索次数<br><code>{url}</code> — 搜索链接<br><code>{index}</code> — 当前序号(从1开始)')
);
$form->addInput($itemTemplate);
// 空数据模板
$emptyTemplate = new Typecho_Widget_Helper_Form_Element_Textarea(
'emptyTemplate',
NULL,
'<p class="hot-search-empty">暂无热门搜索</p>',
_t('空数据模板'),
_t('当没有任何搜索记录时显示的 HTML。可用占位符:无。留空则不输出任何内容。')
);
$form->addInput($emptyTemplate);
}
/**
* 个人用户配置(无需使用)
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form) {}
/**
* 初始化数据库表
*/
private static function initTable()
{
$db = Typecho_Db::get();
$table = $db->getPrefix() . 'search_log';
$sql = "CREATE TABLE IF NOT EXISTS `" . $table . "` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`keyword` varchar(255) NOT NULL DEFAULT '',
`count` int(10) unsigned NOT NULL DEFAULT '1',
`lastsearch` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `keyword` (`keyword`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
$db->query($sql);
}
/**
* 搜索行为追踪(挂载于 Widget_Archive->header)
*/
public static function track($header = '', $archive = null)
{
static $tracked = false;
if ($tracked) {
return $header;
}
$cfg = Helper::options()->plugin('HotSearch');
if ($cfg && $cfg->autoTrack == '0') {
return $header;
}
$isSearch = false;
$keyword = '';
if ($archive && method_exists($archive, 'is') && $archive->is('search')) {
$isSearch = true;
$request = Typecho_Request::getInstance();
$keyword = $request->get('s', '');
if (empty($keyword)) {
$pathInfo = trim($request->getPathInfo(), '/');
if (strpos($pathInfo, 'search/') === 0) {
$keyword = urldecode(substr($pathInfo, 7));
}
}
} else {
$request = Typecho_Request::getInstance();
$keyword = $request->get('s', '');
$pathInfo = trim($request->getPathInfo(), '/');
if (!empty($keyword)) {
$isSearch = true;
} else if (strpos($pathInfo, 'search/') === 0) {
$isSearch = true;
$keyword = urldecode(substr($pathInfo, 7));
}
}
if ($isSearch && !empty($keyword)) {
self::log($keyword);
$tracked = true;
}
return $header;
}
/**
* 公共方法:手动记录搜索词
*/
public static function log($keyword)
{
$keyword = trim($keyword);
if (empty($keyword) || strlen($keyword) > 255) {
return;
}
$keyword = str_replace(array('\\', "\0", "'", '"', '<', '>'), '', $keyword);
$db = Typecho_Db::get();
$table = $db->getPrefix() . 'search_log';
$row = $db->fetchRow(
$db->select()->from($table)->where('keyword = ?', $keyword)
);
if ($row) {
$db->query(
$db->update($table)->rows(array(
'count' => $row['count'] + 1,
'lastsearch' => time()
))->where('id = ?', $row['id'])
);
} else {
$db->query(
$db->insert($table)->rows(array(
'keyword' => $keyword,
'count' => 1,
'lastsearch' => time()
))
);
}
}
/**
* 获取热门搜索列表
*/
public static function getHotSearch($limit = null)
{
if (is_null($limit)) {
$cfg = Helper::options()->plugin('HotSearch');
$limit = ($cfg && $cfg->limit) ? intval($cfg->limit) : 10;
}
$db = Typecho_Db::get();
$table = $db->getPrefix() . 'search_log';
return $db->fetchAll(
$db->select()->from($table)
->order('count', Typecho_Db::SORT_DESC)
->order('lastsearch', Typecho_Db::SORT_DESC)
->limit($limit)
);
}
/**
* 直接输出热门搜索 HTML(支持后台模板占位符)
*/
public static function render($limit = null)
{
$records = self::getHotSearch($limit);
$cfg = Helper::options()->plugin('HotSearch');
// 获取模板配置,若留空则使用默认值
$wrapperTemplate = ($cfg && trim($cfg->wrapperTemplate)) ? $cfg->wrapperTemplate : '<ul class="hot-search-list">{items}</ul>';
$itemTemplate = ($cfg && trim($cfg->itemTemplate)) ? $cfg->itemTemplate : '<li><a href="{url}" title="{keyword}">{keyword}<span class="hot-search-count">({count})</span></a></li>';
$emptyTemplate = ($cfg && trim($cfg->emptyTemplate)) ? $cfg->emptyTemplate : '<p class="hot-search-empty">暂无热门搜索</p>';
// 空数据输出
if (empty($records)) {
if (!empty($emptyTemplate)) {
echo $emptyTemplate;
}
return;
}
// 渲染单项
$itemsHtml = '';
$index = 0;
foreach ($records as $item) {
$index++;
$url = Typecho_Router::url(
'search',
array('keywords' => urlencode($item['keyword'])),
Helper::options()->index
);
$replacements = array(
'{keyword}' => htmlspecialchars($item['keyword']),
'{count}' => intval($item['count']),
'{url}' => $url,
'{index}' => $index
);
$itemsHtml .= strtr($itemTemplate, $replacements);
}
// 渲染外层容器
echo strtr($wrapperTemplate, array('{items}' => $itemsHtml));
}
}