Home > Technology, WordPress > 修正热情评论员

修正热情评论员

热情评论员是我在wordpress主题中添加的一个小功能.通过判断评论者E_mail在设置的指定时间段出现的次数.到达要求的给个小小的五角星提示.我博客上的设置是30天内评论超过10条留言就给予这个称号,在这里留言的人都是老朋友了,所以你会看到大部分人都有这个.

这个东西的功能倒是简单.我也写了教程.具体的实现办法.你可以移步看看.但是要查数据库,很麻烦.涉及到查询次数和效率的问题.其实我早就想到这个问题了,只是当时我的博客访问量很少,评论也不多.但是.现在自从我发布了主题后,IP和流量都上去了点.所以过多的查询必然会对数据库造成比较大的压力.想想如果一个页面有80条评论.那么要查询80次加wordpress本身的查询等.那估计得有100多次了.真是恐怖.

所以我今天做了些改进.将查询的数据写到缓存的表里面,及option表里,这个和主题的选项一样.所有的个人数据都在那里.wordpress在加载的时候会默认的加载.这样个函数就不需要在去另外查数据库了.在这个函数的后面再用个wordpress Hook更新数据.

原理弄清楚了.看看代码:

<?php
//获得缓存的数据并作出判断
function get_xing($comment_email){
//获得主题后台设置
$options = get_option('philna2_options');
//获取热情评论缓存
$result=get_option('philna_commenter_star');
	//为空.从新查数据库.重获缓存.
	if(empty($result)){
	philna_commenter_star($options['xing_limit_days']);
	$result=get_option('philna_commenter_star');
	}
	//判断输出.
	$times=$result[$comment_email];
	if($times>=$options['xing_limit_cm']){
	$style= ' style'."=".'"cursor:pointer;"';
	$output =" ";
	$output .='<img src='.'"'.get_bloginfo('template_url').'/i/star.gif" alt="Enthusiastic commentator! This guy comment '.$times.' times in'.$options['xing_limit_days'].'days . 	
	So get this reward" '.' title="'.'Enthusiastic commentator! This guy comment '.$times.' times in '.$options['xing_limit_days'].' days. So get this reward" />';
	echo $output;
	}else return;
}
//查询并缓存函数
function philna_commenter_star( $limit_days=30){
global $wpdb, $tablecomments;
//获取后台限制条件并查表.
$options = get_option('philna2_options');
$limit_days=$options['xing_limit_days'];
$sql=$wpdb->get_results(
	"SELECT comment_author_email, count( comment_author_email ) AS cmtcount
	FROM  $tablecomments
	WHERE comment_approved =1
	AND comment_type = ''
	AND TO_DAYS( now( ) )-TO_DAYS(`comment_date`)<$limit_days
	GROUP BY comment_author_email
	ORDER BY cmtcount DESC
	LIMIT 0 ,2000"
	);
//历遍结果,生成新的数组.存放邮件和次数.
foreach ($sql as $mycomment){
$mail=$mycomment->comment_author_email;
$comment_times=$mycomment->cmtcount;
$cache[$mail]=$comment_times;
}
//将这个数组更新到缓存.
update_option('philna_commenter_star',$cache);
}
//wordpress API Hook 每次有新评论,运行更新缓存.
add_action('comment_post', 'philna_commenter_star');
?>

几乎每句话都有注释.很好理解了吧.关于查option的问题.因为wordpress在加载的时候就把它读取并缓存了.所以我们把数据写到那里再查的时候不会造成多余的查询.用了这个办法后,你应该看到用这个和不用这个功能都不会多出查询.唯一的查询是更新评论后,但是这个由wordpress在后台自己运行.不会再前端展示.

PS.关于Ajax的交流.我又要延期了.请继续关注吧.我会写出来的.也欢迎新朋友能够订阅我,或者在Twitter Follow 我

  1. 会律博客 Apr 1st, 2009 @ 13:24 | #1

    SF,第一次及时的说,这里现在是经文地带,哦还有交友地带!

  2. Leeiio Apr 1st, 2009 @ 15:14 | #2

    Ajax延迟了,那2.7的新评论页面修改提前了?哈

  3. welee Apr 1st, 2009 @ 15:33 | #3

    这样修正很好,过多的查询很折腾数据库,其实啊,我觉得可以将五星分为 1 2 3 4 5 个星星来表示就更完美了(以上为不负责任发言中。。。)

  4. yinheli Apr 1st, 2009 @ 15:37 | #4

    @会律博客
    你也常来了.今天刚好获得星星.

    @Leeiio
    我尽快写把.

    @welee
    我不希望那样,会有等级的感觉.这个只是标明一下.这家伙常来的. :lol:

  5. disinfeqt Apr 1st, 2009 @ 15:52 | #5

    This is totally AWESOME,looking forward to use it in the future…

  6. yinheli Apr 1st, 2009 @ 16:25 | #6

    @disinfeqt
    I have added it in my theme.

  7. 吖Bee Apr 1st, 2009 @ 21:26 | #7

    @welee
    呵呵~可以变成❤型之类的啊~只不过要付出代价……

  8. 吖Bee Apr 1st, 2009 @ 21:31 | #8

    应该是修改 “philna/app/yinheli_xing.php” 这个文件吧?

  9. yinheli Apr 1st, 2009 @ 21:57 | #9

    @吖Bee
    是的.你也可以等我新版本出来,你看看.

  10. Showfom Apr 1st, 2009 @ 22:10 | #10

    我也很热心的~

  11. Soxiqi Apr 1st, 2009 @ 23:43 | #11

    整的跟淘宝评价一样,有不同的级别,这样评论的积极性就上来了。

  12. onefrozen Apr 2nd, 2009 @ 14:01 | #12

    貌似少了个异常的处理,就是在没有e-mail地址的时候,会出错
    因为我是从PJblog转过来的,所以以前的很多评论都没有e-mail
    也许是这个原因出错的,我也不太清楚了,新下载主题设置中的我把这功能关了 :!:

  13. yinheli Apr 2nd, 2009 @ 14:10 | #13

    @onefrozen
    热情评论员是统计mail数目的.

  14. Lorz Apr 16th, 2009 @ 20:55 | #14

    我估计我是没有星星

  15. 老王 Apr 28th, 2009 @ 23:54 | #15

    好久没来了,报个到! :smile:

  16. yinheli Apr 29th, 2009 @ 00:02 | #16

    @老王
    哈哈 热烈欢迎.最好你还是看看这个 修改一下那个函数吧.否则影响速度的.

  17. 老王 Apr 29th, 2009 @ 09:33 | #17

    @yinheli
    好的,最近瞎忙,都没时间搞博客了。回头好好修改一下。

Submitting Comment, Give me a second...

Leave a comment

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
Trackbacks & Pingbacks ( 0 )
  1. No trackbacks yet.