-
Notifications
You must be signed in to change notification settings - Fork 18
/
functions.php
841 lines (753 loc) · 27.3 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
<?php
//error_reporting( E_ALL&~E_NOTICE );
require_once dirname(__FILE__) . '/framework/cs-framework.php';
require_once dirname(__FILE__) . '/includes/wxShare.php';
require_once dirname(__FILE__) . '/includes/author-avatars.php';
$i_markdown_option = cs_get_option('i_markdown_option'); //markdown编辑器
$i_admin_dark_mode = cs_get_option('i_admin_dark_mode'); //网站后台暗黑模式设置
if($i_markdown_option):
add_filter('use_block_editor_for_post', '__return_false'); // 禁止加载Gutenberg(古腾堡) 编辑器
remove_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' ); // 禁止前端加载样式文件
require_once dirname(__FILE__) . '/includes/wp-editormd/wp-editormd.php';
endif;
require_once dirname(__FILE__) . '/widgets.php';
require_once dirname(__FILE__) . '/includes/ajax-comment/main.php';
// -(or)-
define('CS_ACTIVE_FRAMEWORK', TRUE); // default true
define('CS_ACTIVE_METABOX', TRUE); // default true
define('CS_ACTIVE_TAXONOMY', FALSE); // default true
define('CS_ACTIVE_SHORTCODE', FALSE); // default true
define('CS_ACTIVE_CUSTOMIZE', FALSE); // default true
/*
* 添加一个简单的菜单
* 自行修改 'title' 和 'href' 的值
*/
function custom_adminbar_menu($meta = TRUE)
{
global $wp_admin_bar;
$i_markdown_option = cs_get_option('i_markdown_option'); //markdown编辑器
if (!is_user_logged_in()) {return;}
if (!is_super_admin() || !is_admin_bar_showing()) {return;}
echo '<style>
#wp-admin-bar-version-help .version-help{height:20px;background:#ca4a1f;border-radius:20px;display:inline-block;font-weight:bold;padding:0px 10px;line-height:20px;}
#wp-admin-bar-version-help .version-help:hover{color:#fff!important;}
#wp-admin-bar-version-help .version-help:active{color:#fff!important;}</style>';
$wp_admin_bar->add_menu([
'id' => 'custom_menu',
'title' => __('<i style="position: relative;top:7px;color:#fff"
class="wp-menu-image dashicons-before dashicons-admin-settings">
</i> smarty_hankin 主题设置 <span style="border-radius:10px;padding:3px 6px;color:#fff;background:#1b81c2">v'._the_theme_version().'</span>'),
'href' => '/wp-admin/admin.php?page=cs-framework',
//'meta' => array( 'target' => '_blank' )
]);
if($i_markdown_option):
$wp_admin_bar->add_menu([
'id' => 'markdown_edit',
'title' => __('<i style="position: relative;top:5px;color:#fff" class="wp-menu-image dashicons-before dashicons-admin-generic"></i> Markdown 编辑器设置'),
'href' => '/wp-admin/plugins.php?page=wp-editormd-settings',
// 'meta' => array( target => '_blank' )
]);
endif;
}
add_action('admin_bar_menu', 'custom_adminbar_menu', 71);
register_nav_menu('warp-nav', 'smarty_hankin-左侧菜单');
register_nav_menu('top-warp-nav', 'smarty_hankin-顶部菜单');
/* 设置登录样式*/
function wp_login_style() {
$authorCard = new AuthorCard();
$widget_cs_widget_author = reset(get_option('widget_cs_widget_author'));
$advertising = $widget_cs_widget_author['advertising'];
wp_enqueue_style( "wp-login", get_template_directory_uri() . "/assets/css/admin/wp-login.css?v=".time() );
echo '<style>
.login {
background-image: url("'.get_template_directory_uri().'/assets/images/nav-bg/body-bg-'.rand(1,12).'.png");
}
#login h1 a {
background: #ccc url("'.$advertising.'");
}
</style>';
}
add_action('login_enqueue_scripts', 'wp_login_style');
if($i_admin_dark_mode):
/* 设置后台样式*/
function admin_mycss() {
wp_enqueue_style( "admin-my", get_template_directory_uri() . "/assets/css/admin/wp-admin.css");
}
add_action('admin_head', 'admin_mycss');
endif;
/* 获取主题名称 */
function _the_theme_name()
{
$current_theme = wp_get_theme();
return $current_theme->get('Name');
}
/* 获取主题版本 */
function _the_theme_version()
{
$current_theme = wp_get_theme();
return $current_theme->get('Version');
}
/* 启用标题 */
function show_wp_title()
{
global $page, $paged;
wp_title('-', true, 'right');
// 添加网站标题.
bloginfo('name');
// 为首页添加网站描述.
$site_description = get_bloginfo('description', 'display');
if ($site_description && (is_home() || is_front_page())) {
echo ' - ' . $site_description;
}
// 如果有必要,在标题上显示一个页面数.
if ($paged >= 2 || $page >= 2) {
echo ' - ' . sprintf('第%s页', max($paged, $page));
}
}
/* 清除谷歌字体 */
function coolwp_remove_open_sans_from_wp_core()
{
wp_deregister_style('open-sans');
wp_register_style('open-sans', false);
wp_enqueue_style('open-sans', '');
}
add_action('init', 'coolwp_remove_open_sans_from_wp_core');
/* 清除wp_head无用内容 */
function disable_emojis() {
remove_action( 'wp_head', 'wp_generator' ); //移除WordPress版本
remove_action( 'wp_head', 'rsd_link' ); //移除离线编辑器开放接口
remove_action( 'wp_head', 'wlwmanifest_link' ); //移除离线编辑器开放接口
remove_action( 'wp_head', 'index_rel_link' ); //去除本页唯一链接信息
remove_action( 'wp_head', 'feed_links', 2 ); //移除feed
remove_action( 'wp_head', 'feed_links_extra', 3 ); //移除feed
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); //移除wp-json链
remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); //头部的JS代码
remove_action( 'wp_head', 'wp_print_styles', 8 ); //emoji载入css
remove_action( 'wp_head', 'rel_canonical' ); //rel=canonical
add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 ); //头部加载DNS预获取(dns-prefetch)
}
add_action( 'init', 'disable_emojis' );
//移除WordPress头部加载DNS预获取(dns-prefetch)
function remove_dns_prefetch( $hints, $relation_type ) {
if ( 'dns-prefetch' === $relation_type ) {
return array_diff( wp_dependencies_unique_hosts(), $hints );
}
return $hints;
}
/* 开启文章形式 */
//文章形式
add_theme_support('post-formats', [
'aside',
'image',
'video',
'quote',
'link',
'gallery',
'status',
'audio',
'chat',
]);
/* 把状态改为说说 */
function rename_post_formats($safe_text)
{
if ($safe_text == '状态') return '说说';
return $safe_text;
}
add_filter('esc_html', 'rename_post_formats');
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
/**
* WordPress 添加面包屑导航
* https://www.wpdaxue.com/wordpress-add-a-breadcrumb.html
*/
function cmp_breadcrumbs()
{
$delimiter = ' › '; // 分隔符
$before = '<span class="current">'; // 在当前链接前插入
$after = '</span>'; // 在当前链接后插入
if (!is_home() && !is_front_page() || is_paged())
{
echo '<ul class="breadcrumb" itemscope="">' . __('', 'cmp');
global $post;
$homeLink = home_url() . '/';
echo '<a itemprop="breadcrumb" href="' . $homeLink . '">' . __('首页', 'cmp') . '</a> ' . $delimiter . ' ';
if (is_category())
{ // 分类 存档
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category($thisCat);
$parentCat = get_category($thisCat->parent);
if ($thisCat->parent != 0)
{
$cat_code = get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' ');
echo $cat_code = str_replace('<a', '<a itemprop="breadcrumb"', $cat_code);
}
echo $before . '' . single_cat_title('', FALSE) . '' . $after;
}
elseif (is_day())
{ // 天 存档
echo '<a itemprop="breadcrumb" href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
echo '<a itemprop="breadcrumb" href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
echo $before . get_the_time('d') . $after;
}
elseif (is_month())
{ // 月 存档
echo '<a itemprop="breadcrumb" href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
echo $before . get_the_time('F') . $after;
}
elseif (is_year())
{ // 年 存档
echo $before . get_the_time('Y') . $after;
}
elseif (is_single() && !is_attachment())
{ // 文章
if (get_post_type() != 'post')
{ // 自定义文章类型
$post_type = get_post_type_object(get_post_type());
$slug = $post_type->rewrite;
echo '<a itemprop="breadcrumb" href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a> ' . $delimiter . ' ';
echo $before . get_the_title() . $after;
}
else
{ // 文章 post
$cat = get_the_category();
$cat = $cat[0];
$cat_code = get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
echo $cat_code = str_replace('<a', '<a itemprop="breadcrumb"', $cat_code);
echo $before . '正文' . $after;
}
}
elseif (!is_single() && !is_page() && get_post_type() != 'post')
{
$post_type = get_post_type_object(get_post_type());
echo $before . $post_type->labels->singular_name . $after;
}
elseif (is_attachment())
{ // 附件
$parent = get_post($post->post_parent);
$cat = get_the_category($parent->ID);
$cat = $cat[0];
echo '<a itemprop="breadcrumb" href="' . get_permalink($parent) . '">' . $parent->post_title . '</a> ' . $delimiter . ' ';
echo $before . get_the_title() . $after;
}
elseif (is_page() && !$post->post_parent)
{ // 页面
echo $before . get_the_title() . $after;
}
elseif (is_page() && $post->post_parent)
{ // 父级页面
$parent_id = $post->post_parent;
$breadcrumbs = [];
while ($parent_id)
{
$page = get_page($parent_id);
$breadcrumbs[] = '<a itemprop="breadcrumb" href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
echo $before . get_the_title() . $after;
}
elseif (is_search())
{ // 搜索结果
printf(__('搜索结果: <b style="color:#4680ff">%s</b>', 'cmp'), get_search_query());
}
elseif (is_tag())
{ //标签 存档
echo $before;
printf(__('<b>标签:</b> %s', 'cmp'), single_tag_title('', FALSE));
echo $after;
}
elseif (is_author())
{ // 作者存档
global $author;
$userdata = get_userdata($author);
echo $before;
printf(__('Author Archives: %s', 'cmp'), $userdata->display_name);
echo $after;
}
elseif (is_404())
{ // 404 页面
echo $before;
_e('Not Found', 'cmp');
echo $after;
}
if (get_query_var('paged'))
{ // 分页
if (is_category() || is_day() || is_month() || is_year() || is_tag() || is_author())
echo sprintf(__('( Page %s )', 'cmp'), get_query_var('paged'));
}
echo '</ul>';
}
}
// 获取信息
function getSystemInfo()
{
global $wpdb;
// 获取Mysql版本号
$mysqlVersion = $wpdb->get_row('SELECT VERSION() as v', ARRAY_A);
$mysqlVersion = $mysqlVersion ['v'];
// 获取数据库大小
$status = $wpdb->get_results('SHOW TABLE STATUS', ARRAY_A);
$size = 0;
foreach ($status as $row)
$size += $row ["Data_length"] + $row ["Index_length"];
$decimals = 2;
$mbytes = number_format($size / (1024 * 1024), $decimals);
// 获取上传大小
if (@ini_get('file_uploads')) $fileupload = ini_get('upload_max_filesize');
else $fileupload = '<font color="red">0</font>';
$table = "<table class='wp-list-table widefat fixed striped'>";
$table .= "<tbody>";
$table .= "<tr><td>主题开发</td><td><b>唤醒-hankin<b></td></tr>";
$table .= "<tr><td>QQ群</td><td><b>1018841266<b></td></tr>";
$table .= "<tr><td>服务器系统</td><td>" . php_uname('s') . "</td></tr>";
$table .= "<tr><td>服务器软件</td><td>" . $_SERVER ['SERVER_SOFTWARE'] . "</td></tr>";
$table .= "<tr><td>服务器 IP</td><td>" . $_SERVER ['SERVER_ADDR'] . "</td></tr>";
$table .= "<tr><td>服务器时间</td><td>" . date("Y-m-d H:i:s") . "</td></tr>";
$table .= "<tr><td>PHP版本号</td><td>" . PHP_VERSION . "</td></tr>";
$table .= "<tr><td>上传许可</td><td>" . $fileupload . "</td></tr>";
$table .= "<tr><td>MySQL 版本号</td><td>" . $mysqlVersion . "</td></tr>";
$table .= "<tr><td>MySQL 库占用</td><td>" . $mbytes . "</td></tr>";
$table .= "</tbody>";
$table .= "</table>";
return $table;
}
/**
* 首页调用分页
* @return string
*/
function homePage($query_string='')
{
global $posts_per_page, $paged;
$my_query = new WP_Query($query_string . "&posts_per_page=-1");
$total_posts = $my_query->post_count;
if (empty($paged)) $paged = 1;
$prev = $paged - 1;
$next = $paged + 1;
$range = 4; // only edit this if you want to show more page-links
$showitems = ($range * 2) + 1;
$pages = ceil($total_posts / $posts_per_page);
if (1 != $pages)
{
echo "<ol class='page-navigator'>";
echo ($paged > 2 && $paged + $range + 1 > $pages && $showitems < $pages) ? "
<li><a href='" . get_pagenum_link(1) . "'>首页</a></li>" : "";
echo ($paged > 1 && $showitems < $pages) ? "
<li><a href='" . get_pagenum_link($prev) . "'>上一页</a></li>" : "";
for ($i = 1; $i <= $pages; $i++)
{
if (1 != $pages && (!($i >= $paged + $range + 1 ||
$i <= $paged - $range - 1) || $pages <= $showitems))
{
echo ($paged == $i) ? "<li class='current'><span>" . $i . "</span></li>" :
"<li><a href='" . get_pagenum_link($i) . "' class='inactive' >" . $i . "</a></li>";
}
}
echo ($paged < $pages && $showitems < $pages) ?
"<li><a href='" . get_pagenum_link($next) . "'>下一页</a></li>" : "";
echo ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages) ?
"<li><a href='" . get_pagenum_link($pages) . "'>末页</a></li>" : "";
echo "</ol>\n";
}
}
/**
* 数字分页函数
* 因为wordpress默认仅仅提供简单分页
* 所以要实现数字分页,需要自定义函数
* @Param int $range 数字分页的宽度
* @Return string|empty 输出分页的HTML代码
*/
function categoryPage($range = 4)
{
global $paged, $wp_query;
$max_page = '';
if (!$max_page)
{
$max_page = $wp_query->max_num_pages;
}
if ($max_page > 1)
{
echo "<ol class='page-navigator'>";
if (!$paged)
{
$paged = 1;
}
if ($paged != 1)
{
echo "<li><a href='" . get_pagenum_link(1) . "' class='extend' title='跳转到首页'>首页</a></li>";
}
echo ($paged > 1) ? "<li><a href='" . get_pagenum_link($paged-1) . "'>上一页</a></li>" : "";
if ($max_page > $range)
{
if ($paged < $range)
{
for ($i = 1; $i <= ($range + 1); $i++)
{
echo ($i == $paged) ? "<li class='current'>" : '<li>';
echo "<a href='" . get_pagenum_link($i) . "'>$i</a></li>";
}
}
elseif ($paged >= ($max_page - ceil(($range / 2))))
{
for ($i = $max_page - $range; $i <= $max_page; $i++)
{
echo ($i == $paged) ? "<li class='current'>" : '<li>';
echo "<a href='" . get_pagenum_link($i) . "'>$i</a></li>";
}
}
elseif ($paged >= $range && $paged < ($max_page - ceil(($range / 2))))
{
for ($i = ($paged - ceil($range / 2)); $i <= ($paged + ceil(($range / 2))); $i++)
{
echo ($i == $paged) ? "<li class='current'>" : '<li>';
echo "<a href='" . get_pagenum_link($i) . "'>$i</a></li>";
}
}
}
else
{
for ($i = 1; $i <= $max_page; $i++)
{
echo ($i == $paged) ? "<li class='current'>" : '<li>';
echo "<a href='" . get_pagenum_link($i) . "'>$i</a></li>";
}
}
echo ($paged < $max_page) ? "<li><a href='" . get_pagenum_link($paged+1) . "'>下一页</a></li>" : "";
if ($paged != $max_page)
{
echo "<li><a href='" . get_pagenum_link($max_page) . "' class='extend' title='跳转到最后一页'>尾页</a><li>";
}
echo '<li><a><span>共 ' . $max_page . ' 页</span></a></a>';
echo "</ol>\n";
}
}
/* 获取默认封面 */
function getThumbnail()
{
global $post;
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
if(!empty($large_image_url[0]))
{
return $large_image_url[0]."?version=".time();
}
return "//qiniu.hankin.cn/img".implode(unique_rand(0, 48, 1), ",").".png?version=".time();
}
/*
* array unique_rand( int $min, int $max, int $num )
* 生成一定数量的不重复随机数,指定的范围内整数的数量必须
* 比要生成的随机数数量大
* $min 和 $max: 指定随机数的范围
* $num: 指定生成数量
*/
function unique_rand($min, $max, $num) {
$count = 0;
$return = array();
while ($count < $num) {
$return[] = mt_rand($min, $max);
$return = array_flip(array_flip($return));
$count = count($return);
}
//打乱数组,重新赋予数组新的下标
shuffle($return);
return $return;
}
/* 修改时间格式 */
function timeGo($ptime)
{
$ptime = strtotime($ptime);
$etime = time() - $ptime;
if ($etime < 1)
{
return '刚刚';
}
$interval = [
12 * 30 * 24 * 60 * 60 => '年前 ',
30 * 24 * 60 * 60 => '个月前 ',
7 * 24 * 60 * 60 => '周前 ',
24 * 60 * 60 => '天前',
60 * 60 => '小时前',
60 => '分钟前',
1 => '秒前',
];
foreach ($interval as $secs => $str)
{
$d = $etime / $secs;
if ($d >= 1)
{
$r = round($d);
return $r . $str;
}
}
return TRUE;
}
function qqMsg($qq)
{
//$api = "https://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins=";
$api = "https://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins=";
$avatarApi = "https://q1.qlogo.cn/g?b=qq&s=5&nk=";
$contents = file_get_contents($api.$qq);
if($contents){
$data = iconv("GBK","UTF-8",$contents);
$pattern = '/portraitCallBack\((.*)\)/is';
preg_match($pattern,$data,$result);
$result = $result[1];
$result = json_decode($result, true)[$qq];
return [
'nickname' => $result[6],
'avatar' => $avatarApi.$qq,
];
}
}
/**
* 根据 QQ号获取 昵称和头像
* http://www.hankin.cn/wp-admin/admin-ajax.php?action=get_ajax_qq&qq=315444473
*/
function get_ajax_qq() {
// 输出响应
header( "Content-Type: application/json" );
$api = "https://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins=";
$avatarApi = "https://q1.qlogo.cn/g?b=qq&s=5&nk=";
$qq = isset($_GET['qq']) ? trim($_GET['qq']) : '';
if(!empty($qq) && is_numeric($qq) && strlen($qq) > 4 && strlen($qq) < 13)
{
$contents = file_get_contents($api.$qq);
if($contents){
$data = iconv("GBK","UTF-8",$contents);
$pattern = '/portraitCallBack\((.*)\)/is';
preg_match($pattern,$data,$result);
$result = $result[1];
$result = json_decode($result, true)[$qq];
$nickname = $result[6];
//$avatar = $result[0]; //被禁了
$avatar = $avatarApi.$qq;
}
ajaxResult(200,'',["id"=> $qq,"name"=>$nickname,"avatar"=>$avatar]);
}else{
ajaxResult(1000,'QQ格式不正确',["id"=> "","name"=>"","avatar"=>""]);
}
}
add_action( 'wp_ajax_nopriv_get_ajax_qq', 'get_ajax_qq' );
add_action( 'wp_ajax_get_ajax_qq', 'get_ajax_qq' );
function ajaxResult($resultCode, $message = NULL, $data = NULL){
exit(json_encode([
'result' => [
'code' => $resultCode,
'msg' => $message,
],
'data' => $data,
]));
}
function lo_all_view(){
global $wpdb;
$count=0;
$views= $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key='views'");
foreach($views as $key=>$value){
$meta_value=$value->meta_value;
if($meta_value!=' '){
$count+=(int)$meta_value;
}
}
return $count;
}
//WordPress加速优化前台不加载多语言包
add_filter( 'locale', 'wpjam_locale' );
function wpjam_locale($locale) {
$locale = ( is_admin() ) ? $locale : 'en_US';
return $locale;
}
/* 启用浏览数目 */
function getPostViews($postID)
{
$count_key = 'views';
$count = get_post_meta($postID, $count_key, true);
if ($count=='') {
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0";
}
return $count.' ';
}
function setPostViews($postID,$count_key='views')
{
$count = get_post_meta($postID, $count_key, true);
if ($count=='') {
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
} else {
$count++;
update_post_meta($postID, $count_key, $count);
}
}
//后台显示浏览数目
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views', 5, 2);
function posts_column_views($defaults)
{
$defaults['post_views'] = __('浏览');
return $defaults;
}
function posts_custom_column_views($column_name, $id)
{
if ($column_name === 'post_views') {
echo getPostViews(get_the_ID());
}
}
/* 获取文章的评论人数 by zwwooooo | zww.me */
function getCommentsNumber($postid = 0, $which = 0)
{
$comments = get_comments('status=approve&type=comment&post_id=' . $postid); //获取文章的所有评论
if ($comments)
{
$i = 0;
$j = 0;
$commentusers = [];
foreach ($comments as $comment)
{
++$i;
if ($i == 1)
{
$commentusers[] = $comment->comment_author_email;
++$j;
}
if (!in_array($comment->comment_author_email, $commentusers))
{
$commentusers[] = $comment->comment_author_email;
++$j;
}
}
$output = [$j, $i];
$which = ($which == 0) ? 0 : 1;
return $output[$which]; //返回评论人数
}
return '0'; //没有评论返回0
}
function json_get_avatar_url( $avatar_html ) {
// Strip the avatar url from the get_avatar img tag.
preg_match('/src=["|\'](.+)[\&|"|\']/U', $avatar_html, $matches);
if ( isset( $matches[1] ) && ! empty( $matches[1] ) ) {
return esc_url_raw( $matches[1] );
}
return '';
}
/*
加载infinite scroll插件脚本
*/
function infinitescroll_js() {
wp_register_script('infinite_scroll', 'https://cdn.bootcss.com/jquery-infinitescroll/2.0.2/jquery.infinitescroll.min.js', array('jquery'), null, true);
if (!is_singular()) {
wp_enqueue_script('infinite_scroll');
}
}
add_action('wp_enqueue_scripts', 'infinitescroll_js');
/* 如果判断当前浏览页面为微信浏览器*/
function isWechat(){
if(strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessenger')){
//判断微信浏览器为真
return true;
}
//此处为假
return false;
}
//评论自定义字段
function add_comment_meta_values($comment_id){
//地址
if(isset($_POST['hankin_avatar'])){
$hankin_avatar = wp_filter_nohtml_kses($_POST['hankin_avatar']);
add_comment_meta($comment_id,'hankin_avatar', $hankin_avatar,false);
}
// phone
if(isset($_POST['hankin_qq'])){
$hankin_qq = wp_filter_nohtml_kses($_POST['hankin_qq']);
add_comment_meta($comment_id,'hankin_qq', $hankin_qq,false);
}
// comolay
if(isset($_POST['hankin_username'])){
$hankin_username = wp_filter_nohtml_kses($_POST['hankin_username']);
add_comment_meta($comment_id,'hankin_username', $hankin_username,false);
}
}//end评论自定义字段
add_action ('comment_post','add_comment_meta_values',1);
//添加评论自定义字段标题
function add_comment_meta_title( $columns )
{
return array_merge( $columns, array(
'hankin_avatar'=>'头像',
'hankin_qq'=>'qq号',
'hankin_username'=>'昵称',
));
}//end添加评论自定义字段标题
add_filter('manage_edit-comments_columns','add_comment_meta_title');
//输出自定义字段值
function echo_comment_column_value( $column_name, $comment_ID )
{
switch( $column_name ) {
case "hankin_avatar" :
$qq_url = get_comment_meta( $comment_ID, $column_name ,true);
$qq_url = ($qq_url=='') ? get_template_directory_uri().'/assets/images/user/default-avatar.png' : $qq_url;
echo '<img width="50" height="50" src='.$qq_url.'>';
break;
default :
echo get_comment_meta( $comment_ID, $column_name ,true);
}
}
add_filter('manage_comments_custom_column','echo_comment_column_value',10,2);
/* 文章点赞功能 */
function hankin_like(){
global $wpdb,$post;
$id = $_POST["um_id"];
$action = $_POST["um_action"];
if ( $action == 'like'){
$bigfa_raters = get_post_meta($id,'hankin_like',true);
$expire = time() + 99999999;
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; // make cookies work with localhost
setcookie('hankin_like_'.$id,$id,$expire,'/',$domain,false);
if (!$bigfa_raters || !is_numeric($bigfa_raters)) {
update_post_meta($id, 'hankin_like', 1);
}
else {
update_post_meta($id, 'hankin_like', ($bigfa_raters + 1));
}
echo get_post_meta($id,'hankin_like',true);
}
die;
}
add_action('wp_ajax_nopriv_hankin_like', 'hankin_like');
add_action('wp_ajax_hankin_like', 'hankin_like');
// 获取点赞总数量
function count_post_meta($key){
global $wpdb;
$sql = "SELECT
count(*) as total,
SUM(meta_value) as count
FROM
wp_postmeta
WHERE
`meta_key` = '".$key."'";
$res = $wpdb->get_row($sql,ARRAY_A);
return $res ? $res['count'] : 0;
}
// 数组分组
function dataGroup($dataArr,$keyStr)
{
$newArr=[];
foreach($dataArr as $k=>$v){
$newArr[$v[$keyStr]][] = $v; //根据initial 进行数组重新赋值
}
return $newArr;
}
// 根据评论id 获取文章id
function getPostIdByCommentId($commentId)
{
global $wpdb;
$sql = "SELECT
*
FROM
wp_comments
WHERE
`comment_ID` = '".$commentId."'";
$res = $wpdb->get_row($sql,ARRAY_A);
return $res ? $res['comment_post_ID'] : 0;
}