Discuz! 插件开发手册
匿名 · 更新于 2014/4/16
Discuz! 插件开发手册
2009/9/24 By Monkey
| 插件设计 |
|
您在开始论坛插件的设计之前,有必要了解一下我们所推荐的插件设计方式,更好的规范性和兼容性,将使得您设计的插件受到更多使用者的欢迎,对于程序员而言,也有助于形成良好的编码习惯,实现自身能力的提升。如果您有意编写 Discuz! 论坛插件,请按照先后顺序仔细阅读本文档。 |
| 准备工作 |
|
插件实现流程
|
<p> </p>
<p style="word-break: break-all; margin: 0px; padding: 10px 0px; ">pluginvars 表:</p>
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1333px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
pluginvarid 插件配置的惟一 ID,自动地增
pluginid 本项配置所隶属的插件 ID
displayorder 本项配置的显示顺序,数值低的排在前面
title 插件配置的名称
description 插件配置的简介
variable 插件配置的变量名
type 插件配置的类型
value 插件配置的值
extra 当本项配置为“选择(select)”时,可选的取值范围
<p> </p>
<p style="word-break: break-all; margin: 0px; padding: 10px 0px; ">如果您使用自行编写的插件后台管理模块进行插件参数配置,请尽量将配置项目按照 pluginid 的对应关系,将参数存储于 pluginvars 表中,这样系统就可以自动将您增加的配置参数缓存起来,以供插件程序进行调用。</p>
<ul style="word-break: break-all; margin: 0px 0px 0px 12px; padding-right: 0px; padding-bottom: 14px; padding-left: 0px; color: rgb(102, 102, 102); ">
</ul>
<p class="subtitle" style="word-break: break-all; margin: 0px; padding: 5px 0px; font-size: 14px; font-weight: bold; color: rgb(0, 0, 0); ">插件参数读取</p>
<p style="word-break: break-all; margin: 0px; padding: 10px 0px; ">了解了 Discuz! 插件存储的数据结构后,您可以在插件程序中根据需要选择合适的数据读取方式。由于数据库读取方式可以由数据结构推断而来,因此这里只介绍缓存读取的方式,这种方式是我们强烈推荐的插件数据读取方式。</p>
<p style="word-break: break-all; margin: 0px; padding: 10px 0px; ">在管理者配置好插件信息,或用户进行插件的参数设置之后,系统将根据插件设置的惟一标识符,自动生成一个插件数据的缓存文件,例如惟一标识符为 comsenz_virtual_bank,则缓存文件位于 ./forumdata/cache/plugin_comsenz_virtual_bank.php,您可以打开此文件查看其中的数据内容和格式。缓存采用数组的方式进行存储,引用此文件即可将所需的插件参数一次性赋值。</p>
<p style="word-break: break-all; margin: 0px; padding: 10px 0px; ">其中,$_DPLUGIN['comsenz_virtual_bank'] 这个数组下标,为插件的惟一标识符,所有插件缓存数据,一经被引用,就会赋值到 $_DPLUGIN 这个多维数组中。modules 描述了这个插件的模块信息;vars 描述了这个插件的配置变量,前面为变量名,后面为使用者赋予这个变量的值。</p>
<ul style="word-break: break-all; margin: 0px 0px 0px 12px; padding-right: 0px; padding-bottom: 14px; padding-left: 0px; color: rgb(102, 102, 102); ">
</ul>
</td>
</tr>
<tr style="word-break: break-all; ">
<td class="title" style="word-break: break-all; color: rgb(255, 255, 255); line-height: 21px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 5px 5px 1em; background-image: url(file:///C:/Documents%20and%20Settings/Administrator/%E6%A1%8C%E9%9D%A2/PluginDevloper_Guide/forumbox_head.gif); background-color: rgb(63, 166, 207); background-position: 0px 0px; background-repeat: repeat no-repeat; ">页面嵌入模块开发</td>
</tr>
<tr style="word-break: break-all; ">
<td style="word-break: break-all; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<p style="word-break: break-all; margin: 0px; padding: 10px 0px; "> </p>
<ul style="word-break: break-all; margin: 0px 0px 0px 12px; padding-right: 0px; padding-bottom: 14px; padding-left: 0px; color: rgb(102, 102, 102); ">
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">页面嵌入类型脚本格式
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1311px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
<?php
class plugin_identifier {
function <font color="blue" style="word-break: break-all; ">HookId_1</font>() {
......
return ...;
}
function <font color="blue" style="word-break: break-all; ">HookId_2</font>() {
......
return ...;
}
......
}
?>
identifier
插件的唯一标识符,在插件设置中设置。
HookId
可用函数名以及被调用的脚本请看参照以下列表。这些函数将在 Discuz! 执行到 common.inc.php 的时候调用。如果要在模板输出前调用,需在函数名结尾加上“output”。全局调用的函数(“global”开头的)在模板输出前调用。
如:viewthread_imicons() 在 common.inc.php 时调用,viewthread_imicons_output() 在模板输出前调用。
| 函数名/嵌入点名/HookId | 对应脚本 | 返回值类型 |
| index_header | index.php | string |
| index_hot | index.php | string |
| index_navbar | index.php | string |
| index_top | index.php | string |
| index_middle | index.php | string |
| index_bottom | index.php | string |
| forumdisplay_header | forumdisplay.php | string |
| forumdisplay_forumaction | forumdisplay.php | string |
| forumdisplay_modlink | forumdisplay.php | string |
| forumdisplay_top | forumdisplay.php | string |
| forumdisplay_middle | forumdisplay.php | string |
| forumdisplay_thread | forumdisplay.php | array |
| forumdisplay_bottom | forumdisplay.php | string |
| memcp_side | memcp.php | string |
| profile_baseinfo_top | profile.php | string |
| profile_baseinfo_bottom | profile.php | string |
| profile_extrainfo | profile.php | string |
| profile_side_top | profile.php | string |
| profile_side_bottom | profile.php | string |
| viewthread_top | viewthread.php | string |
| viewthread_fastpost_side | viewthread.php | string |
| viewthread_fastpost_content | viewthread.php | string |
| viewthread_profileside | viewthread.php | array |
| viewthread_imicons | viewthread.php | array |
| viewthread_sidetop | viewthread.php | array |
| viewthread_sidebottom | viewthread.php | array |
| viewthread_postheader | viewthread.php | array |
| viewthread_posttop | viewthread.php | array |
| viewthread_postbottom | viewthread.php | array |
| viewthread_useraction | viewthread.php | string |
| viewthread_postfooter | viewthread.php | array |
| viewthread_endline | viewthread.php | array |
| viewthread_middle | viewthread.php | string |
| viewthread_bottom | viewthread.php | string |
| post_top | post.php | string |
| post_middle | post.php | string |
| post_bottom | post.php | string |
| my_navextra | my.php | string |
| global_header | 全局 | string |
| global_footer | 全局 | string |
| global_footerlink | 全局 | string |
<p style="word-break: break-all; margin: 0px; padding: 10px 0px; ">以上预定义的嵌入点会在页面预置好的位置输出函数返回的内容。函数返回值类型如果是 array 且是空值的,必须输出一个空数组,如:</p>
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1311px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
return array(); 函数名并不限于以上列表,您可以自定义,只要符合以下规则,函数就会在适当的地方被调用。
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1311px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
function CURSCRIPT_USERDEFINE_output
CURSCRIPT 指明了此函数是哪个脚本执行,USERDEFINE 可自定义,如果函数名以“_output”结尾则会在模板输出前调用,否则会在 common.inc.php 的时候调用。
如:attachment_test() 函数会在下载附件的时候执行。
“output”结尾的函数的第一个参数为数组,含义为 array('template' => 要输出的模板名, 'message' => showmessage 的文字)
如:以下函数将在登录的时候输出调试文字
pluginidentifier 类中的其它函数为了便于阅读建议以“”开头,如:
function logging_test_output($a) {
print_r($a);
print_r($POST);
}
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1311px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
<?php
class plugin_sample {
function _updatecache() {
......
return ...;
}
function viewthread_posttop() {
......
return ...;
}
......
}
?>
<p> </p>
</li>
</ul>
</td>
</tr>
<tr style="word-break: break-all; ">
<td class="title" style="word-break: break-all; color: rgb(255, 255, 255); line-height: 21px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 5px 5px 1em; background-image: url(file:///C:/Documents%20and%20Settings/Administrator/%E6%A1%8C%E9%9D%A2/PluginDevloper_Guide/forumbox_head.gif); background-color: rgb(63, 166, 207); background-position: 0px 0px; background-repeat: repeat no-repeat; ">特殊主题模块开发</td>
</tr>
<tr style="word-break: break-all; ">
<td style="word-break: break-all; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<ul style="word-break: break-all; margin: 0px 0px 0px 12px; padding-right: 0px; padding-bottom: 14px; padding-left: 0px; color: rgb(102, 102, 102); ">
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">特殊主题模块用于创建一个特殊主题,特殊主题类型脚本格式
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1311px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
<?php
class threadplugin_identifier {
var <font color="blue" style="word-break: break-all; ">$name</font> = 'XX主题'; //主题类型名称
var <font color="blue" style="word-break: break-all; ">$iconfile</font> = 'icon.gif'; //images/icons/ 目录下新增的主题类型图片文件名
var <font color="blue" style="word-break: break-all; ">$buttontext</font> = '发布xx主题'; //发帖时按钮文字
function <font color="blue" style="word-break: break-all; ">newthread</font>($fid) {
return ...;
}
function <font color="blue" style="word-break: break-all; ">newthread_submit</font>($fid) {
}
function <font color="blue" style="word-break: break-all; ">newthread_submit_end</font>($fid) {
}
function <font color="blue" style="word-break: break-all; ">editpost</font>() {
return ...;
}
function <font color="blue" style="word-break: break-all; ">editpost_submit</font>() {
}
function <font color="blue" style="word-break: break-all; ">editpost_submit_end</font>() {
}
function <font color="blue" style="word-break: break-all; ">newreply_submit_end</font>() {
}
function <font color="blue" style="word-break: break-all; ">viewthread</font>() {
return ...;
}
}
?>
identifier
插件的唯一标识符,在插件设置中设置。
<table border="0" cellpadding="0" cellspacing="0" style="word-break: break-all; empty-cells: show; border-collapse: collapse; width: 600px; ">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">函数名</td>
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">含义</td>
</tr>
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">newthread</td>
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">发主题时页面新增的表单项目,通过 return 返回即可输出到发帖页面中</td>
</tr>
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">newthread_submit</td>
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">主题发布后的数据判断</td>
</tr>
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">newthread_submit_end</td>
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">主题发布后的数据处理</td>
</tr>
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">editpost</td>
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">编辑主题时页面新增的表单项目,通过 return 返回即可输出到编辑主题页面中</td>
</tr>
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">editpost_submit</td>
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">主题编辑后的数据判断</td>
</tr>
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">editpost_submit_end</td>
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">主题编辑后的数据处理</td>
</tr>
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">newreply_submit_end</td>
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">回帖后的数据处理</td>
</tr>
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">viewthread</td>
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">查看主题时页面新增的内容,通过 return 返回即可输出到主题首贴页面中</td>
</tr>
</tbody>
</table>
<p> </p>
</li>
</ul>
</td>
</tr>
<tr style="word-break: break-all; ">
<td class="title" style="word-break: break-all; color: rgb(255, 255, 255); line-height: 21px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 5px 5px 1em; background-image: url(file:///C:/Documents%20and%20Settings/Administrator/%E6%A1%8C%E9%9D%A2/PluginDevloper_Guide/forumbox_head.gif); background-color: rgb(63, 166, 207); background-position: 0px 0px; background-repeat: repeat no-repeat; ">插件安装、卸载、升级脚本的设计</td>
</tr>
<tr style="word-break: break-all; ">
<td style="word-break: break-all; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<ul style="word-break: break-all; margin: 0px 0px 0px 12px; padding-right: 0px; padding-bottom: 14px; padding-left: 0px; color: rgb(102, 102, 102); ">
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">安装、卸载<br style="word-break: break-all; " />
插件作者可以设计 2 个脚本文件用于插件的安装和卸载,文件名任意。脚本中可用 runquery() 函数执行 SQL 语句,表名可以直接写“cdb_”。插件作者只需在导出的 XML 文件结尾加上安装、卸载脚本的文件名即可
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1311px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
<font color="blue" style="word-break: break-all; "><item id="installfile"><![CDATA[install.php]]></item>
<item id="uninstallfile"><![CDATA[uninstall.php]]></item></font>
</item>
</root> 安装、卸载程序中可随意设计页面的跳转,只要在插件安装、卸载结束时候输出添加以下代码即可。
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1311px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
$finish = TRUE;
插件作者可以设计一个脚本文件用于插件的升级,文件名任意。脚本中可用 runquery() 函数执行 SQL 语句,表名可以直接写“cdb_”。插件作者只需在导出的 XML 文件结尾加上升级脚本的文件名即可
<item id="upgradefile"><![CDATA[upgrade.php]]></item> </item> </root> |
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1311px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
$finish = TRUE; 插件的当前版本号位于 XML 文件的以下分支中,可自行更改。
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1311px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
<item id="plugin">
......
<item id="version"><![CDATA[<font color="blue" style="word-break: break-all; ">当前版本</font>]]></item>
......
</item>
插件在安装的时候您可以自定义授权信息文本,文本支持 Discuz 代码,站长同意后才能安装插件。如果插件存在后台管理界面或者变量配置,那么插件介绍文本会显示在插件后台页面中。插件作者只需在导出的 XML 文件结尾加上以下内容即可
<item id="license"><![CDATA[授权协议文本]]></item> <item id="intro"><![CDATA[插件介绍文本]]></item> </item> </root> |
插件中的语言包可以写到导出的 XML 文件结尾,这样在插件安装的时候会自动把语言包生成文件名为 forumdata/plugins/identifier.lang.php 的文件。插件开发时,可直接在后台开启语言包选项后编辑此语言包文件。
<item id="language"> <item id="scriptlang"> <item id="text"><![CDATA[脚本语言文字]]></item> </item> <item id="templatelang"> <item id="text"><![CDATA[模版语言文字]]></item> </item> <item id="installlang"> <item id="text"><![CDATA[安装语言文字]]></item> </item> </item> </item> </root> |
插件的模板文件可以放置在 plugins 目录下,如 plugins/hooktest/templates 子目录下。页面嵌入模块的脚本中可用以下方式调用此目录下的模板。
include template('index_top', 'hooktest', './plugins/hooktest/templates');
|
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1311px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
include plugintemplate('index_top'); 插件模版文件中的语言包中通过 {lang identifier:langvar} 方式调用,例如:
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1311px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
<!--{block return}--> {lang hooktest:text} <!--{/block}--> 上例中对应的变量为语言包文件 forumdata/plugins/identifier.lang.php 中 $templatelang['hooktest']['text'] 的值。脚本中引用语言包无需包含语言包文件,可直接使用变量。如插件脚本中可用变量 $scriptlang['hooktest']['text'],安装脚本中可用变量 $installlang['hooktest']['text']。
插件安装时可以直接导入一个或多个论坛数据,这些论坛数据包括数据调用(request)、论坛方案(project)、表情(smilies)、风格(styles)的数据。在导出的 XML 文件结尾加上需要导入数据的类型和数据文件名即可,多个文件名用逗号(",")分隔。
<item id="importfile"> <item id="request"><![CDATA[discuz_request_test.xml,discuz_request_test2.xml]]></item> <item id="project"><![CDATA[discuz_project_test.xml]]></item> <item id="smilies"><![CDATA[discuz_smilies_test.xml]]></item> <item id="styles"><![CDATA[discuz_styles_test.xml]]></item> </item> </item> </root> |
在新插件内核中,通过 plugin.php 方式访问的插件可直接通过 plugin.php?id=xxx:yyy 方式调用而无需再在后台定义为普通脚本模块,只要 plugins/xxx/yyy.inc.php 文件存在即可。如果 xxx 和 yyy 同名,可直接通过 plugin.php?id=xxx 方式访问。如果导出的 XML 文件名以 SC_GBK、SC_UTF8、TC_BIG5、TC_UTF8 结尾,显示的时候将直接显示为“简体”、“繁体”、“UTF8”等字样。
请在您动手编写插件之前,还需要仔细的阅读以下原则,遵循这些原则,将有效的避免可能发生的问题:
<p style="word-break: break-all; margin: 0px; padding: 10px 0px; "> </p>
<ul style="word-break: break-all; margin: 0px 0px 0px 12px; padding-right: 0px; padding-bottom: 14px; padding-left: 0px; color: rgb(102, 102, 102); ">
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">所有与插件的程序,包括<b style="word-break: break-all; ">其全部的前后台程序</b>,请全部放入 ./plugins 目录中,同时在插件的安装说明中指出,插件的文件需要复制到哪些目录。为了避免与其他插件冲突,请尽量建立 ./plugins 下的子目录,并将插件程序放置于子目录下,这样您编写的插件将获得更好的兼容性。</li>
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">如果您的插件包含“前台调用(前台菜单)”模块,该模块将统一用 plugin.php?identifier=xxx&module=yyy 的方式调用,请在相应链接、表单中使用此方式。其中 xxx 为插件的惟一标识符,yyy 为模块名称。前台插件外壳程序 plugin.php 已经加载了通用初始化模块(./include/common.inc.php),不需再次引用。</li>
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">如果您的插件包含“后台调用(后台菜单)”模块,该模块将统一用 admincp.php?action=plugins&identifier=xxx&mod=yyy 的方式调用,请在相应链接、表单中使用此方式。其中 xxx 和 yyy 的定义与“前台调用(前台菜单)”模块中的相同。系统还允许用 admincp.php?action=plugins&edit=$edit&mod=$mod 的方式来生成链接和表单地址,$edit 和 $mod 变量已经被插件后台管理接口赋值,因此将这两个变量值带入 URL 中也是被支持的。由于后台模块是被 admincp.php 调用,因此已加载了通用初始化模块(./include/common.inc.php)并进行了后台管理人员权限验证,因此模块程序中可直接写功能代码,不需再进行验证。</li>
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">请勿绕过插件的前后台外壳(plugin.php 和 admincp.php)而以直接调用某程序的方式编写插件,因为这样既导致了用户使用不便,代码冗余和不规范,同时又产生了因验证程序考虑不周到而带来的安全隐患。您可以在任何地方,包括链接、表单等处方便的使用上述 URL 地址对插件模块进行调用。</li>
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">所有与插件有关的程序,包括全部的前后台程序,因全部使用外壳调用,请务必在第一行加入
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1311px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
插件钩子的设计,需要您具有一定编程基础,比较了解 Discuz! 论坛程序的结构,并能够使用 PHP 语言撰写代码。对于普通用户,可以略过以下内容。 插件钩子(以下简称“钩子”)属于插件的一部分,因此在设计钩子之前应当首先进入后台——插件管理,新增插件或者编辑一个现有的插件,即可看到相关设置。
<p class="subtitle" style="word-break: break-all; margin: 0px; padding: 5px 0px; font-size: 14px; font-weight: bold; color: rgb(0, 0, 0); ">钩子的添加</p>
<ul style="word-break: break-all; margin: 0px 0px 0px 12px; padding-right: 0px; padding-bottom: 14px; padding-left: 0px; color: rgb(102, 102, 102); ">
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">钩子名称:在一个插件内,钩子的名称是唯一的,不可重复。名称可以由英文字母、数字和“_”组成,不支持中文,最长255个字符。为了便于理解和记忆钩子的作用, 名称应当尽量简洁清晰,能够表述一定的含义。注意:钩子名称对字母大小敏感,例如:Index_start 和 index_start 将视为两个不同的钩子。</li>
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">钩子描述:对钩子的详细说明,如功能介绍、调用方法、使用方法等。</li>
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">PHP代码:这里是钩子的核心内容,也是一段PHP代码,需要您自行设计,完成钩子需要处理的数据或者需要执行的操作。</li>
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">可用:每个插件允许有多个钩子,您可以自由选择关闭或者开启某个钩子。</li>
</ul>
<p class="subtitle" style="word-break: break-all; margin: 0px; padding: 5px 0px; font-size: 14px; font-weight: bold; color: rgb(0, 0, 0); ">钩子的删除</p>
<p style="word-break: break-all; margin: 0px; padding: 10px 0px; ">在钩子管理界面,您可以随时删除某一个钩子。 钩子删除以后,您应当及时修正放置钩子的程序文件,清除钩子标记,以免影响您论坛的正常使用。</p>
<ul style="word-break: break-all; margin: 0px 0px 0px 12px; padding-right: 0px; padding-bottom: 14px; padding-left: 0px; color: rgb(102, 102, 102); ">
</ul>
<p class="subtitle" style="word-break: break-all; margin: 0px; padding: 5px 0px; font-size: 14px; font-weight: bold; color: rgb(0, 0, 0); ">钩子的编辑与升级</p>
<ul style="word-break: break-all; margin: 0px 0px 0px 12px; padding-right: 0px; padding-bottom: 14px; padding-left: 0px; color: rgb(102, 102, 102); ">
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">编辑:插件设计阶段,您可能需要随时编辑钩子,每次更改,系统会自动更新缓存文件,您可以立即看到更改的效果。如果是更改钩子名称,那么您可能需要调整钩子放置的程序,修改钩子调用的名称。</li>
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">升级:论坛程序进行升级之前,您应当使用插件的导出功能,导出插件备份。论坛升级并正常运行后,再导入插件备份,修改相关程序,重新安放钩子。</li>
</ul>
<p class="subtitle" style="word-break: break-all; margin: 0px; padding: 5px 0px; font-size: 14px; font-weight: bold; color: rgb(0, 0, 0); ">钩子的放置与调用</p>
<p style="word-break: break-all; margin: 0px; padding: 10px 0px; ">钩子设计完成以后,您需要在相应的程序中安放钩子,不同钩子由于作用的不同,放置的位置也是不同的。 安放钩子,您仅仅需要将钩子的调用代码放入即可。调用代码格式如下:</p>
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1333px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
eval($hooks['插件唯一标识符(identifier)_钩子名称']); </pre>
</td>
</tr>
</tbody>
</table>
<p> </p>
<p style="word-break: break-all; margin: 0px; padding: 10px 0px; ">例如:调用 插件demo 的钩子 testhook, 我们需要在程序中适当的地方加入下面的代码</p>
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1333px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
eval($hooks['demo_testhook']); </pre>
</td>
</tr>
</tbody>
</table>
<p> </p>
<ul style="word-break: break-all; margin: 0px 0px 0px 12px; padding-right: 0px; padding-bottom: 14px; padding-left: 0px; color: rgb(102, 102, 102); ">
</ul>
<p class="subtitle" style="word-break: break-all; margin: 0px; padding: 5px 0px; font-size: 14px; font-weight: bold; color: rgb(0, 0, 0); ">设计范例</p>
<p style="word-break: break-all; margin: 0px; padding: 10px 0px; ">此处我们为您提供一个使用钩子技术的插件范例,完成功能十分简单,旨在使您直观的了解钩子的使用。此范例实现了当游客访问论坛首页时,强制用户登录的功能。</p>
<ul style="word-break: break-all; margin: 0px 0px 0px 12px; padding-right: 0px; padding-bottom: 14px; padding-left: 0px; color: rgb(102, 102, 102); ">
<li>
<ul style="word-break: break-all; margin: 0px 0px 0px 12px; padding-right: 0px; padding-bottom: 14px; padding-left: 0px; ">
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">进入系统设置中的插件管理,添加一个名称和惟一标识符均为 <b style="word-break: break-all; ">demo</b> 的插件。</li>
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">编辑插件 <b style="word-break: break-all; ">demo</b> 详情,找到“插件钩子设置”,添加一个名为 <b style="word-break: break-all; ">index_force_loggedin</b> 的钩子,然后提交。</li>
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">编辑钩子 <b style="word-break: break-all; ">index_force_loggedin</b>,按如下内容填写设置:
<ul style="word-break: break-all; margin: 0px 0px 0px 12px; padding-right: 0px; padding-bottom: 14px; padding-left: 0px; ">
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">钩子描述:当游客访问论坛首页的时候,程序会弹出提示框,并自动跳转到登陆页面。</li>
<li style="word-break: break-all; margin-left: 22px; line-height: 1.8em; ">PHP代码:
<table border="0" cellpadding="0" cellspacing="0" class="code" style="word-break: break-all; empty-cells: show; border-collapse: collapse; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); width: 1265px; background-position: initial initial; background-repeat: initial initial; " width="80%">
<tbody style="word-break: break-all; ">
<tr style="word-break: break-all; ">
<td style="word-break: break-all; color: rgb(51, 51, 51); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<pre style="word-break: break-all; font-family: Courier, 'Courier New'; color: rgb(0, 0, 0); ">
if(!$discuz_uid) {
showmessage('请您登陆后访问本站,现在将转入登录页面。', 'logging.php?action=login');
}
- 查找代码:
$discuz_action = 1;
- 在上述代码下放添加代码:
eval($hooks['demo_index_alter']);
<p style="word-break: break-all; margin: 0px; padding: 10px 0px; ">Discuz! 插件的钩子技术,为广大的插件开发者提供了一个更加灵活的插件设计机制。当 Discuz! 升级后,用户只需重新将钩子调用代码安放到程序中原来的位置,就几乎可以继续使用原来已安装的插件,降低了对于程序修改的幅度和插件安装的难度,更加有利于插件程序的规范、管理、维护、相互交流。因此我们强烈建议插件开发者能够深入研究并应用这一机制,创作出越来越多的优秀插件。 </p>
</td>
</tr>
<tr style="word-break: break-all; ">
<td class="title" style="word-break: break-all; color: rgb(255, 255, 255); line-height: 21px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 5px 5px 1em; background-image: url(file:///C:/Documents%20and%20Settings/Administrator/%E6%A1%8C%E9%9D%A2/PluginDevloper_Guide/forumbox_head.gif); background-color: rgb(63, 166, 207); background-position: 0px 0px; background-repeat: repeat no-repeat; ">意见反馈</td>
</tr>
<tr style="word-break: break-all; ">
<td style="word-break: break-all; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(232, 237, 242); padding: 5px 10px; ">
<p style="word-break: break-all; margin: 0px; padding: 10px 0px; ">插件接口是 Discuz! 开发组为了方便插件设计、安装和使用而专门开发,虽然经过长期的优化和改进,可能仍然会有不够合理或不够完善的地方,欢迎各位插件程序员在使用此接口的过程中,为我们提出意见和建议,感谢您的支持。</p>
</td>
</tr>
</tbody>
版权所有 ©2001-2009 康盛创想(北京)科技有限公司 Comsenz Inc
