Typecho plugin compilation tutorial (5): Core code, typecho plugin. Typecho plugin compilation tutorial (5): core code. the typecho plugin is too long-winded before and is now writing core code. Analysis: When publishing an article, we need to compile the typecho plug-in tutorial (5): Core code, typecho plug-in
I used to get a lot of code, and now I am writing core code.
Analysis: When publishing an article, we need the URL of the current article. 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 the render has been changed to send)
The code is as follows:
Class BaiduSubmitTest_Plugin implements Typecho_Plugin_Interface
{
Public static function activate (){
// Mount the publishing article and page interface
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 'plugin installed successfully. enter the settings and enter the accesskey ';
}
Public static function deactivate (){
// Do something
Return 'plugin 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 key'), 'Please log on to Baidu webmaster platform to obtain it ');
$ Form-> addInput ($ element );
}
Public static function personalConfig (Typecho_Widget_Helper_Form $ form ){}
Public static function send ($ contents, $ class ){
// Do something
}
}
Get URL
To obtain a permanent link, you must use the routing rule + Typecho_Common: url to generate the link!
The code is as follows:
Class BaiduSubmitTest_Plugin implements Typecho_Plugin_Interface
{
Public static function activate (){
// Mount the publishing article and page interface
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 'plugin installed successfully. enter the settings and enter the accesskey ';
}
Public static function deactivate (){
// Do something
Return 'plugin uninstalled successfully ';
}
Public static function config (Typecho_Widget_Helper_Form $ form ){
// Save the API call address
$ Element = new Typecho_Widget_Helper_Form_Element_Text ('api ', null, null, _ t ('interface call address'), 'Please log on to Baidu webmaster platform to obtain it ');
$ 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 document attribute is hidden or delayed
If ('Publish '! = $ Contents ['Visibility '] | $ contents ['created']> time ()){
Return;
}
// Obtain system configuration
$ Options = Helper: options ();
// Determine whether the API is configured
If (is_null ($ options-> plugin ('baidusubmittest')-> api )){
Return;
}
// Obtain the document type
$ Type = $ contents ['type'];
// Obtain route 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 later.
Now we have obtained a permanent link to the article. The next step is to send data to the Baidu server!
This section is complete!
Coding (5): the core code. the typecho plug-in is too long-winded. now, I am writing the core code. Analyze the information we need when publishing an article...