-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
90 lines (83 loc) · 2.58 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
<?php
/**
* Visitor Statistics 访客量统计
*
* @package VisitorStatistics
* @author jiefei30
* @version 1.0.0
* @link https://makeyourchoice.cn
*/
class VisitorStatistics_Plugin implements Typecho_Plugin_Interface
{
protected static $pluginName = "VisitorStatistics";
/* 激活插件方法 */
public static function activate()
{
Typecho_Plugin::factory('Widget_Archive')->beforeRender = array('VisitorStatistics_Plugin', 'updateVisitors');
return 'The plugin is installed successfully, please enter the settings and set the initial value';
}
/* 禁用插件方法 */
public static function deactivate()
{
return 'Plugin uninstalled successfully';
}
/* 插件配置方法 */
public static function config(Typecho_Widget_Helper_Form $form)
{
$visitors = new Typecho_Widget_Helper_Form_Element_Text("visitors", null, '0', '访问量:', '网站初始访问量,默认为0');
$form->addInput($visitors);
}
/* 个人用户的配置方法 */
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{
// TODO: Implement personalConfig() method.
}
/**
* 更新访问量
* @throws Typecho_Db_Exception
* @throws Typecho_Exception
* @author jiefei30
*/
public static function updateVisitors()
{
if (Typecho_Widget::widget('Widget_Archive')->is('single')) {
$options = Helper::options();
$pluginConfig = $options->plugin(self::$pluginName);
$visitors = $pluginConfig->visitors;
$visitors++;
//更新浏览量
Helper::configPlugin(self::$pluginName, array('visitors' => $visitors));
}
}
/**
* print visitors' value
* usage: VisitorStatistics_Plugin::getVisitorStatistics();
* print: '12,628'
* usage: VisitorStatistics_Plugin::getVisitorStatistics('访问量','次');
* print:'访问量 XX 次'
*
* @param string $prefix
* @param string $suffix
* @throws Typecho_Db_Exception
* @author jiefei30
*/
public static function getVisitorStatistics($prefix = '', $suffix = '')
{
$options = Helper::options();
$pluginConfig = $options->plugin(self::$pluginName);
$visitors = $pluginConfig->visitors;
// 10000 --> 10,000
$arr = str_split($visitors . "");
$res = "";
$length = count($arr);
for ($i = 1; $i <= $length; $i++)
{
$res = $arr[$length - $i] . $res;
if ($i % 3 == 0 && $i != $length)
{
$res = "," . $res;
}
}
echo $prefix . $res . $suffix;
}
}