-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
functions.php
488 lines (444 loc) · 14 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
<?php
require get_template_directory() . '/inc/setting.php';
//setting
require get_template_directory() . '/inc/views.php';
//views
require get_template_directory() . '/inc/rewrite.php';
//页面伪静态
require get_template_directory() . '/inc/metabox.php';
//文章meta
//获取评论人数 by wpdaxue
function get_comments_users($postid = 0, $which = 0)
{
$comments = get_comments('status=approve&type=comment&post_id=' . $postid);
//获取文章的所有评论
if ($comments) {
$i = 0;
$j = 0;
$commentusers = array();
foreach ($comments as $comment) {
++$i;
if ($i == 1) {
$commentusers[] = $comment->comment_author_email;
++$j;
}
if (!in_array($comment->comment_author_email, $commentusers)) {
$commentusers[] = $comment->comment_author_email;
++$j;
}
}
$output = array($j, $i);
$which = ($which == 0) ? 0 : 1;
return $output[$which];
//返回评论人数
}
return 0;
//没有评论返回0
}
//自动获取关键字 by suxing
function deel_keywords()
{
global $s, $post;
//声明$post全局变量
$keywords = '';
if (is_single()) {
//if ( get_the_tags( $post->ID ) ) {
//foreach ( get_the_tags( $post->ID ) as $tag ) $keywords .= $tag->name . ', ';
//}<br> //如果文章有标签,关键词为标签
$category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
//echo $parent;//得到父级分类名称
foreach (get_the_category($post->ID) as $category) $keywords .= $parent . ',' . $category->cat_name . ', ' . get_the_title();
//关键词为父级分类名称,分类名称,文章标题
//下面判断条件为不同模板
$keywords = substr_replace($keywords, '', -2);
}
if ($keywords) {
echo "<meta name=\"keywords\" content=\"$keywords\">\n";
}
}
//关键字加入head头部代码
add_action('wp_head', 'deel_keywords');
//文章内时间以ago显示 by someone
function lb_time_since($older_date, $comment_date = false)
{
$chunks = array(
array(24 * 60 * 60, ' 天前'),
array(60 * 60, ' 小时前'),
array(60, ' 分钟前'),
array(1, ' 秒前')
);
$newer_date = time();
$since = abs($newer_date - $older_date);
if ($since < 30 * 24 * 60 * 60) {
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
if (($count = floor($since / $seconds)) != 0) {
break;
}
}
$output = $count . $name;
} else {
$output = $comment_date ? date('y-m-d', $older_date) : date('Y-m-d', $older_date);
}
return $output;
}
//后台提示信息
function remove_footer_admin()
{
echo 'Theme <b style="letter-spacing:1px;">Tony</b> | Designed with <img draggable="false" class="emoji" alt="love" src="https://s.w.org/images/core/emoji/11/svg/2764.svg"> by <a href="https://www.ouorz.com" target="_blank">TonyHe</a> | 当前 <a href="https://github.com/HelipengTony/tony/releases" target="_blank">' . wp_get_theme()->get('Version') . '</a> 版本</p>';
}
add_filter('admin_footer_text', 'remove_footer_admin');
// 评论添加@ by someone
function comment_add_at($comment_text, $comment = '')
{
if ($comment->comment_parent > 0) {
$comment_text = '<span class="comment-at">@' . get_comment_author($comment->comment_parent) . '</span>' . $comment_text;
}
return $comment_text;
}
add_filter('comment_text', 'comment_add_at', 20, 2);
//插入文章内链 by suxing
//添加按钮
add_action('media_buttons_context', 'mee_insert_post_custom_button');
function mee_insert_post_custom_button($_var)
{
$_var .= '<button type="button" id="insert-media-button" class="button insert-post-embed" data-editor="content"><span class="dashicons dashicons-pressthis"></span>插入文章</button><div class="smilies-wrap"></div><script>jQuery(document).ready(function(){jQuery(document).on("click", ".insert-post-embed",function(){var post_id=prompt("输入文章ID,多个文章,使用英文逗号隔开","");if (post_id!=null && post_id!=""){send_to_editor("[fa_insert_post ids="+ post_id +"]");}return false;});});</script>';
return $_var;
}
//插入文章
function fa_insert_posts($atts, $content = null)
{
extract(shortcode_atts(
array(
'ids' => ''
),
$atts
));
global $post;
$content = '';
$postids = explode(',', $ids);
$inset_posts = get_posts(array('post__in' => $postids));
foreach ($inset_posts as $key => $post) {
setup_postdata($post);
$content .= '<div class="warp-post-embed"><a style="text-decoration: none;" href="' . get_permalink() . '" target="_blank" ><div class="embed-content"><h2>' . get_the_title() . '</h2><p>' . wp_trim_words(get_the_excerpt(), 30) . '</p></div></a></div>';
}
wp_reset_postdata();
return $content;
}
add_shortcode('fa_insert_post', 'fa_insert_posts');
//去除抖动
function wps_login_error()
{
remove_action('login_head', 'wp_shake_js', 12);
}
add_action('login_head', 'wps_login_error');
//错误信息
function failed_login()
{
return '密码不正确';
}
add_filter('login_errors', 'failed_login');
//防止黑客
//去除版本信息
function remove_version()
{
return '';
}
add_filter('the_generator', 'remove_version');
//去除评论隐患
function lxtx_comment_body_class($content)
{
$pattern = "/(.*?)([^>]*)author-([^>]*)(.*?)/i";
$replacement = '$1$4';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('comment_class', 'lxtx_comment_body_class');
add_filter('body_class', 'lxtx_comment_body_class');
//add post thumbnails
if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails');
}
//根据上传时间重命名文件
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter');
function custom_upload_filter($file)
{
$info = pathinfo($file['name']);
$ext = $info['extension'];
$filedate = date('YmdHis') . rand(10, 99);
//为了避免时间重复,再加一段2位的随机数
$file['name'] = $filedate . '.' . $ext;
return $file;
}
add_filter('wp_image_editors', 'change_graphic_lib');
function change_graphic_lib($array)
{
return array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
}
/* WordPress REST API 接口添加 */
add_action('rest_api_init', 'wp_rest_insert_tag_links');
function wp_rest_insert_tag_links()
{
register_rest_field(
'post',
'post_categories',
array(
'get_callback' => 'wp_rest_get_categories_links',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'post',
'post_excerpt',
array(
'get_callback' => 'wp_rest_get_plain_excerpt',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'post',
'post_date',
array(
'get_callback' => 'wp_rest_get_normal_date',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'page',
'post_date',
array(
'get_callback' => 'wp_rest_get_normal_date',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'post',
'post_metas',
array(
'get_callback' => 'get_post_meta_for_api',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'page',
'post_metas',
array(
'get_callback' => 'get_post_meta_for_api',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'post',
'post_img',
array(
'get_callback' => 'get_post_img_for_api',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'post',
'post_tags',
array(
'get_callback' => 'get_post_tags_for_api',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'post',
'post_prenext',
array(
'get_callback' => 'get_post_prenext_for_api',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field('post',
'md_content',
array(
'get_callback' => 'get_post_content_for_api',
'update_callback' => null,
'schema' => null,
)
);
}
function get_post_content_for_api($post) {
$content = get_post($post['id'])->post_content;
return $content;
}
function wp_rest_get_categories_links($post)
{
$post_categories = array();
$categories = wp_get_post_terms($post['id'], 'category', array('fields' => 'all'));
foreach ($categories as $term) {
$term_link = get_term_link($term);
if (is_wp_error($term_link)) {
continue;
}
$post_categories[] = array('term_id' => $term->term_id, 'name' => $term->name, 'link' => $term_link);
}
return $post_categories;
}
function wp_rest_get_plain_excerpt($post)
{
$excerpts = array();
$excerpts['nine'] = wp_trim_words(get_the_excerpt($post['id']), 90);
$excerpts['four'] = wp_trim_words(get_the_excerpt($post['id']), 45);
return $excerpts;
}
function wp_rest_get_normal_date($post)
{
if (get_option('king_date_format'))
$format = get_option('king_date_format');
else $format = 'd-m-y';
$date = get_the_date($format, $post['id']);
return $date;
}
function get_post_meta_for_api($post)
{
$post_meta = array();
$post_meta['views'] = get_post_meta($post['id'], 'post_views_count', true);
$post_meta['author'] = get_user_by('ID', $post['author'])->display_name;
$post_meta['link'] = get_post_meta($post['id'], 'link', true);
$post_meta['img'] = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
$post_meta['title'] = get_the_title($post['id']);
$tagsss = get_the_tags($post['id']);
$post_meta['tag_name'] = $tagsss[0]->name;
return $post_meta;
}
function get_post_img_for_api($post)
{
$post_img = array();
$post_img['url'] = get_the_post_thumbnail_url($post['id']);
return $post_img;
}
/* WordPress REST API 接口添加 */
//获取全部分类
function admin_show_category()
{
global $wpdb;
$request = "SELECT $wpdb->terms.term_id, name FROM $wpdb->terms ";
$request .= " LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id ";
$request .= " WHERE $wpdb->term_taxonomy.taxonomy = 'category' ";
$request .= " ORDER BY term_id asc";
$categorys = $wpdb->get_results($request);
echo "<h3>全部分类及ID</h4><ul style='height:50px'>";
foreach ($categorys as $category) {
echo '<li style="margin-right: 10px;float:left;">' . $category->name . "(<code>" . $category->term_id . '</code>)</li>';
}
echo "</ul>";
}
//设置站点title
function site_page_title()
{
if (is_home()) {
bloginfo('name');
echo " - ";
bloginfo('description');
} elseif (is_category()) {
single_cat_title();
echo " - ";
bloginfo('name');
} elseif (is_single() || is_page()) {
single_post_title();
} elseif (is_search()) {
echo "搜索结果";
echo " - ";
bloginfo('name');
} elseif (is_404()) {
echo '没有找到页面';
} else {
wp_title('', true);
}
}
//默认头像
add_filter('avatar_defaults', 'newgravatar');
function newgravatar($avatar_defaults)
{
$myavatar = 'https://i.loli.net/2019/03/10/5c84adf73a7a2.jpg';
$avatar_defaults[$myavatar] = "Tony主题默认头像";
return $avatar_defaults;
}
//替换 Gravatar 头像为国内源
function tony_get_avatar($avatar)
{
$avatar = preg_replace("/http:\/\/(www|\d).gravatar.com/", "https://gravatar.loli.net", $avatar);
return $avatar;
}
add_filter('get_avatar', 'tony_get_avatar');
//标题描述
function get_tony_ms()
{
if (get_option('king_ms') && !get_option('king_title_ms')) {
echo get_option('king_ms');
} elseif (get_option('king_title_ms')) {
echo get_option('king_title_ms');
} else {
echo '未设置描述';
}
}
//获取文章标签
function get_post_tags_for_api($post)
{
$tag_term = array();
$tags = wp_get_post_tags($post['id']);
$i = 0;
foreach ($tags as $tag) {
$tag_term[$i]['url'] = get_tag_link($tag->term_id);
$tag_term[$i]['name'] = $tag->name;
$i++;
}
return $tag_term;
}
//获取上下篇文章
function get_post_prenext_for_api($post)
{
$array = array();
$prev_post = get_previous_post(false, '');
$next_post = get_next_post(false, '');
$array['prev'][0] = $prev_post->guid;
$array['prev'][1] = $prev_post->post_title;
$array['prev'][2] = wp_get_post_categories($prev_post->ID)[0];
$array['next'][0] = $next_post->guid;
$array['next'][1] = $next_post->post_title;
$array['next'][2] = wp_get_post_categories($next_post->ID)[0];
return $array;
}
//Tony 主题内置判断
function tony_func($key)
{
switch ($key) {
case 'echo_s_cate':
if (get_option('king_cate_cate')) {
echo get_option('king_cate_cate');
} else {
echo '0';
}
break;
case 'echo_ph_cate':
if (get_option('king_cate_cate_ph')) {
echo get_option('king_cate_cate_ph');
} else {
echo 'XX';
}
break;
case 'echo_logo':
if (get_option('king_logo')) {
echo get_option('king_logo');
} else {
echo 'https://static.ouorz.com/t.jpg';
}
break;
default:
break;
}
}