This article mainly introduces the typecho plug-in writing tutorial (5): core code. This article explains the complete core code of the plug-in. For examples, friends in need can refer to the following
I have been 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. We need to find a way to get it from $contents and $class.
Our current plug-in class code is as follows (please note that I changed render to send)
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
Obtaining the permanent link needs to be generated through the routing rule Typecho_Common::url!
Copy the 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 that calls 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!