This repository has been archived by the owner on Sep 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
654 lines (566 loc) · 18 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
<?php
/**
* 樣式
*/
function tunalog_register_styles() {
$theme_version = wp_get_theme()->get( 'Version' );
//wp_enqueue_style( 'tunalog-style', get_stylesheet_uri() . '?v=' . time(), array(), $theme_version );
wp_enqueue_style( 'tunalog-style', get_stylesheet_uri(), array(), $theme_version );
if ( get_theme_mod( 'highlight_js', 'enabled' ) == 'enabled' ) {
wp_enqueue_style( 'tunalog-highlight-style', get_template_directory_uri() . '/assets/css/highlight.css', array(), $theme_version);
}
}
add_action( 'wp_enqueue_scripts', 'tunalog_register_styles' );
/**
* 語系
*/
function tunalog_load_theme_textdomain() {
load_theme_textdomain( 'tunalog', get_template_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'tunalog_load_theme_textdomain' );
/**
* 自訂選單
*/
function tunalog_register_menu() {
register_nav_menu('tunalog-homepage-menu', '首頁選單');
}
add_action( 'init', 'tunalog_register_menu' );
/**
* 小工具
*/
function tunalog_sidebar_registration() {
$shared_args = array(
'before_title' => '<h2 class="widget__title">',
'after_title' => '</h2>',
'before_widget' => '<div class="widget %2$s"><div class="widget__content">',
'after_widget' => '</div></div>',
);
// 側邊欄。
register_sidebar(
array_merge(
$shared_args,
array(
'name' => '側邊欄',
'id' => 'sidebar-1',
'description' => '可供展開的側邊欄位。',
)
)
);
// 文末上方。
register_sidebar(
array_merge(
$shared_args,
array(
'name' => '文末上方',
'id' => 'pageend-1',
'description' => '內文結束的留言區塊上方欄位。',
)
)
);
// 文末下方。
register_sidebar(
array_merge(
$shared_args,
array(
'name' => '文末下方',
'id' => 'pageend-2',
'description' => '文章頁面最底部的欄位。',
)
)
);
// 頁腳左側。
register_sidebar(
array_merge(
$shared_args,
array(
'name' => '頁腳左側',
'id' => 'footer-1',
'description' => '每個頁面的頁腳左側欄位。',
)
)
);
// 頁腳中間。
register_sidebar(
array_merge(
$shared_args,
array(
'name' => '頁腳中間',
'id' => 'footer-2',
'description' => '每個頁面的頁腳中間欄位。',
)
)
);
// 頁腳右側。
register_sidebar(
array_merge(
$shared_args,
array(
'name' => '頁腳右側',
'id' => 'footer-3',
'description' => '每個頁面的頁腳右側欄位。',
)
)
);
}
add_action( 'widgets_init', 'tunalog_sidebar_registration' );
/**
* 特色圖片輔助樣式
*/
function add_featured_image_body_class( $classes ) {
global $post;
if ( isset ( $post->ID ) && get_the_post_thumbnail( $post->ID) ) {
$classes[] = 'has-featured-image';
}
return $classes;
}
add_filter( 'body_class', 'add_featured_image_body_class' );
/**
* 主題支援特色圖片
*/
set_post_thumbnail_size( 1200, 9999 );
/**
* 主題支援
*/
add_theme_support( 'post-thumbnails' );
add_theme_support( 'title-tag' );
add_theme_support( 'align-wide' );
/**
* 自訂尺寸
*/
add_image_size( 'tunalog-fullscreen', 1980, 9999 );
add_image_size( 'tunalog-fullsize', 0, 0, false );
/**
* 主題自訂功能
*/
class Tunalog_Customize {
public static function register( $wp_customize ) {
/**
* 內文設定
*/
$wp_customize->add_section(
'content_options',
array(
'title' => "內文設定",
'priority' => 40,
'capability' => 'edit_theme_options',
)
);
/* 頁腳動力提供者 ---------------------------------------------------- */
$wp_customize->add_setting(
'social_friendly',
array(
'capability' => 'edit_theme_options',
'default' => 'enabled',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'social_friendly',
array(
'type' => 'radio',
'section' => 'content_options',
'priority' => 10,
'label' => '社群網路預覽友善(OG)',
'description' => '啟用此功能後,會讓你的文章連結預覽在社群網站(如:Facebook、Twitter)上有著更好的視覺體驗。但如果你已經有安裝其他類似 SEO 友善的擴充套件,則請將此功能關閉。',
'choices' => array(
'enabled' => '啟用',
'disabled' => '停用',
),
)
);
/* 頁腳動力提供者 ---------------------------------------------------- */
$wp_customize->add_setting(
'display_by',
array(
'capability' => 'edit_theme_options',
'default' => 'enabled_all',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'display_by',
array(
'type' => 'radio',
'section' => 'content_options',
'priority' => 10,
'label' => '頁腳動力提供者',
'description' => '你可以在頁腳選擇要不要顯示這個佈景主題的名稱,好讓更多人知道這個神奇蹦蹦超棒異域主題!拜託啦。',
'choices' => array(
'enabled_all' => '顯示「以套用 Tunalog 佈景主題的 WordPress 發表」',
'enabled' => '顯示「驕傲地採用 WordPress 發表」',
'disabled' => '停用',
),
)
);
/* 頁腳版權聲明 ---------------------------------------------------- */
$wp_customize->add_setting(
'display_copyright',
array(
'capability' => 'edit_theme_options',
'default' => 'enabled',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'display_copyright',
array(
'type' => 'radio',
'section' => 'content_options',
'priority' => 10,
'label' => '頁腳版權聲明',
'description' => '是否在頁腳顯示網站名稱與版權聲明年份?',
'choices' => array(
'enabled' => '啟用',
'disabled' => '停用',
),
)
);
/* 標頭與頁尾寬度 ---------------------------------------------------- */
$wp_customize->add_setting(
'header_width',
array(
'capability' => 'edit_theme_options',
'default' => 'standard',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'header_width',
array(
'type' => 'radio',
'section' => 'content_options',
'priority' => 10,
'label' => '標頭與頁尾寬度',
'description' => '標頭的網站名稱與頁尾的寬度是否要跟文章內容一樣等寬?',
'choices' => array(
'standard' => '與內容同寬',
'wide' => '更寬',
),
)
);
/* 內文左右對齊 ---------------------------------------------------- */
$wp_customize->add_setting(
'text_justify',
array(
'capability' => 'edit_theme_options',
'default' => 'disabled',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'text_justify',
array(
'type' => 'radio',
'section' => 'content_options',
'priority' => 10,
'label' => '內文左右對齊',
'description' => '文章的內容可以左右對齊達到最佳的排版視覺效果,但在字數較少的行數上會有較為明顯的字體間距反而有可能適得其反。',
'choices' => array(
'enabled' => '啟用',
'enabled_mobile' => '僅在行動裝置啟用',
'disabled' => '停用',
),
)
);
/* 主題色系 ---------------------------------------------------- */
$wp_customize->add_setting(
'color_scheme',
array(
'capability' => 'edit_theme_options',
'default' => 'auto',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'color_scheme',
array(
'type' => 'radio',
'section' => 'content_options',
'priority' => 10,
'label' => '主題色系',
'description' => '網站的主要色調可以是亮色(白底黑字)或暗色(黑底白字),但也能取決於讀者的瀏覽器偏好設定(例如:有的行動裝置會在晚上進入夜間模式)。',
'choices' => array(
'light' => '亮色',
'dark' => '暗色',
'auto' => '取決於讀者設定',
),
)
);
/* 強制圖片長寬比 ---------------------------------------------------- */
$wp_customize->add_setting(
'image_aspected',
array(
'capability' => 'edit_theme_options',
'default' => 'disabled',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'image_aspected',
array(
'type' => 'radio',
'section' => 'content_options',
'priority' => 10,
'label' => '強制圖片長寬比',
'description' => '強制將文章裡的所有圖片裁切並以 16:9 的長寬比呈現。',
'choices' => array(
'enabled' => '啟用並置上',
'enabled_center' => '啟用並置中',
'disabled' => '停用',
),
)
);
/* 全域寬度屬性 ---------------------------------------------------- */
$wp_customize->add_setting(
'content_width',
array(
'capability' => 'edit_theme_options',
'default' => '710px',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'content_width',
array(
'type' => 'radio',
'section' => 'content_options',
'priority' => 10,
'label' => '全域寬度屬性',
'description' => '文章的寬度可以更窄,避免內容過短時造成的空虛感。',
'choices' => array(
'710px' => '710px',
'610px' => '610px',
),
)
);
/* 字型樣式 ---------------------------------------------------- */
$wp_customize->add_setting(
'font_family',
array(
'capability' => 'edit_theme_options',
'default' => 'sans',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'font_family',
array(
'type' => 'radio',
'section' => 'content_options',
'priority' => 10,
'label' => '字型樣式',
'description' => '你可以替網站內容選擇一個更相符的字型風格。',
'choices' => array(
'sans' => 'Noto Sans',
'serif' => 'Noto Serif',
),
)
);
/* 字體大小 ---------------------------------------------------- */
$wp_customize->add_setting(
'font_size',
array(
'capability' => 'edit_theme_options',
'default' => '16px',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'font_size',
array(
'type' => 'radio',
'section' => 'content_options',
'priority' => 10,
'label' => '字體大小',
'description' => '網站的字體可以偏大令人更容易閱讀,同時搭配「全域寬度屬性」還有自己的撰寫習慣,可以讓整個網站的版面不再那麼空白。',
'choices' => array(
'16px' => '16px',
'18px' => '18px',
),
)
);
/* 程式碼標註 ---------------------------------------------------- */
$wp_customize->add_setting(
'highlight_js',
array(
'capability' => 'edit_theme_options',
'default' => 'enabled',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'highlight_js',
array(
'type' => 'radio',
'section' => 'content_options',
'priority' => 10,
'label' => '程式碼標註',
'description' => '是否要透過 Highlight.js 螢光標記程式碼區塊?',
'choices' => array(
'enabled' => '啟用',
'disabled' => '停用',
),
)
);
/* 作者資訊位置 ---------------------------------------------------- */
$wp_customize->add_setting(
'display_vcard',
array(
'capability' => 'edit_theme_options',
'default' => 'end',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'display_vcard',
array(
'type' => 'radio',
'section' => 'content_options',
'priority' => 10,
'label' => '作者資訊位置',
'description' => '發文作者的個人資訊(頭像、名稱與簡介)應出現在內文中的何處?',
'choices' => array(
'start' => '文首',
'end' => '文末',
'disabled' => '隱藏',
),
)
);
/* 程式碼區塊寬度 --------- */
$wp_customize->add_setting(
'codeblock_width',
array(
'capability' => 'edit_theme_options',
'default' => 'standard',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'codeblock_width',
array(
'type' => 'radio',
'section' => 'content_options',
'priority' => 10,
'label' => '程式碼區塊寬度',
'description' => '在內文中的程式碼區塊應該以何種寬度呈現?等寬會與內文同寬,而全螢幕會延伸到整個螢幕的寬度。',
'choices' => array(
'standard' => '等寬',
'full' => '全螢幕',
),
)
);
/* 留言樣式 --------- */
$wp_customize->add_setting(
'comment_style',
array(
'capability' => 'edit_theme_options',
'default' => 'standard',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'comment_style',
array(
'type' => 'radio',
'section' => 'content_options',
'priority' => 10,
'label' => '留言樣式',
'description' => '留言的區域應以何種樣式呈現?區塊會帶有底色以呈現成明顯的區塊樣式。',
'choices' => array(
'standard' => '標準',
'block' => '區塊',
),
)
);
/**
* 列表設定
*/
$wp_customize->add_section(
'home_options',
array(
'title' => "列表設定",
'priority' => 40,
'capability' => 'edit_theme_options',
)
);
/* 首頁特色圖片預覽 ---------------------------------------------------- */
$wp_customize->add_setting(
'featured_picture_visibility',
array(
'capability' => 'edit_theme_options',
'default' => 'disabled',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'featured_picture_visibility',
array(
'type' => 'radio',
'section' => 'home_options',
'priority' => 10,
'label' => '首頁特色圖片預覽',
'description' => '是否要在文章列表中顯示其文章的特色圖片?',
'choices' => array(
'enabled' => '顯示於文繞圖',
'enabled_background' => '以背景圖片呈現',
'disabled' => '停用',
),
)
);
/* 發文者名稱 ---------------------------------------------------- */
$wp_customize->add_setting(
'display_author',
array(
'capability' => 'edit_theme_options',
'default' => 'enabled',
'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
)
);
$wp_customize->add_control(
'display_author',
array(
'type' => 'radio',
'section' => 'home_options',
'priority' => 10,
'label' => '發文者名稱',
'description' => '在列表中的文章是否要顯示作者名稱?若部落格有多個作者這將會幫上不小的忙。',
'choices' => array(
'enabled' => '顯示',
'disabled' => '隱藏',
),
)
);
}
public static function sanitize_select( $input, $setting ) {
$input = sanitize_key( $input );
$choices = $setting->manager->get_control( $setting->id )->choices;
return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
}
public static function sanitize_checkbox( $checked ) {
return ( ( isset( $checked ) && true === $checked ) ? true : false );
}
}
add_action( 'customize_register', array( 'Tunalog_Customize', 'register' ) );
/**
* 作者卡片
*/
require('singular-vcard.php');
/**
* 動態 Highlight 載入器
*/
function tunalog_dynamic_highlight_loader() {
if( !is_singular() ) {
return;
}
if ( get_theme_mod( 'highlight_js', 'enabled' ) != 'enabled' ) {
return;
}
echo '<script src="//cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js"></script>';
$languages = array( '1c', 'abnf', 'accesslog', 'actionscript', 'ada', 'angelscript', 'applescript', 'arcade', 'arduino', 'armasm', 'asciidoc', 'aspectj', 'autohotkey', 'autoit', 'avrasm', 'awk', 'axapta', 'basic', 'bnf', 'brainfuck', 'cal', 'capnproto', 'ceylon', 'clean', 'clojure-repl', 'clojure', 'cmake', 'coq', 'cos', 'cpp', 'crmsh', 'crystal', 'csp', 'd', 'dart', 'delphi', 'django', 'dns', 'dockerfile', 'dos', 'dsconfig', 'dts', 'dust', 'ebnf', 'elixir', 'elm', 'erb', 'erlang-repl', 'erlang', 'excel', 'fix', 'flix', 'fortran', 'fsharp', 'gams', 'gauss', 'gcode', 'gherkin', 'glsl', 'gml', 'go', 'golo', 'gradle', 'groovy', 'haml', 'handlebars', 'haskell', 'haxe', 'hsp', 'htmlbars', 'hy', 'inform7', 'irpf90', 'isbl', 'jboss-cli', 'julia-repl', 'julia', 'lasso', 'ldif', 'leaf', 'lisp', 'livecodeserver', 'livescript', 'llvm', 'lsl', 'mathematica', 'matlab', 'maxima', 'mel', 'mercury', 'mipsasm', 'mizar', 'mojolicious', 'monkey', 'moonscript', 'n1ql', 'nimrod', 'nix', 'nsis', 'ocaml', 'openscad', 'oxygene', 'parser3', 'pf', 'pgsql', 'pony', 'powershell', 'processing', 'profile', 'prolog', 'protobuf', 'puppet', 'purebasic', 'q', 'qml', 'r', 'reasonml', 'rib', 'roboconf', 'routeros', 'rsl', 'ruleslanguage', 'sas', 'scala', 'scheme', 'scilab', 'smali', 'smalltalk', 'sml', 'sqf', 'stan', 'stata', 'step21', 'stylus', 'subunit', 'taggerscript', 'tap', 'tcl', 'tex', 'thrift', 'tp', 'twig', 'vala', 'vbnet', 'vbscript-html', 'vbscript', 'verilog', 'vhdl', 'vim', 'x86asm', 'xl', 'xquery', 'zephir' );
foreach( $languages as $value ) {
if ( strpos( get_post()->post_content, 'language-' . $value ) !== false ) {
echo '<script src="//cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/languages/' . $value . '.min.js"></script>';
}
}
echo '<script>hljs.initHighlightingOnLoad();</script>';
}