想要一个个性化签名插件,于是参照了一下官方开发文档

本来想通过接口来进行编写,结果实在没看懂header.php中的$this->options->description()该怎么通过接口触发修改
然后发现官方文档中提到

1
Typecho_Plugin::factory('Widget_Archive')->___charactersNum = array('MyPlugin', 'charactersNum');

通过_来注入内部方法,然后在模板中直接通过
1
<>?php $this->charactersNum(); ?>

进行调用
下面附上代码,插件编写的并不完整,后台数据库手动处理的
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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* RandDesc
*
* @package RandDesc
* @author svz
* @version 1.0
* @link http://blog.svz777.top
*/
class RandDesc_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Archive')->___desc = array('RandDesc_Plugin', 'desc');
$msg = RandDesc_Plugin::install();
return _t($msg);
}

/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){
$config = Typecho_Widget::widget('Widget_Options')->plugin('RandDesc');
$isDrop = $config->isDrop;
if ($isDrop == 0) {
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$db->query("DROP TABLE `" . $prefix . "RandDesc`", Typecho_Db::WRITE);
}
}

/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
//TODO 加入后台添加签名功能
$isDrop = new Typecho_Widget_Helper_Form_Element_Radio(
'isDrop', array(
'0' => '删除',
'1' => '不删除',
), '1', '删除数据表:', '请选择是否在禁用插件时,删除日志数据表');
$form->addInput($isDrop);
}

/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}

/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function desc(){
$db = Typecho_Db::get();
//TODO 自动从数据库获取签名数量
$rand=rand(0,141);
$query= $db->select('description')->from('table.RandDesc')->where('id = ?',$rand);
echo $db->fetchRow($query)["description"];
}

public static function install(){
$db = Typecho_Db::get();
$type = explode('_', $db->getAdapterName());
$type = array_pop($type);
$prefix = $db->getPrefix();
$scripts = "CREATE TABLE `typecho_RandDesc` (
`id` int(10) unsigned NOT NULL auto_increment,
`description` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MYISAM DEFAULT CHARSET=%charset%;";
$scripts = str_replace('typecho_', $prefix, $scripts);
$scripts = str_replace('%charset%', 'utf8', $scripts);
$scripts = explode(';', $scripts);
try {
foreach ($scripts as $script) {
$script = trim($script);
if ($script) {
$db->query($script, Typecho_Db::WRITE);
}
}
return '成功创建数据表,插件启用成功';
} catch (Typecho_Db_Exception $e) {
$code = $e->getCode();
if (('Mysql' == $type && $code == (1050 || '42S01'))) {
$script = 'SELECT * from `' . $prefix . 'access`';
$db->query($script, Typecho_Db::READ);
return '数据表已存在,插件启用成功';
} else {
throw new Typecho_Plugin_Exception('数据表建立失败,插件启用失败。错误号:' . $code);
}
}
}
}


然后在header.php模板中将$this->options->description()改为$this->desc()即可