-
Notifications
You must be signed in to change notification settings - Fork 5
/
functions.php
387 lines (357 loc) · 9.39 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
<?php
use Typecho\Common;
use Typecho\Widget\Helper\Form\Element\Checkbox;
use Typecho\Widget\Helper\Form\Element\Radio;
use Typecho\Widget\Helper\Form\Element\Text;
use Typecho\Widget\Helper\Form\Element\Textarea;
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
function themeConfig($form)
{
$faviconUrl = new Text(
'faviconUrl',
null,
null,
_t('站点图标'),
_t('请填写站点图标地址, 如果不填则默认获取站点根路径的favicon.ico')
);
$form->addInput($faviconUrl);
$logoUrl = new Text(
'logoUrl',
null,
null,
_t('网站LOGO'),
_t('请填写网站 LOGO 地址, 如果填写则显示图片 LOGO ,不填则显示文本标题')
);
$form->addInput($logoUrl);
$notice = new Text(
'notice',
null,
null,
_t('公告设置'),
_t('显示在首页的公告文本,为空则不显示公告')
);
$form->addInput($notice);
$calendar = new Radio(
'calendar',
[
'Show' => _t('显示'),
'Hide' => _t('隐藏')
],
'Show',
_t('日历')
);
$form->addInput($calendar);
$clock = new Radio(
'clock',
[
'Show' => _t('显示'),
'Hide' => _t('隐藏')
],
'Show',
_t('时钟')
);
$form->addInput($clock);
$particles = new Radio(
'particles',
[
'Show' => _t('显示'),
'Hide' => _t('隐藏')
],
'Show',
_t('粒子特效')
);
$form->addInput($particles);
$redirect = new Radio(
'redirect',
[
0 => _t('禁用'),
1 => _t('启用')
],
0,
_t('过渡页面'),
_t('启用外链跳转过渡页面,主要用于安全声明,过渡页面设置了一个广告位,弥补了一下广告位太少的问题')
);
$form->addInput($redirect);
$searchEngines = getAllSearchEngines();
$searchEngineTitles = [];
foreach ($searchEngines as $key => $value) {
$searchEngineTitles[$key] = $value['title'];
}
$search = new Checkbox(
'searchEngines',
$searchEngineTitles,
['self'],
_t('搜索框'),
_t('如果不需要显示搜索框,则全部不勾选即可')
);
$form->addInput($search);
$bgImgs = new Textarea(
'bgImgs',
null,
'[
"/usr/themes/BeaconNav/static/images/bg1.jpg",
"/usr/themes/BeaconNav/static/images/bg2.jpg",
"/usr/themes/BeaconNav/static/images/bg3.jpg",
"/usr/themes/BeaconNav/static/images/bg4.jpg"
]',
_t('背景图片'),
_t('可访问的图片地址,也可以是网络图片的全路径,[]表示不需要背景图片')
);
$form->addInput($bgImgs);
$beian = new Text(
'beian',
null,
null,
_t('备案号'),
_t('请填入形如"粤ICP备xxx号-1"的备案号')
);
$form->addInput($beian);
$googleAd = new Textarea(
'googleAd',
null,
null,
_t('谷歌广告'),
_t('主题预设了Google AdSense广告位,不填则不显示广告,格式:{"publisher":"pub-xxx", "slot":"xxx", "redirect":"xxx"},注意:由于SPA与Google Ads的兼容性存在问题,所以当使用Google Ads时,会禁用打开文章详情页的PJAX功能,需要牺牲一定的性能,请自行权衡')
);
$form->addInput($googleAd);
$footerJs = new Textarea(
'footerJs',
null,
null,
_t('底部JS'),
_t('请填入包括script标签JS代码,主要是统计、广告等相关的代码')
);
$form->addInput($footerJs);
}
/**
* 显示广告
*
* @param string $position 广告位置
* slot: 文章页广告位
* redirect:过渡页广告位
* @param string $classes 广告容器类名
*/
function showGoogleAd($position, $classes = '')
{
$googleAd = getGoogleAd();
if ($googleAd['showAd'] && !empty($googleAd[$position])) {
echo <<<EOF
<section class="rounded overflow-hidden {$classes}">
<ins class="adsbygoogle" style="display:block;text-align:center;" data-ad-client="ca-{$googleAd['publisher']}" data-ad-slot="{$googleAd[$position]}" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</section>
EOF;
}
}
function getGoogleAd()
{
static $settings = [];
if (!empty($settings)) {
return $settings;
}
$options = Typecho\Widget::widget(Widget\Options::class);
if (empty($options->googleAd)) {
$settings = [
'showAd' => false
];
return $settings;
}
$googleAd = json_decode($options->googleAd, true);
if (!empty($googleAd) && !empty($googleAd['publisher']) && strpos($googleAd['publisher'], 'pub-') === 0) {
$settings = [
'showAd' => true,
'publisher' => $googleAd['publisher'],
'slot' => isset($googleAd['slot']) ? $googleAd['slot'] : '',
'redirect' => isset($googleAd['redirect']) ? $googleAd['redirect'] : ''
];
} else {
$settings = [
'showAd' => false
];
}
return $settings;
}
function themeFields($layout)
{
if (preg_match("/write-post.php/", $_SERVER['REQUEST_URI'])) {
$url = new Text(
'url',
null,
null,
_t('跳转链接'),
_t('跳转链接URL,必填')
);
$icon = new Text(
'icon',
null,
null,
_t('站点图标'),
_t('完整网络地址、本站附件地址或不填,如果不填,则获取目标站点根目录下的favicon.ico')
);
$layout->addItem($url);
$layout->addItem($icon);
}
}
function getAllSearchEngines()
{
return [
'self' => [
'title' => _t('站内'),
'url' => '/search/'
],
'baidu' => [
'title' => _t('百度'),
'url' => 'https://www.baidu.com/s?ie=UTF-8&wd='
],
'google' => [
'title' => 'Google',
'url' => 'https://www.google.com/search?q='
],
'bing' => [
'title' => _t('必应'),
'url' => 'https://cn.bing.com/search?q='
],
'360' => [
'title' => '360',
'url' => 'https://www.so.com/s?q='
],
'weibo' => [
'title' => _t('微博'),
'url' => 'https://s.weibo.com/weibo?Refer=top&q='
],
'github' => [
'title' => 'GitHub',
'url' => 'https://github.com/search?type=repositories&q='
]
];
}
function websiteIcon(Widget\Archive $archive)
{
if (!($archive->fields->url)) {
return '';
} elseif ($archive->fields->icon) {
return $archive->fields->icon;
} else {
return Common::url('/favicon.ico', $archive->fields->url);
}
}
//获取Gravatar头像 QQ邮箱取用qq头像
function getGravatar($email, $s = 96, $d = 'mp', $r = 'g', $img = false, $atts = array())
{
preg_match_all('/((\d)*)@qq.com/', $email, $vai);
if (empty($vai['1']['0'])) {
$url = 'https://cdn.sep.cc/avatar/';
$url .= md5(strtolower(trim($email)));
$url .= "?s=$s&d=$d&r=$r";
if ($img) {
$url = '<img src="' . $url . '"';
foreach ($atts as $key => $val)
$url .= ' ' . $key . '="' . $val . '"';
$url .= ' />';
}
} else {
$url = 'https://q2.qlogo.cn/headimg_dl?dst_uin=' . $vai['1']['0'] . '&spec=100';
}
return $url;
}
function getEmojis()
{
return [
'😊',
'😃',
'😄',
'😁',
'😆',
'😅',
'😂',
'🤣',
'🙂',
'🙃',
'😉',
'😇',
'😏',
'😌',
'😍',
'😘',
'😗',
'😙',
'😚',
'😋',
'😛',
'😜',
'😝',
'😒',
'😔',
'😖',
'😞',
'😟',
'😠',
'😡',
'😳',
'😨',
'😰',
'😥',
'😢',
'😭',
'😱',
'😲',
'😵',
'😷',
'🤒',
'🤕',
'🤢',
'😴',
'🤤',
'😪',
'😫',
'😬',
'😮',
'🤲',
'🤜',
'🤛',
'🤚',
'🤝',
'🙏',
'🤞',
'🤟',
'🤘',
'🤙',
'👌',
'👍',
'👎',
'✊',
'👊',
'🤛',
'🤜',
'👏',
'🙌',
'👐',
'🤲',
'🤝',
'🙏',
'💪'
];
}
function getRedirectUrl($url)
{
if (!$url) {
return [
'hasUrl' => false,
'url' => ''
];
}
$options = Typecho\Widget::widget(Widget\Options::class);
if (str_starts_with($url, $options->siteUrl) || $options->redirect != '1') {
return [
'hasUrl' => true,
'url' => $url
];
} else {
return [
'hasUrl' => true,
'url' => $options->siteUrl . '?target=' . urlencode($url)
];
}
}