-
-
Notifications
You must be signed in to change notification settings - Fork 559
/
functions.php
3176 lines (3130 loc) · 109 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: 0;z-index: 2147483647;'>" . __("Argon 主题不支持 Wordpress 4.4 以下版本,请更新 Wordpress", 'argon') . "</div>";
}
function theme_slug_setup() {
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
load_theme_textdomain('argon', get_template_directory() . '/languages');
}
add_action('after_setup_theme','theme_slug_setup');
$argon_version = !(wp_get_theme() -> Template) ? wp_get_theme() -> Version : wp_get_theme(wp_get_theme() -> Template) -> Version;
$GLOBALS['theme_version'] = $argon_version;
$argon_assets_path = get_option("argon_assets_path");
switch ($argon_assets_path) {
case "jsdelivr":
$GLOBALS['assets_path'] = "https://cdn.jsdelivr.net/gh/solstice23/argon-theme@" . $argon_version;
break;
case "fastgit":
$GLOBALS['assets_path'] = "https://raw.fastgit.org/solstice23/argon-theme/v" . $argon_version;
break;
case "sourcegcdn":
$GLOBALS['assets_path'] = "https://gh.sourcegcdn.com/solstice23/argon-theme/v" . $argon_version;
break;
case "jsdelivr_gcore":
$GLOBALS['assets_path'] = "https://gcore.jsdelivr.net/gh/solstice23/argon-theme@" . $argon_version;
break;
case "jsdelivr_fastly":
$GLOBALS['assets_path'] = "https://fastly.jsdelivr.net/gh/solstice23/argon-theme@" . $argon_version;
break;
case "jsdelivr_cf":
$GLOBALS['assets_path'] = "https://testingcf.jsdelivr.net/gh/solstice23/argon-theme@" . $argon_version;
break;
case "custom":
$GLOBALS['assets_path'] = preg_replace('/\/$/', '', get_option("argon_custom_assets_path"));
$GLOBALS['assets_path'] = preg_replace('/%theme_version%/', $argon_version, $GLOBALS['assets_path']);
break;
default:
$GLOBALS['assets_path'] = get_bloginfo('template_url');
}
//翻译 Hook
function argon_locate_filter($locate){
if (substr($locate, 0, 2) == 'zh'){
if ($locate == 'zh_TW'){
return $locate;
}
return 'zh_CN';
}
if (substr($locate, 0, 2) == 'en'){
return 'en_US';
}
if (substr($locate, 0, 2) == 'ru'){
return 'ru_RU';
}
return 'en_US';
}
function argon_get_locate(){
if (function_exists("determine_locale")){
return argon_locate_filter(determine_locale());
}
$determined_locale = get_locale();
if (is_admin()){
$determined_locale = get_user_locale();
}
}
function theme_locale_hook($locate, $domain){
if ($domain == 'argon'){
return argon_locate_filter($locate);
}
return $locate;
}
add_filter('theme_locale', 'theme_locale_hook', 10, 2);
//更新主题版本后的兼容
$argon_last_version = get_option("argon_last_version");
if ($argon_last_version == ""){
$argon_last_version = "0.0";
}
if (version_compare($argon_last_version, $GLOBALS['theme_version'], '<' )){
if (version_compare($argon_last_version, '0.940', '<')){
if (get_option('argon_mathjax_v2_enable') == 'true' && get_option('argon_mathjax_enable') != 'true'){
update_option("argon_math_render", 'mathjax2');
}
if (get_option('argon_mathjax_enable') == 'true'){
update_option("argon_math_render", 'mathjax3');
}
}
if (version_compare($argon_last_version, '0.970', '<')){
if (get_option('argon_show_author') == 'true'){
update_option("argon_article_meta", 'time|views|comments|categories|author');
}
}
if (version_compare($argon_last_version, '1.1.0', '<')){
if (get_option('argon_enable_zoomify') != 'false'){
update_option("argon_enable_fancybox", 'true');
update_option("argon_enable_zoomify", 'false');
}
}
if (version_compare($argon_last_version, '1.3.4', '<')){
switch (get_option('argon_search_post_filter', 'post,page')){
case 'post,page':
update_option("argon_enable_search_filters", 'true');
update_option("argon_search_filters_type", '*post,*page,shuoshuo');
break;
case 'post,page,shuoshuo':
update_option("argon_enable_search_filters", 'true');
update_option("argon_search_filters_type", '*post,*page,*shuoshuo');
break;
case 'post,page,hide_shuoshuo':
update_option("argon_enable_search_filters", 'true');
update_option("argon_search_filters_type", '*post,*page');
break;
case 'off':
default:
update_option("argon_enable_search_filters", 'false');
break;
}
}
update_option("argon_last_version", $GLOBALS['theme_version']);
}
//检测更新
require_once(get_template_directory() . '/theme-update-checker/plugin-update-checker.php');
$argon_update_source = get_option('argon_update_source');
switch ($argon_update_source) {
case "stop":
break;
case "fastgit":
$argonThemeUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
'https://api.solstice23.top/argon/info.json?source=fastgit',
get_template_directory() . '/functions.php',
'argon'
);
break;
case "cfworker":
$argonThemeUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
'https://api.solstice23.top/argon/info.json?source=cfworker',
get_template_directory() . '/functions.php',
'argon'
);
break;
case "solstice23top":
$argonThemeUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
'https://api.solstice23.top/argon/info.json?source=0',
get_template_directory() . '/functions.php',
'argon'
);
break;
case "github":
default:
$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($GLOBALS['theme_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' => __('左侧栏小工具', 'argon'),
'id' => 'leftbar-tools',
'description' => __( '左侧栏小工具 (如果设置会在侧栏增加一个 Tab)', 'argon'),
'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>',
)
);
register_sidebar(
array(
'name' => __('右侧栏小工具', 'argon'),
'id' => 'rightbar-tools',
'description' => __( '右侧栏小工具 (在 "Argon 主题选项" 中选择 "三栏布局" 才会显示)', 'argon'),
'before_widget' => '<div id="%1$s" class="widget %2$s card shadow-sm bg-white border-0">',
'after_widget' => '</div>',
'before_title' => '<h6 class="font-weight-bold text-black">',
'after_title' => '</h6>',
)
);
register_sidebar(
array(
'name' => __('站点概览额外内容', 'argon'),
'id' => 'leftbar-siteinfo-extra-tools',
'description' => __( '站点概览额外内容', 'argon'),
'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 argon_add_admin_color(){
wp_admin_css_color(
'argon',
'Argon',
get_bloginfo('template_directory') . "/admin.css",
array("#5e72e4", "#324cdc", "#e8ebfb"),
array('base' => '#525f7f', 'focus' => '#5e72e4', 'current' => '#fff')
);
}
add_action('admin_init', 'argon_add_admin_color');
function argon_admin_themecolor_css(){
$themecolor = get_option("argon_theme_color", "#5e72e4");
$RGB = hexstr2rgb($themecolor);
$HSL = rgb2hsl($RGB['R'], $RGB['G'], $RGB['B']);
echo "
<style id='themecolor_css'>
:root{
--themecolor: {$themecolor} ;
--themecolor-R: {$RGB['R']} ;
--themecolor-G: {$RGB['G']} ;
--themecolor-B: {$RGB['B']} ;
--themecolor-H: {$HSL['H']} ;
--themecolor-S: {$HSL['S']} ;
--themecolor-L: {$HSL['L']} ;
}
</style>
";
if (get_option("argon_enable_immersion_color", "false") == "true"){
echo "<script> document.documentElement.classList.add('immersion-color'); </script>";
}
}
add_filter('admin_head', 'argon_admin_themecolor_css');
function array_remove(&$arr, $item){
$pos = array_search($item, $arr);
if ($pos !== false){
array_splice($arr, $pos, 1);
}
}
//数字格式化
function format_number_in_kilos($number) {
if ($number < 1000){
return $number;
}
if (1000 <= $number && $number < 1000000){
if (1000 <= $number && $number < 10000){
return round($number / 1000, 1) . "K";
}else{
return round($number / 1000, 0) . "K";
}
}
if (1000000 <= $number && $number <= 10000000){
return round($number / 1000000, 1) . "M";
}else{
return round($number / 1000000, 0) . "M";
}
}
//表情包
require_once(get_template_directory() . '/emotions.php');
//文章特色图片
function argon_get_first_image_of_article(){
global $post;
if (post_password_required()){
return false;
}
$post_content_full = apply_filters('the_content', preg_replace( '<!--more(.*?)-->', '', $post -> post_content));
preg_match('/<img(.*?)(src|data-original)=[\"\']((http:|https:)?\/\/(.*?))[\"\'](.*?)\/?>/', $post_content_full, $match);
if (isset($match[3])){
return $match[3];
}
return false;
}
function argon_has_post_thumbnail($postID = 0){
if ($postID == 0){
global $post;
$postID = $post -> ID;
}
if (has_post_thumbnail()){
return true;
}
$argon_first_image_as_thumbnail = get_post_meta($postID, 'argon_first_image_as_thumbnail', true);
if ($argon_first_image_as_thumbnail == ""){
$argon_first_image_as_thumbnail = "default";
}
if ($argon_first_image_as_thumbnail == "true" || ($argon_first_image_as_thumbnail == "default" && get_option("argon_first_image_as_thumbnail_by_default", "false") == "true")){
if (argon_get_first_image_of_article() != false){
return true;
}
}
return false;
}
function argon_get_post_thumbnail($postID = 0){
if ($postID == 0){
global $post;
$postID = $post -> ID;
}
if (has_post_thumbnail()){
return apply_filters("argon_post_thumbnail", wp_get_attachment_image_src(get_post_thumbnail_id($postID), "full")[0]);
}
return apply_filters("argon_post_thumbnail", argon_get_first_image_of_article());
}
//文末附加内容
function get_additional_content_after_post(){
global $post;
$postID = $post -> ID;
$res = get_post_meta($post -> ID, 'argon_after_post', true);
if ($res == "--none--"){
return "";
}
if ($res == ""){
$res = get_option("argon_additional_content_after_post");
}
$res = str_replace("\n", "</br>", $res);
$res = str_replace("%url%", get_permalink($postID), $res);
$res = str_replace("%link%", '<a href="' . get_permalink($postID) . '" target="_blank">' . get_permalink($postID) . '</a>', $res);
$res = str_replace("%title%", get_the_title(), $res);
$res = str_replace("%author%", get_the_author(), $res);
return $res;
}
//输出分页页码
function get_argon_formatted_paginate_links($maxPageNumbers, $extraClasses = ''){
$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 - ($maxPageNumbers - 1) / 2 , 1);
$to = min($current + $maxPageNumbers - ( $current - $from + 1 ) , $total);
if ($to - $from + 1 < $maxPageNumbers){
$to = min($current + ($maxPageNumbers - 1) / 2 , $total);
$from = max($current - ( $maxPageNumbers - ( $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' . $extraClasses . '">' . $html . '</ul></nav>';
}
function get_argon_formatted_paginate_links_for_all_platforms(){
return get_argon_formatted_paginate_links(7) . get_argon_formatted_paginate_links(5, " pagination-mobile");
}
//访问者 Token & Session
function get_random_token(){
return md5(uniqid(microtime(true), true));
}
function set_user_token_cookie(){
if (!isset($_COOKIE["argon_user_token"]) || strlen($_COOKIE["argon_user_token"]) != 32){
$newToken = get_random_token();
setcookie("argon_user_token", $newToken, time() + 10 * 365 * 24 * 60 * 60, "/");
$_COOKIE["argon_user_token"] = $newToken;
}
}
function session_init(){
set_user_token_cookie();
if (!session_id()){
session_start();
}
}
session_init();
//add_action('init', 'session_init');
//页面 Description Meta
function get_seo_description(){
global $post;
if (is_single() || is_page()){
if (get_the_excerpt() != ""){
return preg_replace('/ \[…]$/', '…', get_the_excerpt());
}
if (!post_password_required()){
return htmlspecialchars(mb_substr(str_replace("\n", '', strip_tags($post -> post_content)), 0, 50)) . "...";
}else{
return __("这是一个加密页面,需要密码来查看", 'argon');
}
}else{
return get_option('argon_seo_description');
}
}
//页面 Keywords
function get_seo_keywords(){
if (is_single()){
global $post;
$tags = get_the_tags('', ',', '', $post -> ID);
if ($tags != null){
$res = "";
foreach ($tags as $tag){
if ($res != ""){
$res .= ",";
}
$res .= $tag -> name;
}
return $res;
}
}
if (is_category()){
return single_cat_title('', false);
}
if (is_tag()){
return single_tag_title('', false);
}
if (is_author()){
return get_the_author();
}
if (is_post_type_archive()){
return post_type_archive_title('', false);
}
if (is_tax()){
return single_term_title('', false);
}
return get_option('argon_seo_keywords');
}
//页面分享预览图
function get_og_image(){
global $post;
$postID = $post -> ID;
$argon_first_image_as_thumbnail = get_post_meta($postID, 'argon_first_image_as_thumbnail', 'true');
if (has_post_thumbnail() || $argon_first_image_as_thumbnail == 'true'){
return argon_get_post_thumbnail($postID);
}
return '';
}
//页面浏览量
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';
}
return number_format_i18n($count);
}
function set_post_views(){
if (!is_single() && !is_page()) {
return;
}
if (!isset($post_id)){
global $post;
$post_id = $post -> ID;
}
if (post_password_required($post_id)){
return;
}
if (isset($_GET['preview'])){
if ($_GET['preview'] == 'true'){
if (current_user_can('publish_posts')){
return;
}
}
}
$noPostView = 'false';
if (isset($_POST['no_post_view'])){
$noPostView = $_POST['no_post_view'];
}
if ($noPostView == 'true'){
return;
}
global $post;
if (!isset($post -> ID)){
return;
}
$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){
preg_match_all('/<pre(.*?)>[\S\s]*?<code(.*?)>([\S\s]*?)<\/code>[\S\s]*?<\/pre>/im', $str, $codeSegments, PREG_PATTERN_ORDER);
$codeSegments = $codeSegments[3];
$codeTotal = 0;
foreach ($codeSegments as $codeSegment){
$codeLines = preg_split('/\r\n|\n|\r/', $codeSegment);
foreach ($codeLines as $line){
if (strlen(trim($str)) > 0){
$codeTotal++;
}
}
}
$str = preg_replace(
'/<code(.*?)>[\S\s]*?<\/code>/im',
'',
$str
);
$str = preg_replace(
'/<pre(.*?)>[\S\s]*?<\/pre>/im',
'',
$str
);
$str = preg_replace(
'/<style(.*?)>[\S\s]*?<\/style>/im',
'',
$str
);
$str = preg_replace(
'/<script(.*?)>[\S\s]*?<\/script>/im',
'',
$str
);
$str = preg_replace('/<[^>]+?>/', ' ', $str);
$str = html_entity_decode(strip_tags($str));
preg_match_all('/[\x{4e00}-\x{9fa5}]/u' , $str , $cnRes);
$cnTotal = count($cnRes[0]);
$enRes = preg_replace('/[\x{4e00}-\x{9fa5}]/u', '', $str);
preg_match_all('/[a-zA-Z0-9_\x{0392}-\x{03c9}\x{0400}-\x{04FF}]+|[\x{4E00}-\x{9FFF}\x{3400}-\x{4dbf}\x{f900}-\x{faff}\x{3040}-\x{309f}\x{ac00}-\x{d7af}\x{0400}-\x{04FF}]+|[\x{00E4}\x{00C4}\x{00E5}\x{00C5}\x{00F6}\x{00D6}]+|\w+/u' , $str , $enRes);
$enTotal = count($enRes[0]);
return array(
'cn' => $cnTotal,
'en' => $enTotal,
'code' => $codeTotal,
);
}
function get_article_words_total($str){
$res = get_article_words($str);
return $res['cn'] + $res['en'] + $res['code'];
}
function get_reading_time($len){
$speedcn = get_option('argon_reading_speed', 300);
$speeden = get_option('argon_reading_speed_en', 160);
$speedcode = get_option('argon_reading_speed_code', 20);
$reading_time = $len['cn'] / $speedcn + $len['en'] / $speeden + $len['code'] / $speedcode;
if ($reading_time < 0.3){
return __("几秒读完", 'argon');
}
if ($reading_time < 1){
return __("1 分钟内", 'argon');
}
if ($reading_time < 60){
return ceil($reading_time) . " " . __("分钟", 'argon');
}
return round($reading_time / 60 , 1) . " " . __("小时", 'argon');
}
//当前文章是否可以生成目录
function have_catalog(){
if (!is_single() && !is_page()){
return false;
}
if (post_password_required()){
return false;
}
if (is_page() && is_page_template('timeline.php')){
return true;
}
$content = get_post(get_the_ID()) -> post_content;
if (preg_match('/<h[1-6](.*?)>/',$content)){
return true;
}else{
return false;
}
}
//获取文章 Meta
function get_article_meta($type){
if ($type == 'sticky'){
return '<div class="post-meta-detail post-meta-detail-stickey">
<i class="fa fa-thumb-tack" aria-hidden="true"></i>
' . _x('置顶', 'pinned', 'argon') . '
</div>';
}
if ($type == 'needpassword'){
return '<div class="post-meta-detail post-meta-detail-needpassword">
<i class="fa fa-lock" aria-hidden="true"></i>
' . __('需要密码', 'argon') . '
</div>';
}
if ($type == 'time'){
return '<div class="post-meta-detail post-meta-detail-time">
<i class="fa fa-clock-o" aria-hidden="true"></i>
<time title="' . __('发布于', 'argon') . ' ' . get_the_time('Y-n-d G:i:s') . ' | ' . __('编辑于', 'argon') . ' ' . get_the_modified_time('Y-n-d G:i:s') . '">' .
get_the_time('Y-n-d G:i') . '
</time>
</div>';
}
if ($type == 'edittime'){
return '<div class="post-meta-detail post-meta-detail-edittime">
<i class="fa fa-clock-o" aria-hidden="true"></i>
<time title="' . __('发布于', 'argon') . ' ' . get_the_time('Y-n-d G:i:s') . ' | ' . __('编辑于', 'argon') . ' ' . get_the_modified_time('Y-n-d G:i:s') . '">' .
get_the_modified_time('Y-n-d G:i') . '
</time>
</div>';
}
if ($type == 'views'){
if (function_exists('pvc_get_post_views')){
$views = pvc_get_post_views(get_the_ID());
}else{
$views = get_post_views(get_the_ID());
}
return '<div class="post-meta-detail post-meta-detail-views">
<i class="fa fa-eye" aria-hidden="true"></i> ' .
$views .
'</div>';
}
if ($type == 'comments'){
return '<div class="post-meta-detail post-meta-detail-comments">
<i class="fa fa-comments-o" aria-hidden="true"></i> ' .
get_post(get_the_ID()) -> comment_count .
'</div>';
}
if ($type == 'categories'){
$res = '<div class="post-meta-detail post-meta-detail-categories">
<i class="fa fa-bookmark-o" aria-hidden="true"></i> ';
$categories = get_the_category();
foreach ($categories as $index => $category){
$res .= '<a href="' . get_category_link($category -> term_id) . '" target="_blank" class="post-meta-detail-catagory-link">' . $category -> cat_name . '</a>';
if ($index != count($categories) - 1){
$res .= '<span class="post-meta-detail-catagory-space">,</span>';
}
}
$res .= '</div>';
return $res;
}
if ($type == 'author'){
$res = '<div class="post-meta-detail post-meta-detail-author">
<i class="fa fa-user-circle-o" aria-hidden="true"></i> ';
global $authordata;
$res .= '<a href="' . get_author_posts_url($authordata -> ID, $authordata -> user_nicename) . '" target="_blank">' . get_the_author() . '</a>
</div>';
return $res;
}
}
//获取文章字数统计和预计阅读时间
function get_article_reading_time_meta($post_content_full){
$post_content_full = apply_filters("argon_html_before_wordcount", $post_content_full);
$words = get_article_words($post_content_full);
$res = '</br><div class="post-meta-detail post-meta-detail-words">
<i class="fa fa-file-word-o" aria-hidden="true"></i>';
if ($words['code'] > 0){
$res .= '<span title="' . sprintf(__( '包含 %d 行代码', 'argon'), $words['code']) . '">';
}else{
$res .= '<span>';
}
$res .= ' ' . get_article_words_total($post_content_full) . " " . __("字", 'argon');
$res .= '</span>
</div>
<div class="post-meta-devide">|</div>
<div class="post-meta-detail post-meta-detail-words">
<i class="fa fa-hourglass-end" aria-hidden="true"></i>
' . get_reading_time(get_article_words($post_content_full)) . '
</div>
';
return $res;
}
//当前文章是否隐藏 阅读时间 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;
}
//当前文章是否隐藏 发布时间和分类 (简洁 Meta)
function is_meta_simple(){
global $post;
if (get_post_meta($post -> ID, 'argon_meta_simple', true) == 'true'){
return true;
}
return false;
}
//根据文章 id 获取标题
function get_post_title_by_id($id){
return get_post($id) -> post_title;
}
//解析 UA 和相应图标
require_once(get_template_directory() . '/useragent-parser.php');
$argon_comment_ua = get_option("argon_comment_ua");
$argon_comment_show_ua = Array();
if (strpos($argon_comment_ua, 'platform') !== false){
$argon_comment_show_ua['platform'] = true;
}
if (strpos($argon_comment_ua, 'browser') !== false){
$argon_comment_show_ua['browser'] = true;
}
if (strpos($argon_comment_ua, 'version') !== false){
$argon_comment_show_ua['version'] = true;
}
function parse_ua_and_icon($userAgent){
global $argon_comment_ua;
global $argon_comment_show_ua;
if ($argon_comment_ua == "" || $argon_comment_ua == "hidden"){
return "";
}
$parsed = argon_parse_user_agent($userAgent);
$out = "<div class='comment-useragent'>";
if (isset($argon_comment_show_ua['platform']) && $argon_comment_show_ua['platform'] == true){
if (isset($GLOBALS['UA_ICON'][$parsed['platform']])){
$out .= $GLOBALS['UA_ICON'][$parsed['platform']] . " ";
}else{
$out .= $GLOBALS['UA_ICON']['Unknown'] . " ";
}
$out .= $parsed['platform'];
}
if (isset($argon_comment_show_ua['browser']) && $argon_comment_show_ua['browser'] == true){
if (isset($GLOBALS['UA_ICON'][$parsed['browser']])){
$out .= " " . $GLOBALS['UA_ICON'][$parsed['browser']];
}else{
$out .= " " . $GLOBALS['UA_ICON']['Unknown'];
}
$out .= " " . $parsed['browser'];
if (isset($argon_comment_show_ua['version']) && $argon_comment_show_ua['version'] == true){
$out .= " " . $parsed['version'];
}
}
$out .= "</div>";
return apply_filters("argon_comment_ua_icon", $out);
}
//发送邮件
function send_mail($to, $subject, $content){
wp_mail($to, $subject, $content, array('Content-Type: text/html; charset=UTF-8'));
}
function check_email_address($email){
return (bool) preg_match( "/^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+(([.\-])[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/", $email );
}
//检验评论 Token 和用户 Token 是否一致
function check_comment_token($id){
if (strlen($_COOKIE['argon_user_token']) != 32){
return false;
}
if ($_COOKIE['argon_user_token'] != get_comment_meta($id, "user_token", true)){
return false;
}
return true;
}
//检验评论发送者 ID 和当前登录用户 ID 是否一致
function check_login_user_same($userid){
if ($userid == 0){
return false;
}
if ($userid != (wp_get_current_user() -> ID)){
return false;
}
return true;
}
function get_comment_user_id_by_id($comment_ID){
$comment = get_comment($comment_ID);
return $comment -> user_id;
}
function check_comment_userid($id){
if (!check_login_user_same(get_comment_user_id_by_id($id))){
return false;
}
return true;
}
//悄悄话
function is_comment_private_mode($id){
if (strlen(get_comment_meta($id, "private_mode", true)) != 32){
return false;
}
return true;
}
function user_can_view_comment($id){
if (!is_comment_private_mode($id)){
return true;
}
if (current_user_can("manage_options")){
return true;
}
if ($_COOKIE['argon_user_token'] == get_comment_meta($id, "private_mode", true)){
return true;
}
return false;
}
//过滤 RSS 中悄悄话
function remove_rss_private_comment_title_and_author($str){
global $comment;
if (isset($comment -> comment_ID) && is_comment_private_mode($comment -> comment_ID)){
return "***";
}
return $str;
}
add_filter('the_title_rss' , 'remove_rss_private_comment_title_and_author');
add_filter('comment_author_rss' , 'remove_rss_private_comment_title_and_author');
function remove_rss_private_comment_content($str){
global $comment;
if (is_comment_private_mode($comment -> comment_ID)){
$comment -> comment_content = __('该评论为悄悄话', 'argon');
return $comment -> comment_content;
}
return $str;
}
add_filter('comment_text_rss' , 'remove_rss_private_comment_content');
//评论回复信息
function get_comment_parent_info($comment){
if (!$GLOBALS['argon_comment_options']['show_comment_parent_info']){
return "";
}
if ($comment -> comment_parent == 0){
return "";
}
$parent_comment = get_comment($comment -> comment_parent);
return '<div class="comment-parent-info" data-parent-id=' . $parent_comment -> comment_ID . '><i class="fa fa-reply" aria-hidden="true"></i> ' . get_comment_author($parent_comment -> comment_ID) . '</div>';
}
//是否可以查看评论编辑记录
function can_visit_comment_edit_history($id){
$who_can_visit_comment_edit_history = get_option("argon_who_can_visit_comment_edit_history");
if ($who_can_visit_comment_edit_history == ""){
$who_can_visit_comment_edit_history = "admin";
}
switch ($who_can_visit_comment_edit_history) {
case 'everyone':
return true;
case 'commentsender':
if (check_comment_token($id) || check_comment_userid($id)){
return true;
}
return false;
default:
if (current_user_can("moderate_comments")){
return true;
}
return false;
}
}
//获取评论编辑记录
function get_comment_edit_history(){
$id = $_POST['id'];
if (!can_visit_comment_edit_history($id)){
exit(json_encode(array(
'id' => $_POST['id'],
'history' => ""
)));
}
$editHistory = json_decode(get_comment_meta($id, "comment_edit_history", true));
$editHistory = array_reverse($editHistory);
$res = "";
$position = count($editHistory) + 1;
date_default_timezone_set(get_option('timezone_string'));
foreach ($editHistory as $edition){
$position -= 1;
$res .= "<div class='comment-edit-history-item'>
<div class='comment-edit-history-title'>
<div class='comment-edit-history-id'>
#" . $position . "
</div>
" . ($edition -> isfirst ? "<span class='badge badge-primary badge-admin'>" . __("最初版本", 'argon') . "</span>" : "") . "
</div>
<div class='comment-edit-history-time'>" . date('Y-m-d H:i:s', $edition -> time) . "</div>
<div class='comment-edit-history-content'>" . str_replace("\n", "</br>", $edition -> content) . "</div>
</div>";
}
exit(json_encode(array(
'id' => $_POST['id'],
'history' => $res
)));
}
add_action('wp_ajax_get_comment_edit_history', 'get_comment_edit_history');
add_action('wp_ajax_nopriv_get_comment_edit_history', 'get_comment_edit_history');
//是否可以置顶/取消置顶
function is_comment_pinable($id){
if (get_comment($id) -> comment_approved != "1"){
return false;
}
if (get_comment($id) -> comment_parent != 0){
return false;
}
if (is_comment_private_mode($id)){
return false;
}
return true;
}
//评论内容格式化
function argon_get_comment_text($comment_ID = 0, $args = array()) {
$comment = get_comment($comment_ID);
$comment_text = get_comment_text($comment, $args);
$enableMarkdown = get_comment_meta(get_comment_ID(), "use_markdown", true);
/*if ($enableMarkdown == false){
return $comment_text;
}*/
//图片
$comment_text = preg_replace(
'/<a data-src="(.*?)" title="(.*?)" class="comment-image"(.*?)>([\w\W]*)<\/a>/',
'<img src="$1" alt="$2" />',
$comment_text
);
$comment_text = preg_replace(
'/<img src="(.*?)" alt="(.*?)" \/>/',
'<a href="$1" title="$2" data-fancybox="comment-' . $comment -> comment_ID . '-image" class="comment-image" rel="nofollow">
<i class="fa fa-image" aria-hidden="true"></i>
' . __('查看图片', 'argon') . '
<img src="" alt="$2" class="comment-image-preview">
<i class="comment-image-preview-mask"></i>
</a>',
$comment_text
);
//表情
if (get_option("argon_comment_emotion_keyboard", "true") != "false"){
global $emotionListDefault;
$emotionList = apply_filters("argon_emotion_list", $emotionListDefault);
foreach ($emotionList as $groupIndex => $group){
foreach ($group['list'] as $index => $emotion){
if ($emotion['type'] != 'sticker'){
continue;
}
if (!isset($emotion['code']) || mb_strlen($emotion['code']) == 0){
continue;
}
if (!isset($emotion['src']) || mb_strlen($emotion['src']) == 0){
continue;
}
$comment_text = str_replace(':' . $emotion['code'] . ':', "<img class='comment-sticker lazyload' src='data:image/svg+xml;base64,PHN2ZyBjbGFzcz0iZW1vdGlvbi1sb2FkaW5nIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGZpbGw9Im5vbmUiIHZpZXdCb3g9Ii04IC04IDQwIDQwIiBzdHJva2U9IiM4ODgiIG9wYWNpdHk9Ii41IiB3aWR0aD0iNjAiIGhlaWdodD0iNjAiPgogIDxwYXRoIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgc3Ryb2tlLXdpZHRoPSIxLjUiIGQ9Ik0xNC44MjggMTQuODI4YTQgNCAwIDAxLTUuNjU2IDBNOSAxMGguMDFNMTUgMTBoLjAxTTIxIDEyYTkgOSAwIDExLTE4IDAgOSA5IDAgMDExOCAweiIvPgo8L3N2Zz4=' data-original='" . $emotion['src'] . "'/><noscript><img class='comment-sticker' src='" . $emotion['src'] . "'/></noscript>", $comment_text);
}
}
}
return apply_filters( 'comment_text', $comment_text, $comment, $args );
}
//评论点赞
function get_comment_upvotes($id) {
$comment = get_comment($id);
if ($comment == null){
return 0;
}
$upvotes = get_comment_meta($comment -> comment_ID, "upvotes", true);
if ($upvotes == null) {
$upvotes = 0;