您好,欢迎访问一九零五行业门户网

typecho插件编写教程(五):核心代码,typecho插件_PHP教程

typecho插件编写教程(五):核心代码,typecho插件之前啰嗦了很多,现在开始写核心代码。
分析一下,发布文章的时候,我们需要的信息就是当前文章的url,我们需要想办法从$contents、 $class中拿到他。
目前我们的插件类代码如下(请注意render被我改成了send)
复制代码 代码如下:
class baidusubmittest_plugin implements typecho_plugin_interface
{
    public static function activate(){
        //挂载发布文章和页面的接口
        typecho_plugin::factory('widget_contents_post_edit')->finishpublish = array('baidusubmittest_plugin', 'send');
        typecho_plugin::factory('widget_contents_page_edit')->finishpublish = array('baidusubmittest_plugin', 'send');
        return '插件安装成功,请进入设置填写准入密钥';
    }
    public static function deactivate(){
        // do something
        return '插件卸载成功';
    }
    public static function config(typecho_widget_helper_form $form){
        $element = new typecho_widget_helper_form_element_text('api', null, null, _t('准入秘钥'), '请登录百度站长平台获取');
        $form->addinput($element);
    }
    public static function personalconfig(typecho_widget_helper_form $form){}
    public static function send($contents, $class){
        //do something
    }
}
获取url
获取永久链接需要通过路由规则 + typecho_common::url 联合生成!
复制代码 代码如下:
class baidusubmittest_plugin implements typecho_plugin_interface
{
    public static function activate(){
        //挂载发布文章和页面的接口
        typecho_plugin::factory('widget_contents_post_edit')->finishpublish = array('baidusubmittest_plugin', 'send');
        typecho_plugin::factory('widget_contents_page_edit')->finishpublish = array('baidusubmittest_plugin', 'send');
        return '插件安装成功,请进入设置填写准入密钥';
    }
    public static function deactivate(){
        // do something
        return '插件卸载成功';
    }
    public static function config(typecho_widget_helper_form $form){
        //保存接口调用地址
        $element = new typecho_widget_helper_form_element_text('api', null, null, _t('接口调用地址'), '请登录百度站长平台获取');
        $form->addinput($element);
    }
    public static function personalconfig(typecho_widget_helper_form $form){}
    /**
     * 准备数据
     * @param $contents 文章内容
     * @param $class 调用接口的类
     * @throws typecho_plugin_exception
     */
    public static function send($contents, $class){
        //如果文章属性为隐藏或滞后发布
        if( 'publish' != $contents['visibility'] || $contents['created'] > time()){
            return;
        }
        //获取系统配置
        $options = helper::options();
        //判断是否配置好api
        if( is_null($options->plugin('baidusubmittest')->api) ){
            return;
        }
        //获取文章类型
        $type = $contents['type'];
        //获取路由信息
        $routeexists = (null != typecho_router::get($type));
        //生成永久连接
        $path_info = $routeexists ? typecho_router::url($type, $contents) : '#';
        $permalink = typecho_common::url($path_info, $options->index);
    }
}
代码中有注释,老高就不在赘述了。
至此我们已经拿到了文章的永久链接,下一步就是给百度服务器发送数据了!
本节完!
http://www.bkjia.com/phpjc/1008004.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1008004.htmltecharticletypecho插件编写教程(五):核心代码,typecho插件 之前啰嗦了很多,现在开始写核心代码。 分析一下,发布文章的时候,我们需要的信息就...
其它类似信息

推荐信息