-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathfunctions.php
511 lines (491 loc) · 18.9 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
<?php
/*
Theme Name: JieStyle Two
Theme URI: https://tangjie.me/jiestyle-two
Author: Jarvis Tang
Author URI: https://tangjie.me/
License: GNU General Public License v3.0
*/
function tangstyle_page_menu_args( $args ) {
$args['show_home'] = true;
return $args;
}
add_filter( 'wp_page_menu_args', 'tangstyle_page_menu_args' );
if ( ! function_exists( 'tangstyle_content_nav' ) ) :
register_nav_menus(array('header-menu' => __( 'JieStyle导航菜单' ),));
//禁用修订版本
add_filter( 'wp_revisions_to_keep', 'specs_wp_revisions_to_keep', 10, 2 );
function specs_wp_revisions_to_keep( $post ) {
return 0;
}
//去除头部无用代码
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
remove_action( 'wp_head', 'locale_stylesheet' );
remove_action( 'wp_head', 'noindex', 1 );
remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'rel_canonical' );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
remove_action( 'wp_head', 'wp_oembed_add_host_js');
remove_action( 'wp_head', 'wp_resource_hints', 2 );
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
remove_action( 'wp_footer', 'wp_print_footer_scripts' );
remove_action( 'publish_future_post', 'check_and_publish_future_post', 10, 1 );
remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );
remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 );
remove_action( 'rest_api_init', 'wp_oembed_register_route');
remove_filter( 'rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4);
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10);
remove_filter( 'oembed_response_data', 'get_oembed_response_data_rich', 10, 4);
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');
//禁用 Emoji 表情选项
function disable_emojis() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
}
add_action( 'init', 'disable_emojis' );
function disable_emojis_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
//移除菜单的多余 CSS 选择器
add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1);
add_filter('page_css_class', 'my_css_attributes_filter', 100, 1);
function my_css_attributes_filter($var) {
return is_array($var) ? array() : '';
}
//替换 Gravatar 服务器
function replace_gravatar($avatar) {
$avatar = str_replace(array("//gravatar.com/", "//secure.gravatar.com/", "//www.gravatar.com/", "//0.gravatar.com/", "//1.gravatar.com/", "//2.gravatar.com/", "//cn.gravatar.com/"), "//dn-qiniu-avatar.qbox.me/", $avatar);
return $avatar;
}
add_filter( 'get_avatar', 'replace_gravatar' );
//启动友情链接
add_filter( 'pre_option_link_manager_enabled', '__return_true' );
//获得热评文章
function tangstyle_get_most_viewed($posts_num=10, $days=180){
global $wpdb;
$sql = "SELECT ID , post_title , comment_count FROM $wpdb->posts WHERE post_type = 'post' AND TO_DAYS(now()) - TO_DAYS(post_date) < $days AND ($wpdb->posts.`post_status` = 'publish' OR $wpdb->posts.`post_status` = 'inherit') ORDER BY comment_count DESC LIMIT 0 , $posts_num ";
$posts = $wpdb->get_results($sql);
$output = "";
foreach ($posts as $post){
$output .= "\n<li><a href=\"".get_permalink($post->ID)."\" target=\"_blank\" >".$post->post_title."</a></li>";
}
echo $output;
}
//彩色标签云
function colorCloud($text) {
$text = preg_replace_callback('|<a (.+?)>|i', 'colorCloudCallback', $text);
return $text;
}
function colorCloudCallback($matches) {
$text = $matches[1];
$color = dechex(rand(0,16777215));
$pattern = '/style=(\'|\")(.*)(\'|\")/i';
$text = preg_replace($pattern, "style=\"color:#{$color};$2;\"", $text);
return "<a $text>";
}
add_filter('wp_tag_cloud', 'colorCloud', 1);
//新窗口打开评论里的链接
function remove_comment_links() {
global $comment;
$url = get_comment_author_url();
$author = get_comment_author();
if ( empty( $url ) || 'http://' == $url )
$return = $author;
else
$return = "<a href='$url' rel='nofollow' target='_blank'>$author</a>";
//带跳转路径,必须根目录有 go.php 解析文件
//$return = "<a href='/go.php?url=$url' rel='nofollow' target='_blank'>$author</a>";
return $return;
}
add_filter('get_comment_author_link', 'remove_comment_links');
remove_filter('comment_text', 'make_clickable', 9);
//分页
function pagination($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 = 5; //分页数设置
$showitems = ($range * 2)+1;
$pages = ceil($total_posts/$posts_per_page);
if(1 != $pages){
echo "<ul class='pagination'>";
echo ($paged > 2 && $paged+$range+1 > $pages && $showitems < $pages)? "<li><a href='".get_pagenum_link(1)."'><i class='fas fa-angle-double-left'></i></a></li>":"";
echo ($paged > 1 && $showitems < $pages)? "<li><a href='".get_pagenum_link($prev)."'><i class='fas fa-angle-left'></i></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='active'><a href='".get_pagenum_link($i)."'>".$i."<span class='sr-only'>(current)</span></a></li>":"<li><a href='".get_pagenum_link($i)."'>".$i."</a></li>";
}
}
echo ($paged < $pages && $showitems < $pages) ? "<li><a href='".get_pagenum_link($next)."'><i class='fas fa-angle-right'></i></a></li>" :"";
echo ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) ? "<li><a href='".get_pagenum_link($pages)."'><i class='fas fa-angle-double-right'></i></a></li>":"";
echo "</ul>";
}
}
//评论模板
function tangstyle_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case 'pingback' :
case 'trackback' :
?>
<li id="comment-<?php comment_ID(); ?>" class="comment_li">
<?php _e( 'Pingback:', 'tangstyle' ); ?>
<?php comment_author_link(); ?>
<?php edit_comment_link( __( '(Edit)', 'tangstyle' ), '<span class="edit-link">', '</span>' ); ?>
<?php
break;
default :
global $post;
?>
<li id="comment-li-<?php comment_ID(); ?>" class="comment_li">
<div id="comment-<?php comment_ID(); ?>">
<div class="comment_top clearfix">
<div class="comment_avatar"><?php echo get_avatar( $comment, 40 );?></div>
<div class="pull-left">
<p class="comment_author"><?php printf(__('%s'), get_comment_author_link()) ?></p>
<p class="comment_time"><?php printf( __('%1$s at %2$s'), get_comment_date(), get_comment_time()) ?></p>
</div>
<div class="pull-right"><?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( '回复TA', 'tangstyle' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?> <?php edit_comment_link( __( '编辑', 'tangstyle' ), '<span class="edit_link">', '</span>' ); ?></div>
</div>
<div class="comment_text"><?php comment_text(); ?></div>
<?php if ( '0' == $comment->comment_approved ) : ?>
<p style="color:#F00;"><?php _e( '您的评论正在等待审核。', 'tangstyle' ); ?></p>
<?php endif; ?>
</div>
<?php
break;
endswitch;
}
endif;
//主题设置
function JieStyle_customize_register( $wp_customize ) {
//SEO 设置
$wp_customize->add_section('setting_seo', array(
'title' => 'SEO 设置',
'priority' => 30,
'capability' => 'edit_theme_options'
));
$wp_customize->add_setting('tang_title', array(
'default' => '它将显示在首页的 title 标签里',
'type' => 'option'
));
$wp_customize->add_control('tang_title', array(
'label' => '首页标题 Title',
'section' => 'setting_seo',
'type' => 'text'
));
$wp_customize->add_setting('tang_description', array(
'default' => '它将显示在首页的 meta 标签的 description 属性里',
'type' => 'option'
));
$wp_customize->add_control('tang_description', array(
'label' => '首页描述 Description',
'section' => 'setting_seo',
'type' => 'textarea'
));
$wp_customize->add_setting('tang_keywords', array(
'default' => '它将显示在首页的 meta 标签的 keywords 属性里,多个关键字以英文逗号隔开',
'type' => 'option'
));
$wp_customize->add_control('tang_keywords', array(
'label' => '首页关键字 KeyWords',
'section' => 'setting_seo',
'type' => 'textarea'
));
//风格设置
$wp_customize->add_section('setting_style', array(
'title' => '风格设置',
'priority' => 31,
'capability' => 'edit_theme_options'
));
$wp_customize->add_setting('tang_avatar', array(
'default' => 'https://tangjie.me/media/avatar.jpg',
'type' => 'option'
));
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'tang_avatar', array(
'label' => '头像图片链接',
'section' => 'setting_style'
)));
$wp_customize->add_setting('tang_color', array(
'default' => '#5bc0eb',
'type' => 'option'
));
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'tang_color', array(
'label' => '主题风格色调',
'section' => 'setting_style'
)));
$wp_customize->add_setting('tang_color_hover', array(
'default' => '#2980b9',
'type' => 'option'
));
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'tang_color_hover', array(
'label' => '链接二态颜色',
'section' => 'setting_style'
)));
//社交媒体设置
$wp_customize->add_section('setting_interactive', array(
'title' => '社交媒体设置',
'priority' => 32,
'capability' => 'edit_theme_options'
));
$wp_customize->add_setting('tang_rss', array(
'default' => '隐藏',
'type' => 'option'
));
$wp_customize->add_control('tang_rss', array(
'label' => '是否显示 RSS 订阅源',
'section' => 'setting_interactive',
'type' => 'select',
'choices' => array(
'隐藏' => __( '隐藏' ),
'显示' => __( '显示' )
)
));
$wp_customize->add_setting('tang_weixin', array(
'default' => '隐藏',
'type' => 'option'
));
$wp_customize->add_control('tang_weixin', array(
'label' => '是否显示微信公众号',
'section' => 'setting_interactive',
'type' => 'select',
'choices' => array(
'隐藏' => __( '隐藏' ),
'显示' => __( '显示' )
)
));
$wp_customize->add_setting('tang_weixin_img', array(
'default' => 'https://tangjie.me/media/wechat/pmtangjie.jpg',
'type' => 'option'
));
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'tang_weixin_img', array(
'label' => '公众号二维码图片',
'section' => 'setting_interactive',
)));
$wp_customize->add_setting('tang_weibo', array(
'default' => '隐藏',
'type' => 'option'
));
$wp_customize->add_control('tang_weibo', array(
'label' => '是否显示微博',
'section' => 'setting_interactive',
'type' => 'select',
'choices' => array(
'隐藏' => __( '隐藏' ),
'显示' => __( '显示' )
)
));
$wp_customize->add_setting('tang_weibo_url', array(
'default' => 'https://weibo.com',
'type' => 'option'
));
$wp_customize->add_control('tang_weibo_url', array(
'label' => '微博地址',
'section' => 'setting_interactive',
'type' => 'url'
));
$wp_customize->add_setting('tang_twitter', array(
'default' => '隐藏',
'type' => 'option'
));
$wp_customize->add_control('tang_twitter', array(
'label' => '是否显示 Twitter',
'section' => 'setting_interactive',
'type' => 'select',
'choices' => array(
'隐藏' => __( '隐藏' ),
'显示' => __( '显示' )
)
));
$wp_customize->add_setting('tang_twitter_url', array(
'default' => 'https://twitter.com',
'type' => 'option'
));
$wp_customize->add_control('tang_twitter_url', array(
'label' => 'Twitter 地址',
'section' => 'setting_interactive',
'type' => 'url'
));
$wp_customize->add_setting('tang_facebook', array(
'default' => '隐藏',
'type' => 'option'
));
$wp_customize->add_control('tang_facebook', array(
'label' => '是否显示 Facebook',
'section' => 'setting_interactive',
'type' => 'select',
'choices' => array(
'隐藏' => __( '隐藏' ),
'显示' => __( '显示' )
)
));
$wp_customize->add_setting('tang_facebook_url', array(
'default' => 'https://facebook.com',
'type' => 'option'
));
$wp_customize->add_control('tang_facebook_url', array(
'label' => 'Facebook 地址',
'section' => 'setting_interactive',
'type' => 'url'
));
$wp_customize->add_setting('tang_github', array(
'default' => '隐藏',
'type' => 'option'
));
$wp_customize->add_control('tang_github', array(
'label' => '是否显示 GitHub',
'section' => 'setting_interactive',
'type' => 'select',
'choices' => array(
'隐藏' => __( '隐藏' ),
'显示' => __( '显示' )
)
));
$wp_customize->add_setting('tang_github_url', array(
'default' => 'https://github.com',
'type' => 'option'
));
$wp_customize->add_control('tang_github_url', array(
'label' => 'GitHub 地址',
'section' => 'setting_interactive',
'type' => 'url'
));
//赞赏设置
$wp_customize->add_section('setting_dashang', array(
'title' => '赞赏设置',
'priority' => 33,
'capability' => 'edit_theme_options'
));
$wp_customize->add_setting('tang_dashang', array(
'default' => '停用',
'type' => 'option'
));
$wp_customize->add_control('tang_dashang', array(
'label' => '是否启用',
'section' => 'setting_dashang',
'type' => 'select',
'choices' => array(
'停用' => __( '停用' ),
'启用' => __( '启用' )
)
));
$wp_customize->add_setting('tang_dashang_info', array(
'default' => '如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!',
'type' => 'option'
));
$wp_customize->add_control('tang_dashang_info', array(
'label' => '想对读者说的话',
'section' => 'setting_dashang',
'type' => 'textarea'
));
$wp_customize->add_setting('tang_dashang_alipay', array(
'default' => 'https://tangjie.me/media/AliPay.png',
'type' => 'option'
));
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'tang_dashang_alipay', array(
'label' => '支付宝收款二维码',
'section' => 'setting_dashang',
)));
$wp_customize->add_setting('tang_dashang_wechat', array(
'default' => 'https://tangjie.me/media/WeixinPay.png',
'type' => 'option'
));
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'tang_dashang_wechat', array(
'label' => '微信收款二维码',
'section' => 'setting_dashang',
)));
//版权信息
$wp_customize->add_section('setting_copy', array(
'title' => '版权信息',
'priority' => 34,
'capability' => 'edit_theme_options'
));
$wp_customize->add_setting('tang_years', array(
'default' => '2011-2021',
'type' => 'option'
));
$wp_customize->add_control('tang_years', array(
'label' => '版权年份',
'section' => 'setting_copy',
'type' => 'text'
));
$wp_customize->add_setting('tang_company', array(
'default' => '唐杰',
'type' => 'option'
));
$wp_customize->add_control('tang_company', array(
'label' => '版权公司',
'section' => 'setting_copy',
'type' => 'text'
));
$wp_customize->add_setting('tang_icp', array(
'type' => 'option'
));
$wp_customize->add_control('tang_icp', array(
'label' => 'ICP 备案号',
'section' => 'setting_copy',
'type' => 'text'
));
$wp_customize->add_setting('tang_beian', array(
'type' => 'option'
));
$wp_customize->add_control('tang_beian', array(
'label' => '公安备案号',
'section' => 'setting_copy',
'type' => 'text'
));
//统计代码
$wp_customize->add_section('setting_tongji', array(
'title' => '统计代码',
'priority' => 34,
'capability' => 'edit_theme_options'
));
$wp_customize->add_setting('tang_tongji', array(
'default' => '代码在 body 页面底部或者 head 中,统计标识不会显示,但不影响统计效果',
'type' => 'option'
));
$wp_customize->add_control('tang_tongji', array(
'label' => '统计代码',
'section' => 'setting_tongji',
'type' => 'textarea'
));
$wp_customize->add_setting('tang_tongji_position', array(
'default' => 'body',
'type' => 'option'
));
$wp_customize->add_control('tang_tongji_position', array(
'label' => '放置位置',
'section' => 'setting_tongji',
'type' => 'select',
'choices' => array(
'body' => __( 'body' ),
'head' => __( 'head' )
)
));
}
add_action( 'customize_register', 'jieStyle_customize_register' );
?>