From e7f090de982911b19bde3ee2ed439d72e7c0c541 Mon Sep 17 00:00:00 2001 From: zyzzc Date: Wed, 14 May 2014 21:52:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E8=AF=BB=E6=80=A7=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Plugin.php | 150 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 84 insertions(+), 66 deletions(-) diff --git a/Plugin.php b/Plugin.php index 6be93d7..b713366 100644 --- a/Plugin.php +++ b/Plugin.php @@ -1,5 +1,6 @@ contentEx = array('Keywords_Plugin', 'kwparse'); - } - - /** - * 禁用插件方法,如果禁用失败,直接抛出异常 - * - * @static - * @access public - * @return void - * @throws Typecho_Plugin_Exception - */ - public static function deactivate(){} - - /** - * 获取插件配置面板 - * - * @access public - * @param Typecho_Widget_Helper_Form $form 配置面板 - * @return void - */ - public static function config(Typecho_Widget_Helper_Form $form){ - $keywords = new Typecho_Widget_Helper_Form_Element_Textarea('keywords',NULL,"",_t('关键词链接'),_t('以“关键词”|(英文半角分隔号)“链接”形式填写,每行一组。如:
google|http://www.google.com')); - $keywords->input->setAttribute('style', 'width:345px;height:150px;'); - $form->addInput($keywords); - $tagslink = new Typecho_Widget_Helper_Form_Element_Checkbox('tagslink',array('true'=>'自动替换'),NULL,_t('标签链接'),_t('文中若出现与本站标签相同的关键词则自动添加标签页链接')); - $form->addInput($tagslink); - $limits = new Typecho_Widget_Helper_Form_Element_Text('limits',NULL,"1",_t('链接频率'),_t('文中有多个重复关键词或标签时可限制链接替换次数')); - $limits->input->setAttribute('style', 'width:60px'); - $form->addInput($limits->addRule('isInteger', _t('请填写整数数字'))); - } + /** + * 激活插件方法,如果激活失败,直接抛出异常 + * + * @access public + * @return void + * @throws Typecho_Plugin_Exception + */ + public static function activate() + { + Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('Keywords_Plugin','kwparse'); + } + + /** + * 禁用插件方法,如果禁用失败,直接抛出异常 + * + * @static + * @access public + * @return void + * @throws Typecho_Plugin_Exception + */ + public static function deactivate(){} + + /** + * 获取插件配置面板 + * + * @access public + * @param Typecho_Widget_Helper_Form $form 配置面板 + * @return void + */ + public static function config(Typecho_Widget_Helper_Form $form) + { + $keywords = new Typecho_Widget_Helper_Form_Element_Textarea('keywords',NULL,"",_t('关键词链接'),_t('以“关键词”|(英文半角分隔号)“链接”形式填写,每行一组。如:
google|http://www.google.com')); + $keywords->input->setAttribute('style','width:345px;height:150px;'); + $form->addInput($keywords); + $tagslink = new Typecho_Widget_Helper_Form_Element_Checkbox('tagslink',array('true'=>'自动替换'),NULL,_t('标签链接'),_t('文中若出现与本站标签相同的关键词则自动添加标签页链接')); + $form->addInput($tagslink); + $limits = new Typecho_Widget_Helper_Form_Element_Text('limits',NULL,"1",_t('链接频率'),_t('文中有多个重复关键词或标签时可限制链接替换次数')); + $limits->input->setAttribute('style','width:60px'); + $form->addInput($limits->addRule('isInteger',_t('请填写整数数字'))); + } + + /** + * 个人用户的配置面板 + * + * @access public + * @param Typecho_Widget_Helper_Form $form + * @return void + */ + public static function personalConfig(Typecho_Widget_Helper_Form $form){} + + /** + * 关键词与标签替换 + * + * @access public + * @param string $content + * @return string + */ + public static function kwparse($content,$widget,$lastResult) + { + $content = empty($lastResult)?$content:$lastResult; + + $db = Typecho_Db::get(); + $settings = Helper::options()->plugin('Keywords'); + $limit = $settings->limits; - /** - * 个人用户的配置面板 - * - * @access public - * @param Typecho_Widget_Helper_Form $form - * @return void - */ - public static function personalConfig(Typecho_Widget_Helper_Form $form){} + $tagselect = $db->select()->from('table.metas')->where('type=?','tag'); + $tagdata = $db->fetchAll($tagselect,array($widget->widget('Widget_Abstract_Metas'),'filter')); - public static function kwparse($content,$widget,$lastResult) { - $content = empty($lastResult)?$content:$lastResult; - $settings = Helper::options()->plugin('Keywords'); - $limit = $settings->limits; - $db = Typecho_Db::get(); - $tagselect = $db->select()->from('table.metas')->where('type = ?', 'tag'); - $tagdata = $db->fetchAll($tagselect,array($widget->widget('Widget_Abstract_Metas'),'filter')); - $wlsets = explode("\n", $settings->keywords); - if (!empty($settings->keywords)&&true==strchr($settings->keywords, '|')) { - foreach ($wlsets as $wlset) { - $wlarray = explode("|", $wlset); - $content = preg_replace('/(?!<[^>]*)('.$wlarray[0].')(?![^<]*>)/i',''.$wlarray[0].'',$content,$limit); + $wlsets = explode("\n", $settings->keywords); + + if (!empty($settings->keywords)&&true==strchr($settings->keywords,'|')) { + foreach ($wlsets as $wlset) { + $wlarray = explode("|",$wlset); + $content = preg_replace('/(?!<[^>]*)('.$wlarray[0].')(?![^<]*>)/i',''.$wlarray[0].'',$content,$limit); + } + } + + if ($tagdata&&$settings->tagslink) { + foreach ($tagdata as $tag) { + $content = preg_replace('/(?!<[^>]*)('.$tag['name'].')(?![^<]*>)/i',''.$tag['name'].'',$content,$limit); } - } - if ($tagdata&&$settings->tagslink) { - foreach ($tagdata as $tag) { - $content = preg_replace('/(?!<[^>]*)('.$tag['name'].')(?![^<]*>)/i',''.$tag['name'].'',$content,$limit); - } - } - return $content; - } + } + + return $content; + } + } \ No newline at end of file