2022-02-02 21:09:24 +08:00
< ? php
if ( ! defined ( '__TYPECHO_ROOT_DIR__' )) exit ;
2022-02-14 17:15:20 +08:00
/**
* 主题设置
*/
function themeConfig ( $form )
{
echo ' < style >
p . notice {
line - height : 1.75 ;
padding : . 5 rem ;
padding - left : . 75 rem ;
border - left : solid 4 px #fbbc05;
background : rgba ( 0 , 0 , 25 , . 025 );
} </ style > ' ;
$defaultBanner = new Typecho_Widget_Helper_Form_Element_Text ( 'defaultBanner' , null , '' , '首页顶部大图' , '可以填写随机图 API。' );
$form -> addInput ( $defaultBanner );
$indexBannerTitle = new Typecho_Widget_Helper_Form_Element_Text ( 'indexBannerTitle' , null , '' , '首页顶部大标题' , '不要太长' );
$form -> addInput ( $indexBannerTitle );
$indexBannerSubtitle = new Typecho_Widget_Helper_Form_Element_Text ( 'indexBannerSubtitle' , null , '' , '首页顶部小标题' , '' );
$form -> addInput ( $indexBannerSubtitle );
$colorScheme = new Typecho_Widget_Helper_Form_Element_Radio ( 'colorScheme' , array ( '0' => '自动切换' , '1' => '日间模式' , '2' => '夜间模式' ), '0' , '主题颜色模式' , '选择主题颜色模式。自动模式下每天 22:00 到次日 06:59 会显示为夜间模式。' );
$form -> addInput ( $colorScheme );
$indexStyle = new Typecho_Widget_Helper_Form_Element_Radio ( 'indexStyle' , array (
'0' => '双栏' ,
'1' => '单栏' ), '0' , '首页版式' , '选择单栏或者双栏瀑布流' );
$form -> addInput ( $indexStyle );
// 高级设置
$reward = new Typecho_Widget_Helper_Form_Element_Text ( 'reward' , null , '' , '打赏二维码' , '图片链接,只允许一张图片,更多请自行合成。' );
$form -> addInput ( $reward );
$serifincontent = new Typecho_Widget_Helper_Form_Element_Radio ( 'serifincontent' , array ( '0' => '不启用' , '1' => '启用' ), '0' , '文章内容使用衬线体' , '是否对文章内容启用衬线体(思源宋体)。此服务由 Google Fonts 提供,可能会有加载较慢的情况。' );
$form -> addInput ( $serifincontent );
$lazyload = new Typecho_Widget_Helper_Form_Element_Radio ( 'lazyload' , array ( '1' => '启用' , '0' => '不启用' ), '1' , '图片懒加载' , '是否启用图片懒加载。' );
$form -> addInput ( $lazyload );
$enableMath = new Typecho_Widget_Helper_Form_Element_Radio ( 'enableMath' , array ( '0' => '不启用' , '1' => '启用' ), '0' , '启用数学公式解析' , '是否启用数学公式解析。启用后会多加载 1~2M 的资源。' );
$form -> addInput ( $enableMath );
$head = new Typecho_Widget_Helper_Form_Element_Textarea ( 'head' , null , '' , 'head 标签输出内容' , '统计代码等。' );
$form -> addInput ( $head );
$footer = new Typecho_Widget_Helper_Form_Element_Textarea ( 'footer' , null , '' , 'footer 标签输出内容' , '备案号等。' );
$form -> addInput ( $footer );
}
2022-02-03 22:53:54 +08:00
/**
* 获取文章缩略图
* 获取顺序: 自定义字段( thumb) > 文章第一张图片 > 系统图库
* @ param $obj
* @ return 获取文章头图
*/
2022-02-05 16:58:32 +08:00
function get_postthumb ( $obj )
{
2022-02-03 22:53:54 +08:00
2022-02-05 16:58:32 +08:00
if ( isset ( $obj -> fields -> thumb )) {
2022-02-03 22:53:54 +08:00
return $obj -> fields -> thumb ;
}
2022-02-05 16:58:32 +08:00
preg_match ( " /<img[^>]+src \ s*= \ s*[' \" ]([^' \" ]+)[' \" ][^>]*>/ " , $obj -> content , $matches );
if ( isset ( $matches [ 1 ])) {
2022-02-03 22:53:54 +08:00
return $matches [ 1 ];
}
2022-02-05 16:58:32 +08:00
preg_match ( " /! \ [(.*?) \ ] \ ((.*?) \ )/ " , $obj -> content , $matches );
if ( isset ( $matches [ 2 ])) {
return $matches [ 2 ];
2022-02-03 22:53:54 +08:00
}
2022-02-05 16:58:32 +08:00
$thumbPath = __DIR__ . '/static/medias/featureimages/' ;
2022-02-03 22:53:54 +08:00
$files = scandir ( $thumbPath );
$thumbs = [];
for ( $i = 0 ; $i < count ( $files ); $i ++ ) {
2022-02-05 16:58:32 +08:00
if ( is_file ( $thumbPath . $files [ $i ])) {
2022-02-03 22:53:54 +08:00
$thumbs [] = $files [ $i ];
}
}
2022-02-05 16:58:32 +08:00
return Helper :: options () -> themeUrl . '/static/medias/featureimages/' . $thumbs [ rand ( 0 , count ( $thumbs ) - 1 )];
2022-02-03 22:53:54 +08:00
}
/**
* @ param $obj 对象
* @ return string 上一页url
*/
2022-02-05 16:58:32 +08:00
function getPrevPageLink ( $obj )
{
2022-02-03 22:53:54 +08:00
ob_start ();
$obj -> pagelink ( " prev " , " prev " );
$pageLink = ob_get_contents ();
ob_end_clean ();
preg_match ( " /<a[^>]+href \ s*= \ s*[' \" ]([^' \" ]+)[' \" ][^>]*>/ " , $pageLink , $matches );
2022-02-05 16:58:32 +08:00
if ( isset ( $matches [ 1 ])) {
2022-02-03 22:53:54 +08:00
return $matches [ 1 ];
}
return " # " ;
}
/**
* @ param $obj 对象
* @ return string 下一页url
*/
2022-02-05 16:58:32 +08:00
function getNextPageLink ( $obj )
{
2022-02-03 22:53:54 +08:00
ob_start ();
$obj -> pageLink ( " next " , " next " );
$pageLink = ob_get_contents ();
ob_end_clean ();
preg_match ( " /<a[^>]+href \ s*= \ s*[' \" ]([^' \" ]+)[' \" ][^>]*>/ " , $pageLink , $matches );
2022-02-05 16:58:32 +08:00
if ( isset ( $matches [ 1 ])) {
2022-02-03 22:53:54 +08:00
return $matches [ 1 ];
}
return " # " ;
2022-02-05 16:58:32 +08:00
}
/**
* 统计文章字数
*
* @ param $cid
*/
function articleCount ( $cid )
{
$db = Typecho_Db :: get ();
$rs = $db -> fetchRow ( $db -> select ( 'table.contents.text' ) -> from ( 'table.contents' ) -> where ( 'table.contents.cid=?' , $cid ) -> order ( 'table.contents.cid' , Typecho_Db :: SORT_ASC ) -> limit ( 1 ));
$text = preg_replace ( " /[^ \ x { 4e00}- \ x { 9fa5}]/u " , " " , $rs [ 'text' ]);
return mb_strlen ( $text , 'UTF-8' );
2022-02-05 18:23:52 +08:00
}
/**
* 获取文章访问数
* @ param $archive
* @ return string | void
* @ throws Typecho_Db_Exception
*/
function articleViewsNum ( $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' )) {
$db -> query ( $db -> update ( 'table.contents' ) -> rows ( array ( 'views' => ( int ) $row [ 'views' ] + 1 )) -> where ( 'cid = ?' , $cid ));
}
if ( $row [ 'views' ] < 1000 ){
return $row [ 'views' ];
} else {
return ( $row [ 'views' ] / 1000 ) . 'k' ;
}
2022-02-03 22:53:54 +08:00
}