Wordpress评级系统:记忆用户选择

时间:2013-10-27 12:54:15

标签: php wordpress rating

我为我的wordpress文章制作了评分系统。您可以downvote或upvote并更新仪表。这是它的样子:http://bit.ly/1eXiAzt

我改编了a wptuts tutorial,这恰好提供了可能性。因此,我有一些困难使我的脚本完全正常运行。这是我的问题:

如果用户喜欢或不喜欢该文章并调整css,我会想要记住。目前,当用户已经投票时,两个按钮都具有相同的样式。

以下是我的文件:

functions.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;
    })

})

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

两个主要问题:

  • CSS类不匹配,您有ilikelikeidislikedislike,使其与{{一致且有意义1}}和<a>

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

作为旁注: