如何开发一个Zblog插件之代码篇

京漂大叔 2020-11-26 PM 10003℃ 0条

pexels-photo-276218.jpeg
如何开发一个Zblog插件 这一篇文章,给大家介绍了下开发一个插件的前期准备工作。
今天通过代码,开发一个插件实例

一、Zblog插件的注册方法

1、ZblogPHP插件是采用主动插入方式来通知系统,所以我们要在include.php 文件开头调用RegisterPlugin函数,让插件进入Zblog插件系统的体系。

//注册插件
RegisterPlugin("插件ID","ActivePlugin_插件ID");
### 实例:注册插件
RegisterPlugin("Fu_oneword","ActivePlugin_Fu_oneword");

2、如需挂载系统接口,还必须在”ActivePlugin_插件ID”函数中完成对接口的挂接工作

//具体的接口挂接
function ActivePlugin_插件ID() {
        Add_Filter_Plugin('API名称','执行代码(函数)');
}
### 实例:插件挂钩方式
function ActivePlugin_Fu_oneword() {
        Add_Filter_Plugin('Filter_Plugin_Index_Begin','Fu_oneword_moduleContent'); //Zbp类的生成模板标签接口
}

3、此时我们挂载了一个Fu_oneword_moduleContent的方法,我们来编写Fu_oneword_moduleContent()

function Fu_oneword_moduleContent(){
    global $zbp;
    if(isset($zbp->modulesbyfilename['Fu_oneword'])){    
    $zbp->modulesbyfilename['Fu_oneword']->Content =Fu_oneword_content();
    }
}

这里我们用到一个$zbp这个全局变量,如果你以后要写Zblog插件或者Zblog主题,要经常用到这个全局变量。$zbp是zblogphp.php的实例化,位于/zb_system/function/lib/zblogphp.php。
这个Fu_oneword_moduleContent()方法是我们通过模块化调用插件Fu_oneword的内容,内容在Fu_oneword_content()这个方法里面。

4、我们来编写Fu_oneword_content(),这个方法里面,我实现的是调用一个接口,来随机展示一句话,可以是名人名言,也可以是一段网络语言,也可以是诗词语句,电影电视剧的台词等。

function Fu_oneword_content(){
    global $zbp;
    $min_length = $zbp->Config('Fu_oneword')->min_length;
    $max_length = $zbp->Config('Fu_oneword')->max_length;
    $worldtype = $zbp->Config('Fu_oneword')->worldtype;
    if($min_length){
        $min_length_value = '&min_length='.$min_length;
    } else {
        $min_length_value = '';
    }
    if($max_length){
        $max_length_value = '&max_length='.$max_length;
    } else {
        $max_length_value = '';
    }
    if($worldtype){
        $worldtype = implode("&c=",$worldtype);
        $worldtype = "&c=".$worldtype;
    } else {
        $worldtype = '';
    }
    
    $url = "https://v1.xxx.cn/?encode=text{$min_length_value}{$max_length_value}{$worldtype}"; 
    $result= Network::Create();
    if (!$result) {
        throw new Exception('主机没有开启网络功能');
    }
    $result->open('get',$url);
    $result->send($url);
    $oneWorld = $result;
    if(!empty($oneWorld)){
        return     $oneWorld->responseText;
    }else{
        return     '<li>木有了,杯具了...请联系作者</li>';          
    }
}

这个方法里面的min_length,max_length,worldtype三个变量的值,是我在main.php文件里面使用Z-BlogPHP的选项机制保存的

$min_length = $zbp->Config('Fu_oneword')->min_length;
$max_length = $zbp->Config('Fu_oneword')->max_length;
$worldtype = $zbp->Config('Fu_oneword')->worldtype;

5、然后我们来创建一个模块,让插件启用的时候,自动生成插件的模块,Zblog系统通过InstallPlugin_插件ID()和UninstallPlugin_插件ID()来执行插件的激活和禁用。

function Fu_oneword_buildModulediv(){
    global $zbp;
    if(!isset($zbp->modulesbyfilename['Fu_oneword'])){
        $t = new Module();
        $t->Name = "一句话";
        $t->FileName = "Fu_oneword";
        $t->Source = "Fu_oneword";
        $t->SidebarID = 0;
        $t->Content = "";
        $t->IsHideTitle=false;
        $t->HtmlID = "divFu_oneword";
        $t->Type = "div";
        $t->MaxLi=0;
        $t->Content = '';
        $t->Save();
    }
}
function InstallPlugin_Fu_oneword() {
    global $zbp;
    Fu_oneword_buildModulediv();
}

上面的代码InstallPlugin_Fu_oneword()方法里面,我们调用Fu_oneword_buildModulediv()这个方法,就是用来激活创建插件的模块。

6、include.php文件的全部代码如下

<?php
#开发者:fusky 联系QQ:1604026450
#注册插件
RegisterPlugin("Fu_oneword","ActivePlugin_Fu_oneword");

function ActivePlugin_Fu_oneword() {
    Add_Filter_Plugin('Filter_Plugin_Index_Begin','Fu_oneword_moduleContent');
}
function Fu_oneword_moduleContent(){
    global $zbp;
    if(isset($zbp->modulesbyfilename['Fu_oneword'])){    
    $zbp->modulesbyfilename['Fu_oneword']->Content =Fu_oneword_content();
    }
}
function Fu_oneword_content(){
    global $zbp;
    $min_length = $zbp->Config('Fu_oneword')->min_length;
    $max_length = $zbp->Config('Fu_oneword')->max_length;
    $worldtype = $zbp->Config('Fu_oneword')->worldtype;
    if($min_length){
        $min_length_value = '&min_length='.$min_length;
    } else {
        $min_length_value = '';
    }
    if($max_length){
        $max_length_value = '&max_length='.$max_length;
    } else {
        $max_length_value = '';
    }
    if($worldtype){
        $worldtype = implode("&c=",$worldtype);
        $worldtype = "&c=".$worldtype;
    } else {
        $worldtype = '';
    }
    
    $url = "https://v1.xxx.cn/?encode=text{$min_length_value}{$max_length_value}{$worldtype}"; 
    $result= Network::Create();
    if (!$result) {
        throw new Exception('主机没有开启网络功能');
    }
    $result->open('get',$url);
    $result->send($url);
    $oneWorld = $result;
    if(!empty($oneWorld)){
        return     $oneWorld->responseText;
    }else{
        return     '<li>木有了,杯具了...请联系作者</li>';          
    }
}
function Fu_oneword_buildModulediv(){
    global $zbp;
    if(!isset($zbp->modulesbyfilename['Fu_oneword'])){
        $t = new Module();
        $t->Name = "一句话";
        $t->FileName = "Fu_oneword";
        $t->Source = "Fu_oneword";
        $t->SidebarID = 0;
        $t->Content = "";
        $t->IsHideTitle=false;
        $t->HtmlID = "divFu_oneword";
        $t->Type = "div";
        $t->MaxLi=0;
        $t->Content = '';
        $t->Save();
    }
}
function InstallPlugin_Fu_oneword() {
    global $zbp;
    Fu_oneword_buildModulediv();
}
function UninstallPlugin_Fu_oneword() {}

二、插件的参数配置

1、在main.php文件里面,我们可以对插件做一些参数的配置,上面我们提到的min_length,max_length,worldtype三个变量的值,就是在main.php里面实现的。项目我们写在<div class="SubMenu"></div>里面,效果如下图红框部分。

<div class="SubMenu">
    <a href="main.php" ><span class="m-left">参数设置</span></a>
    <a href="https://www.caijingzhiku.com/oneworld.html" title="财经智库" alt="财经智库" target="_blank"><span class="m-right">有问题或者建议可以联系我</span></a>
</div>

QQ截图20201126132514.png

2、参数值设置,我们写到<div id="divMain2"></div>里面。

<form id="edit" name="edit" method="post" action="#">
        <input id="reset" name="reset" type="hidden" value="" />
        <table border="1" class="tableFull tableBorder">

        <tr>
            <td class="td30"><p align='left'><b>一句话最小长度</b></p></td>
            <td><input type="text" name="min_length" value="<?php echo htmlspecialchars($zbp->Config('Fu_oneword')->min_length);?>" style="width:30%;" placeholder="只能填数字,其它报错" />(默认0,长度不能大于最大长度)</td>
        </tr>
        <tr>
            <td class="td30"><p align='left'><b>一句话最大长度</b></p></td>
            <td><input type="text" name="max_length" value="<?php echo htmlspecialchars($zbp->Config('Fu_oneword')->max_length);?>" style="width:30%;" placeholder="只能填数字,其它报错" />(默认30,超过30个字不显示)</td>
        </tr>
        <tr>
            <td class="td30"><p align='left'><b>一句话类型</b></p></td>
            <td>
            <?php
                $worldvalue = array('a'=>'动画','b'=>'漫画','c'=>'游戏','d'=>'文学','e'=>'原创','h'=>'影视','i'=>'诗词','k'=>'哲学','l'=>'抖机灵','j'=>'网易云','f'=>'来自网络');
                if($zbp->Config('Fu_oneword')->worldtype){
                   foreach($worldvalue as $wk=>$wv){
                        if(in_array($wk,$zbp->Config('Fu_oneword')->worldtype)){
                           echo '<input type="checkbox" name="worldtype[]" value="'.$wk.'" checked="checked" />'.$wv; 
                        } else {
                           echo '<input type="checkbox" name="worldtype[]" value="'.$wk.'" />'.$wv;  
                        }
                    } 
                } else {
                    foreach($worldvalue as $wk=>$wv){
                           echo '<input type="checkbox" name="worldtype[]" value="'.$wk.'" />'.$wv;  
                    }
                }
                
            ?>
            (默认全部类型)
            </td>
        </tr>
        </table>

              <hr/>
              
        <p><input type="submit" class="button" value="<?php echo $lang['msg']['submit']?>" /></p>

    </form>

3、点提交的值,我们如何保存呢?通过下面的代码进行表单数据的获取和保存。这部分代码,就写在main.php文件的php模块里面,不要放到其它文件。

if(count($_POST)>0){

    $zbp->Config('Fu_oneword')->min_length=trim($_POST['min_length']);
    $zbp->Config('Fu_oneword')->max_length=trim($_POST['max_length']);
    if(isset($_POST['worldtype'])){
       $zbp->Config('Fu_oneword')->worldtype=$_POST['worldtype']; 
    } else {
       $zbp->Config('Fu_oneword')->worldtype=''; 
    }
    
    $zbp->SaveConfig('Fu_oneword');
    $zbp->SetHint('good');
    Redirect('./main.php');
}

这样一个插件就制作完成了。

三、Zblog配置项的获取、保存方法

最后在详细说一下main.php表单里面参数的保存和读取。上面我们提到过Z-BlogPHP的选项机制,Zblog的这个机制,主要用于保存简单的插件选项数据。使用方法如下。

1、设置并保存配置项

$zbp->Config('插件ID')->选项参数=选项值;
$zbp->SaveConfig('插件ID');

记住 一定要执行$zbp->SaveConfig('插件ID');否则保存失败。

2、配置项的读取

$s=$zbp->Config('插件ID')->选项参数;

3、配置项的删除

$zbp->DelConfig('插件ID');

4、判断配置选项是否已创建

$zbp->HasConfig('插件ID'); //return bool

如有问题,欢迎联系。

非特殊说明,文章均为原创。

评论啦~