原方案
替换文章输出
将post.php中的<?php $this->content(); ?>换成
<?php
$db = Typecho_Db::get();
$sql = $db->select()->from('table.comments')
->where('cid = ?',$this->cid)
->where('mail = ?', $this->remember('mail',true))
->where('status = ?', 'approved')
//只有通过审核的评论才能看回复可见内容
->limit(1);
$result = $db->fetchAll($sql);
if($this->user->hasLogin() || $result) {
$content = preg_replace("/\[hide\](.*?)\[\/hide\]/sm",'<div class="reply2view">$1</div>',$this->content);
}
else{
$content = preg_replace("/\[hide\](.*?)\[\/hide\]/sm",'<div class="reply2view">此处内容需要评论回复后方可阅读。</div>',$this->content);
}
echo $content
?>
使用类
后来就新建了一个类,使用<?php Content::echoSomeFunny(); ?>
class Content{
/**
* 替换文章输出实现短代码功能
* Author:Wibus
* Date:12-27
*/
function echoSomeFunny(){
$db = Typecho_Db::get();
$sql = $db->select()->from('table.comments')
->where('cid = ?',$this->cid)
->where('mail = ?', $this->remember('mail',true))
->where('status = ?', 'approved')
//只有通过审核的评论才能看回复可见内容
->limit(1);
$result = $db->fetchAll($sql);
if($this->user->hasLogin() || $result) {
$content = preg_replace("/\[hide\](.*?)\[\/hide\]/sm",'<div class="reply2view">$1</div>',$this->content);
}
else{
$content = preg_replace("/\[hide\](.*?)\[\/hide\]/sm",'<div class="reply2view">此处内容需要评论回复后方可阅读。</div>',$this->content);
}
echo $content;
}
}
那么就是将post.php中的<?php $this->content(); ?>换成<?php Content::echoSomeFunny();
解决feed暴露
解决缩略内容和feed暴露问题。
在functions.php中加入如下代码即可
Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('hide_feed_kill','one');
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('hide_feed_kill','one');
class hide_feed_kill {
public static function one($con,$obj,$text)
{
$text = empty($text)?$con:$text;
if(!$obj->is('single')){
$text = preg_replace("/\[hide\](.*?)\[\/hide\]/sm",'',$text);
}
return $text;
}
}
Typecho_Plugin::factory就是用插件接口,在缩略内容输出之前,隐藏掉或者替换掉回复可见内容,同时使用if判断,来针对非single页面进行隐藏。
使用方法
在写文章需要隐藏部分内容时用以下写法
[hide]要隐藏的内容[/hide]
不过这个方法会有一个问题:只能有一个短代码,所以需要找到其他的方法
后续解决
/**
* 短代码模块
* Author:Wibus
* 参照项目:Wordpress handsome
* Date:2020-12-27
* 相关function:
* get_shortcode_regex,shortcode_parse_atts,get_shortcode_atts_regex,get_markdown_regex,scodeParseCallback,parseContentPublic,postContent,postContentHtml
*/
后来翻找了一下Wordpress程序以及handsome主题,发现有类似的模块,比如
CodeBlock Loading...
先匹配正则表达式,之后我们再获取短代码类型
CodeBlock Loading...
这个时候我们的正则就有用了
/**
* 短代码解析正则替换回调函数
* Date: 2020-12-27
* Style: Super_Code
* @param $matches
* @return bool|string
*/
public static function scodeParseCallback($matches)
{
// 不解析类似 [[player]] 双重括号的代码
if ($matches[1] == '[' && $matches[6] == ']') {
return substr($matches[0], 1, -1);
}
//[scode type="share"]这是灰色的短代码框,常用来引用资料什么的[/scode]
$attr = htmlspecialchars_decode($matches[3]);//还原转义前的参数列表
$attrs = self::shortcode_parse_atts($attr);//获取短代码的参数
$type = "info";
switch (@$attrs['type']) {
//example
case 'yellow':
$type = "Scode-Yellow";
break;
}
return '<div class=" ' . $type . '">'."\n\n" . $matches[5] . "\n".'</div>';
}
最后输出即可
CodeBlock Loading...
其中,getshortcoderegex,shortcodeparseatts,getshortcodeattsregex,getmarkdown_regex都是wp里的东西
剩下的就是用来替换的了,注释应该都写的挺清楚的了吧?
调用方法
将post.php里的<?php $this->content(); ?>替换为<?php Content::postContentHtml($this,$this->user->hasLogin()); ?>·