forked from WuPlus/WP-Theme-Purity
/
functions.php
executable file
·448 lines (394 loc) · 19 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
<?php
register_nav_menus();
$args = array(
'class' => '',
'before_widget' => '<li id="%1$s" class="my_widget">',
'after_widget' => '</li>',
'before_title' => '<h2 class="right_siderbar_subhead">',
'after_title' => '</h2>' );
register_sidebar($args);
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 250, 250,ture);
//page index
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 = 1; // only edit this if you want to show more page-links
$showitems = ($range * 2)+1;
$pages = ceil($total_posts/$posts_per_page);
if(1 != $pages){
echo "<ul class='pure-paginator'>";
echo "<li><a class='pure-button prev' href='".get_pagenum_link(1)."'>«</a></li>";
echo ($paged > 1 && $showitems < $pages)? "<li><a class='pure-button prev' href='".get_pagenum_link($prev)."'>‹</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><a class='pure-button pure-button-active' href='".get_pagenum_link($i)."'>".$i."</a></li>":"<li><a class='pure-button' href='".get_pagenum_link($i)."'>".$i."</a></li>";
}
}
echo ($paged < $pages && $showitems < $pages) ? "<li><a class='pure-button next' href='".get_pagenum_link($next)."'>›</a></li>" :"";
echo "<li><a class='pure-button next' href='".get_pagenum_link($pages)."'>»</a></li>";
echo "</ul>\n";
}
}
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0";
}
return $count;
}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
function get_post_meta_info($postID){
$temp1 = "分类";
$categories = get_the_category($postID);
if($categories != ''){
foreach ($categories as $category ) {
$temp1 .= "<a class='post-category post-category-design' >".$category->name."</a>";
}
}
echo $temp1;
$temp2 = "标签";
$count = 0;
$tags = get_the_tags($postID);
if($tags == ''){
$temp2 = "";
}else{
foreach ($tags as $tag ) {
switch ($count%3) {
case 0:
$temp2 .= "<a class='post-category post-category-pure' >".$tag->name."</a>";
break;
case 1:
$temp2 .= "<a class='post-category post-category-yui' >".$tag->name."</a>";
break;
case 2:
$temp2 .= "<a class='post-category post-category-js' >".$tag->name."</a>";
break;
}
$count++;
if ($count > 6) {
break;
}
}
}
echo $temp2;
}
add_action( 'widgets_init', 'my_unregister_widgets' );
function my_unregister_widgets() {
register_widget( 'purity_fixed_ad_Widget' );
register_widget( 'purity_unfixed_ad_Widget' );
register_widget('My_Widget_Recent_Comments');
unregister_widget( 'WP_Widget_RSS' );
unregister_widget( 'WP_Widget_Recent_Comments' );
unregister_widget( 'WP_Widget_Archives' );
unregister_widget( 'WP_Widget_Search' );
unregister_widget( 'WP_Widget_Meta');
}
function purity_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case 'pingback' :
case 'trackback' :
// Display trackbacks differently than normal comments.
?>
<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
<p><?php comment_author_link(); ?> <?php edit_comment_link( '编辑', '<span class="edit-link">', '</span>' ); ?></p>
<?php
break;
default :
// Proceed with normal comments.
global $post;
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<article id="comment-<?php comment_ID(); ?>" class="comment">
<?php echo get_avatar( $comment, 50);?>
<div class="comment-meta">
<?php
printf( '<b class="fn">%1$s</b> %2$s',
get_comment_author_link(),
// If current post author is also comment author, make it known visually.
( $comment->user_id === $post->post_author ) ? '<span>' . 楼主 . '</span>' : ''
);
printf( '<a href="%1$s"><time datetime="%2$s">%3$s</time></a>',
esc_url( get_comment_link( $comment->comment_ID ) ),
get_comment_time( 'c' ),
/* translators: 1: date, 2: time */
sprintf('%1$s', get_comment_date())
);
?>
</div><!-- .comment-meta -->
<?php if ( '0' == $comment->comment_approved ) : ?>
<p class="comment-awaiting-moderation">您的评论正在等待审核</p>
<?php endif; ?>
<section class="comment-content">
<?php comment_text(); ?>
</section><!-- .comment-content -->
<div class="reply">
<?php edit_comment_link( '编辑', '<button class="pure-button pure-button-small pure-button-secondary">', '</button>' ); ?>
<?php comment_reply_link( array_merge( $args, array( 'reply_text' => '回复', 'class'=>'here', 'before' => '<button class="pure-button pure-button-xsmall">','after' => '</button>', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div><!-- .reply -->
</article><!-- #comment-## -->
<?php
break;
endswitch; // end comment_type check
}
function purity_comment_form(){
$args = array(
'id_form' => 'commentform',
'class_fomr' => 'pure-form',
'id_submit' => 'submit',
'title_reply' => __( 'Leave a Reply' ),
'title_reply_to' => __( 'Leave a Reply to %s' ),
'cancel_reply_link' => __( 'Cancel Reply' ),
'label_submit' => __( 'Post Comment' ),
'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) .
'</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true">' .
'</textarea></p>',
'comment_notes_before' => '<p class="comment-notes">' .
__( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) .
'</p>',
'comment_notes_after' => '',
'fields' => apply_filters( 'comment_form_default_fields', array(
'author' =>
'<p class="comment-form-author">' .
( $req ? '<span class="required">*</span>' : '' ) .
'<input id="author" class="reply_box" name="author" type="text" placeholder="姓名(必填)" value="' . esc_attr( $commenter['comment_author'] ) .
'" size="30"' . $aria_req . ' /></p>',
'email' =>
'<p class="comment-form-email">' .
( $req ? '<span class="required">*</span>' : '' ) .
'<input id="email" class="reply_box" name="email" type="text" placeholder="邮箱(必填)" value="' . esc_attr( $commenter['comment_author_email'] ) .
'" size="30"' . $aria_req . ' /></p>',
'url' =>
'<p class="comment-form-url">' .
'<input id="url" class="reply_box" name="url" type="text" placeholder="主页(选填)" value="' . esc_attr( $commenter['comment_author_url'] ) .
'" size="30" /></p>'
)
),
'label_submit'=>'发表评论',
'comment_field' => '<p class="comment-form-comment">' . '<textarea id="commentbox" placeholder="好好评论,天天向上" name="comment" rows="8" aria-required="true"></textarea></p>',
);
comment_form($args);
}
class purity_fixed_ad_Widget extends WP_Widget {
function purity_fixed_ad_Widget() {
// Instantiate the parent object
$widget_ops = array( 'classname' => 'advertisement',
'description' => '固定广告组件' );
$this->WP_Widget(false, '固定广告组件', $widget_ops );
}
function widget( $args, $instance ) {
// Widget output
extract( $args, EXTR_SKIP );
//初始化参数
$title = empty($instance['title'])?'广告':$instance['title'];
$content = empty($instance['content'])?'':$instance['content'];
$content_attr=esc_attr(strip_tags($content));
//输出结构
?>
<li class="my_widget" id="advertisment">
<h2 class="right_siderbar_subhead"><?=$title?></h2>
<div style='width:100%;height:180px;'><?=$content?></div>
</li>
<?php
}
function update( $new_instance, $old_instance ) {
// Save widget options
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['content'] = $new_instance['content'];
return $instance;
}
function form( $instance ) {
// Output admin widget options form
$title=isset($instance['title'])?esc_attr($instance['title']):'';
$title_name=esc_attr($this->get_field_name('title'));
$content=isset($instance['content'])?esc_attr($instance['content']):'';
$content_name=esc_attr($this->get_field_name('content'));
?>
<p>
<b>说明</b>
<br />
<input style="width:100%" name="<?=$title_name?>" type="text" value="<?=$title?>" />
</p>
<p>
<b>广告内容</b>
<br />
<textarea style="width:100%" name="<?=$content_name?>"><?=$content?></textarea>
</p>
<?php
}
}
class purity_unfixed_ad_Widget extends WP_Widget {
function purity_unfixed_ad_Widget() {
// Instantiate the parent object
$widget_ops = array( 'classname' => 'advertisement',
'description' => '滑动广告组件' );
$this->WP_Widget(false, '滑动广告组件', $widget_ops );
}
function widget( $args, $instance ) {
// Widget output
extract( $args, EXTR_SKIP );
//初始化参数
$title = empty($instance['title'])?'广告':$instance['title'];
$content = empty($instance['content'])?'':$instance['content'];
$content_attr=esc_attr(strip_tags($content));
//输出结构
?>
<div class="broad">
<h2 class="right_siderbar_subhead">广而告之</h2>
<div style='width:100%;height:180px;'><?=$content?></div>
</div>
<?php
}
function update( $new_instance, $old_instance ) {
// Save widget options
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['content'] = $new_instance['content'];
return $instance;
}
function form( $instance ) {
// Output admin widget options form
$title=isset($instance['title'])?esc_attr($instance['title']):'';
$title_name=esc_attr($this->get_field_name('title'));
$content=isset($instance['content'])?esc_attr($instance['content']):'';
$content_name=esc_attr($this->get_field_name('content'));
?>
<p>
<b>说明</b>
<br />
<input style="width:100%" name="<?=$title_name?>" type="text" value="<?=$title?>" />
</p>
<p>
<b>广告内容</b>
<br />
<textarea style="width:100%" name="<?=$content_name?>"><?=$content?></textarea>
</p>
<?php
}
}
class My_Widget_Recent_Comments extends WP_Widget_Recent_Comments {
/**
* 构造方法,主要是定义小工具的名称,介绍
*/
function My_Widget_Recent_Comments() {
$widget_ops = array('classname' => 'my_widget_recent_comments', 'description' => __('显示最新评论内容'));
$this->WP_Widget('my-recent-comments', __('评论', 'my'), $widget_ops);
}
/**
* 小工具的渲染方法,这里就是输出评论
*/
function widget($args, $instance) {
global $wpdb, $comments, $comment;
$cache = wp_cache_get('my_widget_recent_comments', 'widget');
if (!is_array($cache))
$cache = array();
if (!isset($args['widget_id']))
$args['widget_id'] = $this->id;
if (isset($cache[$args['widget_id']])) {
echo $cache[$args['widget_id']];
return;
}
extract($args, EXTR_SKIP);
$output = '';
$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title'], $instance, $this->id_base);
if (empty($instance['number']) || !$number = absint($instance['number']))
$number = 5;
//获取评论,过滤掉管理员自己
$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE user_id !=2 and comment_approved = '1' and comment_type not in ('pingback','trackback') ORDER BY comment_date_gmt DESC LIMIT $number");
$output .= $before_widget;
if ($title)
$output .= $before_title . $title . $after_title;
$output .= '<ul id="myrecentcomments">';
if ($comments) {
// Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
$post_ids = array_unique(wp_list_pluck($comments, 'comment_post_ID'));
_prime_post_caches($post_ids, strpos(get_option('permalink_structure'), '%category%'), false);
foreach ((array) $comments as $comment) {
//头像
$avatar = get_avatar($comment, 35);
//作者名称
$author = get_comment_author();
//评论内容
$content = apply_filters('get_comment_text', $comment->comment_content);
$content = mb_strimwidth(strip_tags($content), 0, '65', '...', 'UTF-8');
$content = convert_smilies($content);
//评论的文章
$post = '<a href="' . esc_url(get_comment_link($comment->comment_ID)) . '">' . get_the_title($comment->comment_post_ID) . '</a>';
//这里就是输出的html,可以根据需要自行修改
$output .= '<li class="recent_comment" >
<div class="recent-comment-avatar" >' . $avatar . '</div>
<div class="recent-comment-meta" >
<p class="comment-author"><span class="fn">' . $author . '</span><span class="says">发表在 ' . $post . '</span></p>
</div>
<div class="recent-comment-content"><p class="last">' . $content . '</p></div>
</li>';
}
}
$output .= '</ul>';
$output .= $after_widget;
echo $output;
$cache[$args['widget_id']] = $output;
wp_cache_set('my_widget_recent_comments', $cache, 'widget');
}
}
function comment_mail_notify($comment_id) {
$admin_notify = '1'; // admin 要不要收回复通知 ( '1'=要 ; '0'=不要 )
$admin_email = get_bloginfo ('admin_email'); // $admin_email 可改为你指定的 e-mail.
$comment = get_comment($comment_id);
$comment_author_email = trim($comment->comment_author_email);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
global $wpdb;
if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
$wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
$wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
$notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
$spam_confirmed = $comment->comment_approved;
if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
$wp_email = 'no-reply@' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
$message = '
<div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px;">
<p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
<p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
. trim(get_comment($parent_id)->comment_content) . '</p>
<p>' . trim($comment->comment_author) . ' 给您的回复:<br />'
. trim($comment->comment_content) . '<br /></p>
<p>您可以点击查看回复的完整內容</p>
<p>还要再度光临 ' . get_option('blogname') . '</p>
<p>(此邮件由系统自动发送,请勿回复.)</p>
</div>';
$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
wp_mail( '435224787@qq.com', 'hello', 'test', $headers );
}
}
add_action('comment_post', 'comment_mail_notify');
function add_checkbox() {
echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="margin-left:20px;" /><label for="comment_mail_notify">有人回复时邮件通知我</label>';
}
add_action('comment_form', 'add_checkbox');
?>