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!