Typecho内置的md解析/非插件实现路由/延伸:评论发布后可修改(可能会用到的

插件开发及发布

版主: woniou

回复
头像
远.山
帖子: 66
注册时间: 2017年 2月 2日 19:48
来自: https://moefo.cn

Typecho内置的md解析/非插件实现路由/延伸:评论发布后可修改(可能会用到的

帖子 远.山 »

非插件实现路由出自。kotori大佬 https://kotori.love/archives/typecho-theme-tips.html


$parser = new HyperDown();
$parser->makeHtml(此处为MD内容);

因为最近在写ajax评论,评论想获取解析后的,所以翻看typecho源码找到此方法.

以下所有方法需要放在主题function.php里的themeInit下。

ajax访问生成的路由, 组成为:网址/邮箱/父评论id

数据库进行查询后按时间排序,利用内置md解析,把评论转为html(如果不开启md评论的话这步可以刨除.

这样就能制作一个很简单的评论ajax请求返回

if ($archive->is('archive', 404)){
header( "HTTP/1.1 200 OK" );
$info=explode("/",$archive->request->getPathinfo());
$db = Typecho_Db::get();
if($info[1]){
$t = $db->fetchRow($db->select('text','coid')->from('table.comments')->where('mail = ?', $info[0])->where('parent = ?', $info[1])->order('mid',Typecho_Db::SORT_DESC));
}else{
$t = $db->fetchRow($db->select('text','coid')->from('table.comments')->where('mail = ?', $info[0])->order('mid',Typecho_Db::SORT_DESC));
}
$parser = new HyperDown();
echo 'coid:'.$t['coid'].'text:'$parser->makeHtml($t['text']);
exit;
}


另一些通过非插件实现路由实现的小玩具

输出所有图片合并成json 可以用来做相册.
if ($archive->is('archive', 404)){$path_info = trim($archive->request->getPathinfo(), '/');
if(strpos($path_info,".img") > 0){ //请求判断,自己改一下就ok
$db = Typecho_Db::get();
$t = $db->fetchAll($db->select('str_value')->from('table.fields')->where('name = ?' , 'img'));
header( "HTTP/1.1 200 OK" );
foreach($t as $key=>$sc){
echo '{"img":'.$sc['str_value'].'}';
}
exit;
}}


文章的md版本小玩具


if ($archive->is('archive', 404)){$path_info = trim($archive->request->getPathinfo(), '/');
if(strpos($path_info,".md") > 0){
$right = strpos($path_info, '.');
$id=substr($path_info, 0, $right);
$db = Typecho_Db::get();
$t = $db->fetchRow($db->select('title','text')->from('table.contents')->where('cid = ?', $id));
header( "HTTP/1.1 200 OK" );
echo '<h3>'.$t['title'].'</h3>'.'<pre style="word-wrap: break-word; white-space: pre-wrap;">'.$t[text].'</pre>';
exit;
}}


对post请求处理.基础版

if ($archive->is('archive', 404)){
header( "HTTP/1.1 200 OK" );
if($archive->request->isPost()){
print_r($_POST['name']);
}
exit;
}

可以延伸出对评论成功后的可编辑,后台做好判断即可.
上次由 远.山 在 2019年 7月 8日 21:38,总共编辑 1 次。
头像
远.山
帖子: 66
注册时间: 2017年 2月 2日 19:48
来自: https://moefo.cn

Re: Typecho内置的md解析/非插件实现路由/延伸:评论发布后可修改(可能会用到的

帖子 远.山 »

评论修改的post请求


if($archive->request->isPost()){
$text = $_POST['text'];//新的评论内容
$coid = $_POST['coid'];//评论 id
$cid = $_POST['cid'];//文章 id
$created=$db->fetchRow($db->select('created')->from('table.comments')->where('cid = ?', $cid)->where('coid = ?', $coid));//取出评论时间戳
$timeD = (time()-$created['created']);//接收到请求的时间戳减去评论时间戳
if( $timeD < 60 &&$timeD > 0 ){//小于 60 秒
$update = $db->update('table.comments')->rows(array('text' => $text))->where('coid = ?', $coid)->where('cid = ?', $cid);//执行修改
$updateRows= $db->query($update);//执行结果
}
echo $updateRows;//打印执行结果
exit;
}
头像
远.山
帖子: 66
注册时间: 2017年 2月 2日 19:48
来自: https://moefo.cn

Re: Typecho内置的md解析/非插件实现路由/延伸:评论发布后可修改(可能会用到的

帖子 远.山 »

翻来复去,还是觉得通过 ip 判断比较好,实现方法想了很多,但是只有 ip 匹配是最直接的.

因为主题是pjax 所以无法获取302重定向的地址,我知道重定向地址里有coid,关键是能不能取的到...


通过用户 ip 来查询数据库,匹配排序按照评论时间,取第一条,也就是时间戳最接近现时的评论,返回 coid。

获取用户 get 的 ip。$_SERVER["REMOTE_ADDR"]

function getgetCommentIp($ip){
$db = Typecho_Db::get();
$i = $db->fetchRow($db->select('parent')
->from('table.comments')
->where('ip = ?', $ip)->order('created',Typecho_Db::SORT_DESC));
return $i['coid'];
}


https://moe.sb/164.sb 在这篇文章里可以评论修改,不过因为测试原因,修改内容为我指定内容
头像
jrotty
帖子: 428
注册时间: 2015年 11月 2日 19:30

Re: Typecho内置的md解析/非插件实现路由/延伸:评论发布后可修改(可能会用到的

帖子 jrotty »

非插件实现路由,如果开启了debug模式好像就不行了
回复