├── Plugin.php └── README.md /Plugin.php: -------------------------------------------------------------------------------- 1 | contentEx = array('Keywords_Plugin','kwparse'); 24 | Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('Keywords_Plugin','kwparse'); 25 | } 26 | 27 | /** 28 | * 禁用插件方法,如果禁用失败,直接抛出异常 29 | * 30 | * @static 31 | * @access public 32 | * @return void 33 | * @throws Typecho_Plugin_Exception 34 | */ 35 | public static function deactivate(){} 36 | 37 | /** 38 | * 获取插件配置面板 39 | * 40 | * @access public 41 | * @param Typecho_Widget_Helper_Form $form 配置面板 42 | * @return void 43 | */ 44 | public static function config(Typecho_Widget_Helper_Form $form) 45 | { 46 | $keywords = new Typecho_Widget_Helper_Form_Element_Textarea('keywords',NULL,'',_t('关键词链接'),_t('每行1组以"关键词|(半角竖线)链接"形式填写, 可用第2个竖线追加参数:
47 | n代表nofollow标记, e代表external nofollow标记, b代表本窗口打开. 例:
google|http://www.google.com|n 即此链接带nofollow(默认新窗口打开)')); 48 | $keywords->input->setAttribute('style','max-width:400px;height:150px;'); 49 | $form->addInput($keywords); 50 | 51 | $autolink = new Typecho_Widget_Helper_Form_Element_Checkbox('autolink',array('catslink'=>_t('分类名称'),'tagslink'=>_t('标签名称')),NULL,_t('自动内链'),_t('将与分类/标签名相同的词替换为分类/标签页链接')); 52 | $form->addInput($autolink); 53 | 54 | $nofollow = new Typecho_Widget_Helper_Form_Element_Checkbox('nofollow', 55 | array(1=>_t('nofollow标记')),NULL,_t('内链设置')); 56 | $form->addInput($nofollow); 57 | 58 | $blank = new Typecho_Widget_Helper_Form_Element_Select('blank', 59 | array(0=>_t('本窗口打开'),1=>_t('新窗口打开')),0,''); 60 | $blank->input->setAttribute('style','position:absolute;bottom:11px;left:115px;'); 61 | $blank->setAttribute('style','position:relative;'); 62 | $form->addInput($blank); 63 | 64 | $limits = new Typecho_Widget_Helper_Form_Element_Text('limits',NULL,'1',_t('链接频次'),_t('文中有多个重复关键词时可指定替换为链接的次数')); 65 | $limits->input->setAttribute('style','width:40px;'); 66 | $limits->addRule('required',_t('链接次数不能为空')); 67 | $form->addInput($limits->addRule('isInteger',_t('请填写整数数字'))); 68 | 69 | $pagelinks = new Typecho_Widget_Helper_Form_Element_Radio('pagelinks',array(1=>_t('是'),0=>_t('否')),1,_t('页面使用'),_t('除文章外是否将替换链接效果作用于独立页面内容')); 70 | $form->addInput($pagelinks); 71 | } 72 | 73 | /** 74 | * 个人用户的配置面板 75 | * 76 | * @access public 77 | * @param Typecho_Widget_Helper_Form $form 78 | * @return void 79 | */ 80 | public static function personalConfig(Typecho_Widget_Helper_Form $form){} 81 | 82 | /** 83 | * 执行数据替换 84 | * 85 | * @access public 86 | * @param string $content 87 | * @return string 88 | */ 89 | public static function kwparse($content,$widget,$lastResult) 90 | { 91 | $content = empty($lastResult) ? $content : $lastResult; 92 | $keywords = self::keywords(); 93 | 94 | if ($widget instanceof Widget_Archive && $keywords) { 95 | $settings = Helper::options()->plugin('Keywords'); 96 | 97 | //关闭页面内容替换 98 | if ($widget->is('page') && !$settings->pagelinks) { 99 | return $content; 100 | } 101 | foreach ($keywords as $i=>$row) { 102 | $txt = trim($row['0']); 103 | if ($txt) { 104 | $link = trim($row['1']); 105 | $set = trim($row['2']); 106 | $rel = ''; 107 | $open = '_blank'; 108 | 109 | //处理标记与打开方式 110 | if ($set) { 111 | if (false!==stripos($set,'e')) { 112 | $rel = ' rel="external nofollow"'; 113 | } elseif (false!==stripos($set,'n')) { 114 | $rel = ' rel="nofollow"'; 115 | } 116 | $open = false!==stripos($set,'b') ? '_self' : $open; 117 | } 118 | 119 | $content = false!==strpos($content,$txt) 120 | //正则排除参数和链接 121 | ? preg_replace('/(?!<[^>]*)'.$txt.'(?![^<]*(>|<\/[a|sc]))/s' 122 | ,''.$txt.'',$content,$settings->limits) : $content; 123 | } 124 | } 125 | } 126 | 127 | return $content; 128 | } 129 | 130 | /** 131 | * 输出关键词数据 132 | * 133 | * @access private 134 | * @return array 135 | */ 136 | private static function keywords() 137 | { 138 | $settings = Helper::options()->plugin('Keywords'); 139 | $autolink = $settings->autolink; 140 | $kwarray = array(); 141 | $keyword = trim(Typecho_Common::stripTags($settings->keywords)); 142 | 143 | if (strpos($keyword,'|')) { 144 | //解析关键词数组 145 | $kwsets = array_filter(preg_split("/(\r|\n|\r\n)/",$keyword)); 146 | foreach ($kwsets as $kwset) { 147 | $kwarray[] = explode('|',$kwset); 148 | } 149 | } 150 | 151 | if ($autolink) { 152 | $db = Typecho_Db::get(); 153 | $nofollow = $settings->nofollow ? 'n' : ''; 154 | $blank = $settings->blank ? '' : 'b'; 155 | 156 | if (in_array('catslink',$autolink)) { 157 | $catselect = $db->select()->from('table.metas')->where('type = ?','category'); 158 | $catdata = $db->fetchAll($catselect,array(Typecho_Widget::widget('Widget_Abstract_Metas'),'filter')); 159 | 160 | //并入分类链接 161 | $cats = array(); 162 | foreach ($catdata as $cat) { 163 | $cats[] = array($cat['name'],$cat['permalink'],$nofollow.$blank); 164 | } 165 | $kwarray = array_merge($kwarray,$cats); 166 | } 167 | 168 | if (in_array('tagslink',$autolink)) { 169 | $tagselect = $db->select()->from('table.metas')->where('type = ?','tag'); 170 | $tagdata = $db->fetchAll($tagselect,array(Typecho_Widget::widget('Widget_Abstract_Metas'),'filter')); 171 | 172 | //并入标签链接 173 | if ($tagdata) { 174 | $tags = array(); 175 | foreach ($tagdata as $tag) { 176 | $tags[] = array($tag['name'],$tag['permalink'],$nofollow.$blank); 177 | } 178 | $kwarray = array_merge($kwarray,$tags); 179 | } 180 | } 181 | } 182 | 183 | if ($kwarray) { 184 | //优先处理长词 185 | usort($kwarray,array(new Keywords_Plugin,'lsort')); 186 | } 187 | 188 | return $kwarray; 189 | } 190 | 191 | /** 192 | * 按字符长短排序 193 | * 194 | * @access private 195 | * @return integer 196 | */ 197 | private static function lsort($a,$b) { 198 | return strlen($a['0']) http://www.yzmb.me/archives/net/keywords-for-typecho --------------------------------------------------------------------------------