typecho插件编写教程(五):核心代码
之前??铝撕芏啵?衷诳?夹春诵拇?搿?/p>
分析一下,发布文章的时候,我们需要的信息就是当前文章的URL,我们需要想办法从$contents、 $class中拿到他。
目前我们的插件类代码如下(请注意render被我改成了send)
class BaiduSubmitTest_Plugin implements Typecho_Plugin_Interface
{
public static function activate(){
//挂载发布文章和页面的接口
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 '插件安装成功,请进入设置填写准入密钥';
}
public static function deactivate(){
// do something
return '插件卸载成功';
}
public static function config(Typecho_Widget_Helper_Form $form){
$element = new Typecho_Widget_Helper_Form_Element_Text('api', null, null, _t('准入秘钥'), '请登录百度站长平台获取');
$form->addInput($element);
}
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
public static function send($contents, $class){
//do something
}
}
获取URL
获取永久链接需要通过路由规则 + Typecho_Common::url 联合生成!
class BaiduSubmitTest_Plugin implements Typecho_Plugin_Interface
{
public static function activate(){
//挂载发布文章和页面的接口
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 '插件安装成功,请进入设置填写准入密钥';
}
public static function deactivate(){
// do something
return '插件卸载成功';
}
public static function config(Typecho_Widget_Helper_Form $form){
//保存接口调用地址
$element = new Typecho_Widget_Helper_Form_Element_Text('api', null, null, _t('接口调用地址'), '请登录百度站长平台获取');
$form->addInput($element);
}
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 准备数据
* @param $contents 文章内容
* @param $class 调用接口的类
* @throws Typecho_Plugin_Exception
*/
public static function send($contents, $class){
//如果文章属性为隐藏或滞后发布
if( 'publish' != $contents['visibility'] || $contents['created'] > time()){
return;
}
//获取系统配置
$options = Helper::options();
//判断是否配置好API
if( is_null($options->plugin('BaiduSubmitTest')->api) ){
return;
}
//获取文章类型
$type = $contents['type'];
//获取路由信息
$routeExists = (NULL != Typecho_Router::get($type));
//生成永久连接
$path_info = $routeExists ? Typecho_Router::url($type, $contents) : '#';
$permalink = Typecho_Common::url($path_info, $options->index);
}
}
代码中有注释,老高就不在赘述了。
至此我们已经拿到了文章的永久链接,下一步就是给百度服务器发送数据了!
本节完!
php实现paypal 授权登录
php实现paypal授权登录php/***@projectpaypallogin*@authorjiangjianhe*@date2015-04-03*/classpaypallogin{//沙箱token链接private$_sanbox_oauth2_auth_uri='https://www.sandbox.paypal.com/webapps/au
typecho插件编写教程(六):调用接口
此篇我们开始调用接口,我们在插件类中新定义一个方法,起名为send_post,在方法中我们通过系统配置获取接口调用地址。百度给的例子中使用了php的CU
php操作redis缓存方法分享
phpredis缓存操作php/***Redis缓存操作*@authorhxm*@version1.0*@since2015.05.04*/classRCacheextendsObjectimplementsCacheFace{private$redis=null;//redis对象private$sId=1;//servier服务IDprivate
