WordPress评级系统:记住用户选择


Wordpress rating system : memorizing user choice

我为我的wordpress文章做了一个评级系统。您可以投反对票或赞成票,它会更新仪表。这是它的样子:http://bit.ly/1eXiAzt

我改编了一个wptuts教程,它只是提供了uplike的可能性。因此,我很难使我的脚本完全正常运行。这是我的问题:

我想记住用户是否喜欢或不喜欢这篇文章并调整 css。目前,当用户已经投票时,这两个按钮具有相同的样式。

这是我的文件:

功能.php :

<?php
// RATING //
$timebeforerevote = 1;
add_action('wp_ajax_nopriv_post-like', 'post_like');  
add_action('wp_ajax_post-like', 'post_like');
add_action('wp_ajax_nopriv_post-like', 'post_dislike');  
add_action('wp_ajax_post-like', 'post_dislike');
wp_enqueue_script('like_post', get_template_directory_uri().'/post-like.js', array('jquery'), '1.0', true );  
wp_localize_script('like_post', 'ajax_var', array(  
    'url' => admin_url('admin-ajax.php'),  
    'nonce' => wp_create_nonce('ajax-nonce')  
));
function post_like()  
{  
    // Check for nonce security  
    $nonce = $_POST['nonce'];  
    if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )  
        die ( 'Busted!');  
    if(isset($_POST['post_like']))  
    {  
        // Retrieve user IP address  
        $ip = $_SERVER['REMOTE_ADDR'];  
        $post_id = $_POST['post_id'];  
        // Get voters'IPs for the current post  
        $meta_IP = get_post_meta($post_id, "voted_IP");  
        $voted_IP = $meta_IP[0];  
        if(!is_array($voted_IP))  
            $voted_IP = array();  
        // Get votes count for the current post  
        $meta_count = get_post_meta($post_id, "votes_count", true);
        $meta_total = get_post_meta($post_id, "votes_total", true);  
        // Use has already voted ?  
        if(!hasAlreadyVoted($post_id))  
        {  
            $voted_IP[$ip] = time();  
            // Save IP and increase votes count  
            update_post_meta($post_id, "voted_IP", $voted_IP);  
            update_post_meta($post_id, "votes_count", ++$meta_count);
            update_post_meta($post_id, "votes_total", ++$meta_total);  
            // Display count (ie jQuery return value)  
            echo $meta_count; 
        }  
        else  
            echo "already";  
    }
    else if(isset($_POST['post_dislike']))
    {
              // Retrieve user IP address  
        $ip = $_SERVER['REMOTE_ADDR'];  
        $post_id = $_POST['post_id'];  
        // Get voters'IPs for the current post  
        $meta_IP = get_post_meta($post_id, "voted_IP");  
        $voted_IP = $meta_IP[0];  
        if(!is_array($voted_IP))  
            $voted_IP = array();  
        // Get votes count for the current post  
        $meta_count = get_post_meta($post_id, "votes_count", true);
        $meta_total = get_post_meta($post_id, "votes_total", true);  
        // Use has already voted ?  
        if(!hasAlreadyVoted($post_id))  
        {  
            $voted_IP[$ip] = time();  
            // Save IP and increase votes count  
            update_post_meta($post_id, "voted_IP", $voted_IP);  
            update_post_meta($post_id, "votes_count", --$meta_count);
            update_post_meta($post_id, "votes_total", ++$meta_total);  
            // Display count (ie jQuery return value)  
            echo $meta_count; 
        }  
        else  
            echo "already";  
    }
    exit;
}
function hasAlreadyVoted($post_id)  
{  
    global $timebeforerevote;  
    // Retrieve post votes IPs  
    $meta_IP = get_post_meta($post_id, "voted_IP");  
    $voted_IP = $meta_IP[0];  
    if(!is_array($voted_IP))  
        $voted_IP = array();  
    // Retrieve current user IP  
    $ip = $_SERVER['REMOTE_ADDR'];  
    // If user has already voted  
    if(in_array($ip, array_keys($voted_IP)))  
    {  
        $time = $voted_IP[$ip];  
        $now = time();  
        // Compare between current time and vote time  
        if(round(($now - $time) / 60) > $timebeforerevote)  
            return false;  
        return true;  
    }  
    return false;  
}

function getPostDislikeLink($post_id)  
{  
    $themename = "twentyeleven";  

    if(hasAlreadyVoted($post_id))  
        $output .= 
    '<span title="'.__('Too kind !', $themename).'" class="like dislike alreadyvoted"></span>';  
    else  
        $output .= 
    '<a href="#" data-post_id="'.$post_id.'" class="idislike"> 
      <span  title="'.__('Too kind !', $themename).'"class="qtip like dislike"></span></a>';  
    return $output;  
}
function getPostLikeLink($post_id)  
{  
    $themename = "twentyeleven";  
    if(hasAlreadyVoted($post_id))  
        $output .= 
    '<span title="'.__('Nasty !', $themename).'" class="like alreadyvoted"></span>';  
    else  
        $output .= '<a href="#" data-post_id="'.$post_id.'" class="ilike"> 
      <span  title="'.__('Nasty !', $themename).'"class="qtip like"></span></a>';  

    return $output;  
}
function getPostTotalLike($post_id)
{
    $vote_count = get_post_meta($post_id, "votes_count", true); 
    $vote_total = get_post_meta($post_id, "votes_total", true);
    if(empty($vote_total)){
    $pourcentage = 'novotes';
    }
    else if ($vote_count < 0){
    $vote_count2 = abs($vote_count);
    $votesPositifs = $vote_total - $vote_count2;
    $pourcentage = ($votesPositifs * 100) / $vote_total;
    }
    else if($vote_count > 0){
    $pourcentage = ($vote_count * 100) / $vote_total;
    }
    else{
    $pourcentage = 50;
    }
    if($pourcentage == 'novotes'){
        $barometre = '<li class="level0"></li><li class="level0"></li><li class="level0"></li><li class="level0"></li>';
    }   
    else if($pourcentage < 30){
        $barometre = '<li class="level1"></li><li class="level0"></li><li class="level0"></li><li class="level0"></li>';
    }
    else if($pourcentage < 60){
        $barometre = '<li class="level1"></li><li class="level2"></li><li class="level0"></li><li class="level0"></li>';
    }
    else if($pourcentage < 85){
        $barometre = '<li class="level1"></li><li class="level2"></li><li class="level3"></li><li class="level0"></li>';
    }
    else{
        $barometre = '<li class="level1"></li><li class="level2"></li><li class="level3"></li><li class="level4"></li>';
    }
    $output2 .= '<ul>'.$barometre.'</ul>';
    return $output2;
}
function getStats($post_id)
{
    $vote_count = get_post_meta($post_id, "votes_count", true); 
    $vote_total = get_post_meta($post_id, "votes_total", true);
    $output3 = '<span>Score : '.$vote_count.' / Total votes : '.$vote_total.'</span>';
    return $output3;
}
?>

.HTML:

    <div class="mechancete post-like">
        <?php echo getPostDislikeLink(get_the_ID());?>
        <?php echo getPostTotalLike(get_the_ID());?>
        <?php echo getPostLikeLink(get_the_ID());?>
        <?php echo getStats(get_the_ID());?>
   </div>

.JS:

jQuery(document).ready(function() {
    jQuery(".post-like a.ilike").click(function(){
        heart = jQuery(this);
        post_id = heart.data("post_id");
        jQuery.ajax({
            type: "post",
            url: ajax_var.url,
            data: "action=post-like&nonce="+ajax_var.nonce+"&post_like=&post_id="+post_id,
            success: function(count){
                if(count != "already")
                {
                    heart.addClass("voted");
                    heart.siblings(".count").text(count);
                }
            }
        });
        return false;
    })
    jQuery(".post-like a.idislike").click(function(){
        heart = jQuery(this);
        post_id = heart.data("post_id");
        jQuery.ajax({
            type: "post",
            url: ajax_var.url,
            data: "action=post-like&nonce="+ajax_var.nonce+"&post_dislike=&post_id="+post_id,
            success: function(count){
                if(count != "already")
                {
                    heart.addClass("voted");
                    heart.siblings(".count").text(count);
                }
            }
        });
        return false;
    })
})

提前感谢您的帮助!

两个主要问题:

  • CSS类不匹配,你有ilikelikeidislikedislike,使其与<a><span>一起使用一致且有意义。

  • 应使用 wp_send_json_success ,以便可以将$count$total作为数组值传递,以便在处理成功响应时更新计数器。并且您需要为计数器容器分配一个类,以便更新其值。

作为旁注:

    将所有
  • 排队包装到wp_enqueue_scripts

  • 本教程展示了如何使用functions.php执行此操作,但这是不正确的,请制作自己的插件。交换主题时会发生什么?