search
Homephp教程php手册typecho插件编写教程(五):核心代码

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

Jun 13, 2016 am 09:03 AM
typechomaincodeplug-inTutorialarticlecorewrite

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);

  }

  }

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

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

  本节完!

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

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

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.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor