-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathxb-custom-nav.php
More file actions
2024 lines (1896 loc) · 91.6 KB
/
xb-custom-nav.php
File metadata and controls
2024 lines (1896 loc) · 91.6 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
/*
Plugin Name: 小半自定义网址导航
Description: 自定义导航插件,支持分类导航、前台页面展示、公告、广告设置及LyToday-JS热搜榜插件。
Plugin URI: https://www.jingxialai.com/4980.html
Version: 1.0.2
Author: Summer
License: GPL License
Author URI: https://www.jingxialai.com/
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
// 定义插件路径常量
define('XB_NAV_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('XB_NAV_PLUGIN_URL', plugin_dir_url(__FILE__));
// 插件激活时执行
register_activation_hook(__FILE__, 'xb_nav_activate');
function xb_nav_activate() {
xb_nav_create_table(); // 创建或更新数据表
xb_nav_create_page(); // 创建前台页面
}
// 创建或更新数据表
function xb_nav_create_table() {
global $wpdb;
$table_nav = $wpdb->prefix . 'xb_nav';
$table_category = $wpdb->prefix . 'xb_nav_categories';
$table_clicks = $wpdb->prefix . 'xb_nav_clicks';
$charset_collate = $wpdb->get_charset_collate();
// 导航分类表
$sql_category = "CREATE TABLE IF NOT EXISTS $table_category (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
icon VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
// 导航内容表
$sql_nav = "CREATE TABLE IF NOT EXISTS $table_nav (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
url VARCHAR(255) NOT NULL,
icon VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
intro_url VARCHAR(255) NOT NULL,
category_id BIGINT(20) UNSIGNED NOT NULL,
redirect_to_intro TINYINT(1) DEFAULT 0,
order_num INT DEFAULT 0,
is_recommended TINYINT(1) DEFAULT 0,
PRIMARY KEY (id),
FOREIGN KEY (category_id) REFERENCES $table_category(id) ON DELETE CASCADE
) $charset_collate;";
// 导航点击统计表
$sql_clicks = "CREATE TABLE IF NOT EXISTS $table_clicks (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
nav_id BIGINT(20) UNSIGNED NOT NULL,
click_date DATE NOT NULL,
click_count BIGINT(20) UNSIGNED DEFAULT 0,
PRIMARY KEY (id),
UNIQUE KEY nav_date (nav_id, click_date),
FOREIGN KEY (nav_id) REFERENCES $table_nav(id) ON DELETE CASCADE
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta($sql_category);
dbDelta($sql_nav);
dbDelta($sql_clicks);
// 检查并添加 order_num 字段
$columns = $wpdb->get_results("SHOW COLUMNS FROM $table_nav LIKE 'order_num'");
if (empty($columns)) {
$wpdb->query("ALTER TABLE $table_nav ADD order_num INT DEFAULT 0 AFTER redirect_to_intro");
}
// 检查并添加 is_recommended 字段
$columns = $wpdb->get_results("SHOW COLUMNS FROM $table_nav LIKE 'is_recommended'");
if (empty($columns)) {
$wpdb->query("ALTER TABLE $table_nav ADD is_recommended TINYINT(1) DEFAULT 0 AFTER order_num");
}
// 确保点击统计表存在
$table_exists = $wpdb->get_var("SHOW TABLES LIKE '$table_clicks'");
if (!$table_exists) {
$wpdb->query($sql_clicks);
}
}
// 创建前台页面,检查是否已有包含短代码的页面
function xb_nav_create_page() {
$pages = get_posts(array(
'post_type' => 'page',
'post_status' => 'publish',
's' => '[xb_nav_page]',
'numberposts' => 1,
));
if (empty($pages)) {
wp_insert_post(array(
'post_title' => '自定义导航页面',
'post_content' => '[xb_nav_page]',
'post_status' => 'publish',
'post_type' => 'page',
));
}
}
// 加载前台 CSS 和 JS 文件,仅在包含 [xb_nav_page] 短代码的页面加载
add_action('wp_enqueue_scripts', 'xb_nav_enqueue_scripts');
function xb_nav_enqueue_scripts() {
global $post;
if (is_a($post, 'WP_Post') && has_shortcode($post->post_content, 'xb_nav_page')) {
wp_enqueue_style('xb-nav-style', XB_NAV_PLUGIN_URL . 'assets/xb-nav.css', array(), '2.5');
wp_enqueue_script('xb-nav-script', XB_NAV_PLUGIN_URL . 'assets/xb-nav.js', array('jquery'), '2.5', true);
wp_localize_script('xb-nav-script', 'xbNavAjax', array(
'ajax_url' => admin_url('admin-ajax.php'),
'show_desc' => get_option('xb_nav_show_desc', '0'),
));
// 动态添加背景颜色样式
$background_color = get_option('xb_nav_background_color', 'linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%)');
$custom_css = ".xb-nav-container { background: {$background_color}; }";
wp_add_inline_style('xb-nav-style', $custom_css);
// 加载 Iconfont CSS 如果设置了
$iconfont_url = get_option('xb_nav_iconfont_url', '');
if (!empty($iconfont_url)) {
wp_enqueue_style('xb-nav-iconfont', esc_url($iconfont_url), array(), null);
}
}
}
// 短代码注册,用于显示前台页面
add_shortcode('xb_nav_page', 'xb_nav_display_page');
function xb_nav_display_page() {
global $wpdb;
$table_nav = $wpdb->prefix . 'xb_nav';
$table_category = $wpdb->prefix . 'xb_nav_categories';
$table_clicks = $wpdb->prefix . 'xb_nav_clicks';
// 获取所有分类
$categories = $wpdb->get_results("SELECT * FROM $table_category ORDER BY id ASC");
// 获取所有导航
$navs = $wpdb->get_results("SELECT n.*, c.name as category_name FROM $table_nav n LEFT JOIN $table_category c ON n.category_id = c.id ORDER BY n.category_id ASC, n.order_num DESC");
// 获取推荐导航
$recommended_navs = $wpdb->get_results("SELECT n.*, c.name as category_name FROM $table_nav n LEFT JOIN $table_category c ON n.category_id = c.id WHERE n.is_recommended = 1 ORDER BY n.order_num DESC");
// 获取设置
$announcement = get_option('xb_nav_announcement', '');
$ad_content = get_option('xb_nav_ad_content', '');
$show_nav_count = get_option('xb_nav_show_count', '0');
$apply_url = get_option('xb_nav_apply_url', '');
$show_apply = get_option('xb_nav_show_apply', '0');
$show_posts = get_option('xb_nav_show_posts', '0');
$post_type = get_option('xb_nav_post_type', 'recent');
$post_count = get_option('xb_nav_post_count', 3);
$page_title = get_option('xb_nav_page_title', '自定义导航页面');
$show_page_title = get_option('xb_nav_show_page_title', '1');
$enable_clock = get_option('xb_nav_enable_clock', '0');
$enable_lytoday = get_option('xb_nav_enable_lytoday', '0');
$enable_hotlist = get_option('xb_nav_enable_hotlist', '0');
// 获取联系方式设置
$contact_qq = get_option('xb_nav_contact_qq', '');
$contact_city = get_option('xb_nav_contact_city', '');
$contact_email = get_option('xb_nav_contact_email', '');
$contact_bilibili_url = get_option('xb_nav_contact_bilibili_url', '');
$contact_bilibili_text = get_option('xb_nav_contact_bilibili_text', '');
$contact_weibo_url = get_option('xb_nav_contact_weibo_url', '');
$contact_netease_url = get_option('xb_nav_contact_netease_url', '');
$contact_xiaohongshu_qr = get_option('xb_nav_contact_xiaohongshu_qr', '');
$contact_douyin_qr = get_option('xb_nav_contact_douyin_qr', '');
$contact_wechat_qr = get_option('xb_nav_contact_wechat_qr', '');
$iconfont_url = get_option('xb_nav_iconfont_url', '');
// 获取热榜数据
$hotlist_data = array(
'daily' => array(),
'monthly' => array(),
'yearly' => array(),
'total' => array()
);
if ($enable_hotlist) {
// 日榜(今日)
$hotlist_data['daily'] = $wpdb->get_results($wpdb->prepare(
"SELECT n.id, n.name, n.url, n.icon, n.intro_url, n.redirect_to_intro, COALESCE(SUM(c.click_count), 0) as total_clicks
FROM $table_nav n
LEFT JOIN $table_clicks c ON n.id = c.nav_id AND DATE(c.click_date) = %s
GROUP BY n.id
HAVING total_clicks > 0
ORDER BY total_clicks DESC
LIMIT 10",
date_i18n('Y-m-d') // 使用 WordPress 本地时间
));
// 月榜(本月)
$hotlist_data['monthly'] = $wpdb->get_results($wpdb->prepare(
"SELECT n.id, n.name, n.url, n.icon, SUM(c.click_count) as total_clicks
FROM $table_nav n
LEFT JOIN $table_clicks c ON n.id = c.nav_id
WHERE YEAR(c.click_date) = YEAR(CURDATE()) AND MONTH(c.click_date) = MONTH(CURDATE())
GROUP BY n.id
ORDER BY total_clicks DESC
LIMIT 10"
));
// 年榜(本年)
$hotlist_data['yearly'] = $wpdb->get_results($wpdb->prepare(
"SELECT n.id, n.name, n.url, n.icon, SUM(c.click_count) as total_clicks
FROM $table_nav n
LEFT JOIN $table_clicks c ON n.id = c.nav_id
WHERE YEAR(c.click_date) = YEAR(CURDATE())
GROUP BY n.id
ORDER BY total_clicks DESC
LIMIT 10"
));
// 总榜
$hotlist_data['total'] = $wpdb->get_results(
"SELECT n.id, n.name, n.url, n.icon, SUM(c.click_count) as total_clicks
FROM $table_nav n
LEFT JOIN $table_clicks c ON n.id = c.nav_id
GROUP BY n.id
ORDER BY total_clicks DESC
LIMIT 10"
);
}
ob_start();
?>
<div class="xb-nav-container">
<?php if ($show_page_title === '1'): ?>
<h1 class="entry-title"><?php echo esc_html($page_title); ?></h1>
<?php endif; ?>
<!-- 搜索板块 -->
<div class="xb-nav-search">
<div class="xb-search-tabs">
<button class="xb-search-tab active" data-engine="baidu">百度</button>
<button class="xb-search-tab" data-engine="google">谷歌</button>
<button class="xb-search-tab" data-engine="github">GitHub</button>
<button class="xb-search-tab" data-engine="bilibili">B站</button>
<button class="xb-search-tab" data-engine="weibo">微博</button>
<button class="xb-search-tab" data-engine="zhihu">知乎</button>
<button class="xb-search-tab" data-engine="taobao">淘宝</button>
<button class="xb-search-tab" data-engine="site">站内搜索</button>
</div>
<div class="xb-search-input-wrapper">
<input type="text" id="xb-search-input" placeholder="输入关键词搜索..." />
<button id="xb-search-btn">
<svg class="xb-search-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="11" cy="11" r="8"></circle>
<line x1="21" y1="21" x2="16.65" y2="16.65"></line>
</svg>
</button>
</div>
</div>
<!-- 手机端菜单栏 -->
<div class="xb-mobile-menu">
<span class="menu-icon">☰</span>
<div class="xb-mobile-menu-content">
<div class="xb-mobile-menu-header">
<span class="xb-title">分类</span>
<span class="menu-close">×</span>
</div>
<ul>
<?php foreach ($categories as $category): ?>
<li class="xb-category-item" data-category-id="<?php echo esc_attr($category->id); ?>">
<img src="<?php echo esc_url($category->icon); ?>" alt="<?php echo esc_attr($category->name); ?>" class="xb-category-icon" />
<span><?php echo esc_html($category->name); ?></span>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<!-- 主内容 -->
<div class="xb-nav-main">
<!-- 左边分类栏(桌面端显示) -->
<div class="xb-nav-sidebar">
<div class="xb-title">分类</div>
<ul>
<?php foreach ($categories as $category): ?>
<li class="xb-category-item" data-category-id="<?php echo esc_attr($category->id); ?>">
<img src="<?php echo esc_url($category->icon); ?>" alt="<?php echo esc_attr($category->name); ?>" class="xb-category-icon" />
<span><?php echo esc_html($category->name); ?></span>
</li>
<?php endforeach; ?>
</ul>
</div>
<!-- 中间导航内容 -->
<div class="xb-nav-content">
<!-- 推荐导航板块 -->
<?php if (!empty($recommended_navs)): ?>
<div class="xb-nav-section xb-recommended-section" id="recommended-navs">
<div class="xb-title">推荐</div>
<div class="xb-nav-items">
<?php foreach ($recommended_navs as $nav): ?>
<a href="<?php echo esc_url($nav->redirect_to_intro ? $nav->intro_url : $nav->url); ?>"
target="_blank"
class="xb-nav-item"
data-nav-id="<?php echo esc_attr($nav->id); ?>">
<img src="<?php echo esc_url($nav->icon); ?>" alt="<?php echo esc_attr($nav->name); ?>" class="xb-nav-icon" />
<div class="xb-nav-info">
<div class="xb-nav-name"><?php echo esc_html($nav->name); ?></div>
<div class="xb-nav-desc-short"><?php echo esc_html(wp_trim_words($nav->description, 10, '...')); ?></div>
<div class="xb-nav-desc-full"><?php echo esc_html($nav->description); ?></div>
</div>
</a>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<?php
$current_category = '';
foreach ($navs as $nav):
if ($nav->category_name !== $current_category):
if ($current_category !== '') echo '</div></div>';
$current_category = $nav->category_name;
?>
<div class="xb-nav-section" id="category-<?php echo esc_attr($nav->category_id); ?>">
<div class="xb-title"><?php echo esc_html($nav->category_name); ?></div>
<div class="xb-nav-items">
<?php endif; ?>
<a href="<?php echo esc_url($nav->redirect_to_intro ? $nav->intro_url : $nav->url); ?>"
target="_blank"
class="xb-nav-item"
data-nav-id="<?php echo esc_attr($nav->id); ?>">
<img src="<?php echo esc_url($nav->icon); ?>" alt="<?php echo esc_attr($nav->name); ?>" class="xb-nav-icon" />
<div class="xb-nav-info">
<div class="xb-nav-name"><?php echo esc_html($nav->name); ?></div>
<div class="xb-nav-desc-short"><?php echo esc_html(wp_trim_words($nav->description, 10, '...')); ?></div>
<div class="xb-nav-desc-full"><?php echo esc_html($nav->description); ?></div>
</div>
</a>
<?php endforeach; ?>
<?php if ($current_category !== '') echo '</div></div>'; ?>
</div>
<!-- 右边小工具栏 -->
<div class="xb-nav-widgets">
<!-- 数字时钟 -->
<?php if ($enable_clock === '1'): ?>
<div class="xb-widget">
<div class="digital-clock">
<div class="time">
<span class="hours">00</span>
<span class="dots">:</span>
<span class="minutes">00</span>
<div class="right-side">
<span class="seconds">00</span>
</div>
</div>
<div class="calender">
<span class="year"></span>年
<span class="month-name"></span>
<span class="day-num"></span>日
<span class="day-name"></span>
</div>
</div>
</div>
<script>
(function() {
function updateTime() {
let today = new Date();
let hours = today.getHours();
let minutes = today.getMinutes();
let seconds = today.getSeconds();
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
document.querySelector(".hours").innerHTML = hours;
document.querySelector(".minutes").innerHTML = minutes;
document.querySelector(".seconds").innerHTML = seconds;
const dayNum = today.getDate();
const year = today.getFullYear();
const dayName = today.toLocaleString("default", { weekday: "long" });
const monthName = today.toLocaleString("default", { month: "short" });
document.querySelector(".year").innerHTML = year;
document.querySelector(".month-name").innerHTML = monthName;
document.querySelector(".day-name").innerHTML = dayName;
document.querySelector(".day-num").innerHTML = dayNum;
}
updateTime();
setInterval(updateTime, 1000);
})();
</script>
<?php endif; ?>
<!-- 联系方式 -->
<?php
$has_contact = !empty($contact_qq) || !empty($contact_city) || !empty($contact_email) ||
!empty($contact_bilibili_url) || !empty($contact_weibo_url) ||
!empty($contact_netease_url) || !empty($contact_xiaohongshu_qr) ||
!empty($contact_douyin_qr) || !empty($contact_wechat_qr);
if ($has_contact):
?>
<div class="xb-widget xb-contact-widget">
<div class="xb-title">联系方式</div>
<div class="xb-contact-items">
<?php if (!empty($contact_qq)): ?>
<div class="xb-contact-item">
<i class="iconfont icon-qq"></i>
<span><?php echo esc_html($contact_qq); ?></span>
</div>
<?php endif; ?>
<?php if (!empty($contact_city)): ?>
<div class="xb-contact-item">
<i class="iconfont icon-weizhi"></i>
<span><?php echo esc_html($contact_city); ?></span>
</div>
<?php endif; ?>
<?php if (!empty($contact_email)): ?>
<div class="xb-contact-item">
<i class="iconfont icon-email"></i>
<span><?php echo esc_html($contact_email); ?></span>
</div>
<?php endif; ?>
<?php if (!empty($contact_bilibili_url)): ?>
<div class="xb-contact-item">
<a href="<?php echo esc_url($contact_bilibili_url); ?>" target="_blank">
<i class="iconfont icon-bilibili"></i>
<span><?php echo esc_html($contact_bilibili_text ? $contact_bilibili_text : 'B站'); ?></span>
</a>
</div>
<?php endif; ?>
<div class="xb-contact-row">
<?php if (!empty($contact_weibo_url)): ?>
<div class="xb-contact-item">
<a href="<?php echo esc_url($contact_weibo_url); ?>" target="_blank">
<i class="iconfont icon-weibo"></i>
</a>
</div>
<?php endif; ?>
<?php if (!empty($contact_xiaohongshu_qr)): ?>
<div class="xb-contact-item xb-contact-qr" data-qr="<?php echo esc_url($contact_xiaohongshu_qr); ?>">
<i class="iconfont icon-xiaohongshu"></i>
<div class="xb-qr-popup">
<img src="<?php echo esc_url($contact_xiaohongshu_qr); ?>" alt="小红书二维码" />
</div>
</div>
<?php endif; ?>
<?php if (!empty($contact_douyin_qr)): ?>
<div class="xb-contact-item xb-contact-qr" data-qr="<?php echo esc_url($contact_douyin_qr); ?>">
<i class="iconfont icon-douyin"></i>
<div class="xb-qr-popup">
<img src="<?php echo esc_url($contact_douyin_qr); ?>" alt="抖音二维码" />
</div>
</div>
<?php endif; ?>
<?php if (!empty($contact_wechat_qr)): ?>
<div class="xb-contact-item xb-contact-qr" data-qr="<?php echo esc_url($contact_wechat_qr); ?>">
<i class="iconfont icon-weixin"></i>
<div class="xb-qr-popup">
<img src="<?php echo esc_url($contact_wechat_qr); ?>" alt="微信二维码" />
</div>
</div>
<?php endif; ?>
<?php if (!empty($contact_netease_url)): ?>
<div class="xb-contact-item">
<a href="<?php echo esc_url($contact_netease_url); ?>" target="_blank">
<i class="iconfont icon-wangyiyun"></i>
</a>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php endif; ?>
<!-- 公告 -->
<?php if (!empty($announcement)): ?>
<div class="xb-widget">
<div class="xb-title">公告</div>
<div class="xb-announcement"><?php echo wp_kses_post($announcement); ?></div>
</div>
<?php endif; ?>
<!-- 广告 -->
<?php if (!empty($ad_content)): ?>
<div class="xb-widget">
<div class="xb-title">广告</div>
<div class="xb-ad"><?php echo wp_kses_post($ad_content); ?></div>
</div>
<?php endif; ?>
<!-- 导航热榜 -->
<?php if ($enable_hotlist === '1'): ?>
<div class="xb-widget xb-hotlist-widget">
<div class="xb-title">热榜</div>
<div class="xb-hotlist-tabs">
<button class="xb-hotlist-tab active" data-type="daily">日榜</button>
<button class="xb-hotlist-tab" data-type="monthly">月榜</button>
<button class="xb-hotlist-tab" data-type="yearly">年榜</button>
<button class="xb-hotlist-tab" data-type="total">总榜</button>
</div>
<?php foreach ($hotlist_data as $type => $items): ?>
<div class="xb-hotlist-content" id="hotlist-<?php echo esc_attr($type); ?>" style="<?php echo $type === 'daily' ? '' : 'display: none;'; ?>">
<?php if (empty($items)): ?>
<p>暂无数据</p>
<?php else: ?>
<ul class="xb-hotlist-items">
<?php foreach ($items as $index => $item): ?>
<li class="xb-hotlist-item">
<span class="xb-hotlist-rank <?php echo $index < 3 ? 'top-rank' : ''; ?>"><?php echo $index + 1; ?></span>
<a href="<?php echo esc_url($item->redirect_to_intro ? $item->intro_url : $item->url); ?>"
target="_blank"
class="xb-hotlist-link"
data-nav-id="<?php echo esc_attr($item->id); ?>">
<img src="<?php echo esc_url($item->icon); ?>" alt="<?php echo esc_attr($item->name); ?>" class="xb-hotlist-icon" />
<span class="xb-hotlist-name"><?php echo esc_html($item->name); ?></span>
</a>
<span class="xb-hotlist-count"><?php echo $item->total_clicks ?: 0; ?> 次</span>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<!-- 导航数量 -->
<?php if ($show_nav_count): ?>
<div class="xb-widget">
<div class="xb-title">收录数量</div>
<div class="xb-nav-count">当前收录导航:<?php echo count($navs); ?> 个</div>
</div>
<?php endif; ?>
<!-- 申请收录 -->
<?php if ($show_apply && !empty($apply_url)): ?>
<div class="xb-widget">
<div class="xb-title">申请收录</div>
<a href="<?php echo esc_url($apply_url); ?>" target="_blank" class="xb-apply-btn">提交网址</a>
</div>
<?php endif; ?>
<!-- 文章显示 -->
<?php if ($show_posts): ?>
<div class ="xb-widget">
<div class="xb-title"><?php echo $post_type === 'recent' ? '最新文章' : '随机推荐'; ?></div>
<?php
$args = array(
'posts_per_page' => $post_count,
'orderby' => $post_type === 'recent' ? 'date' : 'rand',
);
$posts = new WP_Query($args);
if ($posts->have_posts()):
$post_index = 1;
// 检测是否为移动设备
$is_mobile = wp_is_mobile();
$title_length = $is_mobile ? 30 : 15;
?>
<div class="xb-post-list">
<?php while ($posts->have_posts()): $posts->the_post(); ?>
<a href="<?php the_permalink(); ?>" target="_blank" class="xb-post-item" data-index="<?php echo $post_index; ?>">
<span class="xb-post-title"><?php echo esc_html(wp_trim_words(get_the_title(), $title_length, '...')); ?></span>
</a>
<?php $post_index++; ?>
<?php endwhile; ?>
</div>
<?php
wp_reset_postdata();
endif; ?>
</div>
<?php endif; ?>
</div>
</div>
<!-- LyToday-JS 热搜榜 -->
<?php if ($enable_lytoday === '1'): ?>
<div class="xb-lytoday-container" style="max-width: 1400px; margin: 20px auto; padding: 0 20px;">
<style>
.hot-panel {
display: flex;
overflow-x: auto !important;
flex-wrap: nowrap !important;
justify-content: flex-start !important;
align-content: center;
}
@media (max-width: 768px) {
.hot-panel {
flex-wrap: wrap !important;
}
}
</style>
<div id="lytoday"></div>
<script src="https://lytoday.lylme.com/"></script>
</div>
<?php endif; ?>
</div>
<!-- 自定义提示框 -->
<div class="xb-toast" id="xb-toast">
<span id="xb-toast-message"></span>
</div>
<!-- 手机端导航开关功能及提示框自动消失 -->
<script>
jQuery(document).ready(function($) {
$('.xb-mobile-menu .menu-icon').on('click', function() {
$('.xb-mobile-menu-content').toggleClass('active');
});
$('.xb-mobile-menu .menu-close').on('click', function() {
$('.xb-mobile-menu-content').removeClass('active');
});
// 自动消失的提示框
function showToast(message) {
const toast = $('#xb-toast');
$('#xb-toast-message').text(message);
toast.addClass('show');
setTimeout(() => {
toast.removeClass('show');
}, 3000);
}
window.showToast = showToast;
});
</script>
<!-- 提示框样式 -->
<style>
.xb-toast {
position: fixed;
bottom: 20px;
right: 20px;
background: #333;
color: #fff;
padding: 10px 20px;
border-radius: 5px;
opacity: 0;
transition: opacity 0.3s ease;
z-index: 1000;
}
.xb-toast.show {
opacity: 1;
}
</style>
<?php
return ob_get_clean();
}
// 添加后台菜单
add_action('admin_menu', 'xb_nav_admin_menu');
function xb_nav_admin_menu() {
add_menu_page('小半导航设置', '小半网址导航', 'manage_options', 'xb-nav-settings', 'xb_nav_settings_page', 'dashicons-admin-links');
add_submenu_page('xb-nav-settings', '导航设置', '导航设置', 'manage_options', 'xb-nav-settings', 'xb_nav_settings_page');
add_submenu_page('xb-nav-settings', '添加导航', '添加导航', 'manage_options', 'xb-nav-add', 'xb_nav_add_page');
add_submenu_page('xb-nav-settings', '导航管理', '导航管理', 'manage_options', 'xb-nav-manage', 'xb_nav_manage_page');
}
// 自定义 URL 验证函数,允许协议相对 URL
function xb_sanitize_iconfont_url($url) {
$url = trim($url);
if (empty($url)) {
return '';
}
if (preg_match('#^(//|https?://)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(/.*)?$#', $url)) {
return esc_url_raw($url);
}
return '';
}
// 验证 URL 格式
function xb_validate_url($url) {
return filter_var($url, FILTER_VALIDATE_URL) !== false;
}
// 验证输入数据
function xb_validate_input($data, $type, $max_length = 255) {
switch ($type) {
case 'text':
return strlen($data) <= $max_length ? sanitize_text_field($data) : false;
case 'url':
return xb_validate_url($data) && strlen($data) <= $max_length ? esc_url_raw($data) : false;
case 'textarea':
return sanitize_textarea_field($data);
case 'int':
return is_numeric($data) ? intval($data) : false;
case 'checkbox':
return $data ? 1 : 0;
default:
return false;
}
}
// 插件设置页面
function xb_nav_settings_page() {
if (isset($_POST['xb_nav_settings_submit'])) {
$settings = array(
'xb_nav_announcement' => array('value' => $_POST['xb_nav_announcement'], 'type' => 'textarea'),
'xb_nav_ad_content' => array('value' => $_POST['xb_nav_ad_content'], 'type' => 'textarea'),
'xb_nav_show_count' => array('value' => isset($_POST['xb_nav_show_count']), 'type' => 'checkbox'),
'xb_nav_apply_url' => array('value' => $_POST['xb_nav_apply_url'], 'type' => 'url'),
'xb_nav_show_apply' => array('value' => isset($_POST['xb_nav_show_apply']), 'type' => 'checkbox'),
'xb_nav_show_posts' => array('value' => isset($_POST['xb_nav_show_posts']), 'type' => 'checkbox'),
'xb_nav_post_type' => array('value' => $_POST['xb_nav_post_type'], 'type' => 'text'),
'xb_nav_post_count' => array('value' => $_POST['xb_nav_post_count'], 'type' => 'int'),
'xb_nav_page_title' => array('value' => $_POST['xb_nav_page_title'], 'type' => 'text'),
'xb_nav_show_page_title' => array('value' => isset($_POST['xb_nav_show_page_title']), 'type' => 'checkbox'),
'xb_nav_background_color' => array('value' => $_POST['xb_nav_background_color'], 'type' => 'text'),
'xb_nav_show_desc' => array('value' => isset($_POST['xb_nav_show_desc']), 'type' => 'checkbox'),
'xb_nav_enable_clock' => array('value' => isset($_POST['xb_nav_enable_clock']), 'type' => 'checkbox'),
'xb_nav_enable_lytoday' => array('value' => isset($_POST['xb_nav_enable_lytoday']), 'type' => 'checkbox'),
'xb_nav_enable_hotlist' => array('value' => isset($_POST['xb_nav_enable_hotlist']), 'type' => 'checkbox'),
'xb_nav_contact_qq' => array('value' => $_POST['xb_nav_contact_qq'], 'type' => 'text'),
'xb_nav_contact_city' => array('value' => $_POST['xb_nav_contact_city'], 'type' => 'text'),
'xb_nav_contact_email' => array('value' => $_POST['xb_nav_contact_email'], 'type' => 'text'),
'xb_nav_contact_bilibili_url' => array('value' => $_POST['xb_nav_contact_bilibili_url'], 'type' => 'url'),
'xb_nav_contact_bilibili_text' => array('value' => $_POST['xb_nav_contact_bilibili_text'], 'type' => 'text'),
'xb_nav_contact_weibo_url' => array('value' => $_POST['xb_nav_contact_weibo_url'], 'type' => 'url'),
'xb_nav_contact_netease_url' => array('value' => $_POST['xb_nav_contact_netease_url'], 'type' => 'url'),
'xb_nav_contact_xiaohongshu_qr' => array('value' => $_POST['xb_nav_contact_xiaohongshu_qr'], 'type' => 'url'),
'xb_nav_contact_douyin_qr' => array('value' => $_POST['xb_nav_contact_douyin_qr'], 'type' => 'url'),
'xb_nav_contact_wechat_qr' => array('value' => $_POST['xb_nav_contact_wechat_qr'], 'type' => 'url'),
'xb_nav_iconfont_url' => array('value' => $_POST['xb_nav_iconfont_url'], 'type' => 'url', 'sanitize' => 'xb_sanitize_iconfont_url'),
);
foreach ($settings as $key => $config) {
$value = $config['value'];
if (isset($config['sanitize'])) {
$value = call_user_func($config['sanitize'], $value);
} else {
$value = xb_validate_input($value, $config['type']);
}
update_option($key, $value);
}
?>
<div class="notice notice-success is-dismissible">
<p>设置已保存!</p>
</div>
<script>
jQuery(document).ready(function($) {
setTimeout(function() {
$('.notice-success').fadeOut();
}, 3000);
});
</script>
<?php
}
?>
<div class="wrap">
<h1>小半自定义网址导航设置</h1>
<form method="post">
<table class="form-table">
<tr>
<th scope="row"><label for="xb_nav_page_title">前台页面标题</label></th>
<td>
<input type="text" name="xb_nav_page_title" id="xb_nav_page_title" value="<?php echo esc_attr(get_option('xb_nav_page_title', '自定义导航页面')); ?>" class="regular-text" />
<p class="description">设置前台页面的标题。</p>
</td>
</tr>
<tr>
<th scope="row">显示页面标题</th>
<td>
<input type="checkbox" name="xb_nav_show_page_title" value="1" <?php checked(get_option('xb_nav_show_page_title', '1'), '1'); ?> />
<label>在前台显示页面标题</label>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_background_color">页面背景颜色</label></th>
<td>
<input type="text" name="xb_nav_background_color" id="xb_nav_background_color" value="<?php echo esc_attr(get_option('xb_nav_background_color', 'linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%)')); ?>" class="regular-text" />
<p class="description">支持 CSS 背景颜色格式,例如 linear-gradient(45deg, #6a11cb, #2575fc) 或 #ffffff。</p>
</td>
</tr>
<tr>
<th scope="row">启用数字时钟</th>
<td>
<input type="checkbox" name="xb_nav_enable_clock" value="1" <?php checked(get_option('xb_nav_enable_clock', '0'), '1'); ?> />
<label>在前台右侧显示数字时钟</label>
</td>
</tr>
<tr>
<th scope="row">启用 LyToday-JS 热搜榜</th>
<td>
<input type="checkbox" name="xb_nav_enable_lytoday" value="1" <?php checked(get_option('xb_nav_enable_lytoday', '0'), '1'); ?> />
<label>在前台底部显示 LyToday-JS 热搜榜,具体查看https://doc.lylme.com/spage/#/lytoday-js</label>
</td>
</tr>
<tr>
<th scope="row">启用导航热榜</th>
<td>
<input type="checkbox" name="xb_nav_enable_hotlist" value="1" <?php checked(get_option('xb_nav_enable_hotlist', '0'), '1'); ?> />
<label>在前台右侧显示导航热榜(日榜、月榜、年榜、总榜)</label>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_iconfont_url">阿里Iconfont图标样式链接</label></th>
<td>
<input type="text" name="xb_nav_iconfont_url" id="xb_nav_iconfont_url" value="<?php echo esc_attr(get_option('xb_nav_iconfont_url', '')); ?>" class="regular-text" />
<p class="description">输入链接,例如://at.alicdn.com/t/c/font_4815672_l8426erg1w.css 如果你不自己修改插件源码,就需要指定名称具体看网站教程</p>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_contact_qq">QQ号</label></th>
<td>
<input type="text" name="xb_nav_contact_qq" id="xb_nav_contact_qq" value="<?php echo esc_attr(get_option('xb_nav_contact_qq', '')); ?>" class="regular-text" />
<p class="description">输入 QQ 号码,留空则不显示。</p>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_contact_city">城市</label></th>
<td>
<input type="text" name="xb_nav_contact_city" id="xb_nav_contact_city" value="<?php echo esc_attr(get_option('xb_nav_contact_city', '')); ?>" class="regular-text" />
<p class="description">输入城市名称,留空则不显示。</p>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_contact_email">邮箱</label></th>
<td>
<input type="email" name="xb_nav_contact_email" id="xb_nav_contact_email" value="<?php echo esc_attr(get_option('xb_nav_contact_email', '')); ?>" class="regular-text" />
<p class="description">输入邮箱地址,留空则不显示。</p>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_contact_bilibili_url">B站链接</label></th>
<td>
<input type="url" name="xb_nav_contact_bilibili_url" id="xb_nav_contact_bilibili_url" value="<?php echo esc_attr(get_option('xb_nav_contact_bilibili_url', '')); ?>" class="regular-text" />
<p class="description">输入 B站主页链接,留空则不显示。</p>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_contact_bilibili_text">B站链接文本</label></th>
<td>
<input type="text" name="xb_nav_contact_bilibili_text" id="xb_nav_contact_bilibili_text" value="<?php echo esc_attr(get_option('xb_nav_contact_bilibili_text', '')); ?>" class="regular-text" />
<p class="description">输入 B站链接显示的文本,留空则显示“B站”。</p>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_contact_weibo_url">微博链接</label></th>
<td>
<input type="url" name="xb_nav_contact_weibo_url" id="xb_nav_contact_weibo_url" value="<?php echo esc_attr(get_option('xb_nav_contact_weibo_url', '')); ?>" class="regular-text" />
<p class="description">输入微博主页链接,留空则不显示。</p>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_contact_netease_url">网易云音乐链接</label></th>
<td>
<input type="url" name="xb_nav_contact_netease_url" id="xb_nav_contact_netease_url" value="<?php echo esc_attr(get_option('xb_nav_contact_netease_url', '')); ?>" class="regular-text" />
<p class="description">输入网易云音乐主页链接,留空则不显示。</p>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_contact_xiaohongshu_qr">小红书二维码图片地址</label></th>
<td>
<input type="url" name="xb_nav_contact_xiaohongshu_qr" id="xb_nav_contact_xiaohongshu_qr" value="<?php echo esc_attr(get_option('xb_nav_contact_xiaohongshu_qr', '')); ?>" class="regular-text" />
<p class="description">输入小红书二维码图片地址,留空则不显示。</p>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_contact_douyin_qr">抖音二维码图片地址</label></th>
<td>
<input type="url" name="xb_nav_contact_douyin_qr" id="xb_nav_contact_douyin_qr" value="<?php echo esc_attr(get_option('xb_nav_contact_douyin_qr', '')); ?>" class="regular-text" />
<p class="description">输入抖音二维码图片地址,留空则不显示。</p>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_contact_wechat_qr">微信二维码图片地址</label></th>
<td>
<input type="url" name="xb_nav_contact_wechat_qr" id="xb_nav_contact_wechat_qr" value="<?php echo esc_attr(get_option('xb_nav_contact_wechat_qr', '')); ?>" class="regular-text" />
<p class="description">输入微信二维码图片地址,留空则不显示。</p>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_announcement">公告内容</label></th>
<td>
<textarea name="xb_nav_announcement" id="xb_nav_announcement" rows="5" class="large-text"><?php echo esc_textarea(get_option('xb_nav_announcement', '')); ?></textarea>
<p class="description">支持 HTML 格式,留空则不显示。</p>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_ad_content">广告内容</label></th>
<td>
<textarea name="xb_nav_ad_content" id="xb_nav_ad_content" rows="5" class="large-text"><?php echo esc_textarea(get_option('xb_nav_ad_content', '')); ?></textarea>
<p class="description">支持 HTML 格式,留空则不显示。建议图片尺寸为 300x200px。</p>
</td>
</tr>
<tr>
<th scope="row">显示导航数量</th>
<td>
<input type="checkbox" name="xb_nav_show_count" value="1" <?php checked(get_option('xb_nav_show_count', '0'), '1'); ?> />
<label>在前台显示当前收录的导航数量</label>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_apply_url">申请收录网址</label></th>
<td>
<input type="url" name="xb_nav_apply_url" id="xb_nav_apply_url" value="<?php echo esc_attr(get_option('xb_nav_apply_url', '')); ?>" class="regular-text" />
</td>
</tr>
<tr>
<th scope="row">显示申请收录</th>
<td>
<input type="checkbox" name="xb_nav_show_apply" value="1" <?php checked(get_option('xb_nav_show_apply', '0'), '1'); ?> />
<label>在前台显示申请收录入口</label>
</td>
</tr>
<tr>
<th scope="row">显示文章</th>
<td>
<input type="checkbox" name="xb_nav_show_posts" value="1" <?php checked(get_option('xb_nav_show_posts', '0'), '1'); ?> />
<label>在前台显示文章</label>
</td>
</tr>
<tr>
<th scope="row">文章类型</th>
<td>
<select name="xb_nav_post_type">
<option value="recent" <?php selected(get_option('xb_nav_post_type', 'recent'), 'recent'); ?>>最新文章</option>
<option value="random" <?php selected(get_option('xb_nav_post_type', 'recent'), 'random'); ?>>随机文章</option>
</select>
</td>
</tr>
<tr>
<th scope="row"><label for="xb_nav_post_count">文章数量</label></th>
<td>
<input type="number" name="xb_nav_post_count" id="xb_nav_post_count" value="<?php echo esc_attr(get_option('xb_nav_post_count', 3)); ?>" min="1" max="10" />
</td>
</tr>
<tr>
<th scope="row">显示导航描述</th>
<td>
<input type="checkbox" name="xb_nav_show_desc" value="1" <?php checked(get_option('xb_nav_show_desc', '0'), '1'); ?> />
<label>在鼠标悬停时显示导航的完整描述</label>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" name="xb_nav_settings_submit" class="button-primary" value="保存设置" />
</p>
</form>
</div>
<?php
}
// 添加导航页面
function xb_nav_add_page() {
global $wpdb;
$table_nav = $wpdb->prefix . 'xb_nav';
$table_category = $wpdb->prefix . 'xb_nav_categories';
$message = '';
$message_type = 'updated';
// 处理分类添加
if (isset($_POST['xb_nav_add_category'])) {
$category_name = xb_validate_input($_POST['category_name'], 'text');
$category_icon = xb_validate_input($_POST['category_icon'], 'url');
$errors = array();
if (!$category_name) $errors[] = '分类名称无效或过长!';
if (!$category_icon) $errors[] = '分类图标地址无效!';
if ($errors) {
$message = implode('<br>', $errors);
$message_type = 'error';
} else {
try {
$wpdb->query('START TRANSACTION');
$result = $wpdb->insert(
$table_category,
array('name' => $category_name, 'icon' => $category_icon),
array('%s', '%s')
);
if ($result === false) {
throw new Exception('数据库错误:' . $wpdb->last_error);
}
$wpdb->query('COMMIT');
$message = '分类已添加!';
} catch (Exception $e) {
$wpdb->query('ROLLBACK');
$message = '添加分类失败:' . esc_html($e->getMessage());
$message_type = 'error';
}
}
}
// 处理分类编辑
if (isset($_POST['xb_nav_edit_category'])) {
$category_id = xb_validate_input($_POST['category_id'], 'int');
$category_name = xb_validate_input($_POST['category_name'], 'text');
$category_icon = xb_validate_input($_POST['category_icon'], 'url');