-
Notifications
You must be signed in to change notification settings - Fork 28
/
functions.php
326 lines (291 loc) · 9.75 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
<?php
// SNS 数据定义, 此数组顺序对应输出顺序
$clrs_sns = array(
"profile" => "个人页",
"gplus" => "Google+",
"twitter" => "Twitter",
"fb" => "Facebook",
"weibo" => "SinaWeibo",
"qqw" => "QQ",
"github" => "Github",
"telegram" => "Telegram",
);
// 给 style.css 添加修改时间, 解决缓存问题
add_filter('stylesheet_uri', function () {
echo get_template_directory_uri() . '/style.css?t=' . filemtime(get_stylesheet_directory() . '/style.css');
});
// 加载语言包
add_action('after_setup_theme', function () {
load_theme_textdomain('clrs', get_template_directory() . '/lang');
});
// 添加设置菜单
add_action('wp_before_admin_bar_render', function () {
global $wp_admin_bar;
$wp_admin_bar->add_menu(array(
'parent' => false,
'id' => 'theme_setting',
'title' => __('Clearision 调教', 'dpt'),
'href' => admin_url('themes.php?page=clrs_menu'),
));
});
// 自定义标题
add_filter('wp_title', function ($title, $sep) {
if (!is_feed()) {
$title .= get_bloginfo('name');
$description = get_bloginfo('description', 'display');
if ($description && (is_home() || is_front_page())) {
$title .= " $sep $description";
}
global $paged, $page;
if ($paged >= 2 || $page >= 2) {
$title .= " $sep " . sprintf(__('页面 %s', 'clrs'), max($paged, $page));
}
}
return $title;
}, 10, 2);
// 加载文章格式支持
add_theme_support('post-formats', array('quote', 'status'));
// add_theme_support( 'post-formats', array( 'image', 'video' ) );
// 加载菜单设置
register_nav_menus(array(
'main' => __('主菜单', 'clrs'),
'next' => __('辅助链接', 'clrs')
));
// 加载小工具设置
if (function_exists('register_sidebar')) {
register_sidebar(
array(
'name' => __('底部栏1', 'clrs'),
'id' => 'one',
'description' => '底部的工具栏',
'class' => 'side_col',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>',
)
);
register_sidebar(
array(
'name' => __('底部栏2', 'clrs'),
'id' => 'two',
'description' => '底部的工具栏',
'class' => 'side_col',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>',
)
);
}
// 检测主题更新
if (get_option('clrs_upcl') != "0") {
require_once(get_template_directory() . '/func/theme-update-checker.php');
new ThemeUpdateChecker(
'Clearision',
'http://work.dimpurr.com/theme/clearision/update/info.json'
);
}
if (!function_exists('clrs_comment')) {
// TODO: 重构 UserAgent 检测
require(get_template_directory() . '/func/wp-useragent.php');
function clrs_pingback()
{
?>
<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
<p>
Pingback
<?php
comment_author_link();
edit_comment_link('编辑', '<span class="edit-link">', '</span>');
?>
</p>
<?php
}
function clrs_comment($comment, $args, $depth)
{
$GLOBALS['comment'] = $comment;
if ($comment->comment_type == 'pingback' || $comment->comment_type == 'trackback') {
clrs_pingback($comment);
return;
}
global $post;
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<article id="comment-<?php comment_ID(); ?>" class="comment">
<header class="comment-meta comment-author vcard">
<?= get_avatar($comment, 44) ?>
<div class="cmt_meta_head">
<cite class="fn">
<?= get_comment_author_link() ?>
<?php
if ($comment->user_id === $post->post_author) {
echo ('<span class="cmt_meta_auth"> ' . __('文章作者', 'clrs') . '</span>');
}
?>
</cite>
</div>
<span class="cmt_meta_time">
<a href="<?= esc_url(get_comment_link($comment->comment_ID)) ?>">
<time datetime="<?= get_comment_time('c') ?>"><?= get_comment_date() . ' ' . get_comment_time() ?></time>
</a>
</span>
<?php
if (get_option('clrs_wbos')) {
echo '<a href="javascript:void(0)" class="cmt_ua_a">';
clrs_wp_useragent();
echo '</a>';
}
?>
</header>
<?php
if ($comment->comment_approved == '0') {
echo ('<p class="comment-awaiting-moderation">' . __('你的评论正在等待和谐审查', 'clrs') . '</p>');
}
?>
<section class="comment-content comment">
<?php
comment_text();
edit_comment_link(__('编辑', 'clrs'), '<span class="edit-link">', '</span>');
if (current_user_can('level_5')) {
echo ('<a class="comment-edit-link" href="' . admin_url("comment.php?action=cdc&c=" . get_comment_ID()) . '">删除</a> ');
}
comment_reply_link(array_merge($args, array(
'after' => '',
'depth' => $depth,
'max_depth' => $args['max_depth'],
'reply_text' => __('回复', 'clrs')
)));
?>
</section>
</article>
<?php
}
}
// 后台设置页面
add_action('admin_menu', function () {
add_theme_page(
__('Clearision 调教', 'clrs'),
__('Clearision 调教', 'clrs'),
'administrator',
'clrs_menu',
'clrs_config'
);
});
function clrs_config()
{
global $clrs_sns;
static $options = array(
'clrs_opct', 'clrs_opbg',
'clrs_logo', 'clrs_favi',
'clrs_wbos', 'clrs_adis', 'clrs_ldis', 'clrs_upcl',
'clrs_tongji', 'clrs_style'
);
if (isset($_POST['options_save'])) {
if (!wp_verify_nonce($_POST['options_save'], 'clearision-save')) {
_e('Nonce 校验失败, 设置未保存, 请刷新重试', 'clrs');
return;
}
foreach ($options as $opt) {
update_option($opt, stripslashes($_POST[$opt]));
}
foreach (array_keys($clrs_sns) as $key) {
$clrs_sopt = 'clrs_s_' . $key;
update_option($clrs_sopt, stripslashes($_POST[$clrs_sopt]));
}
}
$title = function ($title) {
echo ('<h3>' . __($title, 'clrs') . '</h3>');
};
$radio = function ($option, $yes, $no) {
$status = get_option($option);
echo ('<input type="radio" name="' . $option . '" value="1" required' . ($status ? ' checked="true"' : '') . '>' . __($yes, 'clrs') . '</input> ' .
'<input type="radio" name="' . $option . '" value="0" required' . ($status ? '' : ' checked="true"') . '>' . __($no, 'clrs') . '</input><br>');
};
$textbox = function ($option, $hint, $upload = false) {
echo ('<input type="text" size="80" name="' . $option . '" placeholder="' . __($hint, 'clrs') . '" value="' . get_option($option) . '" />' .
($upload ? '<input type="button" upload_for="' . $option . '" value="' . __('上传', 'clrs') . '" />' : '') . '<br>');
};
$textfield = function ($option, $hint) {
echo ('<textarea name="' . $option . '" rows="10" cols="60" placeholder="' . __($hint, 'clrs') . '" style="font-size: 14px; font-family: Consolas, monospace, sans-serif, sans">' . get_option($option) . '</textarea><br>');
};
echo ('<h1>' . __('Clearision 主题设置', 'clrs') . '</h1>' .
'<form method="post" name="clrs_form" id="clrs_form">' .
'<h3><a href="http://blog.dimpurr.com/clearison">Clearison 主题专页→</a></h3>');
$title('模版样式:');
$radio('clrs_opct', '透明清新', '灰色素雅');
echo ('<br>');
$textbox('clrs_opbg', '透明样式下的页面背景,留空为默认。粘贴链接或点击上传', true);
$title('LOGO头像:');
$textbox('clrs_logo', '粘贴链接或点击上传', true);
echo ('<br><img src="' . get_option('clrs_logo') . '" style="max-width: 114px; -webkit-border-radius: 500px; -moz-border-radius: 500px; border-radius: 500px;" />');
$title('网站图标:');
$textbox('clrs_favi', '输入 ICO/PNG 图标链接,留空调用根目录 favicon.ico', true);
$title('社交图标');
echo (__('请输入完整的 URL (包括 https://)', 'clrs') . '<br>');
foreach ($clrs_sns as $key => $val) {
$clrs_sopt = 'clrs_s_' . $key;
echo '<input type="text" size="80" name="' . $clrs_sopt . '" id="' . $clrs_sopt . '" placeholder="' . $val . '" value="' . get_option($clrs_sopt) . '"/>';
}
wp_enqueue_style('thickbox');
wp_enqueue_script('thickbox');
$title('访客环境:');
$radio('clrs_wbos', '显示', '不显示');
$title('文章作者:');
$radio('clrs_adis', '显示', '不显示');
$title('友情链接:');
$radio('clrs_ldis', '显示', '不显示');
$title('检查更新:');
$radio('clrs_upcl', '启用', '关闭');
_e('可以应对服务器设置导致的无限提示更新问题。需要更新时请手动打开此开关', 'clrs');
$title('统计代码:');
$textfield('clrs_tongji', '输入网站统计代码');
$title('自定义样式:');
$textfield('clrs_style', '输入 CSS 代码,以便更新时不会被覆盖');
wp_nonce_field('clearision-save', 'options_save');
$title('提交更改:');
?>
<input type="hidden" name="page_options" value="clrs_copy_right" />
<input type="submit" value="<?= __('保存全部设置', 'clrs') ?>" />
<style>
input[type="text"] {
max-width: 510px;
}
textarea {
font-size: 14px;
font-family: Consolas, monospace, sans-serif, sans;
}
</style>
<script type="text/javascript">
$(function() {
$('[upload_for]').click(function() {
window.upload_target = $(this).attr('upload_for');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
});
window.send_to_editor = function(html) {
$('[name=' + window.upload_target + ']').val($(html).attr('src'));
tb_remove();
}
});
<?php
// 模板使用统计代码, 仅提交站点 URL, 希望保留 :)
$statistics_code = '$(function() {
$.get("http://work.dimpurr.com/theme/theme_tj.php?theme_name=Clearision&blog_url=' . get_bloginfo('url') . '&t=" + new Date().getTime());
});';
if (!get_option('clrs_fitj')) {
// 首次安装
update_option('clrs_fitj', true);
} else if (date('d') != '01') {
// 仅每月 1 号统计
update_option('clrs_dayv', false);
$statistics_code = '';
} else if (!get_option('clrs_dayv')) {
// 并且仅统计一次
update_option('clrs_dayv', true);
}
echo ($statistics_code);
?>
</script>
</form>
<?php
}