我正在用PHP创建一个类似的系统。我的代码如下所示。
<?php
class Like_System {
private $userid;
private $postid;
private $user_ko_like_count;
private $post_ko_like_count;
private $user_ko_dislike_count;
private $post_ko_dislike_count;
private $user_ip;
public function __construct(){
}
public function our_ajax_script(){
wp_enqueue_script( \'sb_like_post\', get_template_directory_uri().\'/data/js/post-like.min.js\', false, \'1.0\', 1 );
wp_localize_script( \'sb_like_post\', \'ajax_var\', array(
\'url\' => admin_url( \'admin-ajax.php\' ),
\'nonce\' => wp_create_nonce( \'like_system\' )
)
);
}
public function load_our_ajax_script(){
add_action( \'init\', array($this,"our_ajax_script") );
add_action( \'wp_ajax_nopriv_like_system\',array($this,"like_dislike_kernal"));
add_action( \'wp_ajax_like_system\',array($this,"like_dislike_kernal"));
}
public function verify_nonce($nonce){
if ( ! wp_verify_nonce( $nonce, \'like_system\' ) )
die ();
}
public function like_dislike_kernal(){
extract($_POST);
$this->verify_nonce($nonce);
$this->postid=$postid;
$this->userid=get_current_user_id();
$this->post_ko_like_count = get_post_meta( $postid, "_post_ko_like_count", true );
if($system=="like"){
if(is_user_logged_in()){
$this->logged_in_user_like_kernal();
}else{
$this->anonymous_user_like_kernal();
}
}
die();
}
private function make_array_if_not_exist($var){
if(!is_array($var)){
$var=[];
return $var;
}
return $var;
}
private function already_liked_or_disliked($whattocheck="liked"){
if(is_user_logged_in()){
$post_ma_like_garney_user=$this->make_array_if_not_exist(get_post_meta( $this->postid, "_post_ma_like_garney_user", true ));
if(in_array($this->userid,$post_ma_like_garney_user)) return true;
}
return false;
}
private function logged_in_user_like_kernal(){
$user_lay_like_gareko_posts=$this->make_array_if_not_exist(get_user_option( "_user_lay_like_gareko_posts", $this->userid ));
$post_ma_like_garney_user=$this->make_array_if_not_exist(get_post_meta( $this->postid, "_post_ma_like_garney_user", true ));
$user_lay_like_gareko_posts["Post_ID_".$this->postid]=$this->postid;
$post_ma_like_garney_user["User_ID_".$this->userid]=$this->userid;
if($this->already_liked_or_disliked()==false){
update_post_meta( $this->postid, "_post_ko_like_count", $this->post_ko_like_count + 1 );
echo $this->post_ko_like_count + 1; // update count on front end
}else{
$ukey= array_search( $this->userid, $post_ma_like_garney_user);
$pkey= array_search( $this->postid, $user_lay_like_gareko_posts );
unset( $user_lay_like_gareko_posts[$pkey] ); // remove from array
unset( $post_ma_like_garney_user[$ukey] ); // remove from array
update_post_meta( $this->postid, "_post_ko_like_count", --$this->post_ko_like_count );
echo $this->post_ko_like_count;
}
}
}
$like_system=new Like_System();
$like_system->load_our_ajax_script();
?>
基本上,当用户单击like按钮时,ajax工作,而这个like\\u系统类工作将在数据库上添加like count+1
Problem如果用户缓慢地单击like按钮,一切都会正常工作。例如,如果我单击like按钮,它显示like count 5。当我在5秒后单击like(假设)时,它将不象并显示like计数4,正如我们所期望的那样,如果我单击like计数,它将显示5。这是一个测试,我在其中记录了一个类似的计数器,当我在一段时间后单击按钮时,它会正确地运行。
但假设我很快点击了按钮,比如说一秒钟内点击了5次。我们可能希望like count应该在4和5之间切换,但有时它不会显示-1或-3,有时甚至会显示8。为什么会这样?
这是当我快速点击like按钮时的测试。在代码中挖掘了数小时,但仍然找不到任何问题:(
我甚至不知道ajax是个问题还是php问题:(谢谢。