forked from xiaopanglian/icefox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
1402 lines (1220 loc) · 42.1 KB
/
functions.php
File metadata and controls
1402 lines (1220 loc) · 42.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* icefox主题全新3.0版本
*
* @package icefox
* @author 小胖脸
* @version 3.0.3
* @link https://xiaopanglian.com
*/
include_once 'comment_function.php';
include_once 'core/core.php';
include_once __TYPECHO_ROOT_DIR__ . '/var/Utils/Markdown.php';
/**
* 主题初始化
*/
function themeInit($archive)
{
// 设置默认模板
$archive->template = 'index';
// 设置每页显示文章数量
$options = Helper::options();
if ($options->pageSize) {
$archive->parameter->pageSize = $options->pageSize;
}
}
/**
* 主题配置
*/
function themeConfig($form)
{
// 站点顶部背景视频
$topVideo = new Typecho_Widget_Helper_Form_Element_Text('topVideo', NULL, NULL, _t('站点顶部背景视频'), _t('在这里填入一个视频URL地址,优先级高于背景图片'));
$form->addInput($topVideo);
// 站点顶部背景图片
$topImage = new Typecho_Widget_Helper_Form_Element_Text('topImage', NULL, NULL, _t('站点顶部背景图片'), _t('在这里填入一个图片URL地址,当没有设置背景视频时显示'));
$form->addInput($topImage);
// 站点 Logo 地址
$logoUrl = new Typecho_Widget_Helper_Form_Element_Text('logoUrl', NULL, NULL, _t('站点 Logo 地址'), _t('在这里填入站点 Logo 图片的 URL 地址,显示在头部用户信息区域'));
$form->addInput($logoUrl);
// 头像点击跳转链接
$avatarLink = new Typecho_Widget_Helper_Form_Element_Text('avatarLink', NULL, NULL, _t('头像点击跳转链接'), _t('在这里填入头像点击后的跳转地址,留空则不跳转。例如: /archives 或完整URL'));
$form->addInput($avatarLink);
// 网站备案信息
$beianInfo = new Typecho_Widget_Helper_Form_Element_Text('beianInfo', NULL, NULL, _t('网站备案信息'), _t('在这里填入网站备案号,例如:蜀ICP备2000101010号。将显示在设置弹窗的版权信息处,点击自动跳转至工信部备案网站'));
$form->addInput($beianInfo);
// Gravatar 头像加速地址
$gravatarUrl = new Typecho_Widget_Helper_Form_Element_Text('gravatarUrl', NULL, 'https://www.weavatar.com', _t('Gravatar 头像加速地址'), _t('用于替换 Gravatar 官方地址(http://www.gravatar.com),提升国内访问速度。默认使用 WeAvatar 加速服务(https://www.weavatar.com)。其他可选服务:<br>- https://gravatar.loli.net (Loli.net)<br>- https://cravatar.cn (Cravatar)<br>- https://dn-qiniu-avatar.qbox.me (七牛)<br>留空则使用默认值'));
$form->addInput($gravatarUrl);
// 自定义CSS
$customCss = new Typecho_Widget_Helper_Form_Element_Textarea('customCss', NULL, NULL, _t('自定义CSS'), _t('在这里填入CSS代码'));
$form->addInput($customCss);
// 自定义JavaScript
$customJs = new Typecho_Widget_Helper_Form_Element_Textarea('customJs', NULL, NULL, _t('自定义JavaScript'), _t('在这里填入JavaScript代码'));
$form->addInput($customJs);
// 统计代码
$analytics = new Typecho_Widget_Helper_Form_Element_Textarea('analytics', NULL, NULL, _t('统计代码'), _t('在这里填入网站统计代码'));
$form->addInput($analytics);
// 文章发布页地址
$editPageUrl = new Typecho_Widget_Helper_Form_Element_Text('editPageUrl', NULL, '/edit.html', _t('文章发布页地址'), _t('设置文章发布页面的访问地址,默认为 /edit.html'));
$form->addInput($editPageUrl);
// 是否自动收起内容
$autoCollapse = new Typecho_Widget_Helper_Form_Element_Radio(
'autoCollapse',
array(
'1' => _t('是(默认显示摘要,点击展开全文)'),
'0' => _t('否(直接显示完整内容)')
),
'1',
_t('自动收起文章内容'),
_t('设置列表页是否自动截取内容为摘要。选择"是"则显示摘要和展开按钮;选择"否"则直接显示完整内容。')
);
$form->addInput($autoCollapse);
}
function themeFields($layout)
{
?>
<style>
textarea {
width: 100%;
height: 8rem;
}
input[type=text] {
width: 100%;
}
</style>
<?php
$position = new Typecho_Widget_Helper_Form_Element_Text(
'position',
null,
null,
_t('发布定位'),
_t('<span>在这里填定位名称(例:成都市·天府广场)</span>')
);
$position->input->setAttribute('class', 't-default-find');
$layout->addItem($position);
$positionUrl = new Typecho_Widget_Helper_Form_Element_Text(
'positionUrl',
null,
null,
_t('定位跳转地址')
);
$positionUrl->input->setAttribute('class', 't-default-find');
$layout->addItem($positionUrl);
$isAdvertise = new Typecho_Widget_Helper_Form_Element_Radio(
"isAdvertise",
[
"1" => _t("是"),
"0" => _t("不是"),
],
"0",
_t("是否是广告"),
_t('<span>默认不是</span>')
);
$isAdvertise->input->setAttribute('class', 't-default-find');
$layout->addItem($isAdvertise);
}
/**
* 获取 Gravatar 头像 URL(支持加速地址)
*/
function getGravatarUrl($email, $size = 80, $default = 'identicon', $rating = 'g')
{
$options = \Widget\Options::alloc();
// 获取配置的 Gravatar 加速地址,如果未配置则使用默认值
$gravatarBase = $options->gravatarUrl;
if (empty($gravatarBase)) {
$gravatarBase = 'https://www.weavatar.com';
}
// 移除末尾的斜杠
$gravatarBase = rtrim($gravatarBase, '/');
// 生成头像 URL
$hash = md5(strtolower(trim($email)));
$url = $gravatarBase . '/avatar/' . $hash . '?s=' . $size . '&d=' . $default . '&r=' . $rating;
return $url;
}
/**
* 获取文章摘要
*/
function getExcerpt($content, $length = 200)
{
// 定义允许的HTML标签
$allowed_tags = '<p><a><br><strong><em><ul><ol><li><blockquote><code><pre><h1><h2><h3><h4><h5><h6>';
// 保留允许的HTML标签,去除其他标签
$content = strip_tags($content, $allowed_tags);
// 处理不同格式的换行标签
$content = str_replace('<br/>', '<br>', $content);
$content = str_replace('<br />', '<br>', $content);
$content = mb_substr($content, 0, $length, 'utf-8');
return $content . '...';
}
/**
* 获取文章浏览量
*/
function getPostViews($archive)
{
$views = $archive->views;
if (empty($views)) {
$views = 0;
}
return $views;
}
/**
* 时间显示
*/
function showTime($time)
{
if (date('Y-m-d') == date('Y-m-d', $time)) {
return '今天 ' . date('H:i', $time);
} elseif (date('Y-m-d', strtotime('-1 day')) == date('Y-m-d', $time)) {
return '昨天 ' . date('H:i', $time);
} else {
return date('Y-m-d H:i', $time);
}
}
/**
* 获取文章分类
*/
function getCategories($archive)
{
$categories = $archive->categories;
if (empty($categories)) {
return '';
}
$result = '';
foreach ($categories as $category) {
$result .= '<a href="' . $category['permalink'] . '" class="category">' . $category['name'] . '</a>';
}
return $result;
}
/**
* 获取文章标签
*/
function getTags($archive)
{
$tags = $archive->tags;
if (empty($tags)) {
return '';
}
$result = '';
foreach ($tags as $tag) {
$result .= '<a href="' . $tag['permalink'] . '" class="tag">' . $tag['name'] . '</a>';
}
return $result;
}
/**
* 获取文章缩略图
*/
function getThumbnail($archive)
{
// 检查文章是否有自定义字段thumbnail
$thumbnail = $archive->fields->thumbnail;
if ($thumbnail) {
return $thumbnail;
}
// 如果没有,尝试从文章内容中提取第一张图片
$content = $archive->content;
preg_match('/<img.*?src="(.*?)".*?>/i', $content, $matches);
if (isset($matches[1])) {
return $matches[1];
}
// 如果都没有,返回默认图片
return '';
}
/**
* 获取评论数
*/
function getCommentCount($archive)
{
return $archive->commentsNum;
}
/**
* 获取作者信息
*/
function getAuthor($archive)
{
return $archive->author;
}
/**
* 获取上一篇文章
*/
function getPrevPost($archive)
{
return $archive->widget('Widget_Contents_Post_Previous');
}
/**
* 获取下一篇文章
*/
function getNextPost($archive)
{
return $archive->widget('Widget_Contents_Post_Next');
}
/**
* 获取相关文章
*/
function getRelatedPosts($archive, $limit = 5)
{
$db = Typecho_Db::get();
$post = $archive->cid;
// 获取当前文章的分类
$categories = $archive->categories;
if (empty($categories)) {
return array();
}
$categoryIds = array();
foreach ($categories as $category) {
$categoryIds[] = $category['mid'];
}
// 查询相关文章
$relatedPosts = $db->fetchAll($db->select()
->from('table.contents')
->where('table.contents.cid <> ?', $post)
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', 'post')
->join('table.relationships', 'table.contents.cid = table.relationships.cid')
->where('table.relationships.mid IN ?', $categoryIds)
->order('table.contents.created', Typecho_Db::SORT_DESC)
->limit($limit));
return $relatedPosts;
}
/**
* 获取热门文章
*/
function getPopularPosts($limit = 5)
{
$db = Typecho_Db::get();
$posts = $db->fetchAll($db->select()
->from('table.contents')
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', 'post')
->order('table.contents.views', Typecho_Db::SORT_DESC)
->limit($limit));
return $posts;
}
/**
* 获取最新评论
*/
function getRecentComments($limit = 5)
{
$db = Typecho_Db::get();
$comments = $db->fetchAll($db->select()
->from('table.comments')
->where('table.comments.status = ?', 'approved')
->where('table.comments.type = ?', 'comment')
->order('table.comments.created', Typecho_Db::SORT_DESC)
->limit($limit));
return $comments;
}
/**
* 获取分类列表
*/
function getCategoryList()
{
$db = Typecho_Db::get();
$categories = $db->fetchAll($db->select()
->from('table.metas')
->where('table.metas.type = ?', 'category')
->order('table.metas.order', Typecho_Db::SORT_ASC));
return $categories;
}
/**
* 获取标签云
*/
function getTagCloud($limit = 20)
{
$db = Typecho_Db::get();
$tags = $db->fetchAll($db->select()
->from('table.metas')
->where('table.metas.type = ?', 'tag')
->order('table.metas.count', Typecho_Db::SORT_DESC)
->limit($limit));
return $tags;
}
/**
* 获取归档列表
*/
function getArchiveList()
{
$db = Typecho_Db::get();
$archives = $db->fetchAll($db->select()
->from('table.contents')
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', 'post')
->order('table.contents.created', Typecho_Db::SORT_DESC));
return $archives;
}
/**
* 获取搜索表单
*/
function getSearchForm()
{
return '<form method="post" action="" class="search-form">
<input type="text" name="s" placeholder="搜索..." required>
<button type="submit">搜索</button>
</form>';
}
/**
* 获取导航菜单
*/
function getNavigationMenu()
{
$db = Typecho_Db::get();
$pages = $db->fetchAll($db->select()
->from('table.contents')
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', 'page')
->order('table.contents.order', Typecho_Db::SORT_ASC));
return $pages;
}
/**
* 输出分页导航
*/
function pageNav($archive)
{
if ($archive->getTotal() > $archive->parameter->pageSize) {
echo '<div class="page-nav">';
echo $archive->pageNav('«', '»', 1, '...', array('wrapTag' => 'span', 'wrapClass' => 'page-links', 'itemTag' => '', 'currentClass' => 'current'));
echo '</div>';
}
}
/**
* 输出评论列表
*/
function comments($comments, $options)
{
$commentLevelClass = 'comment-child';
if ($comments->levels > 0) {
echo '<div class="comment-' . $comments->levels . '">';
}
$comments->listComments();
if ($comments->children) {
echo '<div class="comment-children">';
foreach ($comments->children as $child) {
comments($child, $options);
}
echo '</div>';
}
if ($comments->levels > 0) {
echo '</div>';
}
}
/**
* 自定义评论样式
*/
function singleComment($comment, $article)
{
$commentClass = '';
if ($comment->authorId) {
if ($comment->authorId == $article->author->uid) {
$commentClass = 'comment-by-author';
}
}
echo '<li id="comment-' . $comment->coid . '" class="comment ' . $commentClass . '">';
echo '<div class="comment-avatar">';
echo '<img src="' . $comment->gravatar . '" alt="' . $comment->author . '" width="32" height="32">';
echo '</div>';
echo '<div class="comment-content">';
echo '<div class="comment-meta">';
echo '<span class="comment-author">' . $comment->author . '</span>';
echo '<span class="comment-time">' . date('Y-m-d H:i', $comment->created) . '</span>';
echo '</div>';
echo '<div class="comment-text">';
echo $comment->content;
echo '</div>';
echo '<div class="comment-reply">';
echo '<a href="#comment-form" onclick="return TypechoComment.reply(\'' . $comment->coid . '\', \'' . $comment->coid . '\');">回复</a>';
echo '</div>';
echo '</div>';
echo '</li>';
}
/**
* 输出评论表单
*/
function commentForm($comments, $options)
{
echo '<div id="comment-form" class="comment-form">';
echo '<form method="post" action="' . $options->index . '/action/comments-post">';
echo '<div class="form-group">';
echo '<label for="author">昵称</label>';
echo '<input type="text" name="author" id="author" value="' . $comments->remember('author') . '" required>';
echo '</div>';
echo '<div class="form-group">';
echo '<label for="mail">邮箱</label>';
echo '<input type="email" name="mail" id="mail" value="' . $comments->remember('mail') . '" required>';
echo '</div>';
echo '<div class="form-group">';
echo '<label for="url">网站</label>';
echo '<input type="url" name="url" id="url" value="' . $comments->remember('url') . '">';
echo '</div>';
echo '<div class="form-group">';
echo '<label for="textarea">评论内容</label>';
echo '<textarea name="text" id="textarea" rows="5" required></textarea>';
echo '</div>';
echo '<div class="form-group">';
echo '<button type="submit" class="submit-btn">发表评论</button>';
echo '</div>';
echo '</form>';
echo '</div>';
}
/**
* 获取网站统计信息
*/
function getSiteStats()
{
$db = Typecho_Db::get();
// 获取文章总数
$postCount = $db->fetchObject($db->select(array('COUNT(cid)' => 'total'))
->from('table.contents')
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', 'post'))->total;
// 获取评论总数
$commentCount = $db->fetchObject($db->select(array('COUNT(coid)' => 'total'))
->from('table.comments')
->where('table.comments.status = ?', 'approved'))->total;
// 获取分类总数
$categoryCount = $db->fetchObject($db->select(array('COUNT(mid)' => 'total'))
->from('table.metas')
->where('table.metas.type = ?', 'category'))->total;
return array(
'posts' => $postCount,
'comments' => $commentCount,
'categories' => $categoryCount
);
}
/**
* 输出面包屑导航
*/
function breadcrumbs($archive)
{
echo '<div class="breadcrumbs">';
echo '<a href="' . $archive->options->siteUrl . '">首页</a>';
if ($archive->is('index')) {
// 首页不需要面包屑
} elseif ($archive->is('category')) {
echo ' » <a href="' . $archive->category['permalink'] . '">' . $archive->category['name'] . '</a>';
} elseif ($archive->is('tag')) {
echo ' » <a href="' . $archive->tag['permalink'] . '">' . $archive->tag['name'] . '</a>';
} elseif ($archive->is('search')) {
echo ' » 搜索: ' . htmlspecialchars($archive->keywords);
} elseif ($archive->is('post') || $archive->is('page')) {
if ($archive->categories) {
foreach ($archive->categories as $category) {
echo ' » <a href="' . $category['permalink'] . '">' . $category['name'] . '</a>';
}
}
echo ' » ' . $archive->title;
} elseif ($archive->is('archive')) {
echo ' » 归档: ' . $archive->date('Y年m月');
}
echo '</div>';
}
/**
* 输出SEO信息
*/
function seoInfo($archive)
{
$options = Helper::options();
if ($archive->is('index')) {
$title = $options->title;
$description = $options->description;
$keywords = $options->keywords;
} elseif ($archive->is('post') || $archive->is('page')) {
$title = $archive->title . ' - ' . $options->title;
// 清理文章摘要:移除HTML标签、换行符,并截取前150个字符
$description = strip_tags($archive->excerpt);
$description = preg_replace('/\s+/', ' ', $description); // 将多个空白字符替换为单个空格
$description = trim($description);
$description = mb_substr($description, 0, 150, 'UTF-8');
$keywords = '';
if ($archive->tags) {
foreach ($archive->tags as $tag) {
$keywords .= $tag['name'] . ',';
}
}
$keywords = rtrim($keywords, ',');
} else {
// 归档页面:根据不同类型生成标题
$archiveTitle = '';
if ($archive->is('category')) {
$archiveTitle = '分类 ' . $archive->getDescription() . ' 下的文章';
} elseif ($archive->is('tag')) {
$archiveTitle = '标签 ' . $archive->getDescription() . ' 下的文章';
} elseif ($archive->is('author')) {
$archiveTitle = $archive->getDescription() . ' 发布的文章';
} elseif ($archive->is('search')) {
// 尝试多种方式获取搜索关键词
$searchKeywords = '';
// 调试:检查所有可能的搜索参数来源
$request = $archive->request;
if ($request) {
$searchKeywords = $request->get('keywords', '');
if (empty($searchKeywords)) {
$searchKeywords = $request->get('s', '');
}
}
// 如果还是空,尝试从$_GET获取
if (empty($searchKeywords) && isset($_GET['keywords'])) {
$searchKeywords = $_GET['keywords'];
}
if (empty($searchKeywords) && isset($_GET['s'])) {
$searchKeywords = $_GET['s'];
}
$archiveTitle = '包含关键字 ' . $searchKeywords . ' 的文章';
} else {
// 使用默认归档标题
$archiveTitle = '归档';
}
$title = $archiveTitle . ' - ' . $options->title;
$description = $options->description;
$keywords = $options->keywords;
}
// 转义所有输出内容,防止HTML注入
$title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
$description = htmlspecialchars($description, ENT_QUOTES, 'UTF-8');
$keywords = htmlspecialchars($keywords, ENT_QUOTES, 'UTF-8');
echo '<title>' . $title . '</title>' . "\n";
echo ' <meta name="description" content="' . $description . '">' . "\n";
echo ' <meta name="keywords" content="' . $keywords . '">' . "\n";
// Open Graph
echo ' <meta property="og:title" content="' . $title . '">' . "\n";
echo ' <meta property="og:description" content="' . $description . '">' . "\n";
echo ' <meta property="og:type" content="website">' . "\n";
echo ' <meta property="og:url" content="' . htmlspecialchars($archive->permalink, ENT_QUOTES, 'UTF-8') . '">' . "\n";
echo ' <meta property="og:site_name" content="' . htmlspecialchars($options->title, ENT_QUOTES, 'UTF-8') . '">' . "\n";
// Twitter Card
echo ' <meta name="twitter:card" content="summary">' . "\n";
echo ' <meta name="twitter:title" content="' . $title . '">' . "\n";
echo ' <meta name="twitter:description" content="' . $description . '">';
}
/**
* 主题激活时执行
*/
function themeActivate()
{
// 创建自定义字段
$db = Typecho_Db::get();
// 检查是否已存在thumbnail字段
$exists = $db->fetchRow($db->select()->from('table.fields')->where('name = ?', 'thumbnail'));
if (!$exists) {
$db->query($db->insert('table.fields')->rows(array(
'name' => 'thumbnail',
'type' => 'str',
'title' => '缩略图',
'description' => '文章缩略图URL'
)));
}
//
// // 添加主题设置主菜单
// $menuIndex = Helper::addMenu('icefox-theme');
//
// // 使用返回的索引添加子面板
// Helper::addPanel($menuIndex, 'admin/theme.php', '主题设置', '配置主题', 'administrator');
}
/**
* 主题停用时执行
*/
function themeDeactivate()
{
// // 清理自定义数据
// $menuIndex = Helper::removeMenu('icefox-theme');
//
// // 使用返回的索引删除子面板
// Helper::removePanel($menuIndex, 'admin/theme.php');
}
/**
* 主题升级时执行
*/
function themeUpgrade()
{
// 执行升级操作
}
// 注册主题激活/停用钩子
//Helper::addPanel(1, 'IceFox/panel.php', 'IceFox主题设置', 'IceFox主题设置', 'administrator');
//Helper::addRoute('icefox_api', '/icefox/api', 'IceFox_Action');
//Helper::removePanel(1,'IceFox/panel.php');
/**
* 归档页面辅助函数
*/
/**
* 获取全站文章总数
*/
function getTotalPostCount()
{
$db = Typecho_Db::get();
$count = $db->fetchObject($db->select(array('COUNT(cid)' => 'count'))->from('table.contents')->where('status = ? AND type = ?', 'publish', 'post'));
return $count->count;
}
/**
* 获取全站分类总数
*/
function getTotalCategoryCount()
{
$db = Typecho_Db::get();
$count = $db->fetchObject($db->select(array('COUNT(mid)' => 'count'))->from('table.metas')->where('type = ?', 'category'));
return $count->count;
}
/**
* 获取全站标签总数
*/
function getTotalTagCount()
{
$db = Typecho_Db::get();
$count = $db->fetchObject($db->select(array('COUNT(mid)' => 'count'))->from('table.metas')->where('type = ?', 'tag'));
return $count->count;
}
/**
* 获取全站评论总数
*/
function getTotalCommentCount()
{
$db = Typecho_Db::get();
$count = $db->fetchObject($db->select(array('COUNT(coid)' => 'count'))->from('table.comments')->where('status = ?', 'approved'));
return $count->count;
}
/**
* 获取微信朋友圈风格的时间轴归档
*/
function getArchiveTimelineMoments()
{
$db = Typecho_Db::get();
$posts = $db->fetchAll($db->select('cid', 'title', 'created', 'slug', 'text')->from('table.contents')
->where('status = ? AND type = ?', 'publish', 'post')
->order('created', Typecho_Db::SORT_DESC));
if (empty($posts)) {
return '<div class="moments-empty">
<div class="empty-icon">📝</div>
<p>还没有发布文章</p>
</div>';
}
$moments = '<div class="moments-container">';
$currentDate = '';
foreach ($posts as $post) {
$year = date('Y', $post['created']);
// 年份标题(仅在年份变化时显示)
if ($currentDate !== $year) {
$currentDate = $year;
$moments .= '<div style="color:#333;"><span style="font-weight:bold;font-size:2rem;">' . $currentDate . '</span>年</div>';
}
$dateDay = '<span class="mg-day">' . date('d', $post['created']) . '</span>';
$dateLabel = '<date>' . date('m月', $post['created']) . '</date>';
// 获取文章内容预览,支持音乐卡片
$filtered = filterContent($post['text']);
$cws = generateContentWithSummaryAndMusic($filtered, 100);
$musicHtml = !empty($cws['music_shortcodes']) ? parseMusicShortcode($cws['music_shortcodes']) : '';
$preview = $cws['summary'];
$postUrl = Typecho_Router::url('post', $post);
$time = date('H:i', $post['created']);
// 图片
$images = extractImageSrcs($post['text']);
$moments .= '<div class="moments-group">';
// 有图片时:图片在最左边
if (count($images) > 0) {
$moments .= '<div class="moment-avatar">';
$moments .= '<a href="' . $postUrl . '"><img src="'.$images[0].'" alt="封面"></a>';
$moments .= '</div>';
}
// 日期
$moments .= '<div class="moments-date">' . $dateDay . $dateLabel . '</div>';
// 内容区
$moments .= '<div class="moment-content">';
$hasImage = count($images) > 0;
$moments .= '<div class="moment-body' . ($hasImage ? '' : ' moment-body-text') . '">';
if ($musicHtml) {
$moments .= $musicHtml;
} elseif ($preview) {
$moments .= '<a href="' . $postUrl . '"><p class="moment-preview">' . filterContent($preview) . '</p></a>';
}
$moments .= '</div>';
$moments .= '</div>';
$moments .= '</div>'; // 关闭 moments-group
}
$moments .= '</div>'; // 关闭 moments-container
return $moments;
}
/**
* 获取分类网格视图
*/
function getArchiveCategoriesGrid()
{
$db = Typecho_Db::get();
$categories = $db->fetchAll($db->select('mid', 'name', 'slug', 'description')
->from('table.metas')
->where('type = ?', 'category')
->order('name', Typecho_Db::SORT_ASC));
if (empty($categories)) {
return '<div class="moments-empty">
<div class="empty-icon">📁</div>
<p>还没有分类</p>
</div>';
}
$html = '<div class="categories-grid">';
foreach ($categories as $category) {
// 获取分类文章数量
$count = $db->fetchObject($db->select(array('COUNT(cid)' => 'count'))
->from('table.relationships')
->where('mid = ?', $category['mid']))->count;
$categoryUrl = Typecho_Router::url('category', $category);
$html .= '<div class="category-card">';
$html .= '<div class="category-icon">📁</div>';
$html .= '<div class="category-info">';
$html .= '<h3 class="category-name">' . htmlspecialchars($category['name']) . '</h3>';
$html .= '<div class="category-count">' . $count . ' 篇文章</div>';
if ($category['description']) {
$html .= '<p class="category-description">' . htmlspecialchars($category['description']) . '</p>';
}
$html .= '</div>';
$html .= '<div class="category-action">';
$html .= '<a href="' . $categoryUrl . '" class="category-link">查看文章</a>';
$html .= '</div>';
$html .= '</div>';
}
$html .= '</div>';
return $html;
}
/**
* 获取标签云视图
*/
function getArchiveTagsCloud()
{
$db = Typecho_Db::get();
$tags = $db->fetchAll($db->select('mid', 'name', 'slug')
->from('table.metas')
->where('type = ?', 'tag')
->order('name', Typecho_Db::SORT_ASC));
if (empty($tags)) {
return '<div class="moments-empty">
<div class="empty-icon">🏷️</div>
<p>还没有标签</p>
</div>';
}
// 获取每个标签的文章数量
foreach ($tags as &$tag) {
$count = $db->fetchObject($db->select(array('COUNT(cid)' => 'count'))
->from('table.relationships')
->where('mid = ?', $tag['mid']))->count;
$tag['count'] = $count;
}
// 按文章数量排序
usort($tags, function ($a, $b) {
return $b['count'] - $a['count'];
});
$html = '<div class="tags-cloud">';
$maxCount = !empty($tags) ? max(array_column($tags, 'count')) : 1;
foreach ($tags as $tag) {
$tagUrl = Typecho_Router::url('tag', $tag);
// 根据文章数量确定标签大小
$sizeRatio = $tag['count'] / $maxCount;
$sizeClass = 'tag-size-small';
if ($sizeRatio > 0.6) {
$sizeClass = 'tag-size-large';
} elseif ($sizeRatio > 0.3) {
$sizeClass = 'tag-size-medium';
}
$html .= '<a href="' . $tagUrl . '" class="tag-chip ' . $sizeClass . '">';
$html .= '<span class="tag-emoji">🏷️</span>';
$html .= '<span class="tag-name">' . htmlspecialchars($tag['name']) . '</span>';
$html .= '<span class="tag-count">' . $tag['count'] . '</span>';
$html .= '</a>';
}
$html .= '</div>';
return $html;
}
/**
* 获取文章位置信息
* @param object|int $archive 文章对象或cid
* @return string 位置信息,如果没有则返回空字符串
*/
function getPostLocation($archive)
{
$cid = is_object($archive) ? $archive->cid : intval($archive);
if (empty($cid)) {
return '';
}
$db = Typecho_Db::get();
$field = $db->fetchRow($db->select('str_value')
->from('table.fields')
->where('cid = ?', $cid)
->where('name = ?', 'location'));
return $field ? $field['str_value'] : '';
}
/**
* 检查文章是否为广告
* @param object|int $archive 文章对象或cid
* @return bool 是否为广告
*/
function isPostAd($archive)
{
$cid = is_object($archive) ? $archive->cid : intval($archive);
if (empty($cid)) {
return false;
}
$db = Typecho_Db::get();
$field = $db->fetchRow($db->select('int_value')
->from('table.fields')
->where('cid = ?', $cid)
->where('name = ?', 'isAd'));
return $field && $field['int_value'] == 1;
}
/**