This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
functions.php
596 lines (573 loc) · 18.2 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
<?php
/**
*
* 函数申明
*
* @author MaiCong <[email protected]>
* @link https://github.com/maicong/stay
* @since 1.5.8
*
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
define('__LAZYIMG__', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIW2P8+vXrfwAJpgPg8gE+iwAAAABJRU5ErkJggg==');
// 主题设置
function themeConfig($form) {
Typecho_Widget::widget('Widget_Metas_Category_List')->to($category);
$listCate = [];
foreach($category->stack as $cat) {
$listCate[$cat['mid']] = $cat['name'];
}
$navCategory = new Typecho_Widget_Helper_Form_Element_Checkbox(
'navCategory',
$listCate,
NULL,
_t('添加分类到导航'),
_t('选择你要添加的分类,可以选择多个')
);
$shortcode = new Typecho_Widget_Helper_Form_Element_Radio(
'shortcode',
array(
'1' => '开启',
'0' => '关闭'
),
'0',
_t('短代码支持'),
_t('是否启用短代码支持,移植的 WordPress 功能')
);
$powerMode = new Typecho_Widget_Helper_Form_Element_Radio(
'powerMode',
array(
'1' => '开启',
'0' => '关闭'
),
'0',
_t('评论框特效'),
_t('是否启用评论框输入特效')
);
// $siteStyle = new Typecho_Widget_Helper_Form_Element_Radio(
// 'siteStyle',
// array(
// 'purple' => '沉稳紫',
// 'green' => '清新绿',
// 'pink' => '俏皮粉',
// 'red' => '喜庆红',
// 'yellow' => '灿烂黄',
// 'blue' => '未来蓝',
// 'black' => '暗夜黑',
// 'white' => '冰雪白',
// 'color' => '斑斓彩'
// ),
// 'purple',
// _t('网站风格'),
// _t('不同的风格不同的态度')
// );
$headBgUrl = new Typecho_Widget_Helper_Form_Element_Text(
'headBgUrl',
NULL,
NULL,
_t('页头背景图'),
_t('填入有效的图片地址,可以为本地或远程 URL')
);
$text2speech = new Typecho_Widget_Helper_Form_Element_Radio(
'text2speech',
array(
'1' => '开启',
'0' => '关闭'
),
'0',
_t('文章语音朗读'),
_t('是否启用文章语音朗读功能')
);
$text2speechSex = new Typecho_Widget_Helper_Form_Element_Radio(
'text2speechSex',
array(
'1' => '普通男声',
'0' => '普通女声',
'3' => '情感男声',
'4' => '情感女声'
),
'3',
_t('语音朗读合成类型'),
_t('喜欢男声还是女声?')
);
$text2speechSpeed = new Typecho_Widget_Helper_Form_Element_Radio(
'text2speechSpeed',
array(
'1' => '超慢',
'3' => '慢速',
'5' => '正常',
'7' => '快速',
'12' => '超快'
),
'5',
_t('语音朗读语速'),
_t('说的快还是慢?')
);
$text2speechLength = new Typecho_Widget_Helper_Form_Element_Text(
'text2speechLength',
NULL,
'3000',
_t('语音朗读分段字数'),
_t('输入分段字数,最大为 5000 字,为防分段失败建议不要超过最大值,默认为 3000 字')
);
$text2speechBegin = new Typecho_Widget_Helper_Form_Element_Text(
'text2speechBegin',
NULL,
'语音小助手为您服务~',
_t('语音朗读开头内容'),
_t('为语音配上一个欢迎词?')
);
$text2speechEnd = new Typecho_Widget_Helper_Form_Element_Text(
'text2speechEnd',
NULL,
'朗读完毕~',
_t('语音朗读结尾内容'),
_t('为语音配上一个结束语?')
);
$baiduBDUSS = new Typecho_Widget_Helper_Form_Element_Text(
'baiduBDUSS',
NULL,
NULL,
_t('百度 BDUSS'),
_t('百度 Cookie 中的 BDUSS,获取方法请看 <a href="https://github.com/maicong/stay/blob/master/README.md#%E7%99%BE%E5%BA%A6bduss">README.md</a>')
);
$openDonate = new Typecho_Widget_Helper_Form_Element_Radio(
'openDonate',
array(
'1' => '开启',
'0' => '关闭'
),
'0',
_t('文章打赏'),
_t('是否启用文章打赏功能')
);
$donateTips = new Typecho_Widget_Helper_Form_Element_Text(
'donateTips',
NULL,
'扫描下面二维码,给我点动力吧~',
_t('打赏提示文字'),
_t('填入打赏提示文字')
);
$donateWeixin = new Typecho_Widget_Helper_Form_Element_Text(
'donateWeixin',
NULL,
NULL,
_t('微信收款二维码'),
_t('填入微信收款二维码地址,建议保持一致尺寸,例如 200x200')
);
$donateAlipay = new Typecho_Widget_Helper_Form_Element_Text(
'donateAlipay',
NULL,
NULL,
_t('支付宝收款二维码'),
_t('填入支付宝收款二维码地址,建议保持一致尺寸,例如 200x200')
);
$gravatarUrl = new Typecho_Widget_Helper_Form_Element_Text(
'gravatarUrl',
NULL,
NULL,
_t('Gravatar 代理地址'),
_t('填入 Gravatar 代理地址,例如 https://v2ex.assets.uxengine.net/gravatar/')
);
$customHead = new Typecho_Widget_Helper_Form_Element_Textarea(
'customHead',
NULL,
NULL,
_t('自定义头部信息'),
_t('HTML 代码,可以是 meta 或 link 等')
);
$customFoot = new Typecho_Widget_Helper_Form_Element_Textarea(
'customFoot',
NULL,
NULL,
_t('自定义页脚代码'),
_t('HTML 代码,显示于页脚版权信息下一行')
);
$analyticsCode = new Typecho_Widget_Helper_Form_Element_Textarea(
'analyticsCode',
NULL,
NULL,
_t('统计代码'),
_t('网站统计代码,隐藏于页脚')
);
$form->addInput($navCategory);
$form->addInput($shortcode);
$form->addInput($powerMode);
// $form->addInput($siteStyle);
$form->addInput($headBgUrl);
$form->addInput($text2speech);
$form->addInput($text2speechSex);
$form->addInput($text2speechSpeed);
$form->addInput($text2speechLength);
$form->addInput($text2speechBegin);
$form->addInput($text2speechEnd);
$form->addInput($baiduBDUSS);
$form->addInput($openDonate);
$form->addInput($donateTips);
$form->addInput($donateWeixin);
$form->addInput($donateAlipay);
$form->addInput($gravatarUrl);
$form->addInput($customHead);
$form->addInput($customFoot);
$form->addInput($analyticsCode);
}
// 主题加载
function themeInit($self) {
if (!defined('__TYPECHO_EXCEPTION_FILE__')) {
define('__TYPECHO_EXCEPTION_FILE__', __DIR__ . '/exception.php');
}
$options = $self->widget('Widget_Options');
if ($options->gravatarUrl && !defined('__TYPECHO_GRAVATAR_PREFIX__')) {
define('__TYPECHO_GRAVATAR_PREFIX__', $options->gravatarUrl);
}
if ($options->shortcode) {
require_once __DIR__ . '/shortcode.php';
}
}
// 添加缩略图字段
function themeFields($layout) {
$thumbnail = new Typecho_Widget_Helper_Form_Element_Text(
'thumbnail',
NULL,
NULL,
_t('缩略图'),
_t('一个有效图片地址,推荐尺寸 900x425')
);
$layout->addItem($thumbnail);
}
// 获取自定义字段
function getFields($post, $key) {
$value = $post->fields->{$key};
if ($value) {
return $value;
}
}
// 获取摘要
function getExcerpt($content, $length = 300, $trim = ' ......') {
$content = getContent($content);
$content = Typecho_Common::subStr(strip_tags($content), 0, $length, $trim);
return preg_replace('/\s/iu', ' ', trim($content));
}
// 获取文章发布时间
function getPostDate ($post, $format = null, $str = '更新于 ') {
$options = Typecho_Widget::widget('Widget_Options');
$format = $format ?: $options->postDateFormat;
if ($post->modified - $post->created > 86400) {
return $str . date($format, $post->modified);
} else {
return date($format, $post->modified);
}
}
// 获取内容
function getContent($content) {
$options = Typecho_Widget::widget('Widget_Options');
// 短代码
if ($options->shortcode) {
$content = do_shortcode($content);
}
// 图片占位符
$content = preg_replace(
['/<p>(<div(.+?)<\/div>)<\/p>/', '/<img(.+?)src="/'],
['$1', '<img$1src="' . __LAZYIMG__ . '" data-original="'],
$content
);
// 代码行号
$content = preg_replace(
'/<pre><code class="lang-/iu',
'<pre class="line-numbers"><code class="lang-',
$content
);
return $content;
}
// 获取打包的资源文件
function getBuildFile($type) {
$file = __DIR__.'/assets/build/manifest.php';
$manifest = null;
if (file_exists($file)) {
require_once(__DIR__.'/assets/build/manifest.php');
$options = Typecho_Widget::widget('Widget_Options');
$manifest = new WebpackManifest();
switch ($type) {
case 'css':
$options->themeUrl($manifest::$cssFiles['stay']);
break;
case 'js':
$options->themeUrl($manifest::$jsFiles['stay']);
break;
}
} else {
echo '资源不存在,请先运行打包命令';
exit;
}
}
// 获取表情
function getFaces ($type) {
$faces = array(
'hehe' => '[呵呵]',
'haha' => '[哈哈]',
'heihei' => '[嘿嘿]',
'xixi' => '[嘻嘻]',
'xiaoku' => '[笑哭]',
'keai' => '[可爱]',
'baibai' => '[拜拜]',
'wa' => '[哇]',
'pu' => '[噗]',
'ku' => '[酷]',
'nanshou' => '[难受]',
'yi' => '[咦]',
'emmm' => '[呃]',
'heng' => '[哼]',
'baiyan' => '[白眼]',
'dai' => '[呆]',
'cahan' => '[擦汗]',
'ganga' => '[尴尬]',
'yun' => '[晕]',
'jiujie' => '[纠结]',
'huaji' => '[滑稽]',
'koubi' => '[抠鼻]',
'sikao' => '[思考]',
'bishi' => '[鄙视]',
'tian' => '[舔]',
'fendou' => '[奋斗]',
'kun' => '[困]',
'haixiu' => '[害羞]',
'qaq' => '[QAQ]',
'doge' => '[Doge]',
'laowang' => '[老王]',
'zhale' => '[炸了]',
'gouyin' => '[勾引]',
'niubi' => '[牛逼]',
'ruoji' => '[弱鸡]',
'woshou' => '[握手]',
'gaoci' => '[告辞]',
'ok' => '[OK]',
'yaowan' => '[药丸]',
'pig' => '[猪]'
);
if ($type === 'map') {
return array_map(function ($val) {
return "<i class=\"face face-{$val}\"></i>";
}, array_flip($faces));
}
if ($type === 'html') {
$html = '';
foreach($faces as $key => $val) {
$html .= "<i class=\"face face-{$key}\" data-tag=\"{$val}\"></i>";
}
echo $html;
}
}
// 获取评论页
function getCommentsPage ($word = '« Previous Entries', $page = 'prev') {
$options = Typecho_Widget::widget('Widget_Options');
$comments = Typecho_Widget::widget('Widget_Comments_Archive');
$total = $comments->parameter->parentContent['commentsNum'];
if (
'last' == $options->commentsPageDisplay &&
!$comments->parameter->commentPage
) {
$currentPage = ceil($total / $options->commentsPageSize);
} else {
$currentPage = $comments->parameter->commentPage ?
$comments->parameter->commentPage :
1;
}
if (
$options->commentsPageBreak &&
$total > $options->commentsPageSize
) {
if (empty($comments->_pageNav)) {
$pageRow = $comments->parameter->parentContent;
$pageRow['permalink'] = $pageRow['pathinfo'];
$query = Typecho_Router::url(
'comment_page',
$pageRow,
$options->index
);
/** 使用盒状分页 */
$comments->_pageNav = new Typecho_Widget_Helper_PageNavigator_Classic(
$total,
$currentPage,
$options->commentsPageSize,
$query
);
$comments->_pageNav->setPageHolder('commentPage');
$comments->_pageNav->setAnchor('comments');
}
$comments->_pageNav->{$page}($word);
}
}
// Http 请求
function mcFetch ($args = array()) {
$args = array_merge(array(
'method' => 'GET',
'url' => null,
'header' => array(),
'data' => array()
), $args);
$args['header'] = array_merge(array(
'Referer' => 'https://www.google.co.uk',
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36'
), $args['header']);
if (!$args['url']) {
return;
}
if ($client = Typecho_Http_Client::get()) {
if (!empty($args['header'])) {
foreach($args['header'] as $key => $val) {
$client->setHeader($key, $val);
}
}
if (!empty($args['data'])) {
if ($args['method'] === 'GET') {
$client->setQuery($args['data']);
}
if ($args['method'] === 'POST') {
$client->setData($args['data']);
}
}
$client->setTimeout(15);
$client->send($args['url']);
return $client->getResponseBody();
}
}
// 获取音频地址
function getSpeech ($title, $content) {
$options = Typecho_Widget::widget('Widget_Options');
$result = mcFetch(array(
'method' => 'POST',
'url' => 'https://developer.baidu.com/vcast/getVcastInfo',
'header' => array(
'Referer' => 'https://developer.baidu.com/vcast',
'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With' => 'XMLHttpRequest',
'Cookie' => 'BDUSS=' . $options->baiduBDUSS
),
'data' => array(
'title' => $title,
'content' => $content,
'sex' => $options->text2speechSex >= 0 ? $options->text2speechSex : 4,
'speed' => $options->text2speechSpeed ? $options->text2speechSpeed : 5,
'volumn' => 9,
'pit' => 5,
'method' => 'TRADIONAL'
)
));
if ($data = json_decode($result)) {
if ($data) {
return $data->bosUrl;
}
}
}
// 分割字符串
function mb_str_split($str, $length = 1) {
if ($length < 1) return false;
$result = array();
for ($i = 0; $i < mb_strlen($str); $i += $length) {
$result[] = mb_substr($str, $i, $length);
}
return $result;
}
// 文字转语音
function text2speech ($cid) {
Typecho_Widget::widget('Widget_Archive', 'type=post&cid=' . $cid)->to($post);
$options = Typecho_Widget::widget('Widget_Options');
$content = $post->content;
if ($options->shortcode) {
$content = do_shortcode($content);
}
$content = strip_tags($content);
$content = str_replace("</p><p>", "。", $content);
$content = str_replace(
array('“', '”', '"', '\'', '@', '#', '%', '&', '——', '…', '*'),
' ',
$content
);
$speech = [];
$length = $options->text2speechLength ? (int) $options->text2speechLength : 3000;
$contentList = mb_str_split($content, $length);
$contentLength = count($contentList);
foreach ($contentList as $key => $val) {
$title = $post->title;
if ($key === 0) {
$title = $options->text2speechBegin . '。文章标题:' . $title;
$val = '。文章内容:' . $val ;
} else {
if ($contentLength > 1) {
$title = mb_substr($val, 0, 2);
$val = mb_substr($val, 2);
}
}
if ($key === $contentLength - 1) {
$val = $val . '。' . $options->text2speechEnd;
}
$speech[] = getSpeech($title, $val);
}
return $speech;
}
// 转换表情
function convertFaces ($content) {
$faces = getFaces('map');
return str_replace(array_keys($faces), array_values($faces), $content);
}
// 转换评论内容
function convertComments ($content) {
$content = convertFaces($content);
// 匹配链接
$content = preg_replace(
'/([^="]+)(https?:\/\/[a-zA-Z0-9\/\-_.:=#?&%]+)/iu',
'$1<a href="$2">$2</a>',
$content
);
// 代码行号
$content = preg_replace(
'/<pre><code class="lang-/iu',
'<pre class="line-numbers"><code class="lang-',
$content
);
return $content;
}
// 评论层
function threadedComments($comments, $options) {
$commentClass = '';
$user_type = '';
if ($comments->authorId) {
if ($comments->authorId == $comments->ownerId) {
$commentClass .= ' author';
} else {
$commentClass .= ' user';
}
}
switch ($comments->authorId) {
case '1':
$user_type = '<i class="identity">博主</i>';
break;
default:
if ('waiting' == $comments->status) {
$user_type = '<i class="pending">评论待审核</i>';
}
}
?>
<li id="li-<?php $comments->theId(); ?>" class="comment">
<div id="<?php $comments->theId(); ?>" class="comment-block">
<div class="comment__vcard">
<div class="comment__avatar">
<?php $comments->gravatar(84, 'retro', true); ?>
</div>
<div class="comment__meta">
<cite class="comment__author"><?php $comments->author(); ?><?php echo $user_type; ?></cite>
<time class="comment__time" datetime="<?php $comments->date('c'); ?>"><?php $comments->date(); ?></time>
</div>
</div>
<div id="comment-content" class="comment__content">
<?php echo convertComments($comments->content); ?>
</div>
<?php $comments->reply(); ?>
</div>
<?php if ($comments->children) { ?>
<div class="children">
<?php $comments->threadedComments($options); ?>
</div>
<?php } ?>
</li>
<?php }