Typecho Plugin Authoring Tutorial (v): Core Code _php Instance

Source: Internet
Author: User

A lot of chatter before, now start to write the core code.

Analysis, the release of the article, we need the information is the current article URL, we need to find a way from the $contents, $class to get him.

At present our plug-in class code is as follows (please note that render was changed to send by me)

Copy Code code as follows:

Class Baidusubmittest_plugin implements Typecho_plugin_interface
{

public static function activate () {
Mount an 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 ' plugin installed successfully, please enter the set Fill entry key ';
}

public static function deactivate () {
Do something
Return ' plugin uninstall succeeded ';
}

public static function config (Typecho_widget_helper_form $form) {
$element = new Typecho_widget_helper_form_element_text (' API ', NULL, NULL, _t (' Access secret key '), ' please login to Baidu Webmaster Platform access ');
$form->addinput ($element);
}

public static function Personalconfig (Typecho_widget_helper_form $form) {}

public static function Send ($contents, $class) {
Do something
}
}

Get URL

Getting a permanent link needs to be generated by routing rules + Typecho_common::url!

Copy Code code as follows:

Class Baidusubmittest_plugin implements Typecho_plugin_interface
{

public static function activate () {
Mount an 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 ' plugin installed successfully, please enter the set Fill entry key ';
}

public static function deactivate () {
Do something
Return ' plugin uninstall succeeded ';
}

public static function config (Typecho_widget_helper_form $form) {
Save Interface Call Address
$element = new Typecho_widget_helper_form_element_text (' API ', NULL, NULL, _t (' Interface call address '), ' please login to Baidu Webmaster platform to get ');
$form->addinput ($element);
}

public static function Personalconfig (Typecho_widget_helper_form $form) {}

/**
* Prepare data
* @param $contents article content
* @param $class The class that invokes the interface
* @throws typecho_plugin_exception
*/
public static function Send ($contents, $class) {

If the article property is a hidden or delayed publication
if (' Publish '!= $contents [' visibility '] | | | | $contents [' created '] > Time ()) {
Return
}

Get System Configuration
$options = Helper::options ();

Determine if the API is configured.
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, the old job does not repeat.

So far we have got the article permanent link, the next step is to send data to the Baidu server!

This is the end of this section!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.