1

我为我的 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;
    })

})

在此先感谢您的帮助 !

4

1 回答 1

1

两个主要问题:

  • CSS 类不匹配,你有ilike, like, idislikeand dislike,这使得它与 and 一起使用是一致和有意义<a><span>

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

作为旁注:

于 2013-10-27T16:45:33.127 回答