forked from solstice23/argon-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
2103 lines (2074 loc) · 82.9 KB
/
functions.php
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 (version_compare( $GLOBALS['wp_version'], '4.4-alpha', '<' )) {
echo "<div style='background: #5e72e4;color: #fff;font-size: 30px;padding: 50px 30px;position: fixed;width: 100%;left: 0;right: 0;bottom: 0px;z-index: 2147483647;'>Argon 主题不支持 Wordpress 4.4 以下版本,请更新 Wordpress</div>";
}
function theme_slug_setup() {
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
}
add_action('after_setup_theme','theme_slug_setup');
//检测更新
require_once(get_template_directory() . '/theme-update-checker/plugin-update-checker.php');
if (get_option('argon_update_source') == 'stop'){}
else if (get_option('argon_update_source') == 'abc233site'){
$argonThemeUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
'https://api.solstice23.top/argon/info.json?source=0',
get_template_directory() . '/functions.php',
'argon'
);
}else{
$argonThemeUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
'https://raw.githubusercontent.com/solstice23/argon-theme/master/info.json',
get_template_directory() . '/functions.php',
'argon'
);
}
//初次使用时发送安装量统计信息 (数据仅用于统计安装量)
function post_analytics_info(){
if(function_exists('file_get_contents')){
$contexts = stream_context_create(
array(
'http' => array(
'method'=>"GET",
'header'=>"User-Agent: ArgonTheme\r\n"
)
)
);
$result = file_get_contents('http://api.solstice23.top/argon_analytics/index.php?domain=' . urlencode($_SERVER['HTTP_HOST']) . '&version='. urlencode(wp_get_theme('argon') -> Version), false, $contexts);
update_option('argon_has_inited', 'true');
return $result;
}else{
update_option('argon_has_inited', 'true');
}
}
if (get_option('argon_has_inited') != 'true'){
post_analytics_info();
}
//时区修正
if (get_option('argon_enable_timezone_fix') == 'true'){
date_default_timezone_set('UTC');
}
//注册小工具
function argon_widgets_init() {
register_sidebar(
array(
'name' => '左侧栏小工具',
'id' => 'leftbar-tools',
'description' => __( '左侧栏小工具 (如果设置会在侧栏增加一个 Tab)' ),
'before_widget' => '<div id="%1$s" class="widget %2$s card bg-white border-0">',
'after_widget' => '</div>',
'before_title' => '<h6 class="font-weight-bold text-black">',
'after_title' => '</h6>',
)
);
}
add_action('widgets_init','argon_widgets_init');
//输出分页页码
function get_argon_formatted_paginate_links(){
$args = array(
'prev_text' => '',
'next_text' => '',
'before_page_number' => '',
'after_page_number' => '',
'show_all' => True
);
$res = paginate_links($args);
//单引号转双引号 & 去除上一页和下一页按钮
$res = preg_replace(
'/\'/',
'"',
$res
);
$res = preg_replace(
'/<a class="prev page-numbers" href="(.*?)">(.*?)<\/a>/',
'',
$res
);
$res = preg_replace(
'/<a class="next page-numbers" href="(.*?)">(.*?)<\/a>/',
'',
$res
);
//寻找所有页码标签
preg_match_all('/<(.*?)>(.*?)<\/(.*?)>/' , $res , $pages);
$total = count($pages[0]);
$current = 0;
$urls = array();
for ($i = 0; $i < $total; $i++){
if (preg_match('/<span(.*?)>(.*?)<\/span>/' , $pages[0][$i])){
$current = $i + 1;
}else{
preg_match('/<a(.*?)href="(.*?)">(.*?)<\/a>/' , $pages[0][$i] , $tmp);
$urls[$i + 1] = $tmp[2];
}
}
if ($total == 0){
return "";
}
//计算页码起始
$from = max($current - 3 , 1);
$to = min($current + 7 - ( $current - $from + 1 ) , $total);
if ($to - $from + 1 < 7){
$to = min($current + 3 , $total);
$from = max($current -( 7 - ( $to - $current + 1 ) ) , 1);
}
//生成新页码
$html = "";
if ($from > 1){
$html .= '<li class="page-item"><a aria-label="First Page" class="page-link" href="' . $urls[1] . '"><i class="fa fa-angle-double-left" aria-hidden="true"></i></a></li>';
}
if ($current > 1){
$html .= '<li class="page-item"><a aria-label="Previous Page" class="page-link" href="' . $urls[$current - 1] . '"><i class="fa fa-angle-left" aria-hidden="true"></i></a></li>';
}
for ($i = $from; $i <= $to; $i++){
if ($current == $i){
$html .= '<li class="page-item active"><span class="page-link" style="cursor: default;">' . $i . '</span></li>';
}else{
$html .= '<li class="page-item"><a class="page-link" href="' . $urls[$i] . '">' . $i . '</a></li>';
}
}
if ($current < $total){
$html .= '<li class="page-item"><a aria-label="Next Page" class="page-link" href="' . $urls[$current + 1] . '"><i class="fa fa-angle-right" aria-hidden="true"></i></a></li>';
}
if ($to < $total){
$html .= '<li class="page-item"><a aria-label="Last Page" class="page-link" href="' . $urls[$total] . '"><i class="fa fa-angle-double-right" aria-hidden="true"></i></a></li>';
}
return '<nav><ul class="pagination">' . $html . '</ul></nav>';
}
//页面 Description Meta
function get_seo_description(){
global $post;
if ((is_single() || is_page())){
if (!post_password_required()){
return
htmlspecialchars(mb_substr(str_replace("\n", '', strip_tags(get_post($post -> ID) -> post_content)), 0, 50)) . "...";
}else{
return "这是一个加密页面,需要密码来查看";
}
}else{
return get_option('argon_seo_description');
}
}
//页面浏览量
function get_post_views($post_id){
$count_key = 'views';
$count = get_post_meta($post_id, $count_key, true);
if ($count==''){
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, '0');
$count = '0';
}
echo number_format_i18n($count);
}
function set_post_views(){
if (post_password_required($post_id)){
return;
}
global $post;
$post_id = $post -> ID;
$count_key = 'views';
$count = get_post_meta($post_id, $count_key, true);
if (is_single() || is_page()) {
if ($count==''){
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, '0');
} else {
update_post_meta($post_id, $count_key, $count + 1);
}
}
}
add_action('get_header', 'set_post_views');
//字数和预计阅读时间
function get_article_words($str){
return mb_strlen(
preg_replace(
'/\s/',
'',
html_entity_decode(
strip_tags($str)
)
),
'UTF-8'
);
}
function get_reading_time($len){
$speed = get_option('argon_reading_speed');
if ($speed == ""){
$speed = 300;
}
$reading_time = $len / $speed;
if ($reading_time < 0.3){
return "几秒读完";
}
if ($reading_time < 1){
return "1 分钟内";
}
if ($reading_time < 60){
return ceil($reading_time) . " 分钟";
}
return round($reading_time / 60 , 1) . " 小时";
}
//当前文章是否可以生成目录
function have_catalog(){
if (!is_single() && !is_page()){
return false;
}
if (post_password_required()){
return false;
}
$content = get_post(get_the_ID()) -> post_content;
if (preg_match('/<h[1-6]>/',$content)){
return true;
}else{
return false;
}
}
//当前文章是否隐藏 阅读时间 Meta
function is_readingtime_meta_hidden(){
if (strpos(get_the_content() , "[hide_reading_time][/hide_reading_time]") !== False){
return True;
}
global $post;
if (get_post_meta($post -> ID, 'argon_hide_readingtime', true) == 'true'){
return True;
}
return False;
}
//评论样式格式化
function argon_comment_format($comment, $args, $depth){
$GLOBALS['comment'] = $comment;?>
<li class="comment-item" id="comment-<?php comment_ID(); ?>">
<div class="comment-item-avatar">
<?php if(function_exists('get_avatar') && get_option('show_avatars')){
echo get_avatar($comment, 40);
}?>
</div>
<div class="comment-item-inner" id="comment-inner-<?php comment_ID();?>">
<div class="comment-item-title">
<?php echo get_comment_author_link();?>
<?php if( user_can($comment -> user_id , "update_core") ){
echo '<span class="badge badge-primary badge-admin">博主</span>';}
?>
<?php if( $comment -> comment_approved == 0 ){
echo '<span class="badge badge-warning badge-unapproved">待审核</span>';}
?>
</div>
<div class="comment-item-text">
<?php comment_text();?>
</div>
<div class="comment-reply-time">
<?php echo human_time_diff(get_comment_time('U') , current_time('timestamp')) . "前";?>
<div class="comment-reply-time-details"><?php echo get_comment_time('Y-n-d G:i:s');?></div>
</div>
<?php /*comment_reply_link(array_merge($args,array('reply_text'=>'回复','depth'=>$depth,'max_depth'=>$args['max_depth'])))*/?>
<button class="comment-reply btn btn-sm btn-outline-primary" data-id="<?php comment_ID(); ?>" type="button">回复</button>
</div>
</li>
<li class="comment-divider"></li>
<li>
<?php }
//评论样式格式化 (说说预览界面)
function argon_comment_shuoshuo_preview_format($comment, $args, $depth){
$GLOBALS['comment'] = $comment;?>
<li class="comment-item" id="comment-<?php comment_ID(); ?>">
<div class="comment-item-inner " id="comment-inner-<?php comment_ID();?>">
<span class="shuoshuo-comment-item-title">
<?php echo get_comment_author_link();?>
<?php if( user_can($comment -> user_id , "update_core") ){
echo '<span class="badge badge-primary badge-admin">博主</span>';}
?>
<?php if( $comment -> comment_approved == 0 ){
echo '<span class="badge badge-warning badge-unapproved">待审核</span>';}
?>
:
</span>
<span class="shuoshuo-comment-item-text">
<?php echo strip_tags(get_comment_text());?>
</span>
</div>
</li>
<li>
<?php }
//评论验证码生成 & 验证
function get_comment_captcha_seed(){
$captchaSeed = rand(0 , 500000000);
return $captchaSeed;
}
function get_comment_captcha($captchaSeed){
mt_srand($captchaSeed + 10007);
$oper = mt_rand(1 , 4);
$num1 = 0;
$num2 = 0;
switch ($oper){
case 1:
$num1 = mt_rand(1 , 20);
$num2 = mt_rand(0 , 20 - $num1);
return $num1 . " + " . $num2 . " = ";
break;
case 2:
$num1 = mt_rand(10 , 20);
$num2 = mt_rand(1 , $num1);
return $num1 . " - " . $num2 . " = ";
break;
case 3:
$num1 = mt_rand(3 , 9);
$num2 = mt_rand(3 , 9);
return $num1 . " * " . $num2 . " = ";
break;
case 4:
$num2 = mt_rand(2 , 9);
$num1 = $num2 * mt_rand(2 , 9);
return $num1 . " / " . $num2 . " = ";
break;
default:
break;
}
}
function get_comment_captcha_answer($captchaSeed){
mt_srand($captchaSeed + 10007);
$oper = mt_rand(1 , 4);
$num1 = 0;
$num2 = 0;
switch ($oper){
case 1:
$num1 = mt_rand(1 , 20);
$num2 = mt_rand(0 , 20 - $num1);
return $num1 + $num2;
break;
case 2:
$num1 = mt_rand(10 , 20);
$num2 = mt_rand(1 , $num1);
return $num1 - $num2;
break;
case 3:
$num1 = mt_rand(3 , 9);
$num2 = mt_rand(3 , 9);
return $num1 * $num2;
break;
case 4:
$num2 = mt_rand(2 , 9);
$num1 = $num2 * mt_rand(2 , 9);
return $num1 / $num2;
break;
default:
break;
}
return "";
}
function wrong_captcha(){
wp_die('验证码错误,评论失败');
}
function check_comment_captcha($comment){
if (get_option('argon_comment_need_captcha') == 'false'){
return $comment;
}
$answer = $_POST['comment_captcha'];
if(current_user_can('level_7')){
return $comment;
}
mt_srand($_POST['comment_captcha_seed'] + 10007);
$oper = mt_rand(1 , 4);
$num1 = 0;
$num2 = 0;
switch ($oper){
case 1:
$num1 = mt_rand(1 , 20);
$num2 = mt_rand(0 , 20 - $num1);
if (($num1 + $num2) != $answer){
wrong_captcha();
}
break;
case 2:
$num1 = mt_rand(10 , 20);
$num2 = mt_rand(1 , $num1);
if (($num1 - $num2) != $answer){
wrong_captcha();
}
break;
case 3:
$num1 = mt_rand(3 , 9);
$num2 = mt_rand(3 , 9);
if ($num1 * $num2 != $answer){
wrong_captcha();
}
break;
case 4:
$num2 = mt_rand(2 , 9);
$num1 = $num2 * mt_rand(2 , 9);
if ($num1 / $num2 != $answer){
wrong_captcha();
}
break;
default:
break;
}
return $comment;
}
if($comment_data['comment_type'] == ''){
add_filter('preprocess_comment' , 'check_comment_captcha');
}
//评论 Markdown 解析
require_once(get_template_directory() . '/parsedown.php');
function comment_markdown_render($comment_content){
if ($_POST['use_markdown'] != 'true'){
return $comment_content;
}
if (get_option("argon_comment_allow_markdown") == "false"){
return $comment_content;
}
//HTML 过滤
global $allowedtags;
//$comment_content = wp_kses($comment_content, $allowedtags);
//允许评论中额外的 HTML Tag
$allowedtags['pre'] = array('class' => array());
$allowedtags['i'] = array('class' => array(), 'aria-hidden' => array());
$allowedtags['img'] = array('src' => array(), 'alt' => array(), 'class' => array());
$allowedtags['a']['class'] = array();
$allowedtags['a']['data-src'] = array();
$allowedtags['a']['target'] = array();
//解析 Markdown
$parsedown = new Parsedown();
$res = $parsedown -> text($comment_content);
/*$res = preg_replace(
'/<code>([\s\S]*?)<\/code>/',
'<pre>$1</pre>',
$res
);*/
$res = preg_replace(
'/<img src="(.*?)" alt="(.*?)" \/>/',
'<a data-src="$1" title="$2" class="comment-image">
<i class="fa fa-image" aria-hidden="true"></i>
查看图片
<img src="" alt="$2" class="comment-image-preview">
<i class="comment-image-preview-mask"></i>
</a>',
$res
);
$res = preg_replace(
'/<a (.*?)>(.*?)<\/a>/',
'<a $1 target="_blank">$2</a>',
$res
);
return $res;
}
add_filter('pre_comment_content' , 'comment_markdown_render');
//获取顶部 Banner 背景图(用户指定或必应日图)
function get_banner_background_url(){
$url = get_option("argon_banner_background_url");
if ($url == "--bing--"){
$lastUpdated = get_option("argon_bing_banner_background_last_updated_time");
if ($lastUpdated == ""){
$lastUpdated = 0;
}
$now = time();
if ($now - $lastUpdated < 3600){
return get_option("argon_bing_banner_background_last_updated_url");
}else{
$data = json_decode(@file_get_contents('https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1') , true);
$url = "//bing.com" . $data['images'][0]['url'];
update_option("argon_bing_banner_background_last_updated_time" , $now);
update_option("argon_bing_banner_background_last_updated_url" , $url);
return $url;
}
}else{
return $url;
}
}
//Lazyload 对 <img> 标签预处理和加入 <script> 以加载 Lazyload
function argon_lazyload($content){
$lazyload_effect = get_option('argon_lazyload_effect');
if ($lazyload_effect == ''){
$lazyload_effect = 'fadeIn';
}
$lazyload_threshold = get_option('argon_lazyload_threshold');
if ($lazyload_threshold == ''){
$lazyload_threshold = 800;
}
$lazyload_loading_style = get_option('argon_lazyload_loading_style');
if ($lazyload_loading_style == ''){
$lazyload_loading_style = 'none';
}
$lazyload_loading_style = "lazyload-style-" . $lazyload_loading_style;
if(!is_feed() || !is_robots()){
$content = preg_replace('/<img(.+)src=[\'"]([^\'"]+)[\'"](.*)>/i',"<img class=\"lazyload " . $lazyload_loading_style . "\" src=\"data:image/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\" \$1data-original=\"\$2\" src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC\"\$3>\n<noscript>\$0</noscript>" , $content);
}
$content .= '<script>
$(function() {
$("article img.lazyload").lazyload(
{
threshold: ' . $lazyload_threshold;
if ($lazyload_effect != "none"){
$content .= ',effect: "' . $lazyload_effect . '"' ;
}
$content .= '
}
);
});
</script>';
$content .= '<noscript><style>article img.lazyload[src^="data:image/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPg"]{display: none;}</style></noscript>';
return $content;
}
//zoomify 插件图片缩放预览
function argon_zoomify($content){
$zoomify_duration = get_option('argon_zoomify_duration');
if ($zoomify_duration == ''){
$zoomify_duration = 200;
}
$zoomify_easing = get_option('argon_zoomify_easing');
if ($zoomify_easing == ''){
$zoomify_easing = 'ease-out';
}
$zoomify_scale = get_option('argon_zoomify_scale');
if ($zoomify_scale == ''){
$zoomify_scale = 0.9;
}
$content .= '<script>
$(function() {
$("article img").zoomify(
{
duration: ' . $zoomify_duration . ',
easing: "' . $zoomify_easing . '",
scale: ' . $zoomify_scale . '
}
);
});
</script>';
return $content;
}
function the_content_filter($content){
if (get_option('argon_enable_lazyload') != 'false'){
$content = argon_lazyload($content);
}
if (get_option('argon_enable_zoomify') != 'false'){
$content = argon_zoomify($content);
}
return $content;
}
add_filter('the_content' , 'the_content_filter');
//使用 v2ex CDN 的 gravatar 头像源加速
function get_avatar_from_v2ex($avatar){
$avatar = preg_replace("/http:\/\/(www|\d).gravatar.com\/avatar\//" , "//cdn.v2ex.com/gravatar/" , $avatar);
return $avatar;
}
if (get_option('argon_enable_v2ex_gravatar') == 'true'){
add_filter('get_avatar', 'get_avatar_from_v2ex');
}
//说说点赞
function get_shuoshuo_upvotes($ID){
$count_key = 'upvotes';
$count = get_post_meta($ID, $count_key, true);
if ($count==''){
delete_post_meta($ID, $count_key);
add_post_meta($ID, $count_key, '0');
$count = '0';
}
return number_format_i18n($count);
}
function set_shuoshuo_upvotes($ID){
$count_key = 'upvotes';
$count = get_post_meta($ID, $count_key, true);
if ($count==''){
delete_post_meta($ID, $count_key);
add_post_meta($ID, $count_key, '1');
} else {
update_post_meta($ID, $count_key, $count + 1);
}
}
function upvote_shuoshuo(){
header('Content-Type:application/json; charset=utf-8');
$ID = $_POST["shuoshuo_id"];
if (isset($_COOKIE['argon_shuoshuo_' . $ID . '_upvoted'])){
exit(json_encode(array(
'status' => 'failed',
'msg' => '该说说已被赞过',
'total_upvote' => get_shuoshuo_upvotes($ID)
)));
}
set_shuoshuo_upvotes($ID);
setcookie('argon_shuoshuo_' . $ID . '_upvoted' , 'true' , time() + 3153600000 , '/');
exit(json_encode(array(
'ID' => $ID,
'status' => 'success',
'msg' => '点赞成功',
'total_upvote' => get_shuoshuo_upvotes($ID)
)));
}
add_action('wp_ajax_upvote_shuoshuo' , 'upvote_shuoshuo');
add_action('wp_ajax_nopriv_upvote_shuoshuo' , 'upvote_shuoshuo');
//检测页面底部版权是否被修改
function alert_footer_copyright_changed(){ ?>
<div class='notice notice-warning is-dismissible'>
<p>警告:你可能修改了 Argon 主题页脚的版权声明,Argon 主题要求你至少保留主题的 Github 链接或主题的发布文章链接。</p>
</div>
<?php }
function check_footer_copyright(){
$footer = file_get_contents(get_theme_root() . "/argon/footer.php");
if ((strpos($footer, "github.com/solstice23/argon-theme") === false) && (strpos($footer, "solstice23.top") === false) && (strpos($footer, "solstice23.top") === false)){
add_action('admin_notices', 'alert_footer_copyright_changed');
}
}
check_footer_copyright();
//颜色计算
function rgb2hsl($R,$G,$B){
$r = $R / 255;
$g = $G / 255;
$b = $B / 255;
$var_Min = min($r, $g, $b);
$var_Max = max($r, $g, $b);
$del_Max = $var_Max - $var_Min;
$L = ($var_Max + $var_Min) / 2;
if ($del_Max == 0){
$H = 0;
$S = 0;
}else{
if ($L < 0.5){
$S = $del_Max / ($var_Max + $var_Min);
}else{
$S = $del_Max / (2 - $var_Max - $var_Min);
}
$del_R = ((($var_Max - $r) / 6) + ($del_Max / 2)) / $del_Max;
$del_G = ((($var_Max - $g) / 6) + ($del_Max / 2)) / $del_Max;
$del_B = ((($var_Max - $b) / 6) + ($del_Max / 2)) / $del_Max;
if ($r == $var_Max){
$H = $del_B - $del_G;
}
else if ($g == $var_Max){
$H = (1 / 3) + $del_R - $del_B;
}
else if ($b == $var_Max){
$H = (2 / 3) + $del_G - $del_R;
}
if ($H < 0) $H += 1;
if ($H > 1) $H -= 1;
}
return array(
'h' => $H,//0~1
's' => $S,
'l' => $L
);
}
function Hue_2_RGB($v1,$v2,$vH){
if ($vH < 0) $vH += 1;
if ($vH > 1) $vH -= 1;
if ((6 * $vH) < 1) return ($v1 + ($v2 - $v1) * 6 * $vH);
if ((2 * $vH) < 1) return $v2;
if ((3 * $vH) < 2) return ($v1 + ($v2 - $v1) * ((2 / 3) - $vH) * 6);
return $v1;
}
function hsl2rgb($h,$s,$l){
if ($s == 0){
$r = $l;
$g = $l;
$b = $l;
}
else{
if ($l < 0.5){
$var_2 = $l * (1 + $s);
}
else{
$var_2 = ($l + $s) - ($s * $l);
}
$var_1 = 2 * $l - $var_2;
$r = Hue_2_RGB($var_1, $var_2, $h + (1 / 3));
$g = Hue_2_RGB($var_1, $var_2, $h);
$b = Hue_2_RGB($var_1, $var_2, $h - (1 / 3));
}
return array(
'R' => round($r * 255),//0~255
'G' => round($g * 255),
'B' => round($b * 255),
'r' => $r,//0~1
'g' => $g,
'b' => $b
);
}
function rgb2hex($r,$g,$b){
$hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
$rh = "";
$gh = "";
$bh = "";
while (strlen($rh) < 2){
$rh = $hex[$r%16] . $rh;
$r = floor($r / 16);
}
while (strlen($gh) < 2){
$gh = $hex[$g%16] . $gh;
$g = floor($g / 16);
}
while (strlen($bh) < 2){
$bh = $hex[$b%16] . $bh;
$b = floor($b / 16);
}
return "#".$rh.$gh.$bh;
}
function hex2rgb($hex){
//$hex: #XXXXXX
return array(
'R' => hexdec(substr($hex,1,2)),//0~255
'G' => hexdec(substr($hex,3,2)),
'B' => hexdec(substr($hex,5,2)),
'r' => hexdec(substr($hex,1,2)) / 255,//0~1
'g' => hexdec(substr($hex,3,2)) / 255,
'b' => hexdec(substr($hex,5,2)) / 255
);
}
function rgb2str($rgb){
return $rgb['R']. "," .$rgb['G']. "," .$rgb['B'];
}
function hex2str($hex){
return rgb2str(hex2rgb($hex));
}
function checkHEX($hex){
if (strlen($hex) != 7){
return False;
}
if (substr($hex,0,1) != "#"){
return False;
}
return True;
}
//编辑文章界面新增 Meta 编辑模块
function argon_meta_box_1(){
wp_nonce_field("argon_meta_box_nonce_action", "argon_meta_box_nonce");
global $post;
?>
<h4>显示字数和预计阅读时间</h4>
<?php $argon_meta_hide_readingtime = get_post_meta($post->ID, "argon_hide_readingtime", true);?>
<select name="argon_meta_hide_readingtime" id="argon_meta_hide_readingtime">
<option value="false" <?php if ($argon_meta_hide_readingtime=='false'){echo 'selected';} ?>>跟随全局设置</option>
<option value="true" <?php if ($argon_meta_hide_readingtime=='true'){echo 'selected';} ?>>不显示</option>
</select>
<p style="margin-top: 15px;">是否显示字数和预计阅读时间 Meta 信息</p>
<?php
}
function argon_add_meta_boxes(){
add_meta_box('argon_meta_box_1', "文章设置", 'argon_meta_box_1', array('post', 'page'), 'side', 'low');
}
add_action('admin_menu', 'argon_add_meta_boxes');
function argon_save_meta_data($post_id){
if (!isset($_POST['argon_meta_box_nonce'])){
return $post_id;
}
$nonce = $_POST['argon_meta_box_nonce'];
if (!wp_verify_nonce($nonce, 'argon_meta_box_nonce_action')){
return $post_id;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return $post_id;
}
if ($_POST['post_type'] == 'post'){
if (!current_user_can('edit_post', $post_id)){
return $post_id;
}
}
if ($_POST['post_type'] == 'page'){
if (!current_user_can('edit_page', $post_id)){
return $post_id;
}
}
update_post_meta($post_id, 'argon_hide_readingtime', $_POST['argon_meta_hide_readingtime']);
}
add_action('save_post', 'argon_save_meta_data');
//主题文章短代码解析
add_shortcode('br','shortcode_br');
function shortcode_br($attr,$content=""){
return "</br>";
}
add_shortcode('label','shortcode_label');
function shortcode_label($attr,$content=""){
$out = "<span class='badge";
$color = isset($attr['color']) ? $attr['color'] : 'indigo';
switch ($color){
case 'indigo':
$out .= " badge-primary";
break;
case 'green':
$out .= " badge-success";
break;
case 'red':
$out .= " badge-danger";
break;
case 'orange':
$out .= " badge-warning";
break;
case 'blue':
$out .= " badge-info";
break;
default:
$out .= " badge-primary";
break;
}
$shape = isset($attr['shape']) ? $attr['shape'] : 'square';
if ($shape=="round"){
$out .= " badge-pill";
}
$out .= "'>" . $content . "</span>";
return $out;
}
add_shortcode('progressbar','shortcode_progressbar');
function shortcode_progressbar($attr,$content=""){
$out = "<div class='progress-wrapper'><div class='progress-info'>";
if ($content != ""){
$out .= "<div class='progress-label'><span>" . $content . "</span></div>";
}
$progress = isset($attr['progress']) ? $attr['progress'] : 100;
$out .= "<div class='progress-percentage'><span>" . $progress . "%</span></div>";
$out .= "</div><div class='progress'><div class='progress-bar";
$color = isset($attr['color']) ? $attr['color'] : 'indigo';
switch ($color){
case 'indigo':
$out .= " bg-primary";
break;
case 'green':
$out .= " bg-success";
break;
case 'red':
$out .= " bg-danger";
break;
case 'orange':
$out .= " bg-warning";
break;
case 'blue':
$out .= " bg-info";
break;
default:
$out .= " bg-primary";
break;
}
$out .= "' style='width: " . $progress . "%;'></div></div></div>";
return $out;
}
add_shortcode('checkbox','shortcode_checkbox');
function shortcode_checkbox($attr,$content=""){
$checked = isset($attr['checked']) ? $attr['checked'] : 'false';
$inline = isset($attr['inline']) ? $attr['checked'] : 'false';
$out = "<div class='shortcode-todo custom-control custom-checkbox";
if ($inline == 'true'){
$out .= " inline";
}
$out .= "'>
<input class='custom-control-input' type='checkbox'" . ($checked == 'true' ? ' checked' : '') . ">
<label class='custom-control-label'>
<span>" . $content . "</span>
</label>
</div>";
return $out;
}
add_shortcode('alert','shortcode_alert');
function shortcode_alert($attr,$content=""){
$out = "<div class='alert";
$color = isset($attr['color']) ? $attr['color'] : 'indigo';
switch ($color){
case 'indigo':
$out .= " alert-primary";
break;
case 'green':
$out .= " alert-success";
break;
case 'red':
$out .= " alert-danger";
break;
case 'orange':
$out .= " alert-warning";
break;
case 'blue':
$out .= " alert-info";
break;
case 'black':
$out .= " alert-default";
break;
default:
$out .= " alert-primary";
break;
}
$out .= "'>";
if (isset($attr['icon'])){
$out .= "<span class='alert-inner--icon'><i class='fa fa-" . $attr['icon'] . "'></i></span>";
}
$out .= "<span class='alert-inner--text'>";
if (isset($attr['title'])){
$out .= "<strong>" . $attr['title'] . "</strong> ";
}
$out .= $content . "</div>";
return $out;
}
add_shortcode('admonition','shortcode_admonition');
function shortcode_admonition($attr,$content=""){
$out = "<div class='admonition shadow-sm";
$color = isset($attr['color']) ? $attr['color'] : 'indigo';
switch ($color){
case 'indigo':
$out .= " admonition-primary";
break;
case 'green':
$out .= " admonition-success";
break;
case 'red':
$out .= " admonition-danger";
break;
case 'orange':
$out .= " admonition-warning";
break;
case 'blue':
$out .= " admonition-info";
break;
case 'black':
$out .= " admonition-default";
break;
case 'grey':
$out .= " admonition-grey";
break;
default:
$out .= " admonition-primary";
break;
}
$out .= "'>";
if (isset($attr['title'])){
$out .= "<div class='admonition-title'>";
if (isset($attr['icon'])){
$out .= "<i class='fa fa-" . $attr['icon'] . "'></i> ";
}
$out .= $attr['title'] . "</div>";
}
if ($content != ''){
$out .= "<div class='admonition-body'>" . $content . "</div>";
}
$out .= "</div>";
return $out;
}
add_shortcode('collapse','shortcode_collapse_block');
function shortcode_collapse_block($attr,$content=""){
$collapse_id = mt_rand(1000000000 , 9999999999);
$collapsed = isset($attr['collapsed']) ? $attr['collapsed'] : 'true';
$show_border_left = isset($attr['showleftborder']) ? $attr['showleftborder'] : 'false';
$out = "<div collapse-id='" . $collapse_id . "'" ;
$out .= " class='collapse-block shadow-sm";
$color = isset($attr['color']) ? $attr['color'] : 'none';
switch ($color){
case 'indigo':
$out .= " collapse-block-primary";
break;
case 'green':
$out .= " collapse-block-success";
break;
case 'red':
$out .= " collapse-block-danger";
break;
case 'orange':
$out .= " collapse-block-warning";
break;
case 'blue':
$out .= " collapse-block-info";
break;
case 'black':
$out .= " collapse-block-default";
break;
case 'grey':
$out .= " collapse-block-grey";
break;
case 'none':
$out .= " collapse-block-transparent";
break;
default:
$out .= " collapse-block-transparent";
break;
}
if ($collapsed == 'true'){
$out .= " collapsed";
}
if ($show_border_left != 'true'){
$out .= " hide-border-left";
}
$out .= "'>";
$out .= "<div class='collapse-block-title' collapse-id='" . $collapse_id . "'>";
if (isset($attr['icon'])){