search
HomeBackend DevelopmentPHP Tutorialtypecho插件编写教程(五):核心代码_PHP

之前啰嗦了很多,现在开始写核心代码。

分析一下,发布文章的时候,我们需要的信息就是当前文章的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);
    }
}

代码中有注释,老高就不在赘述了。

至此我们已经拿到了文章的永久链接,下一步就是给百度服务器发送数据了!

本节完!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
atom中 40+ 个常用插件推荐分享(附插件安装方法)atom中 40+ 个常用插件推荐分享(附插件安装方法)Dec 20, 2021 pm 04:14 PM

本篇文章给大家分享40+ 个atom常用插件,并附上在atom中安装插件的方法,希望对大家有所帮助!

vscode插件分享: 6个Vue3开发必备插件vscode插件分享: 6个Vue3开发必备插件Dec 09, 2022 pm 08:36 PM

本篇文章给大家整理分享 6 个 Vue3 开发必备的 VSCode 插件,可以直接用过 VSCode 的插件中心直接安装使用,希望对大家有所帮助!

2023年最新最全的VScode插件推荐2023年最新最全的VScode插件推荐Jan 24, 2023 am 05:30 AM

这篇文章主要介绍了这么多年来我在使用 VSCode 过程中用到的一些不错的插件。这些VSCode插件,帮你打造地表最强IDE!

【吐血总结】23个VSCode 插件,助你提高开发效率和美观性【吐血总结】23个VSCode 插件,助你提高开发效率和美观性Mar 10, 2022 pm 08:01 PM

本篇文章给大家总结了23个各种功能的VSCode 插件,可以帮助开发者提高开发效率和美观性,希望对大家有所帮助!

如何利用PHP和Typecho构建社交媒体网站如何利用PHP和Typecho构建社交媒体网站Jul 22, 2023 am 11:40 AM

如何利用PHP和Typecho构建社交媒体网站随着社交媒体的兴起,越来越多的人希望拥有一个属于自己的社交媒体网站。而搭建一个社交媒体网站的其中一种方式是使用PHP和Typecho这两个开源工具。PHP是一种被广泛应用于Web开发的脚本语言,而Typecho是一个小巧、易用、安全的博客系统,不仅功能强大,而且可以方便的进行二次开发。下面我将介绍如何使用PHP和

如何使用PHP和Typecho打造多语言支持的网站如何使用PHP和Typecho打造多语言支持的网站Jul 21, 2023 pm 11:21 PM

如何使用PHP和Typecho打造多语言支持的网站导语:随着全球化的发展,构建一个多语言支持的网站逐渐成为企业和个人所追求的目标。而PHP作为一种流行的编程语言,结合Typecho这一优秀的PHP开源博客程序,可以轻松实现多语言网站的搭建。本文将介绍如何使用PHP和Typecho来打造一个多语言支持的网站,并提供相关的代码示例。一、安装和配置Typecho首

浏览器增强版ChatGPT无敌了?超强插件Monica,能聊能写效率Max浏览器增强版ChatGPT无敌了?超强插件Monica,能聊能写效率MaxMay 03, 2023 pm 06:58 PM

提起Monica,你会想到什么?是老友记里的主角之一Monica·Geller,一个热心肠的女主人形象;还是心跳文学部里的疯疯癫癫的Monika?或者,最近爆火的Chrome插件——Monica。它的功能实在是太强大了,用完一次保你爱不释手。毕竟,搭载了ChatGPT的网页助手,能是俗物吗?Monica功能大赏首先明确一点,Monica是ChatGPT在网页上的应用,换句话说,Monica就是靠着ChatGPTAPI的强大功能才厉害。而仔细看看Chrome商店中的介绍,我们就会发现Monica真

30个实用VSCode 插件,让你的开发效率倍增!30个实用VSCode 插件,让你的开发效率倍增!Apr 24, 2022 pm 09:12 PM

本篇文章给大家总结分享30个实用VSCode 插件,让你的日常开发工作效率倍增,希望对大家有所帮助!

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.