summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBigCoke233 <hi@guhub.cn>2020-07-10 22:58:49 +0800
committerBigCoke233 <hi@guhub.cn>2020-07-10 22:58:49 +0800
commit4901b642cd90a9124a4ca5732210707caa030290 (patch)
tree5bb5954a5b306e88e5125d4b44a80710c1f146c0
parent39b4e65156bb35e5a23ef1904d16716cd9562ab1 (diff)
:zap: Perf: 兼容 typecho 自带反垃圾功能,并优化代码结构
-rw-r--r--functions.php93
-rw-r--r--includes/comments.php19
-rw-r--r--includes/footer.php4
-rw-r--r--includes/head.php4
-rw-r--r--includes/header.php4
-rw-r--r--libs/Comments.php51
-rw-r--r--libs/Contents.php92
-rw-r--r--libs/Utils.php31
8 files changed, 166 insertions, 132 deletions
diff --git a/functions.php b/functions.php
index 7b52301..e9e6d79 100644
--- a/functions.php
+++ b/functions.php
@@ -18,7 +18,7 @@ Typecho_Plugin::factory('admin/write-page.php')->bottom = array('Utils', 'addBut
*/
function themeInit($archive) {
if ($archive->hidden) header('HTTP/1.1 200 OK');//暴力解决访问加密文章会被 pjax 刷新页面的问题
- Helper::options()->commentsAntiSpam = false;//关闭反垃圾
+ //Helper::options()->commentsAntiSpam = false; 关闭反垃圾
Helper::options()->commentsHTMLTagAllowed = '<a href=""> <img src=""> <img src="" class=""> <code> <del>';
Helper::options()->commentsMaxNestingLevels = '9999';//最大嵌套层数
Helper::options()->commentsPageDisplay = 'first';//强制评论第一页
@@ -46,93 +46,4 @@ function themeFields(Typecho_Widget_Helper_Layout $layout) {
function themeVersion() {
$info = Typecho_Plugin::parseInfo(__DIR__ . '/index.php');
return $info['version'];
-}
-
-/**
- * 统计文章浏览数
- * from:http://docs.qqdie.com/
- */
-function get_post_view($archive) {
- $cid = $archive->cid;
- $db = Typecho_Db::get();
- $prefix = $db->getPrefix();
- if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {
- $db->query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT(10) DEFAULT 0;');
- echo 0;
- return;
- }
- $row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid));
- if ($archive->is('single')) {
- $views = Typecho_Cookie::get('extend_contents_views');
- if(empty($views)){
- $views = array();
- }else{
- $views = explode(',', $views);
- }
-if(!in_array($cid,$views)){
- $db->query($db->update('table.contents')->rows(array('views' => (int) $row['views'] + 1))->where('cid = ?', $cid));
-array_push($views, $cid);
- $views = implode(',', $views);
- Typecho_Cookie::set('extend_contents_views', $views); //记录查看cookie
- }
- }
- echo $row['views'];
-}
-/**
- * 自动生成引用
- */
-function generate_require($files,$type,$cdn,$custom) {
- if ($cdn=='1'){
- $path = 'https://cdn.jsdelivr.net/gh/BigCoke233/miracles@'.themeVersion()."/assets/";
- }
- elseif ($cdn=='2'){
- $path = 'https://raw.githack.com/BigCoke233/miracles/master/assets/';
- }
- elseif ($cdn=='4'){
- $path = 'https://rawcdn.githack.com/BigCoke233/miracles/master/assets/';
- }
- elseif ($cdn=='5'){
- $path = 'https://cdn.9jojo.cn/Miracles/'.themeVersion().'/';
- }
- elseif ($cdn=='3'){
- $path = $custom;
- }else{
- $path = Helper::options()->themeUrl("","Miracles/assets");
- }
- foreach ($files as $file) {
- if ($type == "js"){
- echo "<script src=".$path. $type."/".$file.".".$type."></script>";
- }
- elseif ($type == "css"){
- echo "<link rel=\"stylesheet\" href=".$path. $type."/".$file.".".$type." />";
- }
- }
-}
-
-/**
- * 获取上级评论人
- */
- function getCommentHF($coid){
- $db = Typecho_Db::get();
- $prow = $db->fetchRow($db->select('parent')
- ->from('table.comments')
- ->where('coid = ? AND status = ?', $coid, 'approved'));
- $parent = $prow['parent'];
- if ($parent != "0") {
- $arow = $db->fetchRow($db->select('text','author','status')
- ->from('table.comments')
- ->where('coid = ?', $parent));
- $author = $arow['author'];
- $status = $arow['status'];
- if($author){
- if($status=='approved'){
- $href = '<a class="at" uid="'.$parent.'" href="#li-comment-'.$parent.'">@'.$author.'</a>';;
- }else if($status=='waiting'){
- $href = '<a>评论审核中···</a>';
- }
- }
- echo $href;
- } else {
- echo "";
- }
- } \ No newline at end of file
+} \ No newline at end of file
diff --git a/includes/comments.php b/includes/comments.php
index 1b5ace5..b414021 100644
--- a/includes/comments.php
+++ b/includes/comments.php
@@ -28,7 +28,7 @@ echo $commentClass;
<span class="comment-reply">
<?php $comments->reply('<i class="iconfont icon-return"></i>'); ?>
</span>
- <?php getCommentHF($comments->coid); echo Contents::parseEmo($comments->content); ?>
+ <?php Comments::getCommentHF($comments->coid); echo Contents::parseEmo($comments->content); ?>
</div>
<p class="comment-meta">
<span class="comment-author"><a href="<?php echo $comments->url; ?>" target="_blank"><?php echo $comments->author; ?></a></span>
@@ -63,8 +63,8 @@ echo $commentClass;
'dateFormat' => 'Y-m-d H:i'
)); ?>
<div class="comment-pagenav">
- <?php $comments->pageNav('<i class="iconfont icon-chevron-left comment-pagenav-icon"></i>', '<i class="iconfont icon-chevron-right comment-pagenav-icon"></i>'); ?>
- </div>
+ <?php $comments->pageNav('<i class="iconfont icon-chevron-left comment-pagenav-icon"></i>', '<i class="iconfont icon-chevron-right comment-pagenav-icon"></i>'); ?>
+ </div>
</div>
<?php endif;//有无评论判断结束?>
@@ -103,11 +103,10 @@ echo $commentClass;
</form>
</div>
<!-- 若当前文章不允许进行评论 -->
- <?php else: ?>
- <div class="comment-hidden"><?php _e('博主已关闭本页面的评论功能'); ?></div>
- <!-- 防止找不到 owo 容器而报错 -->
- <div style="display:none" class="OwO"></div>
- <?php endif; ?>
+ <?php else: ?>
+ <div class="comment-hidden"><?php _e('博主已关闭本页面的评论功能'); ?></div>
+ <?php endif; ?>
</div>
- </div>
- \ No newline at end of file
+ </div>
+ <?php //兼容 typecho 反垃圾
+ if ($this->options->commentsAntiSpam) { Comments::AntiSpam($this); } ?> \ No newline at end of file
diff --git a/includes/footer.php b/includes/footer.php
index 64f8c7c..e9e0c08 100644
--- a/includes/footer.php
+++ b/includes/footer.php
@@ -24,7 +24,7 @@
<?php
$js_files=array("jquery","pjax.jquery","gazeimg","nprogress","OwO.min","highlight","highlight-line-number","pangu","qrcode.min","details-element-polyfill");
if($this->options->customCDN): $custom=$this->options->customCDN; else: $custom=Helper::options()->themeUrl("","Miracles"); endif;
- generate_require($js_files,"js",$this->options->CDN,$custom);
+ Utils::addRequires($js_files,"js",$this->options->CDN,$custom);
?>
<!-- Options -->
<script>var navSlide = <?php if($this->options->navSlide==1):?>false<?php else:?>true<?php endif;?>;
@@ -42,7 +42,7 @@
<?php
$js_files=array("miracles.min","cmt.miracles");
if($this->options->customCDN): $custom=$this->options->customCDN; else: $custom=Helper::options()->themeUrl("","Miracles"); endif;
- generate_require($js_files,"js",$this->options->CDN,$custom);
+ Utils::addRequires($js_files,"js",$this->options->CDN,$custom);
?>
<!-- JavaScript -->
<script>
diff --git a/includes/head.php b/includes/head.php
index e39cce1..e3e7a93 100644
--- a/includes/head.php
+++ b/includes/head.php
@@ -40,13 +40,13 @@
$this->options->bannerUrl();
endif;}?>" />
<link rel="icon" id="icon" type="image/png" href="<?php if($this->options->favicon):$this->options->favicon();else:echo Utils::indexTheme('favicon.ico');endif; ?>" />
- <?php $this->header('description=&commentReply='); ?>
+ <?php $header->header('generator=&pingback=&xmlrpc=&wlw=&commentReply=&description=&antiSpam='); ?>
<?php $this->options->headerEcho(); ?>
<!-- css -->
<?php
$css_files=array("codestyle","owo.min","nprogress","gazeimg","main/miracles.min");
if($this->options->customCDN): $custom=$this->options->customCDN; else: $custom=Helper::options()->themeUrl("","Miracles"); endif;
- generate_require($css_files,"css",$this->options->CDN,$custom);
+ Utils::addRequires($css_files,"css",$this->options->CDN,$custom);
?>
<!-- icon font -->
<link rel="stylesheet" href="//at.alicdn.com/t/font_1165190_1djdjjwge4m.css" />
diff --git a/includes/header.php b/includes/header.php
index c38fb55..56c0db1 100644
--- a/includes/header.php
+++ b/includes/header.php
@@ -147,10 +147,10 @@
</div>
<div class="header-meta-line-two">
<i class="iconfont icon-clock" title="该文章发布于 <?php $this->date(); ?>"></i> <?php $this->date(); ?>&emsp;
- <i class="icon-view iconfont" title="该文章被浏览 <?php get_post_view($this) ?> 次"></i> <?php get_post_view($this) ?>
+ <i class="icon-view iconfont" title="该文章被浏览 <?php Contents::postViews($this) ?> 次"></i> <?php Contents::postViews($this) ?>
</div>
<?php else: ?>
- <?php $this->fields->meta(); ?><div style="display:none"><?php get_post_view($this) ?></div>
+ <?php $this->fields->meta(); ?><div style="display:none"><?php Contents::postViews($this) ?></div>
<?php endif; ?>
</div>
<?php elseif($this->is('archive')): ?>
diff --git a/libs/Comments.php b/libs/Comments.php
new file mode 100644
index 0000000..78fd087
--- /dev/null
+++ b/libs/Comments.php
@@ -0,0 +1,51 @@
+<?php
+/**
+ * Comments.php
+ * 评论处理
+ *
+ * Author: Eltrac(BigCoke233)
+ * License: MIT
+ */
+if (!defined('__TYPECHO_ROOT_DIR__')) exit;
+class Comments
+{
+ /**
+ * 评论反垃圾
+ * from ohmyga233/castle-Typecho-Theme
+ *
+ * @access public
+ */
+ public static function AntiSpam($comment) {
+ if ($comment->options->switch && in_array('html', $comment->options->switch)) { echo '<!--<nocompress>-->'; }
+ echo '<script>(function(){var a=document.addEventListener?{add:"addEventListener",focus:"focus",load:"DOMContentLoaded"}:{add:"attachEvent",focus:"onfocus",load:"onload"};var c,d,e,f,b=document.getElementById("'.$comment->respondId.'");null!=b&&(c=b.getElementsByTagName("form"),c.length>0&&(d=c[0],e=d.getElementsByTagName("textarea")[0],f=!1,null!=e&&"text"==e.name&&e[a.add](a.focus,function(){if(!f){var a=document.createElement("input");a.type="hidden",a.name="_",d.appendChild(a),f=!0,a.value='.Typecho_Common::shuffleScriptVar($comment->security->getToken($comment->request->getRequestUrl())).'}})))})();</script>';
+ if ($comment->options->switch && in_array('html', $comment->options->switch)) { echo '<!--</nocompress>-->'; }
+ }
+
+ /**
+ * 获取上级评论人
+ */
+ public static function getCommentHF($coid){
+ $db = Typecho_Db::get();
+ $prow = $db->fetchRow($db->select('parent')
+ ->from('table.comments')
+ ->where('coid = ? AND status = ?', $coid, 'approved'));
+ $parent = $prow['parent'];
+ if ($parent != "0") {
+ $arow = $db->fetchRow($db->select('text','author','status')
+ ->from('table.comments')
+ ->where('coid = ?', $parent));
+ $author = $arow['author'];
+ $status = $arow['status'];
+ if($author){
+ if($status=='approved'){
+ $href = '<a class="at" uid="'.$parent.'" href="#li-comment-'.$parent.'">@'.$author.'</a>';;
+ }else if($status=='waiting'){
+ $href = '<a>评论审核中···</a>';
+ }
+ }
+ echo $href;
+ } else {
+ echo "";
+ }
+ }
+} \ No newline at end of file
diff --git a/libs/Contents.php b/libs/Contents.php
index b424add..bad09a9 100644
--- a/libs/Contents.php
+++ b/libs/Contents.php
@@ -1,6 +1,17 @@
-<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit;
+<?php
+/**
+ * Contents.php
+ * 内容处理
+ *
+ * Author: Eltrac(BigCoke233)
+ * License: MIT
+ * Notice: Parts of codes are from AlanDecode/Typecho-Theme-VOID
+ */
+
+if (!defined('__TYPECHO_ROOT_DIR__')) exit;
class Contents
{
+ /* == 解析文章内容 == */
/**
* 内容解析器入口
@@ -248,6 +259,8 @@ class Contents
return '<h'.$matchs[1].$matchs[2].' id="'.$id.'">'.$matchs[3].'<a href="#'.$id.'" title="章节链接" class="post-toc-link no-line"><i class="iconfont icon-paragraph"></i></a></h'.$matchs[1].'>';
}
+ /* == 其他内容 == */
+
/**
* 解析自定义导航栏
*/
@@ -278,8 +291,7 @@ class Contents
* @param string $table 表名, 支持 contents, comments, metas, users
* @return Widget_Abstract
*/
- public static function widgetById($table, $pkId)
- {
+ public static function widgetById($table, $pkId) {
$table = ucfirst($table);
if (!in_array($table, array('Contents', 'Comments', 'Metas', 'Users'))) {
return NULL;
@@ -304,8 +316,7 @@ class Contents
/**
* 输出完备的标题
*/
- public static function title(Widget_Archive $archive)
- {
+ public static function title(Widget_Archive $archive) {
$archive->archiveTitle(array(
'category' => '分类 %s 下的文章',
'search' => '包含关键字 %s 的文章',
@@ -315,26 +326,57 @@ class Contents
Helper::options()->title();
}
- /**
- * 内容归档
- */
- public static function archives($widget) {
- $db = Typecho_Db::get();
- $rows = $db->fetchAll($db->select()
- ->from('table.contents')
- ->order('table.contents.created', Typecho_Db::SORT_DESC)
- ->where('table.contents.type = ?', 'post')
- ->where('table.contents.status = ?', 'publish'));
+ /**
+ * 内容归档
+ */
+ public static function archives($widget) {
+ $db = Typecho_Db::get();
+ $rows = $db->fetchAll($db->select()
+ ->from('table.contents')
+ ->order('table.contents.created', Typecho_Db::SORT_DESC)
+ ->where('table.contents.type = ?', 'post')
+ ->where('table.contents.status = ?', 'publish'));
- $stat = array();
- foreach ($rows as $row) {
- $row = $widget->filter($row);
- $arr = array(
- 'title' => $row['title'],
- 'permalink' => $row['permalink']);
-
- $stat[date('Y', $row['created'])][$row['created']] = $arr;
+ $stat = array();
+ foreach ($rows as $row) {
+ $row = $widget->filter($row);
+ $arr = array(
+ 'title' => $row['title'],
+ 'permalink' => $row['permalink']
+ );
+ $stat[date('Y', $row['created'])][$row['created']] = $arr;
+ }
+ return $stat;
+ }
+
+ /**
+ * 统计文章浏览数
+ * from:http://docs.qqdie.com/
+ */
+ public static function postViews($archive) {
+ $cid = $archive->cid;
+ $db = Typecho_Db::get();
+ $prefix = $db->getPrefix();
+ if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {
+ $db->query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT(10) DEFAULT 0;');
+ echo 0;
+ return;
+ }
+ $row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid));
+ if ($archive->is('single')) {
+ $views = Typecho_Cookie::get('extend_contents_views');
+ if(empty($views)){
+ $views = array();
+ }else{
+ $views = explode(',', $views);
+ }
+ if(!in_array($cid,$views)){
+ $db->query($db->update('table.contents')->rows(array('views' => (int) $row['views'] + 1))->where('cid = ?', $cid));
+ array_push($views, $cid);
+ $views = implode(',', $views);
+ Typecho_Cookie::set('extend_contents_views', $views); //记录查看cookie
+ }
+ }
+ echo $row['views'];
}
- return $stat;
- }
} \ No newline at end of file
diff --git a/libs/Utils.php b/libs/Utils.php
index ab3cb9a..69ebe60 100644
--- a/libs/Utils.php
+++ b/libs/Utils.php
@@ -109,4 +109,35 @@ class Utils
}
}</style>';
}
+
+ /**
+ * 自动生成引用
+ */
+ function addRequires($files,$type,$cdn,$custom) {
+ if ($cdn=='1'){
+ $path = 'https://cdn.jsdelivr.net/gh/BigCoke233/miracles@'.themeVersion()."/assets/";
+ }
+ elseif ($cdn=='2'){
+ $path = 'https://raw.githack.com/BigCoke233/miracles/master/assets/';
+ }
+ elseif ($cdn=='4'){
+ $path = 'https://rawcdn.githack.com/BigCoke233/miracles/master/assets/';
+ }
+ elseif ($cdn=='5'){
+ $path = 'https://cdn.9jojo.cn/Miracles/'.themeVersion().'/';
+ }
+ elseif ($cdn=='3'){
+ $path = $custom;
+ }else{
+ $path = Helper::options()->themeUrl("","Miracles/assets");
+ }
+ foreach ($files as $file) {
+ if ($type == "js"){
+ echo "<script src=".$path. $type."/".$file.".".$type."></script>";
+ }
+ elseif ($type == "css"){
+ echo "<link rel=\"stylesheet\" href=".$path. $type."/".$file.".".$type." />";
+ }
+ }
+ }
} \ No newline at end of file