Home > Backend Development > PHP Tutorial > Typecho plug-in writing tutorial (5): core code, typecho plug-in_PHP tutorial

Typecho plug-in writing tutorial (5): core code, typecho plug-in_PHP tutorial

WBOY
Release: 2016-07-13 09:52:27
Original
875 people have browsed it

Typecho plug-in writing tutorial (5): core code, typecho plug-in

I was a lot verbose before, now I start writing the core code.

Analysis, when publishing an article, the information we need is the URL of the current article, and we need to find a way to get it from $contents and $class.

Currently our plug-in class code is as follows (please note that I changed render to send)

Copy code The code is as follows:
class BaiduSubmitTest_Plugin implements Typecho_Plugin_Interface
{

public static function activate(){
//Mount the interface for publishing articles and pages
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 'The plug-in is installed successfully, please enter the settings to fill in the access key';
}

public static function deactivate(){
               // do something
           return 'Plug-in uninstalled successfully';
}

public static function config(Typecho_Widget_Helper_Form $form){
           $element = new Typecho_Widget_Helper_Form_Element_Text('api', null, null, _t('Access Key'), 'Please log in to Baidu Webmaster Platform to obtain');
$form->addInput($element);
}

public static function personalConfig(Typecho_Widget_Helper_Form $form){}

public static function send($contents, $class){
           //do something
}
}

Get URL

Getting the permanent link needs to be jointly generated through the routing rule Typecho_Common::url!

Copy code The code is as follows:
class BaiduSubmitTest_Plugin implements Typecho_Plugin_Interface
{

public static function activate(){
//Mount the interface for publishing articles and pages
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 'The plug-in is installed successfully, please enter the settings to fill in the access key';
}

public static function deactivate(){
               // do something
           return 'Plug-in uninstalled successfully';
}

public static function config(Typecho_Widget_Helper_Form $form){
//Save the interface calling address
           $element = new Typecho_Widget_Helper_Form_Element_Text('api', null, null, _t('Interface call address'), 'Please log in to Baidu Webmaster Platform to obtain');
$form->addInput($element);
}

public static function personalConfig(Typecho_Widget_Helper_Form $form){}

/**
* Prepare data
* @param $contents Article content
* @param $class The class to call the interface
* @throws Typecho_Plugin_Exception
​​*/
Public static function send($contents, $class){

//If the article attribute is hidden or delayed publishing
If( 'publish' != $contents['visibility'] || $contents['created'] > time()){
             return;
}

//Get system configuration
          $options = Helper::options();

//Determine whether the API is configured properly
If( is_null($options->plugin('BaiduSubmitTest')->api) ){
             return;
}

//Get article type
         $type = $contents['type'];

//Get routing information
$routeExists = (NULL != Typecho_Router::get($type));

//Generate a permanent connection
          $path_info = $routeExists ? Typecho_Router::url($type, $contents) : '#';
          $permalink = Typecho_Common::url($path_info, $options->index);
}
}

There are comments in the code, so I won’t go into details.

At this point we have obtained the permanent link of the article, the next step is to send the data to Baidu server!

This section is over!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1008004.htmlTechArticleTutorial on writing the typecho plug-in (5): Core code, the typecho plug-in has been a lot long before, now let’s start writing the core code. Let's analyze it. When publishing an article, the information we need is...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template