一些wordpress网站可能有很多注册用户,为了特殊要求,限制注册用户的每日评论数量。
实现原理,创建一个cookie,有效期为24小时,当用户在cookie有效期内提交评论时记下评论数量,当达到特定值时,提示评论数量超过日限额。
代码来自IT路人的 wp-itluren-comment-filter 插件的部分代码。
创建cookie
<code>
//插入评论时设置Cookie
function itluren_comment_setcookie($comment_id){
$comment_cookie_expire=1;
$comment=get_comment($comment_id);
$itluren_post_ID =$comment->comment_post_ID;
if (! isset($_COOKIE["WP-LastCommentedPosts"])) {
$itluren_cookiearray = array($itluren_post_ID);
} else {
$itluren_cookiearray = unserialize(preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", stripslashes($_COOKIE["WP-LastCommentedPosts"])));
if (! is_array($itluren_cookiearray)) {
$itluren_cookiearray = array($itluren_post_ID);
}
}
if (in_array($itluren_post_ID, $itluren_cookiearray)) {
$itluren_key = array_search($itluren_post_ID, $itluren_cookiearray);
array_splice($itluren_cookiearray, $itluren_key, 1);
}
array_unshift($itluren_cookiearray, $itluren_post_ID);
$itluren_blog_url_array = parse_url(get_bloginfo('url'));
$itluren_blog_url = $itluren_blog_url_array['host'];
$itluren_blog_url = str_replace('www.', '', $itluren_blog_url);
$itluren_blog_url_dot = '.';
$itluren_blog_url_dot .= $itluren_blog_url;
$itluren_path_url = $itluren_blog_url_array['path'];
$itluren_path_url_slash = '/';
$itluren_path_url .= $itluren_path_url_slash;
setcookie("WP-LastCommentedPosts", serialize($itluren_cookiearray), (time()+($comment_cookie_expire*86400)), $itluren_path_url, $itluren_blog_url_dot, 0);
}
add_action('comment_post','itluren_comment_setcookie');
</code>
//插入评论时设置Cookie
function itluren_comment_setcookie($comment_id){
$comment_cookie_expire=1;
$comment=get_comment($comment_id);
$itluren_post_ID =$comment->comment_post_ID;
if (! isset($_COOKIE["WP-LastCommentedPosts"])) {
$itluren_cookiearray = array($itluren_post_ID);
} else {
$itluren_cookiearray = unserialize(preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", stripslashes($_COOKIE["WP-LastCommentedPosts"])));
if (! is_array($itluren_cookiearray)) {
$itluren_cookiearray = array($itluren_post_ID);
}
}
if (in_array($itluren_post_ID, $itluren_cookiearray)) {
$itluren_key = array_search($itluren_post_ID, $itluren_cookiearray);
array_splice($itluren_cookiearray, $itluren_key, 1);
}
array_unshift($itluren_cookiearray, $itluren_post_ID);
$itluren_blog_url_array = parse_url(get_bloginfo('url'));
$itluren_blog_url = $itluren_blog_url_array['host'];
$itluren_blog_url = str_replace('www.', '', $itluren_blog_url);
$itluren_blog_url_dot = '.';
$itluren_blog_url_dot .= $itluren_blog_url;
$itluren_path_url = $itluren_blog_url_array['path'];
$itluren_path_url_slash = '/';
$itluren_path_url .= $itluren_path_url_slash;
setcookie("WP-LastCommentedPosts", serialize($itluren_cookiearray), (time()+($comment_cookie_expire*86400)), $itluren_path_url, $itluren_blog_url_dot, 0);
}
add_action('comment_post','itluren_comment_setcookie');
</code>
从cookie中获取评论数量
<code>
//获取最近的评论数量
function recently_Commented_PostCount() {
if(isset($_COOKIE["WP-LastCommentedPosts"])) {
$itluren_post_IDs = unserialize(preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", stripslashes($_COOKIE["WP-LastCommentedPosts"])));
$post_count=count($itluren_post_IDs);
}else{
$post_count=0;
}
$post_count=(int)$post_count;
return $post_count;
}
</code>
//获取最近的评论数量
function recently_Commented_PostCount() {
if(isset($_COOKIE["WP-LastCommentedPosts"])) {
$itluren_post_IDs = unserialize(preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", stripslashes($_COOKIE["WP-LastCommentedPosts"])));
$post_count=count($itluren_post_IDs);
}else{
$post_count=0;
}
$post_count=(int)$post_count;
return $post_count;
}
</code>
在评论时提示是否超额
<code>
add_filter( 'preprocess_comment', 'minimal_comment_length' );
function minimal_comment_length( $commentdata ) {
$today_comment_count=recently_Commented_PostCount();
$today_comment_count=(int)$today_comment_count;
$comment_count_backlist=15;//每日评论数量
if($today_comment_count>$comment_count_backlist){
err( '今天你的评论已近达到上限15条了,24小时后再来吧~' );
}
return $commentdata;
}
</code>
add_filter( 'preprocess_comment', 'minimal_comment_length' );
function minimal_comment_length( $commentdata ) {
$today_comment_count=recently_Commented_PostCount();
$today_comment_count=(int)$today_comment_count;
$comment_count_backlist=15;//每日评论数量
if($today_comment_count>$comment_count_backlist){
err( '今天你的评论已近达到上限15条了,24小时后再来吧~' );
}
return $commentdata;
}
</code>
以上代码仅作笔记使用,还没测试,可能或多或少存在问题。